make_sample_checks.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from random import randrange
  2. from django.utils import timezone
  3. from apps.uptime.models import Monitor, MonitorCheck
  4. from glitchtip.base_commands import MakeSampleCommand
  5. class Command(MakeSampleCommand):
  6. help = "Create a number of monitors each with checks for dev and demonstration purposes."
  7. def add_arguments(self, parser):
  8. self.add_org_project_arguments(parser)
  9. parser.add_argument("--monitor-quantity", type=int, default=10)
  10. parser.add_argument("--checks-quantity-per", type=int, default=100)
  11. parser.add_argument("--first-check-down", type=bool, default=False)
  12. def handle(self, *args, **options):
  13. super().handle(*args, **options)
  14. monitor_quantity = options["monitor_quantity"]
  15. checks_quantity_per = options["checks_quantity_per"]
  16. first_check_down = options["first_check_down"]
  17. monitors = [
  18. Monitor(
  19. project=self.project,
  20. name=f"Test Monitor #{i}",
  21. organization=self.organization,
  22. url="https://example.com",
  23. interval="60",
  24. monitor_type="Ping",
  25. expected_status="200",
  26. )
  27. for i in range(monitor_quantity)
  28. ]
  29. Monitor.objects.bulk_create(monitors)
  30. # Create checks sequentially based on time
  31. # Creates a better representation of data on disk
  32. checks = []
  33. start_time = timezone.now() - timezone.timedelta(minutes=checks_quantity_per)
  34. for time_i in range(checks_quantity_per):
  35. for monitor in monitors:
  36. is_first = time_i == 0
  37. is_up = True
  38. if first_check_down and is_first:
  39. is_up = False
  40. checks.append(
  41. MonitorCheck(
  42. monitor=monitor,
  43. is_up=is_up,
  44. is_change=is_first,
  45. start_check=start_time + timezone.timedelta(minutes=time_i),
  46. response_time=randrange(1, 5000)
  47. )
  48. )
  49. if len(checks) > 10000:
  50. MonitorCheck.objects.bulk_create(checks)
  51. self.progress_tick()
  52. checks = []
  53. if checks:
  54. MonitorCheck.objects.bulk_create(checks)
  55. self.success_message('Successfully created "%s" monitors' % monitor_quantity)