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.
This commit is contained in:
ahosking 2022-08-04 10:44:37 -04:00
parent fb2aad93fd
commit 85a03dc098

View File

@ -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")'
|> filter(fn: (r) => r["_field"] == "odometer")\
|> sort(columns: ["_value"], desc: false)\
|> limit(n:10)'
#query = 'select TOP(odometer, 5) from odyssey'
#data = client.query(query)
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)