Browse Source

time estimates for management commands

Rafał Pitoń 10 years ago
parent
commit
172165b090

+ 13 - 1
misago/core/management/progressbar.py

@@ -1,9 +1,21 @@
-def show_progress(command, step, total):
+import time
+
+
+def show_progress(command, step, total, since=None):
     progress = step * 100 / total
     progress = step * 100 / total
     filled = progress / 2
     filled = progress / 2
     blank = 50 - filled
     blank = 50 - filled
 
 
     line = '\r%s%% [%s%s]'
     line = '\r%s%% [%s%s]'
     rendered_line = line % (str(progress).rjust(3), '=' * filled, ' ' * blank)
     rendered_line = line % (str(progress).rjust(3), '=' * filled, ' ' * blank)
+
+    if since:
+        if step > 0:
+            estimated_time = ((time.time() - since) / step) * (total - step)
+            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(rendered_line, ending='')
     command.stdout.flush()
     command.stdout.flush()

+ 13 - 10
misago/faker/management/commands/createfakethreads.py

@@ -1,4 +1,5 @@
 import random
 import random
+import time
 
 
 from faker import Factory
 from faker import Factory
 
 
@@ -38,10 +39,11 @@ class Command(BaseCommand):
         message = '\nSuccessfully created %s fake threads'
         message = '\nSuccessfully created %s fake threads'
 
 
         created_threads = 0
         created_threads = 0
+        start_time = time.time()
         show_progress(self, created_threads, fake_threads_to_create)
         show_progress(self, created_threads, fake_threads_to_create)
         for i in xrange(fake_threads_to_create):
         for i in xrange(fake_threads_to_create):
             with atomic():
             with atomic():
-                time = timezone.now()
+                datetime = timezone.now()
                 forum = random.choice(forums)
                 forum = random.choice(forums)
                 user = User.objects.order_by('?')[:1][0]
                 user = User.objects.order_by('?')[:1][0]
 
 
@@ -52,10 +54,10 @@ class Command(BaseCommand):
                 thread = Thread(
                 thread = Thread(
                     forum=forum,
                     forum=forum,
                     weight=0,
                     weight=0,
-                    started_on=time,
+                    started_on=datetime,
                     starter_name='-',
                     starter_name='-',
                     starter_slug='-',
                     starter_slug='-',
-                    last_post_on=time,
+                    last_post_on=datetime,
                     last_poster_name='-',
                     last_poster_name='-',
                     last_poster_slug='-',
                     last_poster_slug='-',
                     replies=random.randint(0, 2000),
                     replies=random.randint(0, 2000),
@@ -74,8 +76,8 @@ class Command(BaseCommand):
                     poster_ip=fake.ipv4(),
                     poster_ip=fake.ipv4(),
                     original=fake_message,
                     original=fake_message,
                     parsed=linebreaks_filter(fake_message),
                     parsed=linebreaks_filter(fake_message),
-                    posted_on=time,
-                    updated_on=time)
+                    posted_on=datetime,
+                    updated_on=datetime)
                 update_post_checksum(post)
                 update_post_checksum(post)
                 post.save(update_fields=['checksum'])
                 post.save(update_fields=['checksum'])
 
 
@@ -93,17 +95,18 @@ class Command(BaseCommand):
                 user.save()
                 user.save()
 
 
                 created_threads += 1
                 created_threads += 1
-                show_progress(self, created_threads, fake_threads_to_create)
+                show_progress(
+                    self, created_threads, fake_threads_to_create, start_time)
 
 
-        pinned_threads = random.randint(0, int(created_threads * 0.05)) or 1
-        self.stdout.write('\Pinning %s threads...' % pinned_threads)
+        pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
+        self.stdout.write('\nPinning %s threads...' % pinned_threads)
         for i in xrange(0, pinned_threads):
         for i in xrange(0, pinned_threads):
             thread = Thread.objects.order_by('?')[:1][0]
             thread = Thread.objects.order_by('?')[:1][0]
             thread.weight = 1
             thread.weight = 1
             thread.save()
             thread.save()
 
 
-        announcements = random.randint(0, int(created_threads * 0.01)) or 1
-        self.stdout.write('\Making %s announcements...' % announcements)
+        announcements = random.randint(0, int(created_threads * 0.001)) or 1
+        self.stdout.write('\nMaking %s announcements...' % announcements)
         for i in xrange(0, announcements):
         for i in xrange(0, announcements):
             thread = Thread.objects.order_by('?')[:1][0]
             thread = Thread.objects.order_by('?')[:1][0]
             thread.weight = 2
             thread.weight = 2

+ 4 - 1
misago/faker/management/commands/createfakeusers.py

@@ -1,5 +1,6 @@
 import random
 import random
 import sys
 import sys
+import time
 
 
 from django.contrib.auth import get_user_model
 from django.contrib.auth import get_user_model
 from django.core.exceptions import ValidationError
 from django.core.exceptions import ValidationError
@@ -35,6 +36,7 @@ class Command(BaseCommand):
         message = '\n\nSuccessfully created %s fake user accounts'
         message = '\n\nSuccessfully created %s fake user accounts'
 
 
         created_count = 0
         created_count = 0
+        start_time = time.time()
         show_progress(self, created_count, fake_users_to_create)
         show_progress(self, created_count, fake_users_to_create)
         for i in xrange(fake_users_to_create):
         for i in xrange(fake_users_to_create):
             try:
             try:
@@ -49,6 +51,7 @@ class Command(BaseCommand):
                 pass
                 pass
             else:
             else:
                 created_count += 1
                 created_count += 1
-                show_progress(self, created_count, fake_users_to_create)
+                show_progress(
+                    self, created_count, fake_users_to_create, start_time)
 
 
         self.stdout.write(message % created_count)
         self.stdout.write(message % created_count)