From 85a03dc098c0a8bfb0fb628e06f0845e666ff23c Mon Sep 17 00:00:00 2001 From: ahosking Date: Thu, 4 Aug 2022 10:44:37 -0400 Subject: [PATCH] Combine queries and sort by date There is lots of debugging output here to get rid of but, this change helps to ensure that on the index page, we sort by date to ensure our data looks correct. This also attempts to get the last entry for easy duplication but this is limited by the time specified and needs to be fixed. --- influx_frontend.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/influx_frontend.py b/influx_frontend.py index 2af2f5e..791a8e6 100644 --- a/influx_frontend.py +++ b/influx_frontend.py @@ -5,6 +5,7 @@ import pendulum from dotenv import dotenv_values import influxdb_client from influxdb_client.client.write_api import SYNCHRONOUS +from operator import itemgetter config = dotenv_values(".env") @@ -25,21 +26,36 @@ def success(): def main_page(): query_api = client.query_api() query = 'from(bucket: "gas")\ - |> range(start: -96h)\ + |> range(start: -30d)\ |> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\ - |> filter(fn: (r) => r["_field"] == "odometer")' - - #query = 'select TOP(odometer, 5) from odyssey' - #data = client.query(query) + |> filter(fn: (r) => r["_field"] == "odometer")\ + |> sort(columns: ["_value"], desc: false)\ + |> limit(n:10)' + result = query_api.query(org=config['DB_ORG'], query=query) - # print(result) + print() + print() + print(result) + print() results = [] for table in result: for record in table.records: - results.append((record.get_time(), record.get_field(), record.get_value())) - # print(results) + temp = {} + temp[record.get_field()] = record.get_value() + temp['time'] = record.get_time() + try: + temp['oilhealth'] = record['oilhealth'] + except KeyError: + pass + results.append(temp) + # .append((record.get_time(), record.get_field(), record.get_value())) + print(results) + print(len(results)) + print() + newlist = sorted(results, key=itemgetter('time'), reverse=True) + print(newlist) data= results - return render_template('index.html', data=data) + return render_template('index.html', data=newlist) @app.route('/add_time', methods=['POST', 'GET']) def add_time(): @@ -101,12 +117,15 @@ def add_time(): @app.route('/repeat_last_odometer') def repeat_last_odometer(): + #TODO: Can we pass in the data from the loaded page to avoid another query for lookup? query_api = client.query_api() query = 'from(bucket: "gas")\ |> range(start: -7d)\ |> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\ |> filter(fn: (r) => r["_field"] == "odometer")\ - |> last()' + |> sort(columns: ["_value"], desc: false)\ + |> last()\ + |> limit(n:10)' result = query_api.query(org=config['DB_ORG'], query=query) print() @@ -115,8 +134,10 @@ def repeat_last_odometer(): print() results = {} for table in result: + print(table) for record in table.records: results[record.get_field()] = record.get_value() + results[record.get_] results['time'] = record.get_time() # .append((record.get_time(), record.get_field(), record.get_value())) print(results)