make_checks.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from django.core.management.base import BaseCommand
  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.uptime.models import Monitor, MonitorCheck
  7. from organizations_ext.models import Organization
  8. from projects.models import Project
  9. baker.generators.add("organizations.fields.SlugField", gen_slug)
  10. baker.generators.add("django.db.models.JSONField", gen_json)
  11. class Command(BaseCommand):
  12. help = "Create a number of monitors with 1,000 checks each for dev and demonstration purposes."
  13. def add_arguments(self, parser):
  14. parser.add_argument("monitor_number", nargs="?", type=int, default=10)
  15. def handle(self, *args, **options):
  16. organization = Organization.objects.first()
  17. if not organization:
  18. organization = baker.make("organizations_ext.Organization")
  19. project = Project.objects.filter(organization=organization).first()
  20. if not project:
  21. project = baker.make("projects.Project", organization=organization)
  22. monitor_number = options["monitor_number"]
  23. for x in range(monitor_number):
  24. monitor = Monitor.objects.create(
  25. project=project,
  26. name=f"Test Monitor #{x}",
  27. organization=organization,
  28. url="https://example.com",
  29. interval="60",
  30. monitor_type="Ping",
  31. expected_status="200",
  32. )
  33. for y in range(1000):
  34. with freeze_time(timezone.now() - timezone.timedelta(minutes=y)):
  35. MonitorCheck.objects.create(
  36. monitor=monitor,
  37. is_up=True,
  38. start_check=timezone.now() - timezone.timedelta(minutes=y),
  39. response_time=timezone.timedelta(milliseconds=60),
  40. )
  41. self.stdout.write(
  42. self.style.SUCCESS('Successfully created "%s" monitors' % monitor_number)
  43. )