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
|
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
|
2022-08-04 14:44:37 +00:00
|
|
|
from operator import itemgetter
|
2022-06-28 11:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
config = dotenv_values(".env")
|
2016-12-20 06:50:26 +00:00
|
|
|
|
2016-12-06 12:46:40 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2022-07-03 04:31:01 +00:00
|
|
|
client = influxdb_client.InfluxDBClient(
|
2022-08-12 06:26:11 +00:00
|
|
|
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!"
|
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2016-12-20 06:50:26 +00:00
|
|
|
def main_page():
|
2022-07-03 04:31:01 +00:00
|
|
|
query_api = client.query_api()
|
|
|
|
query = 'from(bucket: "gas")\
|
2022-08-12 06:26:11 +00:00
|
|
|
|> range(start: -30d)\
|
|
|
|
|> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\
|
|
|
|
|> filter(fn: (r) => r["_field"] == "odometer")\
|
2022-08-04 14:44:37 +00:00
|
|
|
|> sort(columns: ["_value"], desc: false)\
|
2022-08-12 06:26:11 +00:00
|
|
|
|> yield(name: "last")\
|
2022-08-04 14:44:37 +00:00
|
|
|
|> limit(n:10)'
|
|
|
|
|
2022-07-03 04:31:01 +00:00
|
|
|
result = query_api.query(org=config['DB_ORG'], query=query)
|
|
|
|
results = []
|
|
|
|
for table in result:
|
|
|
|
for record in table.records:
|
2022-08-04 14:44:37 +00:00
|
|
|
temp = {}
|
|
|
|
temp[record.get_field()] = record.get_value()
|
|
|
|
temp['time'] = record.get_time()
|
|
|
|
try:
|
|
|
|
temp['oilhealth'] = record['oilhealth']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2022-08-12 06:26:11 +00:00
|
|
|
try:
|
|
|
|
temp['fuel'] = record['fuel']
|
|
|
|
temp['fuelcost'] = record['fuelcost']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2022-08-04 14:44:37 +00:00
|
|
|
results.append(temp)
|
|
|
|
# .append((record.get_time(), record.get_field(), record.get_value()))
|
|
|
|
newlist = sorted(results, key=itemgetter('time'), reverse=True)
|
2022-08-12 06:26:11 +00:00
|
|
|
oldlist = newlist # temporary to strip the non-unique data
|
|
|
|
newlist = []
|
|
|
|
for value in oldlist:
|
|
|
|
if value in newlist:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
newlist.append(value)
|
|
|
|
|
|
|
|
# print(newlist)
|
|
|
|
return render_template('index.html', data=newlist[:10])
|
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
|
2017-01-28 07:09:09 +00:00
|
|
|
@app.route('/add_time', methods=['POST', 'GET'])
|
2016-12-27 03:43:38 +00:00
|
|
|
def add_time():
|
2017-01-29 05:37:38 +00:00
|
|
|
today = strftime("%Y-%m-%d")
|
|
|
|
now = strftime("%H:%M:%S")
|
2016-12-27 14:30:20 +00:00
|
|
|
# database = request.form['db']
|
|
|
|
# table = request.form['table']
|
|
|
|
date = request.form['date']
|
2017-01-29 05:37:38 +00:00
|
|
|
if date == '':
|
|
|
|
date = today
|
2016-12-27 14:30:20 +00:00
|
|
|
time = request.form['time']
|
2017-01-29 05:37:38 +00:00
|
|
|
if time == '':
|
|
|
|
time = now
|
2016-12-27 14:30:20 +00:00
|
|
|
odometer = request.form['odometer']
|
2022-08-12 06:26:11 +00:00
|
|
|
oilhealth = request.form['oilhealth']
|
2017-02-03 05:32:23 +00:00
|
|
|
if oilhealth != '':
|
|
|
|
oilhealth = float(oilhealth)
|
2016-12-27 14:30:20 +00:00
|
|
|
fuel = request.form['fuel']
|
2017-01-29 05:16:51 +00:00
|
|
|
if fuel != '':
|
|
|
|
fuel = float(fuel)
|
2016-12-27 14:30:20 +00:00
|
|
|
fuelcost = request.form['fuelcost']
|
2017-01-29 05:16:51 +00:00
|
|
|
if fuelcost != '':
|
|
|
|
fuelcost = float(fuelcost)
|
2016-12-27 14:30:20 +00:00
|
|
|
winter = request.form['winter']
|
2017-04-09 22:36:01 +00:00
|
|
|
if winter == 'TRUE':
|
|
|
|
winter = True
|
|
|
|
else:
|
|
|
|
winter = False
|
2016-12-27 14:30:20 +00:00
|
|
|
|
|
|
|
json_body = [
|
2022-08-12 06:26:11 +00:00
|
|
|
{
|
|
|
|
"time": date + "T" + time + "Z",
|
|
|
|
"measurement": "odyssey",
|
|
|
|
"fields": {
|
|
|
|
"odometer": float(odometer),
|
|
|
|
"oilhealth": oilhealth,
|
|
|
|
"fuel": fuel,
|
|
|
|
"fuelcost": fuelcost,
|
|
|
|
"winter": winter
|
2016-12-27 14:30:20 +00:00
|
|
|
}
|
2022-08-12 06:26:11 +00:00
|
|
|
}
|
2016-12-27 14:30:20 +00:00
|
|
|
]
|
2022-08-12 06:26:11 +00:00
|
|
|
|
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)
|
2022-08-12 06:05:13 +00:00
|
|
|
point = influxdb_client.Point("2016_odyssey").time(timestamp).tag("oilhealth", oilhealth)\
|
2022-07-03 05:20:43 +00:00
|
|
|
.tag("fuel", fuel).tag("fuelcost", fuelcost).tag("winter", winter).field("odometer", float(odometer))
|
2022-08-12 06:05:13 +00:00
|
|
|
write_api.write(bucket="gas", org=config['DB_ORG'], record=point)
|
2022-08-12 06:26:11 +00:00
|
|
|
print("Submitting data to DB: {0}".format(json_body))
|
|
|
|
# client.write_points(json_body)
|
2016-12-27 14:30:20 +00:00
|
|
|
|
2022-07-15 05:05:23 +00:00
|
|
|
return redirect(url_for('main_page'))
|
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2022-08-12 06:05:13 +00:00
|
|
|
@app.route('/repeat_last_odometer/<odometer>')
|
|
|
|
def repeat_last_odometer(odometer):
|
|
|
|
'''Retrieve the passed in odometer value and submit as a new entry'''
|
2022-07-15 05:05:23 +00:00
|
|
|
date = strftime("%Y-%m-%d")
|
|
|
|
time = strftime("%H:%M:%S")
|
|
|
|
timestamp = date + "T" + time + "Z"
|
2022-08-12 06:05:13 +00:00
|
|
|
print("Putting %s to database for %s" % (odometer, timestamp))
|
2022-07-15 05:05:23 +00:00
|
|
|
|
|
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
2022-08-12 06:26:11 +00:00
|
|
|
point = influxdb_client.Point("2016_odyssey").time(
|
|
|
|
timestamp).field("odometer", float(odometer))
|
2022-08-12 06:05:13 +00:00
|
|
|
write_api.write(bucket="gas", org=config['DB_ORG'], record=point)
|
2017-01-28 07:09:09 +00:00
|
|
|
return redirect(url_for('main_page'))
|
2016-12-27 03:43:38 +00:00
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
@app.route('/admin')
|
|
|
|
def admin_index():
|
|
|
|
return 'admin'
|
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
@app.route('/login')
|
|
|
|
def login():
|
|
|
|
pass
|
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
pass
|
|
|
|
|
2022-08-12 06:26:11 +00:00
|
|
|
|
2016-12-27 03:43:38 +00:00
|
|
|
if __name__ == '__main__':
|
2022-07-03 04:31:01 +00:00
|
|
|
app.run(debug=True, host='0.0.0.0', port='9001')
|