event_generator.py 1.5 KB

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