make_bulk_events.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.core.management.base import BaseCommand
  2. from model_bakery import baker
  3. from model_bakery.random_gen import gen_json, gen_slug
  4. from events.models import Event
  5. from events.test_data import event_generator
  6. from issues.models import Issue
  7. from issues.tasks import update_search_index_issue
  8. from organizations_ext.models import Organization
  9. from projects.models import Project
  10. from events.test_data import bulk_event_data
  11. baker.generators.add("organizations.fields.SlugField", gen_slug)
  12. baker.generators.add("django.db.models.JSONField", gen_json)
  13. class Command(BaseCommand):
  14. help = "Create an issue with a large number of events for dev and demonstration purposes"
  15. def add_arguments(self, parser):
  16. parser.add_argument("quantity", nargs="?", type=int, default=10000)
  17. parser.add_argument("batch_size", nargs="?", type=int, default=10000)
  18. def handle(self, *args, **options):
  19. organization = Organization.objects.first()
  20. if not organization:
  21. organization = baker.make("organizations_ext.Organization")
  22. project = Project.objects.filter(organization=organization).first()
  23. if not project:
  24. project = baker.make("projects.Project", organization=organization)
  25. issue_data = bulk_event_data.large_event
  26. title = issue_data.get("title")
  27. culprit = issue_data.get("culprit")
  28. metadata = issue_data.get("metadata")
  29. issue, issue_created = Issue.objects.get_or_create(
  30. title=title, culprit=culprit, metadata=metadata, project=project,
  31. )
  32. quantity = options["quantity"]
  33. batch_size = options["batch_size"]
  34. if quantity < batch_size:
  35. batches = 1
  36. else:
  37. batches = quantity // batch_size
  38. for _ in range(batches):
  39. if quantity < batch_size:
  40. batch_size = quantity
  41. event_list = []
  42. for _ in range(batch_size):
  43. event = Event(
  44. data=event_generator.make_event_unique(bulk_event_data.large_event),
  45. issue=issue,
  46. )
  47. event_list.append(event)
  48. Event.objects.bulk_create(event_list)
  49. quantity -= batch_size
  50. update_search_index_issue(args=[issue.pk, issue_created])
  51. self.stdout.write(
  52. self.style.SUCCESS('Successfully created "%s" events' % options["quantity"])
  53. )