fixed stats
This commit is contained in:
parent
34905921d9
commit
ca7166f731
|
@ -23,6 +23,8 @@ app.config.from_object(__name__)
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
mail = Mail(app)
|
mail = Mail(app)
|
||||||
|
|
||||||
|
from rollerverbrauch.tools import \
|
||||||
|
VehicleStats
|
||||||
|
|
||||||
from rollerverbrauch.forms import \
|
from rollerverbrauch.forms import \
|
||||||
CreatePitstopForm, \
|
CreatePitstopForm, \
|
||||||
|
@ -197,28 +199,8 @@ def get_account_page():
|
||||||
@app.route('/statistics', methods=['GET'])
|
@app.route('/statistics', methods=['GET'])
|
||||||
@login_required
|
@login_required
|
||||||
def get_statistics():
|
def get_statistics():
|
||||||
pitstops = Pitstop.query.all()
|
stats = []
|
||||||
count = len(pitstops)
|
for vehicle in current_user.vehicles:
|
||||||
distance = 0
|
stats.append(VehicleStats(vehicle))
|
||||||
sum_litres = 0
|
return render_template('statistics.html', data=stats)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,62 @@
|
||||||
{% extends "layout.html" %}
|
{% extends "layout.html" %}
|
||||||
|
|
||||||
{% block body %}
|
{% 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">
|
<div class="table-responsive">
|
||||||
<table class="table table-striped table-bordered table-condensed">
|
<table class="table table-striped table-bordered table-condensed">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Number of Pitstops:</th>
|
<th>Number of Pitstops:</th>
|
||||||
<td>{{ data.count }}</td>
|
<td>{{ vehicle.pitstop_count }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Logged Distance:</th>
|
<th>Logged Distance:</th>
|
||||||
<td>{{ data.distance | round(2) }} km</td>
|
<td>{{ vehicle.overall_distance | round(2) }} km</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Average Distance:</th>
|
<th>Average Distance:</th>
|
||||||
<td>{{ data.averageDistance | round(2) }} km</td>
|
<td>{{ vehicle.average_distance | round(2) }} km</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Litres fuelled:</th>
|
<th>Litres fuelled:</th>
|
||||||
<td>{{ data.litres | round(2) }} l</td>
|
<td>{{ vehicle.overall_litres | round(2) }} l</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Average Litres fuelled:</th>
|
<th>Average Litres fuelled:</th>
|
||||||
<td>{{ data.averageListresFuelled | round(2) }} l</td>
|
<td>{{ vehicle.average_litres_fuelled | round(2) }} l</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Average Litres used:</th>
|
<th>Average Litres used:</th>
|
||||||
<td>{{ data.averageListresUsed | round(2) }} l/100km</td>
|
<td>{{ vehicle.average_litres_used | round(2) }} l/100km</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</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>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue