2020-10-12 04:44:30 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
import time
|
2020-10-12 04:44:30 +00:00
|
|
|
from itertools import islice
|
2023-02-09 12:48:25 +00:00
|
|
|
from random import seed
|
2020-10-12 04:44:30 +00:00
|
|
|
|
|
|
|
from orgs.models import Organization
|
|
|
|
|
|
|
|
|
|
|
|
class FakeDataGenerator:
|
|
|
|
resource = 'Fake'
|
|
|
|
|
|
|
|
def __init__(self, batch_size=100, org_id=None):
|
|
|
|
self.batch_size = batch_size
|
|
|
|
self.org = self.switch_org(org_id)
|
|
|
|
seed()
|
|
|
|
|
|
|
|
def switch_org(self, org_id):
|
2021-03-05 06:29:54 +00:00
|
|
|
o = Organization.get_instance(org_id, default=Organization.default())
|
2020-10-12 04:44:30 +00:00
|
|
|
if o:
|
|
|
|
o.change_to()
|
|
|
|
return o
|
|
|
|
|
|
|
|
def do_generate(self, batch, batch_size):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def pre_generate(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after_generate(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def generate(self, count=100):
|
|
|
|
self.pre_generate()
|
|
|
|
counter = iter(range(count))
|
|
|
|
created = 0
|
|
|
|
while True:
|
|
|
|
batch = list(islice(counter, self.batch_size))
|
|
|
|
if not batch:
|
|
|
|
break
|
2023-02-09 12:48:25 +00:00
|
|
|
start = time.time()
|
2020-10-12 04:44:30 +00:00
|
|
|
self.do_generate(batch, self.batch_size)
|
2023-02-09 12:48:25 +00:00
|
|
|
end = time.time()
|
|
|
|
using = end - start
|
2020-10-12 04:44:30 +00:00
|
|
|
from_size = created
|
|
|
|
created += len(batch)
|
2023-02-13 12:04:17 +00:00
|
|
|
print('Generate %s: %s-%s [%s]' % (self.resource, from_size, created, using))
|
2023-02-09 12:48:25 +00:00
|
|
|
self.after_generate()
|
2023-02-13 12:04:17 +00:00
|
|
|
time.sleep(20)
|