Commit 6bfe542c authored by Kristian Tan's avatar Kristian Tan

Improve the way records are queried in generate_graph

parent 31eaac57
...@@ -85,18 +85,23 @@ def generate_graph_data(): ...@@ -85,18 +85,23 @@ def generate_graph_data():
max = 0 max = 0
# Create data for chart # Create data for chart
count = 0 # count = 0
records = DailyUsage.query.order_by(desc(DailyUsage.date)).all() records = DailyUsage.query.order_by(desc(DailyUsage.date)).limit(5).all()
print(records)
for record in records: for record in records:
labels.append(date(record.date.year, record.date.month, record.date.day)) labels.append(date(record.date.year, record.date.month, record.date.day))
values.append(record.kwhUsed) values.append(record.kwhUsed)
if record.kwhUsed > max: if record.kwhUsed > max:
max = record.kwhUsed max = record.kwhUsed
count += 1 # count += 1
if count >= 5: # if count >= 5:
break # break
labels.reverse()
values.reverse()
return labels, values, max return labels, values, max
db.create_all() db.create_all()
daily_total = get_todays_usage() daily_total = get_todays_usage()
...@@ -119,6 +124,7 @@ for pin in pins: ...@@ -119,6 +124,7 @@ for pin in pins:
labels, values, max = generate_graph_data() labels, values, max = generate_graph_data()
@app.route("/") @app.route("/")
def main(): def main():
# For each pin, read the pin state and store it in the pins dictionary: # For each pin, read the pin state and store it in the pins dictionary:
...@@ -246,6 +252,7 @@ def delete_pin(delete_pin): ...@@ -246,6 +252,7 @@ def delete_pin(delete_pin):
} }
return render_template('main.html', **template_data) return render_template('main.html', **template_data)
@app.route("/update_info/kWh") @app.route("/update_info/kWh")
def change_kWh(): def change_kWh():
template_data = { template_data = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment