event_generator.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import copy
  2. import uuid
  3. import string
  4. import random
  5. import itertools
  6. from django.utils import timezone
  7. from . import django_error_factory
  8. from .csp import mdn_sample_csp
  9. events = django_error_factory.all_django_events
  10. events.append(mdn_sample_csp)
  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. if unique_issue:
  21. title = get_random_string()
  22. if "message" in new_event:
  23. new_event["message"] = title
  24. elif "exception" in new_event:
  25. new_event["exception"]["values"][0]["value"] = title
  26. elif "csp-report" in new_event:
  27. new_event["csp-report"]["document-uri"] = title
  28. return new_event
  29. def generate_random_event(unique_issue=False):
  30. """ Return a random event from library of samples with unique event id """
  31. event = random.choice(events) # nosec
  32. result = make_event_unique(event, unique_issue)
  33. return result
  34. def get_seeded_benchmark_events(quantity=100, seed=1337):
  35. """Non-random events that attempt to simulate common use cases"""
  36. random.seed(seed)
  37. result = []
  38. for i in range(quantity):
  39. every_other = i % 2
  40. result.append(generate_random_event(every_other))
  41. return result