dockerized it
This commit is contained in:
37
app/main.py
37
app/main.py
@@ -5,9 +5,10 @@ from flask import render_template
|
||||
from flask import url_for
|
||||
from flask import request, redirect, g
|
||||
from contextlib import closing
|
||||
import os, os.path
|
||||
|
||||
app = Flask(__name__)
|
||||
DATABASE = '/tmp/rollerverbrauch.db'
|
||||
DATABASE = '/data/rollerverbrauch.db'
|
||||
DEBUG = True
|
||||
SECRET_KEY = 'development key'
|
||||
USERNAME = 'admin'
|
||||
@@ -21,7 +22,9 @@ def connect_db():
|
||||
def init_db():
|
||||
with closing(connect_db()) as db:
|
||||
with app.open_resource('schema.sql', mode='r') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
sql_commands = f.read()
|
||||
print(sql_commands)
|
||||
db.cursor().executescript(sql_commands)
|
||||
db.commit()
|
||||
|
||||
@app.before_request
|
||||
@@ -62,6 +65,34 @@ def getPitStops():
|
||||
data = {'pitstops': preparePitStops(getAllPitStops())}
|
||||
return render_template('pitstops.html', data=data)
|
||||
|
||||
@app.route('/statistics', methods=['GET'])
|
||||
def getStatistics():
|
||||
pitstops = getAllPitStops()
|
||||
count = len(pitstops)
|
||||
distance = 0
|
||||
sumLitres = 0
|
||||
averageDistance = 0
|
||||
averageLitresFuelled = 0
|
||||
averageLitresUsed = 0
|
||||
count = len(pitstops)
|
||||
if len(pitstops) > 0:
|
||||
sumLitres = 0
|
||||
for pitstop in pitstops:
|
||||
sumLitres += pitstop['litres']
|
||||
averageLitresFuelled = round(sumLitres/count, 2)
|
||||
if len(pitstops) > 1:
|
||||
distance = pitstops[-1]['odometer'] - pitstops[0]['odometer']
|
||||
averageDistance = round(distance/(count - 1), 2)
|
||||
averageLitresUsed = round(100 * (sumLitres-pitstops[0]['litres'])/distance, 2)
|
||||
data = {
|
||||
'distance':distance,
|
||||
'count': count,
|
||||
'litres': round(sumLitres, 2),
|
||||
'averageDistance': averageDistance,
|
||||
'averageListresFuelled': averageLitresFuelled,
|
||||
'averageListresUsed': averageLitresUsed}
|
||||
return render_template('statistics.html', data=data)
|
||||
|
||||
def preparePitStops(pitstops):
|
||||
for index in range(1, len(pitstops)):
|
||||
last = pitstops[index - 1]
|
||||
@@ -92,4 +123,6 @@ def addPitStop(date, odometer, litres):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not os.path.isfile(DATABASE) or os.stat(DATABASE).st_size == 0:
|
||||
init_db()
|
||||
app.run(debug=True, host='0.0.0.0')
|
||||
@@ -3,6 +3,7 @@
|
||||
{% block navigation %}
|
||||
<li><a href='{{ url_for('getPitStops') }}'>Home</a></li>
|
||||
<li><a href='{{ url_for('createPitStopForm') }}' class="active">Create Pitstop</a></li>
|
||||
<li><a href='{{ url_for('getStatistics') }}'>Statistics</a></li>
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
{% block navigation %}
|
||||
<li><a href='{{ url_for('getPitStops') }}' class="active">Home</a></li>
|
||||
<li><a href='{{ url_for('createPitStopForm') }}'>Create Pitstop</a></li>
|
||||
<li><a href='{{ url_for('getStatistics') }}'>Statistics</a></li>
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
@@ -49,5 +50,5 @@
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table
|
||||
</table>
|
||||
{% endblock %}
|
||||
36
app/templates/statistics.html
Normal file
36
app/templates/statistics.html
Normal file
@@ -0,0 +1,36 @@
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% block navigation %}
|
||||
<li><a href='{{ url_for('getPitStops') }}'>Home</a></li>
|
||||
<li><a href='{{ url_for('createPitStopForm') }}'>Create Pitstop</a></li>
|
||||
<li><a href='{{ url_for('getStatistics') }}' class="active">Statistics</a></li>
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>Number of Pitstops:</th>
|
||||
<td>{{ data.count }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Logged Distance:</th>
|
||||
<td>{{ data.distance }} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Average Distance:</th>
|
||||
<td>{{ data.averageDistance }} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Litres fuelled:</th>
|
||||
<td>{{ data.litres }} l</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Average Litres fuelled:</th>
|
||||
<td>{{ data.averageListresFuelled }} l</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Average Litres used:</th>
|
||||
<td>{{ data.averageListresUsed }} l/100km</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user