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 flask import Flask, request, render_template, redirect, url_for
from time import strftime from time import strftime
import datetime
import pendulum import pendulum
from dotenv import dotenv_values from dotenv import dotenv_values
import influxdb_client import influxdb_client
@ -105,9 +104,9 @@ def add_time():
print(dt) print(dt)
print() print()
write_api = client.write_api(write_options=SYNCHRONOUS) 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)) .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)) print ("Submitting data to DB: {0}".format(json_body))
#client.write_points(json_body) #client.write_points(json_body)
@ -115,43 +114,17 @@ def add_time():
return redirect(url_for('main_page')) return redirect(url_for('main_page'))
@app.route('/repeat_last_odometer') @app.route('/repeat_last_odometer/<odometer>')
def repeat_last_odometer(): def repeat_last_odometer(odometer):
#TODO: Can we pass in the data from the loaded page to avoid another query for lookup? '''Retrieve the passed in odometer value and submit as a new entry'''
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()
date = strftime("%Y-%m-%d") date = strftime("%Y-%m-%d")
time = strftime("%H:%M:%S") time = strftime("%H:%M:%S")
timestamp = date + "T" + time + "Z" timestamp = date + "T" + time + "Z"
print("Putting %s to database for %s" % (odometer, timestamp))
print(results['odometer'])
write_api = client.write_api(write_options=SYNCHRONOUS) write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(results['odometer'])) point = influxdb_client.Point("2016_odyssey").time(timestamp).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)
return redirect(url_for('main_page')) return redirect(url_for('main_page'))
@app.route('/admin') @app.route('/admin')

View File

@ -54,7 +54,7 @@
</div> </div>
</div><br> </div><br>
<input type="submit" value="Go"> <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> </div>
</form> </form>
<br> <br>
@ -62,7 +62,7 @@ Last Mileage
<ul> <ul>
{% for point in data %} {% for point in data %}
<li> <li>
{{ point[0] }} = {{ point[1] }}, {{point[2]}} {{ point['time'] }} = {{ point['odometer'] }}, {{point['oilhealth']}}
</li> </li>
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}