1
1
from flask import Flask , jsonify , render_template , g , request
2
2
3
+ from sqlalchemy import func , text
4
+
3
5
from .db import Session , APCReading
4
6
from .config import Config
5
7
8
+ freq_opts = {
9
+ "hourly" : "%Y-%m-%d %H:00:00.000000" ,
10
+ "daily" : "%Y-%m-%d 00:00:00.000000" ,
11
+ "monthly" : "%Y-%m-01 00:00:00.000000" ,
12
+ }
13
+
6
14
7
15
def create_app (config = Config ):
8
16
app = Flask (__name__ )
@@ -20,11 +28,18 @@ def teardown_request(exception):
20
28
21
29
@app .get ("/" )
22
30
def index ():
23
- readings = g .session .query (APCReading ).all ()
31
+ freq = request .args .get ("freq" , "hourly" )
32
+
33
+ statement = text (
34
+ "SELECT id, STRFTIME('{0}', date) as date, AVG(load / no_logs) as load FROM apc_reading GROUP BY STRFTIME('{0}', date)" .format (
35
+ freq_opts .get (freq ) or freq_opts ["hourly" ]
36
+ )
37
+ )
38
+ readings = g .session .query (APCReading ).from_statement (statement ).all ()
24
39
readings_combined = "Date,Load\\ n"
25
40
for reading in readings :
26
41
readings_combined = readings_combined + "{},{}\\ n" .format (
27
- reading .date , round (reading .load / reading . no_logs )
42
+ reading .date , round (reading .load ),
28
43
)
29
44
return render_template ("index.html" , readings = readings_combined )
30
45
0 commit comments