-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtempdisplay.py
executable file
·93 lines (72 loc) · 2.74 KB
/
webtempdisplay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
import sqlite3
from flask import Flask
from flask import request, session, g, redirect, url_for, \
abort, render_template, flash
app = Flask(__name__)
# configuration
DATABASE = '/media/nas-rpi/TempMonData/rfmonDB.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
@app.before_request
def before_request():
g.db = connect_db()
g.db.row_factory = sqlite3.Row
@app.after_request
def add_header(response):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
response.headers['Cache-Control'] = 'public, max-age=0'
return response
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.route("/gauge_data/<metricName>/<metricGuid>/")
def gauge_data( metricName, metricGuid ):
WHERE = "WHERE "
WHERE += "metricname=\'"+metricName+"\' and metricguid=\'"+metricGuid+"\'"
SQLSTMT = "select nodeid, metricname, metric, metricdt from rawdata "+WHERE+" order by metricdt desc limit 1"
curs = g.db.execute(SQLSTMT)
node = curs.fetchone()
returnString = '{{"id":"{}","current_value":"{}","at":"{}"}}'.format(metricName, node["metric"], node["metricdt"])
return returnString
@app.route("/bootstrap")
def bootstrap():
return render_template('bootstrap.html')
@app.route("/monitor/")
def withTemplate():
WHERE=""
node = request.args.get('node')
if node is not None and node != "all":
WHERE += "and nodeid=\'"+node+"\'"
else:
node="all"
metricguid = request.args.get('metricguid')
if metricguid is not None and metricguid != "all":
WHERE += "and metricguid=\'"+metricguid+"\'"
else:
metricguid="all"
if( WHERE is not None ):
# Remove initial "and"
WHERE = WHERE.replace( "and ", "where ", 1)
numrows = request.args.get('numentries')
if numrows is None:
numrows="50"
curs = g.db.execute("select distinct nodeid, nodedescription from xmitters order by 1")
nodelist = curs.fetchall()
curs = g.db.execute("select distinct metricguid, metricname from rawdata order by 1")
nodetypelist = curs.fetchall()
curs = g.db.execute("select nodeid, metricname, metric, metricdt from rawdata "+WHERE+" order by metricdt desc limit "+numrows)
entries = curs.fetchall()
return render_template('metrics_table.html', nodelist=nodelist, nodetypelist=nodetypelist, entries=entries, node=node, metricguid=metricguid, maxrows=numrows)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=81, debug=True)