mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2024-11-14 21:26:34 +03:00
39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
|
import pygal
|
||
|
from pygal.style import RotateStyle
|
||
|
from jinja2.filters import do_filesizeformat
|
||
|
|
||
|
|
||
|
number_formatter = lambda v: '{:,}'.format(v)
|
||
|
bytes_formatter = lambda v: do_filesizeformat(v, True)
|
||
|
|
||
|
|
||
|
def tables_piechart(db, by_field, value_formatter):
|
||
|
Tables = db.get_model_for_table('tables', system_table=True)
|
||
|
qs = Tables.objects_in(db).filter(database=db.db_name, is_temporary=False).exclude(engine='Buffer')
|
||
|
tuples = [(getattr(table, by_field), table.name) for table in qs]
|
||
|
return _generate_piechart(tuples, value_formatter)
|
||
|
|
||
|
|
||
|
def columns_piechart(db, tbl_name, by_field, value_formatter):
|
||
|
ColumnsTable = db.get_model_for_table('columns', system_table=True)
|
||
|
qs = ColumnsTable.objects_in(db).filter(database=db.db_name, table=tbl_name)
|
||
|
tuples = [(getattr(col, by_field), col.name) for col in qs]
|
||
|
return _generate_piechart(tuples, value_formatter)
|
||
|
|
||
|
|
||
|
def _get_top_tuples(tuples, n=15):
|
||
|
non_zero_tuples = [t for t in tuples if t[0]]
|
||
|
sorted_tuples = sorted(non_zero_tuples, reverse=True)
|
||
|
if len(sorted_tuples) > n:
|
||
|
others = (sum(t[0] for t in sorted_tuples[n:]), 'others')
|
||
|
sorted_tuples = sorted_tuples[:n] + [others]
|
||
|
return sorted_tuples
|
||
|
|
||
|
|
||
|
def _generate_piechart(tuples, value_formatter):
|
||
|
style = RotateStyle('#9e6ffe', background='white', legend_font_family='Roboto', legend_font_size=18, tooltip_font_family='Roboto', tooltip_font_size=24)
|
||
|
chart = pygal.Pie(style=style, margin=0, title=' ', value_formatter=value_formatter, truncate_legend=-1)
|
||
|
for t in _get_top_tuples(tuples):
|
||
|
chart.add(t[1], t[0])
|
||
|
return chart.render(is_unicode=True, disable_xml_declaration=True)
|