optimize awx-manage callback_stats for larger datasets

to monitor this historically, we'd probably need to introduce a new
index on the modified column of all our event types
This commit is contained in:
Ryan Petrello
2020-01-22 16:51:37 -05:00
parent c983b6a755
commit f9af5e8959

View File

@@ -9,6 +9,13 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
with connection.cursor() as cursor: with connection.cursor() as cursor:
start = {}
for relation in (
'main_jobevent', 'main_inventoryupdateevent',
'main_projectupdateevent', 'main_adhoccommandevent'
):
cursor.execute(f"SELECT MAX(id) FROM {relation};")
start[relation] = cursor.fetchone()[0] or 0
clear = False clear = False
while True: while True:
lines = [] lines = []
@@ -17,19 +24,15 @@ class Command(BaseCommand):
'main_projectupdateevent', 'main_adhoccommandevent' 'main_projectupdateevent', 'main_adhoccommandevent'
): ):
lines.append(relation) lines.append(relation)
for label, interval in ( minimum = start[relation]
('last minute: ', '1 minute'), cursor.execute(
('last 5 minutes:', '5 minutes'), f"SELECT MAX(id) - MIN(id) FROM {relation} WHERE id > {minimum} AND modified > now() - '1 minute'::interval;"
('last hour: ', '1 hour'), )
): events = cursor.fetchone()[0] or 0
cursor.execute( lines.append(f'↳ last minute {events}')
f"SELECT MAX(id) - MIN(id) FROM {relation} WHERE modified > now() - '{interval}'::interval;"
)
events = cursor.fetchone()[0] or 0
lines.append(f'{label} {events}')
lines.append('') lines.append('')
if clear: if clear:
for i in range(20): for i in range(12):
sys.stdout.write('\x1b[1A\x1b[2K') sys.stdout.write('\x1b[1A\x1b[2K')
for l in lines: for l in lines:
print(l) print(l)