make_bulk_transactions.py 1.9 KB

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