-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (61 loc) · 1.73 KB
/
app.py
File metadata and controls
77 lines (61 loc) · 1.73 KB
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
import datetime
import threading
from flask import Flask, jsonify
import sys
from apiworker import update_db
from grapher import plot_vals
from models import GageHeight, DischargeRate
from flask_apscheduler import APScheduler
from descriptor import make_descriptions
import base64
import os
app = Flask(__name__)
logger_lock = threading.Lock()
class Config(object):
JOBS = [
{
'id': 'job1',
'func': 'app:apiworker_update',
'args': (),
'trigger': 'interval',
'seconds': 3600 # Update the database once an hour
}
]
def log(string):
with logger_lock:
log = open('log.txt', 'a')
now = datetime.datetime.now()
print(str(now), end=": ", file=sys.stderr)
print(string, file=sys.stderr)
print(str(now), end=": ", file=log)
print(string, file=log)
log.close()
def apiworker_update():
log("Updating database...")
update_db(1)
log("Finished updating database!")
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
if not os.path.exists("./gen"):
os.makedirs("./gen")
@app.route("/")
def hello():
return app.send_static_file('main.html')
@app.route("/descriptor")
def descriptor():
d = make_descriptions()
return jsonify(d)
@app.route("/graph/<graph_type>/<int:days>")
def generate_graph(graph_type, days):
if graph_type == "GageHeight":
plot_vals(GageHeight, days)
elif graph_type == "DischargeRate":
plot_vals(DischargeRate, days)
filename = "./gen/" + graph_type + ".png"
with open(filename, 'rb') as img:
data = img.read()
return base64.b64encode(data)
if __name__ == "__main__":
app.run()