jumpserver/utils/generate_fake_data/resources/base.py

50 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/python
2023-02-09 12:48:25 +00:00
import time
from itertools import islice
2023-02-09 12:48:25 +00:00
from random import seed
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())
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()
self.do_generate(batch, self.batch_size)
2023-02-09 12:48:25 +00:00
end = time.time()
using = end - start
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)