make_bulk_transactions.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.core.management.base import BaseCommand
  2. from django.utils import timezone
  3. from model_bakery import baker
  4. from model_bakery.random_gen import gen_json, gen_slug
  5. from organizations_ext.models import Organization
  6. from performance.models import TransactionEvent
  7. from performance.test_data import generate_fake_transaction_event
  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 = (
  13. "Create a large number of transaction events for dev and demonstration purposes"
  14. )
  15. def add_arguments(self, parser):
  16. parser.add_argument("quantity", nargs="?", type=int, default=10000)
  17. def handle(self, *args, **options):
  18. organization = Organization.objects.first()
  19. if not organization:
  20. organization = baker.make("organizations_ext.Organization")
  21. project = Project.objects.filter(organization=organization).first()
  22. if not project:
  23. project = baker.make("projects.Project", organization=organization)
  24. quantity = options["quantity"]
  25. batch_size = 10000
  26. if quantity < batch_size:
  27. batches = 1
  28. else:
  29. batches = quantity // batch_size
  30. for _ in range(batches):
  31. if quantity < batch_size:
  32. batch_size = quantity
  33. event_list = []
  34. for _ in range(batch_size):
  35. event = generate_fake_transaction_event(project, timezone.now())
  36. event_list.append(event)
  37. TransactionEvent.objects.bulk_create(event_list)
  38. quantity -= batch_size
  39. self.stdout.write(self.style.NOTICE("."), ending="")
  40. self.stdout.write(
  41. self.style.SUCCESS(
  42. 'Successfully created "%s" transaction events' % options["quantity"]
  43. )
  44. )