|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | import time |
5 | | - |
| 5 | +from collections import OrderedDict |
6 | 6 | from mamonsu.plugins.pgsql.pool import Pooler |
7 | 7 | from mamonsu.tools.report.format import header_h1, key_val_h1, humansize |
8 | 8 |
|
@@ -144,7 +144,9 @@ class PostgresInfo(object): |
144 | 144 | else round(100*io.heap_blks_hit::float8/(io.heap_blks_read+io.heap_blks_hit)::float8) end as "heap_hit_perc", |
145 | 145 |
|
146 | 146 | 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" |
148 | 150 |
|
149 | 151 | from |
150 | 152 | pg_catalog.pg_stat_all_tables as s |
@@ -301,17 +303,21 @@ def _collect_connections(self): |
301 | 303 | return result |
302 | 304 |
|
303 | 305 | def _collect_biggest(self): |
304 | | - result = {} |
| 306 | + result, sizes, sorted_result = {}, {}, OrderedDict({}) |
305 | 307 | for info_dbs in Pooler.query('select datname \ |
306 | 308 | from pg_catalog.pg_database where datistemplate = false'): |
307 | 309 | try: |
308 | 310 | for info in Pooler.query(self.BigTableInfo[0], info_dbs[0]): |
309 | 311 | table_name = '{0}.{1}'.format(info_dbs[0], info[0]) |
310 | 312 | 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: |
312 | 316 | result[table_name] = "{0}\t\t{1}".format( |
313 | 317 | result[table_name], val) |
314 | 318 | except Exception as e: |
315 | 319 | logging.error("Connect to db {0} error: {1}".format( |
316 | 320 | 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