statistics 2nd step

This commit is contained in:
Joachim Lusiardi 2016-07-11 06:59:34 +02:00
parent 4ea42a7efd
commit ca5bd9bb02
3 changed files with 97 additions and 101 deletions

View File

@ -85,30 +85,6 @@
{% endmacro %} {% endmacro %}
{% macro chartScript(divId, data, unit)%}
{% set hash = divId | md5 %}
data_{{ hash }} = [{% for stop in data %}{
"date": "{{stop.date}}",
"value": {{stop.value}}
}{% if not loop.last %},{%endif%}
{% endfor%}
]
var chart_{{ hash }} = createChart('{{divId}}', data_{{ hash }}, '{{unit}}');
function zoom_chart_{{ hash }}() {
chart_{{ hash }}.zoomToIndexes(
chart_{{ hash }}.dataProvider.length - 40,
chart_{{ hash }}.dataProvider.length - 1
);
}
chart_{{ hash }}.addListener("rendered", zoom_chart_{{ hash }});
zoom_chart_{{ hash }}()
{% endmacro %}
<!doctype html> <!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]--> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->

View File

@ -1,5 +1,44 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% macro chartScript(divId, data, unit)%}
{% set hash = divId | md5 %}
data_{{ hash }} = [{% for stop in data %}{
"date": "{{stop.date}}",
"value": {{stop.value}}
}{% if not loop.last %},{%endif%}
{% endfor%}
]
var chart_{{ hash }} = createChart('{{divId}}', data_{{ hash }}, '{{unit}}');
function zoom_chart_{{ hash }}() {
chart_{{ hash }}.zoomToIndexes(
chart_{{ hash }}.dataProvider.length - 40,
chart_{{ hash }}.dataProvider.length - 1
);
}
chart_{{ hash }}.addListener("rendered", zoom_chart_{{ hash }});
zoom_chart_{{ hash }}()
{% endmacro %}
{% macro chart(data, baseId, unit, link, active)%}
{% set chartID = 'chart_' + baseId %}
<div class="tab-pane {% if active %}active{% endif %}" id="{{ baseId }}">
{% if data|length > 0 %}
<div id="{{ chartID }}" style="width:100%; height:500px;"></div>
<script type="text/javascript">
{{ chartScript(chartID, data, unit) }}
</script>
{% else %}
<div class="alert alert-warning" role="alert">
not enough data: <a href="{{ link }}">log a pitstop</a>?
</div>
{% endif %}
</div>
{% endmacro %}
{% block body %} {% block body %}
<div id="content"> <div id="content">
<ul id="vehicle_tabs" class="nav nav-tabs" data-tabs="tabs"> <ul id="vehicle_tabs" class="nav nav-tabs" data-tabs="tabs">
@ -47,7 +86,7 @@
</tr> </tr>
<tr> <tr>
<th>Amount fuelled:</th> <th>Amount fuelled:</th>
<td>{{ consumable.amount | round(2) }} {{ consumable.unit }}</td> <td>{{ consumable.overall_amount | round(2) }} {{ consumable.unit }}</td>
</tr> </tr>
<tr> <tr>
<th>Average Amount fuelled:</th> <th>Average Amount fuelled:</th>
@ -58,52 +97,56 @@
<td>{{ consumable.average_amount_used | round(2) }} {{ consumable.unit }}/100km</td> <td>{{ consumable.average_amount_used | round(2) }} {{ consumable.unit }}/100km</td>
</tr> </tr>
</table> </table>
{% set ID = vehicle.id|string + '_c' + consumable.id|string %}
{% set odometerID = ID + '_odometer' %}
{% set consumptionID = ID + '_consumption' %}
{% set amountID = ID + '_amount' %}
<ul id="charts_{{ID}}_tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="active">
<a href="#v{{odometerID}}" id="i{{odometerID}}" data-toggle="tab" >
Odometer
</a>
</li>
<li>
<a href="#v{{consumptionID}}" id="i{{consumptionID}}" data-toggle="tab" >
Consumption
</a>
</li>
<li>
<a href="#v{{amountID}}" id="i{{amountID}}" data-toggle="tab" >
Amount
</a>
</li>
</ul>
<div id="charts_{{ID}}_tabs-content" class="tab-content">
{% set baseId = 'v' + odometerID %}
{% set link = url_for('select_consumable_for_new_pitstop', vid=vehicle.id) %}
{{ chart(vehicle.odometers, baseId, 'km', link, true) }}
{% set baseId = 'v' + consumptionID %}
{% set link = url_for('create_pit_stop_form', vid=vehicle.id, cid=consumable.id) %}
{{ chart(consumable.average_amount, baseId, consumable.unit + '/100km', link, false) }}
{% set baseId = 'v' + amountID %}
{% set link = url_for('create_pit_stop_form', vid=vehicle.id, cid=consumable.id) %}
{{ chart(consumable.amounts, baseId, consumable.unit, link, false) }}
</div>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#charts_{{ID}}_tabs').tab();
if(window.location.hash != "") {
$('a[href="' + window.location.hash + '"]').click()
}
});
</script>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<ul id="charts_tabs" class="nav nav-tabs" data-tabs="tabs">
<li class="active">
<a href="#v{{vehicle.id}}_codometer" id="i{{vehicle.id}}_codometer" data-toggle="tab" >
Odometer
</a>
</li>
{% for consumable in vehicle.consumables %}
<li>
<a href="#v{{vehicle.id}}_c{{consumable.id}}_consumption" id="i{{vehicle.id}}_c{{consumable.id}}_consumption" data-toggle="tab" >
{{ consumable.name }} consumption
</a>
</li>
{% endfor %}
</ul>
<div id="charts_tabs-content" class="tab-content">
<div class="tab-pane active" id="v{{vehicle.id}}_codometer">
{% if vehicle.odometers|length > 0 %}
<div id="odometerChartDiv{{vehicle.id}}" style="width:100%; height:500px;"></div>
<script type="text/javascript">
{{ chartScript('odometerChartDiv'+vehicle.id|str, vehicle.odometers, 'km') }}
</script>
{% else %}
<div class="alert alert-warning" role="alert">
not enough data: <a href="{{ url_for('select_consumable_for_new_pitstop', vid=vehicle.id) }}">log a pitstop</a>?
</div>
{% endif %}
</div>
{% for consumable in vehicle.consumables %}
<div class="tab-pane active" id="v{{vehicle.id}}_c{{consumable.id}}_consumption">
{% if consumable.average_amount|length > 0 %}
<div id="consumptionChartDiv{{vehicle.id}}_c{{consumable.id}}" style="width:100%; height:500px;"></div>
<script type="text/javascript">
{{ chartScript('consumptionChartDiv'+vehicle.id|str+'_c'+consumable.id|str, consumable.average_amount, consumable.unit+'/100km') }}
</script>
{% else %}
<div class="alert alert-warning" role="alert">
not enough data: <a href="{{ url_for('create_pit_stop_form', vid=vehicle.id, cid=consumable.id) }}">log a pitstop</a>?
</div>
{% endif %}
</div>
{% endfor %}
</div>
</div> </div>
{% endfor %} {% endfor %}

