-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaskServer.py
60 lines (49 loc) · 1.74 KB
/
flaskServer.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
from flask import Flask, render_template, jsonify
from random import sample
import flask
from influxdb import InfluxDBClient
app = flask.Flask(__name__)
infdb = InfluxDBClient(host='localhost', port=8086)
dblist = infdb.get_list_database()
checkdb = False
def checkInfluxdb():
for element in dblist:
if element.get('name',None) == 'iiot':
infdb.switch_database('iiot')
print("iiot database existes and switched...")
return True
print("There is no database...")
# def getLatency(clientId):
# client.query('SELECT "value" FROM "iiot"."autogen"."latency"')
@app.route('/')
def index():
return render_template('chart.html')
@app.route('/chart')
def chart():
if(checkdb):
#print(infdb.query('SELECT "value" FROM "iiot"."autogen"."latency" WHERE "client"=1'))
#return jsonify({'results' : sample(range(1,10), 5)})
return jsonify({'results' : queryLatency('1')})
else:
return print("There is no database...")
@app.route('/data')
def data():
if(checkdb):
#print(infdb.query('SELECT "value" FROM "iiot"."autogen"."latency" WHERE "client"=1'))
#return jsonify({'results' : sample(range(1,10), 5)})
return jsonify({'results' : queryLatency('0')})
else:
return print("There is no database...")
def queryLatency(client):
latencyVal=[]
rs = infdb.query("SELECT * from latency")
for element in list(rs.get_points(tags={'client': client})):
latencyVal.append(element['value'])
return latencyVal
if __name__=='__main__':
checkdb = checkInfluxdb()
app.run(host="0.0.0.0", port=int("80"), debug=True)
# from influxdb import InfluxDBClient
# client = InfluxDBClient(host='localhost', port=8086)
# client.switch_database('iiot')
# print(client.query('SELECT "value" FROM "iiot"."autogen"."latency"'))