make_sample_events.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from model_bakery import baker
  2. from events.models import Event
  3. from events.test_data import bulk_event_data, event_generator
  4. from glitchtip.base_commands import MakeSampleCommand
  5. from issues.models import Issue
  6. from issues.tasks import update_search_index_issue
  7. from organizations_ext.models import Organization
  8. from projects.models import Project
  9. from projects.tasks import update_event_project_hourly_statistic
  10. class Command(MakeSampleCommand):
  11. help = "Create an issue with a large number of events for dev and demonstration purposes"
  12. def add_arguments(self, parser):
  13. super().add_arguments(parser)
  14. parser.add_argument("batch_size", nargs="?", type=int, default=10000)
  15. def handle(self, *args, **options):
  16. super().handle(*args, **options)
  17. issue_data = bulk_event_data.large_event
  18. title = issue_data.get("title")
  19. culprit = issue_data.get("culprit")
  20. metadata = issue_data.get("metadata")
  21. issue, _ = Issue.objects.get_or_create(
  22. title=title,
  23. culprit=culprit,
  24. metadata=metadata,
  25. project=self.project,
  26. )
  27. quantity = options["quantity"]
  28. batch_size = options["batch_size"]
  29. if quantity < batch_size:
  30. batches = 1
  31. else:
  32. batches = quantity // batch_size
  33. for _ in range(batches):
  34. if quantity < batch_size:
  35. batch_size = quantity
  36. event_list = []
  37. for _ in range(batch_size):
  38. event = Event(
  39. data=event_generator.make_event_unique(bulk_event_data.large_event),
  40. issue=issue,
  41. )
  42. event_list.append(event)
  43. Event.objects.bulk_create(event_list)
  44. quantity -= batch_size
  45. update_search_index_issue(args=[issue.pk])
  46. update_event_project_hourly_statistic(args=[self.project.pk, event.created])
  47. self.success_message('Successfully created "%s" events' % options["quantity"])