influx_stats/influx_frontend.py
2017-04-09 18:36:01 -04:00

89 lines
2.2 KiB
Python

from flask import Flask, request, render_template, redirect, url_for
from time import strftime
import datetime
app = Flask(__name__)
from influxdb import InfluxDBClient
def db_main(host='localhost', port=8086):
user = 'root'
password = 'root'
dbname = 'gas'
@app.route('/success')
def success():
return "Form 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)
return render_template('index.html', data=data)
@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
}
}
]
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'gas')
print ("Submitting data to DB: {0}".format(json_body))
client.write_points(json_body)
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')