From 0af1c76254e402ebea9aeae3b222261ccd74e5c4 Mon Sep 17 00:00:00 2001 From: Adrian Schmutzler Date: Wed, 10 Jan 2018 21:58:42 +0100 Subject: [PATCH] statistics.html: Consolidate code Signed-off-by: Adrian Schmutzler --- ffmap/web/application.py | 52 ++++++++--------------------- ffmap/web/templates/statistics.html | 19 ++++++----- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/ffmap/web/application.py b/ffmap/web/application.py index de47e7f..4e441a8 100755 --- a/ffmap/web/application.py +++ b/ffmap/web/application.py @@ -325,62 +325,38 @@ def user_info(nickname): @app.route('/statistics') def global_statistics(): mysql = FreifunkMySQL() - hoods = stattools.hoods(mysql) - stats = mysql.fetchall("SELECT * FROM stats_global") - stats = mysql.utcawaretupleint(stats,"time") - - numnew = len(hoods)-18 - if numnew < 1: - numnew = 1 - - newest_routers = mysql.fetchall(""" - SELECT id, hostname, hood, created - FROM router - WHERE hardware <> 'Legacy' - ORDER BY created DESC - LIMIT %s - """,(numnew,)) - newest_routers = mysql.utcawaretuple(newest_routers,"created") - - clients = stattools.total_clients(mysql) - router_status = stattools.router_status(mysql) - router_models = stattools.router_models(mysql) - router_firmwares = stattools.router_firmwares(mysql) - hoods_sum = stattools.hoods_sum(mysql) - mysql.close() - - return render_template("statistics.html", - selecthood = "All Hoods", - stats = stats, - clients = clients, - router_status = router_status, - router_models = router_models, - router_firmwares = router_firmwares, - hoods = hoods, - hoods_sum = hoods_sum, - newest_routers = newest_routers - ) + return helper_statistics(mysql,stats,None) @app.route('/hoodstatistics/') def global_hoodstatistics(selecthood): mysql = FreifunkMySQL() + stats = mysql.fetchall("SELECT * FROM stats_hood WHERE hood = %s",(selecthood,)) + return helper_statistics(mysql,stats,selecthood) + +def helper_statistics(mysql,stats,selecthood): hoods = stattools.hoods(mysql) - stats = mysql.fetchall("SELECT * FROM stats_hood WHERE hood = %s",(selecthood,)) stats = mysql.utcawaretupleint(stats,"time") numnew = len(hoods)-18 if numnew < 1: numnew = 1 + if selecthood: + where = " AND hood = %s" + tup = (selecthood,numnew,) + else: + where = "" + tup = (numnew,) + newest_routers = mysql.fetchall(""" SELECT id, hostname, hood, created FROM router - WHERE hardware <> 'Legacy' AND hood = %s + WHERE hardware <> 'Legacy' {} ORDER BY created DESC LIMIT %s - """,(selecthood,numnew,)) + """.format(where),tup) newest_routers = mysql.utcawaretuple(newest_routers,"created") clients = stattools.total_clients(mysql) diff --git a/ffmap/web/templates/statistics.html b/ffmap/web/templates/statistics.html index 8500805..36377f9 100644 --- a/ffmap/web/templates/statistics.html +++ b/ffmap/web/templates/statistics.html @@ -1,5 +1,5 @@ {% extends "bootstrap.html" %} -{% block title %}{{super()}} :: Statistics for {{ selecthood }}{% endblock %} +{% block title %}{{super()}} :: Statistics{%- if selecthood %} for {{ selecthood }}{%- endif -%}{% endblock %} {% block head %}{{super()}} @@ -87,7 +87,7 @@
-
Routers @ {{ selecthood }}
+
Routers{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -95,7 +95,7 @@
-
Clients @ {{ selecthood }}
+
Clients{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -103,7 +103,7 @@
-
Newest Routers @ {{ selecthood }}
+
Newest Routers{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -128,7 +128,7 @@
-
Router Firmwares @ {{ selecthood }}
+
Router Firmwares{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -136,7 +136,7 @@
-
Router Models @ {{ selecthood }}
+
Router Models{%- if selecthood %} @ {{ selecthood }}{%- endif -%}
@@ -148,11 +148,12 @@ var router_firmwares = {{ router_firmwares|tojson }}; var router_models = {{ router_models|tojson }}; var routers_page_url = "{{ url_for('router_list') }}"; + {%- if selecthood %} var hood = "{{selecthood}}"; + var hoodstr = "hood:^" + hood.replace(/ /g, '_') + "$"; + {%- else %} var hoodstr = ""; - if (hood != "All Hoods") { - hoodstr = "hood:^" + hood.replace(/ /g, '_') + "$"; - } + {%- endif -%} $(document).ready(function() { global_client_graph(); global_router_graph();