monitoring/ffmap/web/application.py

91 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2015-09-03 19:35:09 +02:00
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '../..'))
from ffmap.web.api import api
from ffmap.web.filters import filters
from ffmap.dbtools import FreifunkDB
2015-11-11 15:27:50 +01:00
from ffmap import stattools
2015-10-07 16:19:07 +02:00
from flask import Flask, render_template, request, Response
2015-11-06 18:19:21 +01:00
import bson
2015-11-11 15:27:50 +01:00
import pymongo
2015-09-03 19:35:09 +02:00
from bson.json_util import dumps as bson2json
2015-10-02 23:53:47 +02:00
from bson.objectid import ObjectId
2015-09-03 19:35:09 +02:00
app = Flask(__name__)
2015-10-07 16:19:07 +02:00
app.register_blueprint(api, url_prefix='/api')
app.register_blueprint(filters)
db = FreifunkDB().handle()
2015-09-03 19:35:09 +02:00
tileurls = {
"links_and_routers": "/tiles/links_and_routers",
"hoods": "/tiles/hoods",
2015-09-03 19:35:09 +02:00
}
@app.route('/')
def index():
2015-10-02 23:53:47 +02:00
return render_template("index.html")
@app.route('/map')
def router_map():
2015-09-03 19:35:09 +02:00
return render_template("map.html", tileurls=tileurls)
2015-10-02 23:53:47 +02:00
@app.route('/routers')
def router_list():
2015-11-15 20:47:43 +01:00
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, {
2015-10-02 23:53:47 +02:00
"hostname": 1,
"status": 1,
"hood": 1,
"user.nickname": 1,
"hardware.name": 1,
2015-11-15 20:47:43 +01:00
"created": 1,
"system.uptime": 1,
"system.clients": 1,
2015-10-02 23:53:47 +02:00
}))
@app.route('/routers/<dbid>')
def router_info(dbid):
2015-11-06 18:19:21 +01:00
try:
router = db.routers.find_one({"_id": ObjectId(dbid)})
assert router
except (bson.errors.InvalidId, AssertionError):
return "Router not found"
if request.args.get('json', None) != None:
2015-11-15 20:47:43 +01:00
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)
2015-10-02 23:53:47 +02:00
2015-11-11 15:27:50 +01:00
@app.route('/statistics')
def global_statistics():
hoods = stattools.hoods()
return render_template("statistics.html",
stats = db.stats.find({}, {"_id": 0}),
clients = stattools.total_clients(),
router_status = stattools.router_status(),
router_models = stattools.router_models(),
router_firmwares = stattools.router_firmwares(),
hoods = hoods,
hoods_sum = stattools.hoods_sum(),
newest_routers = db.routers.find({}, {"hostname": 1, "hood": 1, "created": 1}).sort("created", pymongo.DESCENDING).limit(len(hoods)+1)
)
2015-09-03 19:35:09 +02:00
if __name__ == '__main__':
2015-10-02 23:53:47 +02:00
app.run(host='0.0.0.0', debug=True)
else:
app.template_folder = "/usr/share/ffmap/templates"
app.static_folder = "/usr/share/ffmap/static"
#app.debug = True