From 94c493fa9bbf3a449c9a1e9ee8a393f7f21e47d7 Mon Sep 17 00:00:00 2001 From: ahosking Date: Sun, 3 Jul 2022 00:31:01 -0400 Subject: [PATCH] Major change to Influx 2 This includes new queries for data, New queries for creating/submitting data and some new code that needs to be refactored --- influx_frontend.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/influx_frontend.py b/influx_frontend.py index 48bb070..a4d6bf7 100644 --- a/influx_frontend.py +++ b/influx_frontend.py @@ -8,14 +8,14 @@ from influxdb_client.client.write_api import SYNCHRONOUS config = dotenv_values(".env") -print(config) app = Flask(__name__) -def db_main(host='localhost', port=8086): - user = 'root' - password = 'root' - dbname = 'gas' +client = influxdb_client.InfluxDBClient( + url=config['DB_URL'], + token=config['DB_TOKEN'], + org=config['DB_ORG'] + ) @app.route('/success') def success(): @@ -23,14 +23,22 @@ def success(): @app.route('/', methods=['GET', 'POST']) def main_page(): - client = influxdb_client.InfluxDBClient( - url=config['DB_URL'], - token=config['DB_TOKEN'], - org=config['DB_ORG'] - ) + query_api = client.query_api() + query = 'from(bucket: "gas")\ + |> range(start: -96h)\ + |> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\ + |> filter(fn: (r) => r["_field"] == "odometer")' + #query = 'select TOP(odometer, 5) from odyssey' #data = client.query(query) - data = [] + result = query_api.query(org=config['DB_ORG'], query=query) + print(result) + results = [] + for table in result: + for record in table.records: + results.append((record.get_time(), record.get_field(), record.get_value())) + print(results) + data= results return render_template('index.html', data=data) @app.route('/add_time', methods=['POST', 'GET']) @@ -79,16 +87,12 @@ def add_time(): print() print(dt) print() - client = influxdb_client.InfluxDBClient( - url=config['DB_URL'], - token=config['DB_TOKEN'], - org=config['DB_ORG'] - ) write_api = client.write_api(write_options=SYNCHRONOUS) - p = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(odometer)) + p = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(odometer)).field("oilhealth", oilhealth)\ + .field("fuel", fuel).field("fuelcost", fuelcost).field("winter", winter) write_api.write(bucket="gas", org=config['DB_ORG'], record=p) print ("Submitting data to DB: {0}".format(json_body)) - client.write_points(json_body) + #client.write_points(json_body) @@ -107,4 +111,4 @@ def logout(): pass if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0') + app.run(debug=True, host='0.0.0.0', port='9001')