From ca7166f7310e61459f420dae7bb88935523e6204 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 25 Apr 2016 22:28:35 +0200 Subject: [PATCH] fixed stats --- app/rollerverbrauch/__init__.py | 30 ++----- app/rollerverbrauch/templates/statistics.html | 85 +++++++++++++------ app/rollerverbrauch/tools.py | 18 ++++ 3 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 app/rollerverbrauch/tools.py diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 47ba0e1..8a959cd 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -23,6 +23,8 @@ app.config.from_object(__name__) db = SQLAlchemy(app) mail = Mail(app) +from rollerverbrauch.tools import \ + VehicleStats from rollerverbrauch.forms import \ CreatePitstopForm, \ @@ -197,28 +199,8 @@ def get_account_page(): @app.route('/statistics', methods=['GET']) @login_required def get_statistics(): - pitstops = Pitstop.query.all() - count = len(pitstops) - distance = 0 - sum_litres = 0 - average_distance = 0 - average_litres_fuelled = 0 - average_litres_used = 0 - - if count > 0: - sum_litres = 0 - for pitstop in pitstops: - sum_litres += pitstop.litres - average_litres_fuelled = sum_litres/count - if count > 1: - distance = pitstops[-1].odometer - pitstops[0].odometer - average_distance = distance/(count - 1) - average_litres_used = 100 * (sum_litres-pitstops[0].litres)/distance - g.data['distance'] = distance - g.data['count'] = count - g.data['litres'] = sum_litres - g.data['averageDistance'] = average_distance - g.data['averageListresFuelled'] = average_litres_fuelled - g.data['averageListresUsed'] = average_litres_used - return render_template('statistics.html', data=g.data) + stats = [] + for vehicle in current_user.vehicles: + stats.append(VehicleStats(vehicle)) + return render_template('statistics.html', data=stats) diff --git a/app/rollerverbrauch/templates/statistics.html b/app/rollerverbrauch/templates/statistics.html index e9be16b..9fd7b0f 100644 --- a/app/rollerverbrauch/templates/statistics.html +++ b/app/rollerverbrauch/templates/statistics.html @@ -1,33 +1,62 @@ {% extends "layout.html" %} {% block body %} +
+ +
+ {% for vehicle in data %} +
+

{{vehicle.name}}

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Number of Pitstops:{{ vehicle.pitstop_count }}
Logged Distance:{{ vehicle.overall_distance | round(2) }} km
Average Distance:{{ vehicle.average_distance | round(2) }} km
Litres fuelled:{{ vehicle.overall_litres | round(2) }} l
Average Litres fuelled:{{ vehicle.average_litres_fuelled | round(2) }} l
Average Litres used:{{ vehicle.average_litres_used | round(2) }} l/100km
+
+
+ {% endfor %} +
+
+ + + -
- - - - - - - - - - - - - - - - - - - - - - - - - -
Number of Pitstops:{{ data.count }}
Logged Distance:{{ data.distance | round(2) }} km
Average Distance:{{ data.averageDistance | round(2) }} km
Litres fuelled:{{ data.litres | round(2) }} l
Average Litres fuelled:{{ data.averageListresFuelled | round(2) }} l
Average Litres used:{{ data.averageListresUsed | round(2) }} l/100km
-
{% endblock %} diff --git a/app/rollerverbrauch/tools.py b/app/rollerverbrauch/tools.py new file mode 100644 index 0000000..a6281dc --- /dev/null +++ b/app/rollerverbrauch/tools.py @@ -0,0 +1,18 @@ +class VehicleStats: + def __init__(self, vehicle): + self.name = vehicle.name + self.id = vehicle.id + self.pitstop_count = len(vehicle.pitstops) + self.overall_distance = 0 + self.average_distance = 0 + self.overall_litres = 0 + self.average_litres_fuelled = 0 + self.average_litres_used = 0 + if self.pitstop_count > 0: + for pitstop in vehicle.pitstops: + self.overall_litres += pitstop.litres + self.average_litres_fuelled = self.overall_litres / self.pitstop_count + if self.pitstop_count > 1: + self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer + self.average_distance = self.overall_distance / (self.pitstop_count - 1) + self.average_litres_used = 100 * (self.overall_litres - vehicle.pitstops[0].litres) / self.overall_distance