diff --git a/influx_frontend.py b/influx_frontend.py index 791a8e6..4bc1830 100644 --- a/influx_frontend.py +++ b/influx_frontend.py @@ -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/') +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') diff --git a/templates/index.html b/templates/index.html index 732fcc0..96c1199 100644 --- a/templates/index.html +++ b/templates/index.html @@ -54,7 +54,7 @@
- + Repeat
@@ -62,7 +62,7 @@ Last Mileage