From 34a7c4c58e8ee415e1e988f4f36e8a2f04dfb478 Mon Sep 17 00:00:00 2001 From: Adrian Schmutzler Date: Wed, 10 Jan 2018 22:49:01 +0100 Subject: [PATCH] statistics.html: Add gateway overview Signed-off-by: Adrian Schmutzler --- ffmap/stattools.py | 53 +++++++++++++++++++++++++++++ ffmap/web/application.py | 6 +++- ffmap/web/filters.py | 4 +++ ffmap/web/templates/statistics.html | 29 ++++++++++++++-- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/ffmap/stattools.py b/ffmap/stattools.py index 187304f..d872361 100644 --- a/ffmap/stattools.py +++ b/ffmap/stattools.py @@ -116,6 +116,59 @@ def hoods_sum(mysql): result[rs["hood"]] = {"routers": rs["count"], "clients": rs["clients"]} return result +def gws(mysql,selecthood=None): + if selecthood: + where = " AND hood=%s" + tup = (selecthood,) + else: + where = "" + tup = () + + selected = mysql.fetchall(""" + SELECT router_gw.mac, router.status, COUNT(router_gw.router) AS count + FROM router + INNER JOIN router_gw ON router.id = router_gw.router + WHERE router_gw.selected = TRUE {} + GROUP BY router_gw.mac, router.status + """.format(where),tup) + others = mysql.fetchall(""" + SELECT router_gw.mac, router.status, COUNT(router_gw.router) AS count + FROM router + INNER JOIN router_gw ON router.id = router_gw.router + WHERE router_gw.selected = FALSE {} + GROUP BY router_gw.mac, router.status + """.format(where),tup) + result = {} + 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 + +def gws_sum(mysql,selecthood=None): + if selecthood: + where = " AND hood=%s" + tup = (selecthood,) + else: + where = "" + tup = () + + data = mysql.fetchall(""" + SELECT router_gw.mac, COUNT(router_gw.router) AS count, SUM(router.clients) AS clients + FROM router + INNER JOIN router_gw ON router.id = router_gw.router + WHERE router_gw.selected = TRUE {} + GROUP BY router_gw.mac + """.format(where),tup) + result = {} + for rs in data: + result[rs["mac"]] = {"routers": rs["count"], "clients": rs["clients"]} + return result + def record_global_stats(mysql): threshold=(utcnow() - datetime.timedelta(days=CONFIG["global_stat_days"])).timestamp() time = mysql.utctimestamp() diff --git a/ffmap/web/application.py b/ffmap/web/application.py index 4e441a8..796d1e2 100755 --- a/ffmap/web/application.py +++ b/ffmap/web/application.py @@ -364,6 +364,8 @@ def helper_statistics(mysql,stats,selecthood): router_models = stattools.router_models(mysql,selecthood) router_firmwares = stattools.router_firmwares(mysql,selecthood) hoods_sum = stattools.hoods_sum(mysql) + gws = stattools.gws(mysql,selecthood) + gws_sum = stattools.gws_sum(mysql,selecthood) mysql.close() return render_template("statistics.html", @@ -375,7 +377,9 @@ def helper_statistics(mysql,stats,selecthood): router_firmwares = router_firmwares, hoods = hoods, hoods_sum = hoods_sum, - newest_routers = newest_routers + newest_routers = newest_routers, + gws = gws, + gws_sum = gws_sum ) @app.route('/register', methods=['GET', 'POST']) diff --git a/ffmap/web/filters.py b/ffmap/web/filters.py index 55dfc96..77e3733 100644 --- a/ffmap/web/filters.py +++ b/ffmap/web/filters.py @@ -34,6 +34,10 @@ def neighbour_color(quality): color = "#79ff7c" return color +@filters.app_template_filter('sumdict') +def sumdict(d): + return sum(d.values()) + @filters.app_template_filter('utc2local') def utc2local(dt): return dt.astimezone(tz.tzlocal()) diff --git a/ffmap/web/templates/statistics.html b/ffmap/web/templates/statistics.html index da6af99..1a3e159 100644 --- a/ffmap/web/templates/statistics.html +++ b/ffmap/web/templates/statistics.html @@ -122,6 +122,33 @@
+
+
+
Gateways (selected / others)
+
+ + + + + + + + + + {%- for mac, value in gws|dictsort %} + + + + + + + + + {%- endfor %} +
GatewayOnlineOfflineUnknownTotalClients
{{ mac }}{{ value["selected"]["online"] or 0 }} / {{ value["others"]["online"] or 0 }}{{ value["selected"]["offline"] or 0 }} / {{ value["others"]["offline"] or 0 }}{{ value["selected"]["unknown"] or 0 }} / {{ value["others"]["unknown"] or 0 }}{{ gws_sum[mac]["routers"] if gws_sum[mac] else 0 }} / {{ value["others"]|sumdict if value["others"] else 0 }}{{ gws_sum[mac]["clients"] if gws_sum[mac] }}
+
+
+
Router Firmwares{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -129,8 +156,6 @@
-
-
Router Models{%- if selecthood %} @ {{ selecthood }}{%- endif -%}