Add Repeat functionality

This commit is contained in:
ahosking 2022-08-12 02:05:13 -04:00
parent 85a03dc098
commit 66a031d9a2
2 changed files with 10 additions and 37 deletions

View File

@ -1,6 +1,5 @@
from flask import Flask, request, render_template, redirect, url_for
from time import strftime
import datetime
import pendulum
from dotenv import dotenv_values
import influxdb_client
@ -105,9 +104,9 @@ def add_time():
print(dt)
print()
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("2016_odyssey").time(timestamp).tag("oilhealth", oilhealth)\
point = influxdb_client.Point("2016_odyssey").time(timestamp).tag("oilhealth", oilhealth)\
.tag("fuel", fuel).tag("fuelcost", fuelcost).tag("winter", winter).field("odometer", float(odometer))
write_api.write(bucket="gas", org=config['DB_ORG'], record=p)
write_api.write(bucket="gas", org=config['DB_ORG'], record=point)
print ("Submitting data to DB: {0}".format(json_body))
#client.write_points(json_body)
@ -115,43 +114,17 @@ def add_time():
return redirect(url_for('main_page'))
@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")\
|> sort(columns: ["_value"], desc: false)\
|> last()\
|> limit(n:10)'
result = query_api.query(org=config['DB_ORG'], query=query)
print()
print()
print(result)
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)
print()
@app.route('/repeat_last_odometer/<odometer>')
def repeat_last_odometer(odometer):
'''Retrieve the passed in odometer value and submit as a new entry'''
date = strftime("%Y-%m-%d")
time = strftime("%H:%M:%S")
timestamp = date + "T" + time + "Z"
print(results['odometer'])
print("Putting %s to database for %s" % (odometer, timestamp))
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(results['odometer']))
write_api.write(bucket="gas", org=config['DB_ORG'], record=p)
point = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(odometer))
write_api.write(bucket="gas", org=config['DB_ORG'], record=point)
return redirect(url_for('main_page'))
@app.route('/admin')

View File

@ -54,7 +54,7 @@
</div>
</div><br>
<input type="submit" value="Go">
<button><a href="{{ url_for('repeat_last_odometer') }}">Repeat</a></button>
<a href="{{ url_for('repeat_last_odometer', odometer=data[0]['odometer'] ) }}">Repeat</a>
</div>
</form>
<br>
@ -62,7 +62,7 @@ Last Mileage
<ul>
{% for point in data %}
<li>
{{ point[0] }} = {{ point[1] }}, {{point[2]}}
{{ point['time'] }} = {{ point['odometer'] }}, {{point['oilhealth']}}
</li>
{% endfor %}
{% endblock %}