event_generator.py 1.7 KB

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