#!/usr/bin/env python3 import dash_core_components as dcc import dash_html_components as html import dash_table as dtbl import dash_bootstrap_components as dbc from dash.dependencies import Input, Output import datetime as dt from prettytime import prettytime import db from app import app def layout(): return dbc.Container(fluid=True, children=[ dbc.Row([ dbc.Col([ html.Div(className='table-responsive-lg', children=[dtbl.DataTable( id='satellites-table', columns=[ {'id': 'name', 'name': 'Name', 'type': 'text'}, {'id': 'date', 'name': 'Last Report', 'type': 'text'}, {'id': 'version', 'name': 'Version', 'type': 'text'}, {'id': 'numReports', 'name': '#Reports'}, {'id': 'cpuCount', 'name': '#CPUs'}, {'id': 'cpuModel', 'name': 'CPU Model'}, {'id': 'uptime', 'name': 'Uptime'}, {'id': 'memPercent', 'name': 'Mem Usage'}, {'id': 'swapPercent', 'name': 'Swap Usage'} ], style_cell_conditional=[ { 'if': {'column_type': 'text'}, 'textAlign': 'left' } ], sort_action='custom' )]) ]) ]) ]) @app.callback( Output('satellites-table', 'data'), [Input('satellites-table', "sort_by")]) def load_table(sort): dbcon = db.getConnection() cursor = dbcon.cursor() sortstr = "ORDER BY r.date DESC" if sort != None and len(sort) > 0: mappings = { 'name': 'name', 'date': 'r.date', 'version': 'r.version', 'numReports': 'n.numReports', 'cpuCount': 's.cpuCount', 'cpuModel': 's.cpuModel', 'uptime': 's.uptime', 'memPercent': 'memPercent', 'swapPercent': 'swapPercent' } sortstr = 'ORDER BY ' + mappings[sort[0]['column_id']] + ' ' sortstr += 'ASC' if sort[0]['direction'] == 'asc' else ' DESC' cursor.execute(""" SELECT COALESCE(nm.name, r.ip) AS name, r.version, r.date, s.cpuCount, s.cpuModel, s.uptime, n.numReports, ROUND(100 - s.memFree / s.memTotal * 100, 1) as memPercent, ROUND(s.swapUsed / s.swapTotal * 100, 1) as swapPercent FROM reports_newest r LEFT OUTER JOIN names nm ON r.ip = nm.ip LEFT OUTER JOIN server s ON r.id = s.report LEFT OUTER JOIN (SELECT ip, COUNT(date) AS numReports FROM reports GROUP BY ip) n ON r.ip = n.ip """ + sortstr) data = cursor.fetchall() for record in data: record['uptime'] = prettytime(record['uptime']) record['date'] = prettytime((dt.date.today() - record['date']).total_seconds()) db.closeConnection(dbcon) return data