View File

@ -11,24 +11,26 @@ class ConsumableStats:
self.name = consumable.name self.name = consumable.name
self.id = consumable.id self.id = consumable.id
self.unit = consumable.unit self.unit = consumable.unit
self.amount = 0 self.overall_amount = 0
self.average_distance = 0 self.average_distance = 0
self.average_amount_fuelled = 0 self.average_amount_fuelled = 0
self.average_amount_used = 0 self.average_amount_used = 0
self.average_amount = [] self.average_amount = []
self.amounts = []
pitstops = [stop for stop in vehicle.pitstops if stop.consumable_id == consumable.id] pitstops = [stop for stop in vehicle.pitstops if stop.consumable_id == consumable.id]
pitstop_count = len(pitstops) pitstop_count = len(pitstops)
if pitstop_count > 0: if pitstop_count > 0:
for pitstop in pitstops: for pitstop in pitstops:
self.amount += pitstop.amount self.overall_amount += pitstop.amount
self.average_amount_fuelled = self.amount / pitstop_count self.amounts.append(StatsEvent(pitstop.date, pitstop.amount))
self.average_amount_fuelled = self.overall_amount / pitstop_count
print(self.amounts)
if pitstop_count > 1: if pitstop_count > 1:
overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer
self.average_distance = overall_distance / (pitstop_count - 1) self.average_distance = overall_distance / (pitstop_count - 1)
self.average_amount_used = 100 * (self.amount - pitstops[0].amount) / overall_distance self.average_amount_used = 100 * (self.overall_amount - pitstops[0].amount) / overall_distance
for index in range(1, pitstop_count): for index in range(1, pitstop_count):
last_ps = pitstops[index - 1] last_ps = pitstops[index - 1]
current_ps = pitstops[index] current_ps = pitstops[index]
@ -37,6 +39,7 @@ class ConsumableStats:
current_ps.date, current_ps.date,
round(100 * current_ps.amount/(current_ps.odometer - last_ps.odometer), 2))) round(100 * current_ps.amount/(current_ps.odometer - last_ps.odometer), 2)))
class VehicleStats: class VehicleStats:
def __init__(self, vehicle): def __init__(self, vehicle):
self.name = vehicle.name self.name = vehicle.name
@ -49,41 +52,15 @@ class VehicleStats:
for consumable in vehicle.consumables: for consumable in vehicle.consumables:
self.consumables.append(ConsumableStats(vehicle, consumable)) self.consumables.append(ConsumableStats(vehicle, consumable))
# self.overall_litres = 0
#
# self.litres = []
# self.average_litres = []
# self.costsPerLitre = []
# self.costs = []
# self.average_costs_per_litre = 0
# cost_count = 0;
pitstop_count = len(vehicle.pitstops) pitstop_count = len(vehicle.pitstops)
if pitstop_count > 0: if pitstop_count > 0:
for pitstop in vehicle.pitstops: for pitstop in vehicle.pitstops:
# self.overall_litres += pitstop.amount
# self.litres.append(StatsEvent(pitstop.date, pitstop.amount))
self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer)) self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer))
# self.costsPerLitre.append(StatsEvent(pitstop.date, pitstop.costs / pitstop.amount))
# self.costs.append(StatsEvent(pitstop.date, pitstop.costs))
self.overall_costs += pitstop.costs self.overall_costs += pitstop.costs
# self.average_costs_per_litre += (pitstop.costs / pitstop.amount)
# if pitstop.costs > 0:
# cost_count += 1
# if cost_count > 0:
# self.average_costs_per_litre = self.average_costs_per_litre / cost_count
# else:
# self.average_costs_per_litre = 0
if pitstop_count > 1: if pitstop_count > 1:
self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer self.overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer
# for index in range(1, self.pitstop_count):
# last_ps = vehicle.pitstops[index - 1]
# current_ps = vehicle.pitstops[index]
# self.average_litres.append(StatsEvent(
# current_ps.date,
# round(100 * current_ps.litres/(current_ps.odometer - last_ps.odometer), 2)))
class StatsEvent: class StatsEvent:
@ -178,13 +155,13 @@ def compute_lower_limits_for_new_pitstop(latest_pitstop, last_pitstop_consumable
# if last_pitstop_consumable is not None and last_pitstop_consumable != latest_pitstop: # if last_pitstop_consumable is not None and last_pitstop_consumable != latest_pitstop:
# if latest_pitstop.id > last_pitstop_consumable.id: # if latest_pitstop.id > last_pitstop_consumable.id:
# return Pitstop(latest_pitstop.odometer, # return Pitstop(latest_pitstop.odometer,
# last_pitstop_consumable.amount, # last_pitstop_consumable.overall_amount,
# latest_pitstop.date, # latest_pitstop.date,
# last_pitstop_consumable.costs, # last_pitstop_consumable.costs,
# consumable_id) # consumable_id)
# else: # else:
# return Pitstop(last_pitstop_consumable.odometer, # return Pitstop(last_pitstop_consumable.odometer,
# last_pitstop_consumable.amount, # last_pitstop_consumable.overall_amount,
# last_pitstop_consumable.date, # last_pitstop_consumable.date,
# last_pitstop_consumable.costs, # last_pitstop_consumable.costs,
# consumable_id) # consumable_id)
@ -193,7 +170,7 @@ def compute_lower_limits_for_new_pitstop(latest_pitstop, last_pitstop_consumable
# litres = 0 # litres = 0
# costs = 0 # costs = 0
# if latest_pitstop.consumable_id == last_pitstop_consumable.consumable_id: # if latest_pitstop.consumable_id == last_pitstop_consumable.consumable_id:
# litres = latest_pitstop.amount # litres = latest_pitstop.overall_amount
# costs = latest_pitstop.costs # costs = latest_pitstop.costs
# return Pitstop(latest_pitstop.odometer, litres, latest_pitstop.date, costs, consumable_id) # return Pitstop(latest_pitstop.odometer, litres, latest_pitstop.date, costs, consumable_id)
# else: # else: