From f14e309e8b9cd2e25227df8af95e53162d5ce0b4 Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Thu, 25 Jun 2020 13:54:07 +0200 Subject: Fixes for sattelites --- dash/pages/satellites.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/dash/pages/satellites.py b/dash/pages/satellites.py index 232d8c7..0b02f5e 100644 --- a/dash/pages/satellites.py +++ b/dash/pages/satellites.py @@ -20,7 +20,9 @@ def layout(): id='satellites-table', columns=[ {'id': 'ip', 'name': 'IP', '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'}, @@ -33,29 +35,53 @@ def layout(): 'textAlign': 'left' } ], - data=load_table() + sort_action='custom' )]) ]) ]) ]) -def load_table(): + +@app.callback( + Output('satellites-table', 'data'), + [Input('satellites-table', "sort_by")]) +def load_table(sort): + print(sort) dbcon = db.getConnection() cursor = dbcon.cursor() + sortstr = "ORDER BY r.date DESC" + if sort != None and len(sort) > 0: + mappings = { + 'ip': 'r.ip', + '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' + print(sortstr) + cursor.execute(""" SELECT - r.ip, r.version, s.cpuCount, s.cpuModel, s.uptime, + r.ip, 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 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'] = humanize.naturaldelta(dt.timedelta(seconds=record['uptime'])) + record['date'] = humanize.naturaldelta(dt.date.today() - record['date']) db.closeConnection(dbcon) return data -- cgit v1.2.3-55-g7522