make_bulk_events.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.objects.get_or_create(
  30. title=title,
  31. culprit=culprit,
  32. metadata=metadata,
  33. project=project,
  34. )
  35. quantity = options["quantity"]
  36. batch_size = options["batch_size"]
  37. if quantity < batch_size:
  38. batches = 1
  39. else:
  40. batches = quantity // batch_size
  41. for _ in range(batches):
  42. if quantity < batch_size:
  43. batch_size = quantity
  44. event_list = []
  45. for _ in range(batch_size):
  46. event = Event(
  47. data=event_generator.make_event_unique(bulk_event_data.large_event),
  48. issue=issue,
  49. )
  50. event_list.append(event)
  51. Event.objects.bulk_create(event_list)
  52. quantity -= batch_size
  53. update_search_index_issue(args=[issue.pk])
  54. self.stdout.write(
  55. self.style.SUCCESS('Successfully created "%s" events' % options["quantity"])
  56. )