event_generator.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import copy
  2. import random
  3. import uuid
  4. from django.utils import timezone
  5. from glitchtip.utils import get_random_string
  6. from . import django_error_factory
  7. from .csp import mdn_sample_csp
  8. events = django_error_factory.all_django_events
  9. events.append(mdn_sample_csp)
  10. things = ["a", "b", "c", None]
  11. def make_event_unique(event, unique_issue=False):
  12. """Assign event a random new event_id and current timestamp"""
  13. new_event = copy.deepcopy(event)
  14. new_event["event_id"] = uuid.uuid4().hex
  15. new_event["timestamp"] = timezone.now().isoformat()
  16. new_event["release"] = random.choice(things)
  17. new_event["environment"] = random.choice(things)
  18. if unique_issue:
  19. title = get_random_string()
  20. if "message" in new_event:
  21. new_event["message"] = title
  22. elif "exception" in new_event:
  23. new_event["exception"]["values"][0]["value"] = title
  24. elif "csp-report" in new_event:
  25. new_event["csp-report"]["document-uri"] = title
  26. return new_event
  27. def generate_random_event(unique_issue=False):
  28. """Return a random event from library of samples with unique event id"""
  29. event = random.choice(events) # nosec
  30. result = make_event_unique(event, unique_issue)
  31. return result
  32. def get_seeded_benchmark_events(quantity=100, seed=1337):
  33. """Non-random events that attempt to simulate common use cases"""
  34. random.seed(seed)
  35. result = []
  36. for i in range(quantity):
  37. every_other = i % 2
  38. result.append(generate_random_event(every_other))
  39. return result