Skip to content

Commit d919dd3

Browse files
committed
sort table in report tool
1 parent d64496f commit d919dd3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

mamonsu/tools/report/pgsql.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44
import time
5-
5+
from collections import OrderedDict
66
from mamonsu.plugins.pgsql.pool import Pooler
77
from mamonsu.tools.report.format import header_h1, key_val_h1, humansize
88

@@ -144,7 +144,9 @@ class PostgresInfo(object):
144144
else round(100*io.heap_blks_hit::float8/(io.heap_blks_read+io.heap_blks_hit)::float8) end as "heap_hit_perc",
145145
146146
case coalesce(io.idx_blks_read + io.idx_blks_hit, 0) when 0 then 0
147-
else round(100*io.idx_blks_hit::float8/(io.idx_blks_read + io.idx_blks_hit)::float8) end as "idx_hit_perc"
147+
else round(100*io.idx_blks_hit::float8/(io.idx_blks_read + io.idx_blks_hit)::float8) end as "idx_hit_perc",
148+
149+
b.size as "size_b"
148150
149151
from
150152
pg_catalog.pg_stat_all_tables as s
@@ -301,17 +303,21 @@ def _collect_connections(self):
301303
return result
302304

303305
def _collect_biggest(self):
304-
result = {}
306+
result, sizes, sorted_result = {}, {}, OrderedDict({})
305307
for info_dbs in Pooler.query('select datname \
306308
from pg_catalog.pg_database where datistemplate = false'):
307309
try:
308310
for info in Pooler.query(self.BigTableInfo[0], info_dbs[0]):
309311
table_name = '{0}.{1}'.format(info_dbs[0], info[0])
310312
result[table_name] = ''
311-
for val in info[1:]:
313+
values = info[1:] # remove first elements (table name with schema)
314+
sizes[table_name] = values.pop() # size in bytes in last element
315+
for val in values:
312316
result[table_name] = "{0}\t\t{1}".format(
313317
result[table_name], val)
314318
except Exception as e:
315319
logging.error("Connect to db {0} error: {1}".format(
316320
info_dbs[0], e))
317-
return result
321+
for table_name in sorted(result, key=sizes.__getitem__, reverse=True):
322+
sorted_result[table_name] = result[table_name]
323+
return sorted_result

0 commit comments

Comments
 (0)