make_sample_checks.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from random import randrange
  2. from django.utils import timezone
  3. from freezegun import freeze_time
  4. from model_bakery import baker
  5. from model_bakery.random_gen import gen_json, gen_slug
  6. from glitchtip.base_commands import MakeSampleCommand
  7. from glitchtip.uptime.models import Monitor, MonitorCheck
  8. from organizations_ext.models import Organization
  9. from projects.models import Project
  10. baker.generators.add("organizations.fields.SlugField", gen_slug)
  11. baker.generators.add("django.db.models.JSONField", gen_json)
  12. class Command(MakeSampleCommand):
  13. help = "Create a number of monitors each with checks for dev and demonstration purposes."
  14. def add_arguments(self, parser):
  15. self.add_org_project_arguments(parser)
  16. parser.add_argument("--monitor-quantity", type=int, default=10)
  17. parser.add_argument("--checks-quantity-per", type=int, default=100)
  18. def handle(self, *args, **options):
  19. super().handle(*args, **options)
  20. monitor_quantity = options["monitor_quantity"]
  21. checks_quantity_per = options["checks_quantity_per"]
  22. for x in range(monitor_quantity):
  23. monitor = Monitor.objects.create(
  24. project=self.project,
  25. name=f"Test Monitor #{x}",
  26. organization=self.organization,
  27. url="https://example.com",
  28. interval="60",
  29. monitor_type="Ping",
  30. expected_status="200",
  31. )
  32. checks = []
  33. for y in range(checks_quantity_per):
  34. with freeze_time(timezone.now() - timezone.timedelta(minutes=y)):
  35. checks.append(
  36. MonitorCheck(
  37. monitor=monitor,
  38. is_up=True,
  39. start_check=timezone.now() - timezone.timedelta(minutes=y),
  40. response_time=timezone.timedelta(
  41. milliseconds=randrange(1, 5000)
  42. ),
  43. )
  44. )
  45. MonitorCheck.objects.bulk_create(checks)
  46. self.progress_tick()
  47. self.success_message('Successfully created "%s" monitors' % monitor_quantity)