-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
396 lines (344 loc) · 14.6 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!flask/bin/python
from flask import Flask, request, jsonify, render_template, make_response, app
import json
import valid
import re
import subprocess
from config import collection, dateRegex
import helper
import datetime
app = Flask(__name__)
app.debug = True
# cookie name route tuple
navbarInfo = [
("cards", "/cards"),
("test", "/tests"),
("subtest", "/subtests"),
("type", "/types"),
("labels", "/labels"),
("dates", "/dates"),
("exclude", "/includeDates"),
("graph", "/graph")
]
def genNavbar(request, currentPage):
toRet = [("Home", "/")]
for (cookieName, route) in navbarInfo:
cookie = request.cookies.get(cookieName)
if cookie:
if cookieName == "exclude":
cookie = "include"
toRet.append((cookie, route))
elif cookieName == "dates":
start = request.cookies.get("start")
end = request.cookies.get("end")
if start and end:
toRet.append((start + " to " + end, route))
elif cookieName == currentPage:
toRet.append((currentPage, route))
return toRet
@app.route("/")
def index():
return render_template("index.html",
navbar = [
("Home","/"),
("Documentation","https://github.com/KPostOffice/qeWebsite"),
("Online tool", "/cards")
],
curr="Home"), 200
"""
These methods are for dynamically generating valid lists based on current
input. Information is located in charts.json
"""
###############################################################################
@app.route("/charts", methods = ["GET"])
def getChart():
file = open("charts.json")
return jsonify(json.loads(file.read()))
@app.route("/valid/tests", methods = ["GET"])
def getTestReg():
return jsonify(list(valid.validTests())), 200
@app.route("/valid/cards", methods = ["GET"])
def getCardsReg():
return jsonify(list(valid.validCards())), 200
@app.route("/valid/subtests", methods = ["GET"])
def getSubtestReg():
args = request.args
return jsonify(list(valid.validSubtests(args["test"]))), 200
@app.route("/valid/types", methods = ["GET"])
def getTypesReg():
args = request.args
return jsonify(list(valid.validTypes(args["test"], args["subtest"]))), 200
@app.route("/valid/labels", methods = ["GET"])
def getLabelsReg():
args = request.args
return jsonify(list(valid.validLabels(args["test"], args["subtest"], args["type"]))), 200
###############################################################################
"""
This route takes two dates and runs an update on the collection QE_Test_Data
"""
###############################################################################
@app.route("/update", methods = ["POST"])
def update():
args = request.form
if(not "start" in args or not re.match(dateRegex,args["start"]) or ("end" in args and not re.match(dateRegex, args["end"]))):
return jsonify({"400 error": "Bad request"}), 400
start = args["start"]
end = args["start"] if not "end" in args else args["end"]
subprocess.Popen(["python3.4", "dbUpdate.py", "-s", start, "-e", end])
return "OK", 200
###############################################################################
"""
Take any number of kwargs and run a search using them. Fields must match
those that are in the collection QE_Test_Data to get results
"""
###############################################################################
@app.route("/data", methods = ["GET"])
def getData():
args = request.args
query = {}
if("test" in args):
query["test"] = args["test"]
if("type" in args):
query["type"] = args["type"]
if("cardName" in args):
query["cardName"] = args["cardName"]
if("subtest" in args):
query["subtest"] = args["subtest"]
if("epochEnd" in args or "epochStart" in args):
query["datetime"] = {}
if("epochEnd" in args):
query["datetime"]["$lte"] = float(args["epochEnd"])
if("epochStart" in args):
query["datetime"]["$gte"] = float(args["epochStart"])
if("sheetId" in args):
query["sheetId"] = args["sheetId"]
data = (collection.find(
query,
projection = {
"_id": False,
"sheetId": True,
"data": True,
"datetime": True,
"test": True,
"subtest": True,
"type": True
})).sort("datetime", 1)
return json.dumps(list(data)), 200
###############################################################################
"""These paths are for front end for people looking for interactive graphs"""
###############################################################################
@app.route("/cards", methods = ["GET"])
def genFormCards():
data = list(valid.validCards())
data.sort()
cards = request.cookies.get("cards")
if not cards:
cards = "cards"
return render_template("card.html", result = data,
navbar = genNavbar(request, cards),
curr = cards), 200
@app.route("/tests", methods = ["GET"])
def genFormTests():
cards = request.cookies.get("cards").split(",")
test = request.cookies.get("test")
if not test:
test = "test"
# Validity check/rerouting
###########################################################################
if(not cards):
return render_template("reroute.html", page= "cards")
###########################################################################
data = list(valid.validTests())
data.sort()
response = make_response(render_template("test.html", result = data,
navbar = genNavbar(request, test),
curr = test))
return response, 200
@app.route("/subtests", methods = ["GET"])
def genFormSubtests():
test = request.cookies.get("test")
cards = request.cookies.get("cards").split(",")
subtest = request.cookies.get("subtest")
if not subtest:
subtest = "subtest"
# Validity check/rerouting
###########################################################################
if(not cards):
return render_template("reroute.html", page = "cards")
elif not test:
return render_template("reroute.html", page = "tests")
###########################################################################
data = list(valid.validSubtests(test))
data.sort()
response = make_response(render_template("subtest.html", result = data,
navbar = genNavbar(request, subtest),
curr = subtest))
return response, 200
@app.route("/types", methods = ["GET"])
def genFormTypes():
subtest = request.cookies.get("subtest")
test = request.cookies.get("test")
cards = request.cookies.get("cards").split(",")
type = request.cookies.get("type")
if not type:
type = "type"
# Validity check/rerouting
###########################################################################
if(not cards):
return render_template("reroute.html", page = "cards")
elif not test:
return render_template("reroute.html", page = "tests")
elif not subtest or not subtest in valid.validSubtests(test):
return render_template("reroute.html", page = "subtests")
###########################################################################
data = list(valid.validTypes(test, subtest))
data.sort()
response = make_response(render_template("type.html", result = data,
navbar = genNavbar(request, type),
curr = type))
return response, 200
@app.route("/labels", methods = ["GET"])
def genFormLabels():
type = request.cookies.get("type")
subtest = request.cookies.get("subtest")
test = request.cookies.get("test")
cards = request.cookies.get("cards").split(",")
labels = request.cookies.get("labels")
if not labels:
labels = "labels"
# Validity check/rerouting
###########################################################################
if(not cards):
return render_template("reroute.html", page = "cards")
elif not test:
return render_template("reroute.html", page = "tests")
elif not subtest or not subtest in valid.validSubtests(test):
return render_template("reroute.html", page = "subtests")
elif not type or not type in valid.validTypes(test, subtest):
return render_template("reroute.html", page = "type")
###########################################################################
data = list(valid.validLabels(test, subtest, type))
data.sort()
response = make_response(render_template("label.html", result = data,
navbar = genNavbar(request, labels),
curr = labels))
return response, 200
@app.route("/dates", methods = ["GET"])
def genDatePage():
labels = request.cookies.get("labels").split(",")
type = request.cookies.get("type")
subtest = request.cookies.get("subtest")
test = request.cookies.get("test")
cards = request.cookies.get("cards").split(",")
start = request.cookies.get("start")
end = request.cookies.get("end")
if not start or not end:
dates = "dates"
else:
dates = start + " to " + end
# Validity check/rerouting
###########################################################################
if(not cards):
return render_template("reroute.html", page = "cards")
elif not test:
return render_template("reroute.html", page = "tests")
elif not subtest or not subtest in valid.validSubtests(test):
return render_template("reroute.html", page = "subtests")
elif not type or not type in valid.validTypes(test,subtest):
return render_template("reroute.html", page = "type")
elif not labels or not set(labels) <= set(valid.validLabels(test, subtest, type)):
return render_template("reroute.html", page = "Labels")
###########################################################################
response = make_response(render_template("date.html",
navbar = genNavbar(request, dates),
curr = dates))
return response, 200
@app.route("/graph", methods=["GET"])
def genGraph():
start = request.cookies.get("start")
end = request.cookies.get("end")
update = request.cookies.get("update") == "on"
cards=request.cookies.get("cards").split(",")
test=request.cookies.get("test")
subtest=request.cookies.get("subtest")
type=request.cookies.get("type")
labels=request.cookies.get("labels").split(",")
start_month = start[0:7]
end_month = end[0:7]
print(start_month)
if update:
print(subprocess.Popen(["python3.4", "dbUpdate.py", "-s", start_month, "-e", end_month]))
response = make_response(render_template("graph.html",
navbar = genNavbar(request, "Graph"),
curr="Graph"))
return response, 200
@app.route("/includeDates", methods = ["GET"])
def getIncludeDates():
start = [int(i) for i in request.cookies.get("start").split("-")]
end = [int(i) for i in request.cookies.get("end").split("-")]
query = {}
query["test"] = request.cookies.get("test")
query["type"] = request.cookies.get("type")
query["cardName"] = {"$in": request.cookies.get("cards").split(",")}
query["subtest"] = request.cookies.get("subtest")
query["datetime"] = {}
query["datetime"]["$lte"] = int((datetime.datetime(year = end[0], month = end[1], day = end[2]) - datetime.datetime.utcfromtimestamp(0)).total_seconds())
query["datetime"]["$gte"] = int((datetime.datetime(year = start[0], month = start[1], day = start[2]) - datetime.datetime.utcfromtimestamp(0)).total_seconds())
labels = request.cookies.get("labels").split(",")
tempData = (collection.find(
query,
projection = {
"_id": False,
"datetime": True,
"cardName": True,
"sheetId": True,
"data": True
})).sort("datetime", 1)
data = []
for d in tempData:
currLabels = list(d["data"].keys())
for lab in currLabels:
if lab in labels:
data.append(d)
response = make_response(render_template("includeDate.html", result=[
[
datetime.datetime.utcfromtimestamp(i["datetime"]).date().isoformat() + " : " + i["cardName"],
i["sheetId"]
] for i in data], ## Pass sheet ID because it is more reliable at getting the correct datapoints than date alone
navbar = genNavbar(request, "exclude"),
curr = "include"))
return response, 200
###############################################################################
@app.route("/js/<fileName>", methods = ["GET"])
def getJavascript(fileName):
return render_template("js/" + fileName), 200
@app.route("/css/<fileName>", methods = ["GET"])
def getCss(fileName):
return render_template("css/" + fileName), 200, {"Content-Type": "text/css"};
"""
Gets data for interactive graph. Gets all datapoints rather than a range
because it"s easier to store the extra data than reload everytime the graph
is changed
"""
###############################################################################
@app.route("/genData", methods=["GET"])
def getSimpleData():
query = {
"card": card,
"test": test,
"subtest": subtest,
"type": type,
"label": label
}
date = collection.find(
query,
projection = {
"_id": False,
"datetime": True,
"value": True
}
).sort("datetime",1)
return json.dumps(list(data)), 200
###############################################################################
if __name__ == "__main__":
app.run(debug=True)