make_sample_issues.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import random
  2. from django.contrib.postgres.search import SearchVector
  3. from model_bakery import baker
  4. from events.models import Event
  5. from events.test_data import bulk_event_data, event_generator
  6. from events.views import EventStoreAPIView
  7. from glitchtip.base_commands import MakeSampleCommand
  8. from glitchtip.utils import get_random_string
  9. from issues.models import EventType, Issue
  10. from projects.models import Project
  11. from .issue_generator import CULPRITS, EXCEPTIONS, SDKS, TITLE_CHOICES, generate_tag
  12. class Command(MakeSampleCommand):
  13. help = "Create sample issues and events for dev and demonstration purposes"
  14. def add_arguments(self, parser):
  15. self.add_org_project_arguments(parser)
  16. parser.add_argument("--issue-quantity", type=int, default=100)
  17. parser.add_argument(
  18. "--events-quantity-per",
  19. type=int,
  20. help="Defaults to a random amount from 1-100",
  21. )
  22. parser.add_argument(
  23. "--tag-keys-per-event", type=int, default=0, help="Extra random tag keys"
  24. )
  25. parser.add_argument(
  26. "--tag-values-per-key", type=int, default=1, help="Extra random tag values"
  27. )
  28. def handle(self, *args, **options):
  29. super().handle(*args, **options)
  30. issue_quantity = options["issue_quantity"]
  31. events_quantity_per = options["events_quantity_per"]
  32. random_tags = {
  33. get_random_string(): [
  34. get_random_string() for _ in range(options["tag_values_per_key"])
  35. ]
  36. for _ in range(options["tag_keys_per_event"])
  37. }
  38. def get_events_count():
  39. if events_quantity_per:
  40. return events_quantity_per
  41. return random.randint(1, 100)
  42. issues = []
  43. issue_ids = []
  44. for _ in range(issue_quantity):
  45. title = random.choice(TITLE_CHOICES) + " " + get_random_string()
  46. tags = generate_tag()
  47. if tags:
  48. tags = {tag[0]: [tag[1]] for tag in tags}
  49. else:
  50. tags = {}
  51. event_count = get_events_count()
  52. issues.append(
  53. Issue(
  54. title=title,
  55. culprit=random.choice(CULPRITS),
  56. level=EventType.ERROR,
  57. metadata={"title": title},
  58. tags=tags,
  59. project=self.project,
  60. count=event_count,
  61. )
  62. )
  63. if len(issues) > 10000:
  64. issues = Issue.objects.bulk_create(issues)
  65. issue_ids += [issue.pk for issue in issues]
  66. issues = []
  67. self.progress_tick()
  68. if issues:
  69. issues = Issue.objects.bulk_create(issues)
  70. issue_ids += [issue.pk for issue in issues]
  71. issues = Issue.objects.filter(pk__in=issue_ids)
  72. self.progress_tick()
  73. events = []
  74. for issue in issues:
  75. for _ in range(issue.count):
  76. data = issue.metadata.copy()
  77. data["sdk"] = random.choice(SDKS)
  78. data["culprit"] = issue.culprit
  79. data["exception"] = random.choice(EXCEPTIONS)
  80. tags = generate_tag() or {}
  81. events.append(
  82. Event(issue=issue, level=issue.level, data=data, tags=tags)
  83. )
  84. if len(events) > 10000:
  85. Event.objects.bulk_create(events)
  86. events = []
  87. self.progress_tick()
  88. if events:
  89. Event.objects.bulk_create(events)
  90. issues.update(search_vector=SearchVector("title"))
  91. self.success_message('Successfully created "%s" issues' % issue_quantity)