make_sample_issues.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import random
  2. from model_bakery import baker
  3. from events.models import Event
  4. from events.test_data import bulk_event_data, event_generator
  5. from events.views import EventStoreAPIView
  6. from glitchtip.base_commands import MakeSampleCommand
  7. from projects.models import Project
  8. class Command(MakeSampleCommand):
  9. help = "Create sample issues and events for dev and demonstration purposes"
  10. def add_arguments(self, parser):
  11. self.add_org_project_arguments(parser)
  12. parser.add_argument("--issue-quantity", type=int, default=100)
  13. parser.add_argument(
  14. "--events-quantity-per",
  15. type=int,
  16. help="Defaults to a random amount from 1-100",
  17. )
  18. parser.add_argument(
  19. "--only-real",
  20. action="store_true",
  21. help="Only include real sample events. Issue quanity will be the number of generated, real-looking events.",
  22. )
  23. def generate_real_event(self, project, unique_issue=False):
  24. """Generate an event based on real sample data"""
  25. data = event_generator.generate_random_event(unique_issue)
  26. project.release_id = None
  27. project.environment_id = None
  28. serializer = EventStoreAPIView().get_event_serializer_class(data)(
  29. data=data, context={"project": project}
  30. )
  31. serializer.is_valid()
  32. serializer.save()
  33. def generate_issue(self, get_events_count, quantity):
  34. issues = baker.make_recipe(
  35. "issues.issue_recipe",
  36. count=get_events_count,
  37. project=self.project,
  38. _quantity=quantity,
  39. _bulk_create=True,
  40. )
  41. for i, issue in enumerate(issues):
  42. if i % 100 == 0:
  43. self.progress_tick()
  44. event_list = []
  45. for _ in range(issue.count):
  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. def handle(self, *args, **options):
  53. super().handle(*args, **options)
  54. issue_quantity = options["issue_quantity"]
  55. events_quantity_per = options["events_quantity_per"]
  56. only_real = options["only_real"]
  57. def get_events_count():
  58. if events_quantity_per:
  59. return events_quantity_per
  60. return random.randint(1, 100)
  61. if only_real:
  62. for i in range(issue_quantity):
  63. if i % 100 == 0:
  64. self.progress_tick()
  65. self.generate_real_event(self.project)
  66. else:
  67. # Chunk issue generation to lessen ram requirements
  68. for _ in range(int(issue_quantity / self.batch_size)):
  69. self.generate_issue(get_events_count, self.batch_size)
  70. self.progress_tick()
  71. if remainder := issue_quantity % self.batch_size:
  72. self.generate_issue(get_events_count, remainder)
  73. self.success_message('Successfully created "%s" issues' % issue_quantity)