From 2c461ad4f725a5bd945e8ababba5fa4cd18678b2 Mon Sep 17 00:00:00 2001 From: Alexander Hosking Date: Tue, 28 Jun 2022 07:42:26 -0400 Subject: [PATCH] Upgrade to influx2 --- docker_run.sh | 3 +++ influx_frontend.py | 36 +++++++++++++++++++++++++++++------- requirements.txt | 12 ++++++------ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 docker_run.sh diff --git a/docker_run.sh b/docker_run.sh new file mode 100644 index 0000000..9fb9ce3 --- /dev/null +++ b/docker_run.sh @@ -0,0 +1,3 @@ +docker run -p 8086:8086 \ + -v $PWD:/var/lib/influxdb2 \ + influxdb:2.0 diff --git a/influx_frontend.py b/influx_frontend.py index ca86d22..48bb070 100644 --- a/influx_frontend.py +++ b/influx_frontend.py @@ -1,9 +1,16 @@ 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 +from influxdb_client.client.write_api import SYNCHRONOUS + + +config = dotenv_values(".env") +print(config) app = Flask(__name__) -from influxdb import InfluxDBClient def db_main(host='localhost', port=8086): user = 'root' @@ -16,9 +23,14 @@ def success(): @app.route('/', methods=['GET', 'POST']) def main_page(): - client = InfluxDBClient('localhost', 8086, 'root', 'root', 'gas') - query = 'select TOP(odometer, 5) from odyssey' - data = client.query(query) + client = influxdb_client.InfluxDBClient( + url=config['DB_URL'], + token=config['DB_TOKEN'], + org=config['DB_ORG'] + ) + #query = 'select TOP(odometer, 5) from odyssey' + #data = client.query(query) + data = [] return render_template('index.html', data=data) @app.route('/add_time', methods=['POST', 'GET']) @@ -62,9 +74,19 @@ def add_time(): } } ] - - client = InfluxDBClient('localhost', 8086, 'root', 'root', 'gas') - + timestamp = date + "T" + time + "Z" + dt = pendulum.parse(timestamp) + 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)) + 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) diff --git a/requirements.txt b/requirements.txt index ecb08d2..d831ddb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,14 @@ -wheel==0.29.0 +certifi==2022.6.15 click==6.6 -Flask==0.11.1 -Flask-WTF==0.13.1 -influxdb==4.0.0 +influxdb-client==1.30.0 itsdangerous==0.24 -Jinja2==2.8 MarkupSafe==0.23 -python-dateutil==2.6.0 +pendulum==2.1.2 pytz==2016.10 +pytzdata==2020.1 requests==2.12.4 +Rx==3.2.0 six==1.10.0 +urllib3==1.26.9 Werkzeug==0.11.11 WTForms==2.1