From 452aa5a00932a294df91d70819aaeb72df4aca45 Mon Sep 17 00:00:00 2001 From: Adrian Schmutzler Date: Fri, 12 Jan 2018 15:05:20 +0100 Subject: [PATCH] statistics.html: Provide sorted list of gateways Gateways are listed alphabetically based on their name, then all without name based on their MAC address. Signed-off-by: Adrian Schmutzler --- ffmap/stattools.py | 23 ++++++++++++++++++----- ffmap/web/templates/statistics.html | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ffmap/stattools.py b/ffmap/stattools.py index 8fc0629..e404932 100644 --- a/ffmap/stattools.py +++ b/ffmap/stattools.py @@ -9,6 +9,8 @@ from ffmap.gwtools import gw_name, gw_bat from ffmap.misc import * from ffmap.config import CONFIG +from collections import OrderedDict + def total_clients(mysql,selecthood=None): if selecthood: return mysql.findone(""" @@ -160,11 +162,23 @@ def hoods_sum(mysql,selectgw=None): def gws(mysql,selecthood=None): if selecthood: where = " AND hood=%s" + wherewhere = "WHERE hood=%s" tup = (selecthood,) else: where = "" + wherewhere = "" tup = () + macs = mysql.fetchall(""" + SELECT router_gw.mac + FROM router + INNER JOIN router_gw ON router.id = router_gw.router + LEFT JOIN (gw_netif INNER JOIN gw ON gw_netif.gw = gw.id) + ON router_gw.mac = gw_netif.mac + {} + GROUP BY router_gw.mac + ORDER BY ISNULL(gw.name), gw.name ASC, router_gw.mac ASC + """.format(wherewhere),tup,"mac") selected = mysql.fetchall(""" SELECT router_gw.mac, router.status, COUNT(router_gw.router) AS count FROM router @@ -179,14 +193,13 @@ def gws(mysql,selecthood=None): WHERE router_gw.selected = FALSE {} GROUP BY router_gw.mac, router.status """.format(where),tup) - result = {} + + result = OrderedDict() + for m in macs: + result[m] = {"selected":{},"others":{}} for rs in selected: - if not rs["mac"] in result: - result[rs["mac"]] = {"selected":{},"others":{}} result[rs["mac"]]["selected"][rs["status"]] = rs["count"] for rs in others: - if not rs["mac"] in result: - result[rs["mac"]] = {"selected":{},"others":{}} result[rs["mac"]]["others"][rs["status"]] = rs["count"] return result diff --git a/ffmap/web/templates/statistics.html b/ffmap/web/templates/statistics.html index 235387c..74997bc 100644 --- a/ffmap/web/templates/statistics.html +++ b/ffmap/web/templates/statistics.html @@ -136,7 +136,7 @@ Clients Stats - {%- for mac, value in gws|dictsort %} + {%- for mac, value in gws.items() %} {{ gws_info[mac]["label"] }} {{ value["selected"]["online"] or 0 }} / {{ value["others"]["online"] or 0 }}