Make data unique and in order

This commit is contained in:
ahosking 2022-08-12 02:26:11 -04:00
parent 7d6bd7cc70
commit d2bda44411

View File

@ -12,30 +12,29 @@ config = dotenv_values(".env")
app = Flask(__name__)
client = influxdb_client.InfluxDBClient(
url=config['DB_URL'],
token=config['DB_TOKEN'],
org=config['DB_ORG']
)
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")\
|> range(start: -30d)\
|> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\
|> filter(fn: (r) => r["_field"] == "odometer")\
|> sort(columns: ["_value"], desc: false)\
|> yield(name: "last")\
|> 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:
@ -46,15 +45,25 @@ def main_page():
temp['oilhealth'] = record['oilhealth']
except KeyError:
pass
try:
temp['fuel'] = record['fuel']
temp['fuelcost'] = record['fuelcost']
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)
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])
@app.route('/add_time', methods=['POST', 'GET'])
def add_time():
@ -69,7 +78,7 @@ def add_time():
if time == '':
time = now
odometer = request.form['odometer']
oilhealth= request.form['oilhealth']
oilhealth = request.form['oilhealth']
if oilhealth != '':
oilhealth = float(oilhealth)
fuel = request.form['fuel']
@ -85,19 +94,19 @@ def add_time():
winter = False
json_body = [
{
"time": date + "T" + time + "Z",
"measurement": "odyssey",
"fields": {
"odometer": float(odometer),
"oilhealth": oilhealth,
"fuel": fuel,
"fuelcost": fuelcost,
"winter": winter
}
{
"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()
@ -107,13 +116,12 @@ def add_time():
point = 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=point)
print ("Submitting data to DB: {0}".format(json_body))
#client.write_points(json_body)
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/<odometer>')
def repeat_last_odometer(odometer):
'''Retrieve the passed in odometer value and submit as a new entry'''
@ -123,21 +131,26 @@ def repeat_last_odometer(odometer):
print("Putting %s to database for %s" % (odometer, timestamp))
write_api = client.write_api(write_options=SYNCHRONOUS)
point = influxdb_client.Point("2016_odyssey").time(timestamp).field("odometer", float(odometer))
point = influxdb_client.Point("2016_odyssey").time(
timestamp).field("odometer", float(odometer))
write_api.write(bucket="gas", org=config['DB_ORG'], record=point)
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')