85a03dc098
There is lots of debugging output here to get rid of but, this change helps to ensure that on the index page, we sort by date to ensure our data looks correct. This also attempts to get the last entry for easy duplication but this is limited by the time specified and needs to be fixed.
171 lines
4.9 KiB
Python
171 lines
4.9 KiB
Python
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
|
|
from operator import itemgetter
|
|
|
|
|
|
config = dotenv_values(".env")
|
|
|
|
app = Flask(__name__)
|
|
|
|
client = influxdb_client.InfluxDBClient(
|
|
url=config['DB_URL'],
|
|
token=config['DB_TOKEN'],
|
|
org=config['DB_ORG']
|
|
)
|
|
|
|
@app.route('/success')
|
|
def success():
|
|
return "Form success!"
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def main_page():
|
|
query_api = client.query_api()
|
|
query = 'from(bucket: "gas")\
|
|
|> range(start: -30d)\
|
|
|> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\
|
|
|> filter(fn: (r) => r["_field"] == "odometer")\
|
|
|> sort(columns: ["_value"], desc: false)\
|
|
|> limit(n:10)'
|
|
|
|
result = query_api.query(org=config['DB_ORG'], query=query)
|
|
print()
|
|
print()
|
|
print(result)
|
|
print()
|
|
results = []
|
|
for table in result:
|
|
for record in table.records:
|
|
temp = {}
|
|
temp[record.get_field()] = record.get_value()
|
|
temp['time'] = record.get_time()
|
|
try:
|
|
temp['oilhealth'] = record['oilhealth']
|
|
except KeyError:
|
|
pass
|
|
results.append(temp)
|
|
# .append((record.get_time(), record.get_field(), record.get_value()))
|
|
print(results)
|
|
print(len(results))
|
|
print()
|
|
newlist = sorted(results, key=itemgetter('time'), reverse=True)
|
|
print(newlist)
|
|
data= results
|
|
return render_template('index.html', data=newlist)
|
|
|
|
@app.route('/add_time', methods=['POST', 'GET'])
|
|
def add_time():
|
|
today = strftime("%Y-%m-%d")
|
|
now = strftime("%H:%M:%S")
|
|
# database = request.form['db']
|
|
# table = request.form['table']
|
|
date = request.form['date']
|
|
if date == '':
|
|
date = today
|
|
time = request.form['time']
|
|
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']
|
|
if winter == 'TRUE':
|
|
winter = True
|
|
else:
|
|
winter = False
|
|
|
|
json_body = [
|
|
{
|
|
"time": date + "T" + time + "Z",
|
|
"measurement": "odyssey",
|
|
"fields": {
|
|
"odometer": float(odometer),
|
|
"oilhealth": oilhealth,
|
|
"fuel": fuel,
|
|
"fuelcost": fuelcost,
|
|
"winter": winter
|
|
}
|
|
}
|
|
]
|
|
|
|
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).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)
|
|
print ("Submitting data to DB: {0}".format(json_body))
|
|
#client.write_points(json_body)
|
|
|
|
|
|
|
|
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()
|
|
|
|
date = strftime("%Y-%m-%d")
|
|
time = strftime("%H:%M:%S")
|
|
timestamp = date + "T" + time + "Z"
|
|
|
|
print(results['odometer'])
|
|
|
|
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)
|
|
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')
|