fixed stats

This commit is contained in:
Joachim Lusiardi 2016-04-25 22:28:35 +02:00
parent 34905921d9
commit ca7166f731
3 changed files with 81 additions and 52 deletions

View File

@ -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)

View File

@ -1,33 +1,62 @@
{% extends "layout.html" %}
{% block body %}
<div id="content">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
{% for vehicle in data %}
<li {% if loop.first %}class="active" {%endif %}>
<a href="#v{{vehicle.id}}" id="i{{vehicle.id}}" data-toggle="tab">
{{ vehicle.name }}
</a>
</li>
{% endfor %}
</ul>
<div id="my-tab-content" class="tab-content">
{% for vehicle in data %}
<div class="tab-pane {% if loop.first %}active{% endif %}" id="v{{vehicle.id}}">
<h3>{{vehicle.name}}</h3>
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<tr>
<th>Number of Pitstops:</th>
<td>{{ vehicle.pitstop_count }}</td>
</tr>
<tr>
<th>Logged Distance:</th>
<td>{{ vehicle.overall_distance | round(2) }} km</td>
</tr>
<tr>
<th>Average Distance:</th>
<td>{{ vehicle.average_distance | round(2) }} km</td>
</tr>
<tr>
<th>Litres fuelled:</th>
<td>{{ vehicle.overall_litres | round(2) }} l</td>
</tr>
<tr>
<th>Average Litres fuelled:</th>
<td>{{ vehicle.average_litres_fuelled | round(2) }} l</td>
</tr>
<tr>
<th>Average Litres used:</th>
<td>{{ vehicle.average_litres_used | round(2) }} l/100km</td>
</tr>
</table>
</div>
</div>
{% endfor %}
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#tabs').tab();
if(window.location.hash != "") {
$('a[href="' + window.location.hash + '"]').click()
}
});
</script>
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<tr>
<th>Number of Pitstops:</th>
<td>{{ data.count }}</td>
</tr>
<tr>
<th>Logged Distance:</th>
<td>{{ data.distance | round(2) }} km</td>
</tr>
<tr>
<th>Average Distance:</th>
<td>{{ data.averageDistance | round(2) }} km</td>
</tr>
<tr>
<th>Litres fuelled:</th>
<td>{{ data.litres | round(2) }} l</td>
</tr>
<tr>
<th>Average Litres fuelled:</th>
<td>{{ data.averageListresFuelled | round(2) }} l</td>
</tr>
<tr>
<th>Average Litres used:</th>
<td>{{ data.averageListresUsed | round(2) }} l/100km</td>
</tr>
</table>
</div>
{% endblock %}

View File

@ -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