Skip to content

Commit cc6a68a

Browse files
committed
adds hourly, daily, monthly aggregations
1 parent 99cc91a commit cc6a68a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

apc/templates/index.html

+17
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,27 @@
1414
margin: 0;
1515
overflow: hidden;
1616
}
17+
18+
ul {
19+
line-height: 28px;
20+
font-size: 16px;
21+
margin: 0;
22+
position: absolute;
23+
z-index: 10;
24+
}
25+
26+
ul>li {
27+
display: inline-block;
28+
}
1729
</style>
1830
</head>
1931

2032
<body>
33+
<ul>
34+
<li><a href="?freq=hourly">Hourly</a></li>
35+
<li><a href="?freq=daily">Daily</a></li>
36+
<li><a href="?freq=monthly">Monthly</a></li>
37+
</ul>
2138
<div id="div_g" style="width: 100%; height: 99%;"></div>
2239
<script type="text/javascript" src="{{ url_for('static', filename='dygraph/dygraph.min.js') }}"></script>
2340
<script type="text/javascript">

apc/web.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
from flask import Flask, jsonify, render_template, g, request
22

3+
from sqlalchemy import func, text
4+
35
from .db import Session, APCReading
46
from .config import Config
57

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+
614

715
def create_app(config=Config):
816
app = Flask(__name__)
@@ -20,11 +28,18 @@ def teardown_request(exception):
2028

2129
@app.get("/")
2230
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()
2439
readings_combined = "Date,Load\\n"
2540
for reading in readings:
2641
readings_combined = readings_combined + "{},{}\\n".format(
27-
reading.date, round(reading.load / reading.no_logs)
42+
reading.date, round(reading.load),
2843
)
2944
return render_template("index.html", readings=readings_combined)
3045

0 commit comments

Comments
 (0)