diff --git a/ffmap/misc.py b/ffmap/misc.py index bb4fe6b..f1e153c 100644 --- a/ffmap/misc.py +++ b/ffmap/misc.py @@ -15,3 +15,36 @@ def writelog(path, content): def writefulllog(content): with open(CONFIG["debug_dir"] + "/fulllog.log", "a") as csv: csv.write(time.strftime('{%Y-%m-%d %H:%M:%S}') + " - " + content + "\n") + +def neighbor_color(quality,rt_protocol): + if rt_protocol=="BATMAN_V": + color = "#04ff0a" + if quality < 0: + color = "#06a4f4" + elif quality < 10: + color = "#ff1e1e" + elif quality < 20: + color = "#ff4949" + elif quality < 40: + color = "#ff6a6a" + elif quality < 80: + color = "#ffac53" + elif quality < 1000: + color = "#ffeb79" + else: + color = "#04ff0a" + if quality < 0: + color = "#06a4f4" + elif quality < 105: + color = "#ff1e1e" + elif quality < 130: + color = "#ff4949" + elif quality < 155: + color = "#ff6a6a" + elif quality < 180: + color = "#ffac53" + elif quality < 205: + color = "#ffeb79" + elif quality < 230: + color = "#79ff7c" + return color diff --git a/ffmap/web/api.py b/ffmap/web/api.py index 9bf51e5..e57f4d2 100755 --- a/ffmap/web/api.py +++ b/ffmap/web/api.py @@ -6,7 +6,7 @@ from ffmap.maptools import * from ffmap.mysqltools import FreifunkMySQL from ffmap.stattools import record_global_stats, record_hood_stats from ffmap.config import CONFIG -from ffmap.misc import writelog, writefulllog +from ffmap.misc import writelog, writefulllog, neighbor_color from flask import Blueprint, request, make_response, redirect, url_for, jsonify, Response from bson.json_util import dumps as bson2json @@ -37,8 +37,8 @@ def get_nearest_router(): where = " AND h.id IS NULL " mysql = FreifunkMySQL() - res_router = mysql.findone(""" - SELECT r.id, r.hostname, r.lat, r.lng, r.description, + router = mysql.findone(""" + SELECT r.id, r.hostname, r.lat, r.lng, r.description, r.routing_protocol, ( acos( cos( radians(%s) ) * cos( radians( r.lat ) ) * cos( radians( r.lng ) - radians(%s) ) @@ -54,17 +54,19 @@ def get_nearest_router(): LIMIT 1 """,(lat,lng,lat,)) - res_router["neighbours"] = mysql.fetchall(""" + router["neighbours"] = mysql.fetchall(""" SELECT nb.mac, nb.netif, nb.quality, r.hostname, r.id FROM router_neighbor AS nb INNER JOIN ( SELECT router, mac FROM router_netif GROUP BY mac, router ) AS net ON nb.mac = net.mac INNER JOIN router as r ON net.router = r.id - WHERE nb.router = %s""",(res_router["id"],)) + WHERE nb.router = %s""",(router["id"],)) mysql.close() + for n in router["neighbours"]: + n["color"] = neighbor_color(n["quality"],router["routing_protocol"]) - r = make_response(bson2json(res_router)) + r = make_response(bson2json(router)) r.mimetype = 'application/json' return r diff --git a/ffmap/web/application.py b/ffmap/web/application.py index daaea69..0d8c267 100755 --- a/ffmap/web/application.py +++ b/ffmap/web/application.py @@ -13,7 +13,7 @@ from ffmap.routertools import delete_router, ban_router from ffmap.gwtools import gw_name, gw_bat from ffmap.web.helpers import * from ffmap.config import CONFIG -from ffmap.misc import writelog, writefulllog +from ffmap.misc import writelog, writefulllog, neighbor_color from flask import Flask, render_template, request, Response, redirect, url_for, flash, session import bson @@ -223,6 +223,10 @@ def router_info(dbid): n["description"] = desc n["color"] = color + ## Set color for neighbors AFTER json if clause + for n in router["neighbours"]: + n["color"] = neighbor_color(n["quality"],router["routing_protocol"]) + router["stats"] = mysql.fetchall("""SELECT * FROM router_stats WHERE router = %s""",(dbid,)) for s in router["stats"]: s["time"] = mysql.utcawareint(s["time"]) diff --git a/ffmap/web/filters.py b/ffmap/web/filters.py index 49790a3..da6fa30 100644 --- a/ffmap/web/filters.py +++ b/ffmap/web/filters.py @@ -15,25 +15,6 @@ from ffmap.misc import * filters = Blueprint("filters", __name__) -@filters.app_template_filter('neighbour_color') -def neighbour_color(quality): - color = "#04ff0a" - if quality < 0: - color = "#06a4f4" - elif quality < 105: - color = "#ff1e1e" - elif quality < 130: - color = "#ff4949" - elif quality < 155: - color = "#ff6a6a" - elif quality < 180: - color = "#ffac53" - elif quality < 205: - color = "#ffeb79" - elif quality < 230: - color = "#79ff7c" - return color - @filters.app_template_filter('sumdict') def sumdict(d): return sum(d.values()) diff --git a/ffmap/web/static/js/map.js b/ffmap/web/static/js/map.js index 7ef3169..d3c880b 100644 --- a/ffmap/web/static/js/map.js +++ b/ffmap/web/static/js/map.js @@ -144,15 +144,7 @@ map.on('click', function(pos) { neighbour = router.neighbours[i]; // skip unknown neighbours if ('id' in neighbour) { - var tr_color = "#04ff0a"; - if (neighbour.quality < 0) { tr_color = "#06a4f4"; } - else if (neighbour.quality < 105) { tr_color = "#ff1e1e"; } - else if (neighbour.quality < 130) { tr_color = "#ff4949"; } - else if (neighbour.quality < 155) { tr_color = "#ff6a6a"; } - else if (neighbour.quality < 180) { tr_color = "#ffac53"; } - else if (neighbour.quality < 205) { tr_color = "#ffeb79"; } - else if (neighbour.quality < 230) { tr_color = "#79ff7c"; } - popup_html += ""; + popup_html += ""; popup_html += ''+escapeHTML(neighbour.hostname)+''; popup_html += ""+neighbour.quality+""; popup_html += ""+escapeHTML(neighbour.netif)+""; diff --git a/ffmap/web/templates/router.html b/ffmap/web/templates/router.html index 141af7c..4d42057 100644 --- a/ffmap/web/templates/router.html +++ b/ffmap/web/templates/router.html @@ -233,7 +233,7 @@ Interface {%- for neighbour in router.neighbours %} - + {%- if neighbour.hostname -%}{{ neighbour.hostname }}{%- else -%}---{%- endif -%} {{ neighbour.mac }}