Skip to content

Commit

Permalink
Merge pull request #1100 from rafalp/improve-fakers
Browse files Browse the repository at this point in the history
Small improvements for fake data generators
  • Loading branch information
rafalp committed Sep 15, 2018
2 parents e90d56c + 459710b commit cceb4e1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
35 changes: 22 additions & 13 deletions misago/core/management/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ def show_progress(command, step, total, since=None):
filled = progress // 2
blank = 50 - filled

line = '\r%s%% [%s%s]'
rendered_line = line % (str(progress).rjust(3), '=' * filled, ' ' * blank)
template = '\r%(step)s (%(progress)s%%) [%(progressbar)s]%(estimation)s'
variables = {
"step": str(step).rjust(len(str(total))),
"progress": str(progress).rjust(3),
"progressbar": "".join(['=' * filled, ' ' * blank]),
"estimation": get_estimation_str(since, progress, step, total),
}

if since:
progress_float = float(step) * 100.0 / float(total)
if progress_float > 0:
step_time = (time.time() - since) / progress_float
estimated_time = (100 - progress) * step_time
clock = time.strftime('%H:%M:%S', time.gmtime(estimated_time))
rendered_line = '%s %s est.' % (rendered_line, clock)
else:
rendered_line = '%s --:--:-- est.' % rendered_line

command.stdout.write(rendered_line, ending='')
command.stdout.write(template % variables, ending='')
command.stdout.flush()


def get_estimation_str(since, progress, step, total):
if not since:
return ""

progress_float = float(step) * 100.0 / float(total)
if progress_float == 0:
return ' --:--:-- est.'

step_time = (time.time() - since) / progress_float
estimated_time = (100 - progress) * step_time
clock = time.strftime('%H:%M:%S', time.gmtime(estimated_time))
return ' %s est.' % clock
3 changes: 1 addition & 2 deletions misago/faker/management/commands/createfakebans.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def handle(self, *args, **options):
message = 'Creating %s fake bans...\n'
self.stdout.write(message % fake_bans_to_create)

message = '\n\nSuccessfully created %s fake bans'

created_count = 0
show_progress(self, created_count, fake_bans_to_create)
for _ in range(fake_bans_to_create):
Expand All @@ -120,4 +118,5 @@ def handle(self, *args, **options):
created_count += 1
show_progress(self, created_count, fake_bans_to_create)

message = '\n\nSuccessfully created %s fake bans'
self.stdout.write(message % created_count)
3 changes: 1 addition & 2 deletions misago/faker/management/commands/createfakecategories.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def handle(self, *args, **options):
message = 'Creating %s fake categories...\n'
self.stdout.write(message % items_to_create)

message = '\n\nSuccessfully created %s fake categories in %s'

created_count = 0
start_time = time.time()
show_progress(self, created_count, items_to_create)
Expand Down Expand Up @@ -91,4 +89,5 @@ def handle(self, *args, **options):

total_time = time.time() - start_time
total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
message = '\n\nSuccessfully created %s fake categories in %s'
self.stdout.write(message % (created_count, total_humanized))
3 changes: 1 addition & 2 deletions misago/faker/management/commands/createfakefollowers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def handle(self, *args, **options):
message = 'Adding fake followers to %s users...\n'
self.stdout.write(message % total_users)

message = '\nSuccessfully added %s fake followers in %s'

total_followers = 0
processed_count = 0

Expand Down Expand Up @@ -54,4 +52,5 @@ def handle(self, *args, **options):

total_time = time.time() - start_time
total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
message = '\nSuccessfully added %s fake followers in %s'
self.stdout.write(message % (total_followers, total_humanized))
9 changes: 5 additions & 4 deletions misago/faker/management/commands/createfakethreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


class Command(BaseCommand):
help = 'Creates random threads and posts for dev and testing purposes.'
help = 'Creates random threads for dev and testing purposes.'

def add_arguments(self, parser):
parser.add_argument(
Expand All @@ -44,9 +44,8 @@ def handle(self, *args, **options):

fake = Factory.create()

self.stdout.write('Creating fake threads...\n')

message = '\nSuccessfully created %s fake threads in %s'
message = 'Creating %s fake threads...\n'
self.stdout.write(message % items_to_create)

created_threads = 0
start_time = time.time()
Expand Down Expand Up @@ -160,6 +159,7 @@ def handle(self, *args, **options):

pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
self.stdout.write('\nPinning %s threads...' % pinned_threads)

for _ in range(0, pinned_threads):
thread = Thread.objects.order_by('?')[:1][0]
if random.randint(0, 100) > 75:
Expand All @@ -174,6 +174,7 @@ def handle(self, *args, **options):

total_time = time.time() - start_time
total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
message = '\nSuccessfully created %s fake threads in %s'
self.stdout.write(message % (created_threads, total_humanized))

def fake_post_content(self):
Expand Down
12 changes: 9 additions & 3 deletions misago/faker/management/commands/createfakeusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ def handle(self, *args, **options):
message = 'Creating %s fake user accounts...\n'
self.stdout.write(message % items_to_create)

message = '\n\nSuccessfully created %s fake user accounts in %s'

created_count = 0
start_time = time.time()
show_progress(self, created_count, items_to_create)

while created_count < items_to_create:
try:
user = UserModel.objects.create_user(
possible_usernames = [
fake.first_name(),
fake.last_name(),
fake.name().replace(' ', ''),
fake.user_name(),
]

user = UserModel.objects.create_user(
random.choice(possible_usernames),
fake.email(),
'pass123',
set_default_avatar=False,
Expand All @@ -64,4 +69,5 @@ def handle(self, *args, **options):

total_time = time.time() - start_time
total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
message = '\n\nSuccessfully created %s fake user accounts in %s'
self.stdout.write(message % (created_count, total_humanized))

0 comments on commit cceb4e1

Please sign in to comment.