make router list nice and filterable

This commit is contained in:
Dominik Heidler 2015-11-15 20:47:43 +01:00
parent 1d0ef0f321
commit b442d33954
3 changed files with 48 additions and 6 deletions

View File

@ -36,12 +36,23 @@ def router_map():
@app.route('/routers')
def router_list():
return render_template("router_list.html", routers=db.routers.find({}, {
query = {}
for allowed_filter in ('hostname', 'status', 'hood', 'user.nickname', 'hardware.name', 'netifs.mac'):
if allowed_filter in request.args:
query[allowed_filter] = request.args[allowed_filter]
if allowed_filter == 'netifs.mac':
query[allowed_filter] = query[allowed_filter].lower()
if query[allowed_filter] == "EXISTS_NOT":
query[allowed_filter] = {"$exists": False}
return render_template("router_list.html", routers=db.routers.find(query, {
"hostname": 1,
"status": 1,
"hood": 1,
"user.nickname": 1,
"hardware.name": 1,
"created": 1,
"system.uptime": 1,
"system.clients": 1,
}))
@app.route('/routers/<dbid>')
@ -52,6 +63,7 @@ def router_info(dbid):
except (bson.errors.InvalidId, AssertionError):
return "Router not found"
if request.args.get('json', None) != None:
del router["stats"]
return Response(bson2json(router, sort_keys=True, indent=4), mimetype='application/json')
else:
return render_template("router.html", router=router, tileurls=tileurls)

View File

@ -60,6 +60,27 @@ def format_dt_ago(dt):
else:
return '%i hours ago' % (s/3600)
@filters.app_template_filter('format_ts_diff')
def format_dt_diff(ts):
diff = datetime.timedelta(seconds=ts)
s = diff.seconds
if diff.days > 1:
return '%i days' % diff.days
elif diff.days == 1:
return '1 day'
elif s <= 1:
return '< 1 sec'
elif s < 60:
return '%i sec' % s
elif s < 120:
return '1 min'
elif s < 3600:
return '%i min' % (s/60)
elif s < 7200:
return '1 hour'
else:
return '%i hours' % (s/3600)
@filters.app_template_filter('bson2json')
def bson_to_json(bsn):
return bson2json(bsn)

View File

@ -1,21 +1,30 @@
{% extends "bootstrap.html" %}
{% block content %}
<table class="table table-condensed">
<table class="table table-condensed table-striped table-bordered" style="margin-bottom: 8px;">
<tr>
<th>Hostname</th>
<th>Status</th>
<th style="width: 1%;">Hostname</th>
<th style="width: 45px;">Status</th>
<th>Hood</th>
<th>User</th>
<th>Hardware</th>
<th>Created</th>
<th>Uptime</th>
<th>Clients</th>
</tr>
{%- for router in routers %}
<tr>
<td><a href="{{ url_for("router_info", dbid=router._id) }}">{{ router.hostname }}</a></td>
<td><span class="{{ router.status|status2css }}">{{ router.status }}</span></td>
<td class="text-nowrap"><a href="{{ url_for("router_info", dbid=router._id) }}">{{ router.hostname }}</a></td>
<td class="text-center"><span class="{{ router.status|status2css }}">{{ router.status }}</span></td>
<td>{{ router.hood }}</td>
<td>{{ router.user.nickname if router.user }}</td>
<td>{{ router.get("hardware", {}).get("name", "") }}</td>
<td>{{ router.created|utc2local|format_dt }}</td>
<td>{{ router.system.uptime|format_ts_diff }}</td>
<td>{{ router.system.clients }}</td>
</tr>
{%- endfor %}
</table>
<div style="margin-bottom: 20px;">
{{ routers.count() }} Router{{ "s" if (routers.count() > 1) else "" }} found.
</div>
{% endblock %}