influx_stats/influx_frontend.py

115 lines
3.1 KiB
Python
Raw Normal View History

2017-01-28 07:09:09 +00:00
from flask import Flask, request, render_template, redirect, url_for
2017-01-29 05:37:38 +00:00
from time import strftime
import datetime
2022-06-28 11:42:26 +00:00
import pendulum
from dotenv import dotenv_values
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
config = dotenv_values(".env")
2016-12-20 06:50:26 +00:00
2016-12-06 12:46:40 +00:00
app = Flask(__name__)
client = influxdb_client.InfluxDBClient(
url=config['DB_URL'],
token=config['DB_TOKEN'],
org=config['DB_ORG']
)
2016-12-06 12:46:40 +00:00
2016-12-20 06:50:26 +00:00
@app.route('/success')
def success():
return "Form success!"
@app.route('/', methods=['GET', 'POST'])
2016-12-20 06:50:26 +00:00
def main_page():
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")'
2022-06-28 11:42:26 +00:00
#query = 'select TOP(odometer, 5) from odyssey'
#data = client.query(query)
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)
2017-01-28 07:09:09 +00:00
@app.route('/add_time', methods=['POST', 'GET'])
def add_time():
2017-01-29 05:37:38 +00:00
today = strftime("%Y-%m-%d")
now = strftime("%H:%M:%S")
# database = request.form['db']
# table = request.form['table']
date = request.form['date']
2017-01-29 05:37:38 +00:00
if date == '':
date = today
time = request.form['time']
2017-01-29 05:37:38 +00:00
if time == '':
time = now
odometer = request.form['odometer']
oilhealth= request.form['oilhealth']
if oilhealth != '':
oilhealth = float(oilhealth)
fuel = request.form['fuel']
if fuel != '':
fuel = float(fuel)
fuelcost = request.form['fuelcost']
if fuelcost != '':
fuelcost = float(fuelcost)
winter = request.form['winter']
2017-04-09 22:36:01 +00:00
if winter == 'TRUE':
winter = True
else:
winter = False
json_body = [
{
"time": date + "T" + time + "Z",
"measurement": "odyssey",
"fields": {
2017-01-28 07:09:09 +00:00
"odometer": float(odometer),
"oilhealth": oilhealth,
"fuel": fuel,
"fuelcost": fuelcost,
2017-04-09 22:36:01 +00:00
"winter": winter
}
}
]
2022-06-28 11:42:26 +00:00
timestamp = date + "T" + time + "Z"
dt = pendulum.parse(timestamp)
print()
print(dt)
print()
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(odometer)).field("oilhealth", oilhealth)\
.field("fuel", fuel).field("fuelcost", fuelcost).field("winter", winter)
2022-06-28 11:42:26 +00:00
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)
2017-01-28 07:09:09 +00:00
return redirect(url_for('main_page'))
@app.route('/admin')
def admin_index():
return 'admin'
@app.route('/login')
def login():
pass
@app.route('/logout')
def logout():
pass
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port='9001')