Compare commits

..

2 Commits

Author SHA1 Message Date
326791b825 Update Queries to enable editing records
Change Winter values to 1 or 0
2023-12-03 16:25:18 -05:00
be94b0e373 Add Data for existing query into form 2023-12-03 16:09:50 -05:00
2 changed files with 99 additions and 23 deletions

View File

@ -22,15 +22,29 @@ client = influxdb_client.InfluxDBClient(
def success(): def success():
return "Form success!" return "Form success!"
def fetch_timeseries_data(timestamp=None): def fetch_timeseries_data(timestamp=None):
query_api = client.query_api() query_api = client.query_api()
query = 'from(bucket: "gas")\ if timestamp is not None:
|> range(start: -90d)\ newTime = pendulum.parse(timestamp)
|> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\ print(newTime.to_atom_string())
|> filter(fn: (r) => r["_field"] == "odometer")\ # "2023-11-14T08:21:00.000Z"
|> sort(columns: ["_value"], desc: false)\ query = 'from(bucket: "gas")\
|> yield(name: "last")\ |> range(start: time(v: "' + str(newTime.subtract(seconds=1).to_atom_string()) + '"), stop: time(v: "' + str(newTime.add(seconds=1).to_atom_string()) + '"))\
|> limit(n:10)' |> filter(fn: (r) => r["_measurement"] == "2016_odyssey")\
|> filter(fn: (r) => r["_field"] == "odometer")\
|> sort(columns: ["_value"], desc: false)\
|> yield(name: "last")\
|> limit(n:10)'
print("searching for fixed time")
else:
query = 'from(bucket: "gas")\
|> range(start: -90d)\
|> 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) result = query_api.query(org=config['DB_ORG'], query=query)
results = [] results = []
@ -62,11 +76,14 @@ def fetch_timeseries_data(timestamp=None):
continue continue
else: else:
newlist.append(value) newlist.append(value)
print(newlist)
print()
return newlist return newlist
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def main_page(): def main_page():
### Call fetch function # Call fetch function
newlist = fetch_timeseries_data() newlist = fetch_timeseries_data()
# print(newlist) # print(newlist)
return render_template('index.html', data=newlist[:10]) return render_template('index.html', data=newlist[:10])
@ -96,9 +113,9 @@ def add_time():
fuelcost = float(fuelcost) fuelcost = float(fuelcost)
winter = request.form['winter'] winter = request.form['winter']
if winter == 'TRUE': if winter == 'TRUE':
winter = True winter = 1
else: else:
winter = False winter = 0
json_body = [ json_body = [
{ {
@ -128,17 +145,19 @@ def add_time():
return redirect(url_for('main_page')) return redirect(url_for('main_page'))
@app.route('/edit/<timestamp>') @app.route('/edit/<timestamp>')
def edit_odometer_entry(timestamp): def edit_odometer_entry(timestamp):
'''Retrieve the time entry from influxdb''' '''Retrieve the time entry from influxdb'''
print('Fetching data!') print('Fetching data!')
### Call fetch function # Call fetch function
newlist = fetch_timeseries_data() newlist = fetch_timeseries_data()
# Fill entry_data with timestamp based data # Fill entry_data with timestamp based data
entry_data = fetch_timeseries_data(timestamp) entry_data = fetch_timeseries_data(timestamp)
return render_template('index.html', page_title="Edit Entry", timestamp_data=entry_data, data=newlist[:10]) return render_template('index.html', page_title="Edit Entry", timestamp_data=entry_data, data=newlist[:10])
@app.route('/repeat_last_odometer/<odometer>/<winter>') @app.route('/repeat_last_odometer/<odometer>/<winter>')
def repeat_last_odometer(odometer, winter=False): def repeat_last_odometer(odometer, winter=False):
'''Retrieve the passed in odometer value and submit as a new entry''' '''Retrieve the passed in odometer value and submit as a new entry'''

View File

@ -4,20 +4,73 @@
{% if page_title %} {% if page_title %}
<h3> {{ page_title }}</h3> <h3> {{ page_title }}</h3>
{% endif %} {% endif %}
{% if timestamp_data %}
{% for point in timestamp_data %}
<b>{{ point['time']}}</b>
<form method="POST" action="{{ url_for('add_time') }}" class=add-time>
<div class="form group">
<div class="row">
<div class="col-xs-4">
<label for="date">Date:</label>
<input type=date name="date" class="form-control" placeholder="Ex: 2009-11-10" value="{{ point['time'].strftime('%Y-%m-%d')}}">
</div>
<div class="col-xs-4">
<label for="time">Time:</label>
<input type=time name="time" class="form-control" placeholder="Ex: 17:59:03" step="1" value="{{ point['time'].strftime('%H:%M:%S')}}">
</div>
</div>
<div class="row">
<div class="col-xs-4">
<label for="odometer">Odometer:</label>
<input type=number name="odometer" class="form-control" placeholder="Ex: 3008" value="{{ point['odometer']}}"></input>
</div>
<div class="col-xs-4">
<label for="oilhealth">Oil Health:</label>
<input type=number name="oilhealth" class="form-control" placeholder="Ex: 80" value="{{ point['oilhealth']}}">
</div>
</div>
<div class="row">
<div class="col-xs-4">
<label for="fuel">Fuel (L):</label>
<input type=number step="0.001" name="fuel" class="form-control" placeholder="Ex: 70.175" value="{{ point['fuel']}}">
</div>
<div class="col-xs-4">
<label for="fuelcost">Fuel Cost ($/L):</label>
<input type=number step="0.001" name="fuelcost" class="form-control" placeholder="Ex: 1.059" value="{{ point['fuelcost']}}">
</div>
</div>
<div class="row">
<div class="col-xs-4">
<label for="winter">Winter Tires:</label>
<select name="winter" class="form-control">
{% if data %}
{% if data[0]['winter'] == "True" %}
<option value="TRUE" selected>True</option>
<option value="FALSE">False</option>
{% else %}
<option value="TRUE">True</option>
<option value="FALSE" selected>False</option>
{% endif %}
{% else %}
<option value="TRUE">True</option>
<option value="FALSE" selected>False</option>
{% endif %}
</select>
</div>
</div><br>
<input type="submit" value="Go">
{% if data %}
<a href="{{ url_for('repeat_last_odometer', odometer=data[0]['odometer'], winter=data[0]['winter'] ) }}">Repeat</a>
{% endif %}
</div>
</form>
{% endfor %}
{% else %}
<form method="POST" action="{{ url_for('add_time') }}" class=add-time> <form method="POST" action="{{ url_for('add_time') }}" class=add-time>
<div class="form group"> <div class="form group">
<!-- <div class="row">
<div class="col-xs-4">
<label for="db">Database:</label>
<input type=text name="db" class="form-control" placeholder="Ex: Gas">
</div>
<div class="col-xs-4">
<label for="table">Table:</label>
<input type=text name="table" class="form-control" placeholder="Ex: Odyssey">
</div>
</div>
-->
<div class="row"> <div class="row">
<div class="col-xs-4"> <div class="col-xs-4">
<label for="date">Date:</label> <label for="date">Date:</label>
@ -74,6 +127,9 @@
{% endif %} {% endif %}
</div> </div>
</form> </form>
{% endif %}
<br> <br>
Last Mileage Last Mileage
<table class="table table-striped"> <table class="table table-striped">
@ -87,6 +143,7 @@ Last Mileage
<th scope="col">$ Total</th> <th scope="col">$ Total</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for point in data %} {% for point in data %}
<tr> <tr>