From 4ea42a7efd3ed8a52e2989ae3e5ec04cbb9243ef Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 10 Jul 2016 13:40:49 +0200 Subject: [PATCH] first step for statistics --- app/rollerverbrauch/templates/statistics.html | 203 ++++++++---------- app/rollerverbrauch/tools.py | 107 +++++---- 2 files changed, 160 insertions(+), 150 deletions(-) diff --git a/app/rollerverbrauch/templates/statistics.html b/app/rollerverbrauch/templates/statistics.html index 85cb81c..b6a3aaf 100644 --- a/app/rollerverbrauch/templates/statistics.html +++ b/app/rollerverbrauch/templates/statistics.html @@ -17,129 +17,94 @@

{{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
Logged Costs: {{ vehicle.overall_costs | round(2) }} €
Average Costs per Litre:{{ vehicle.average_costs_per_litre | round(2) }} €/l
- -
-
- {% if vehicle.pitstop_count > 1 %} -
- - {% else %} - - {% endif %} -
-
- {% if vehicle.pitstop_count > 0 %} -
- - {% else %} - - {% endif %} -
-
- {% if vehicle.pitstop_count > 0 %} -
- - {% else %} - - {% endif %} -
-
- {% if vehicle.pitstop_count > 0 %} -
- - {% else %} - - {% endif %} -
-
- {% if vehicle.pitstop_count > 0 %} -
- - {% else %} - - {% endif %} -
-
+ + +
+ {% for consumable in vehicle.consumables %} +
+ + + + + + + + + + + + + + + + + +
Average Distance:{{ consumable.average_distance | round(2) }} km
Amount fuelled:{{ consumable.amount | round(2) }} {{ consumable.unit }}
Average Amount fuelled:{{ consumable.average_amount_fuelled | round(2) }} {{ consumable.unit }}
Average Amount used:{{ consumable.average_amount_used | round(2) }} {{ consumable.unit }}/100km
+
+ {% endfor %} +
+ + +
+
+ {% if vehicle.odometers|length > 0 %} +
+ + {% else %} + + {% endif %} +
+ {% for consumable in vehicle.consumables %} +
+ {% if consumable.average_amount|length > 0 %} +
+ + {% else %} + + {% endif %} +
+ {% endfor %} +
+ {% endfor %} @@ -153,6 +118,18 @@ $('a[href="' + window.location.hash + '"]').click() } }); + jQuery(document).ready(function ($) { + $('#consumable_tabs').tab(); + if(window.location.hash != "") { + $('a[href="' + window.location.hash + '"]').click() + } + }); + jQuery(document).ready(function ($) { + $('#charts_tabs').tab(); + if(window.location.hash != "") { + $('a[href="' + window.location.hash + '"]').click() + } + }); diff --git a/app/rollerverbrauch/tools.py b/app/rollerverbrauch/tools.py index b5dfe5c..3cbaa9a 100644 --- a/app/rollerverbrauch/tools.py +++ b/app/rollerverbrauch/tools.py @@ -5,52 +5,85 @@ from dis import code_info from rollerverbrauch.entities import \ Pitstop + +class ConsumableStats: + def __init__(self, vehicle, consumable): + self.name = consumable.name + self.id = consumable.id + self.unit = consumable.unit + self.amount = 0 + self.average_distance = 0 + self.average_amount_fuelled = 0 + self.average_amount_used = 0 + self.average_amount = [] + + pitstops = [stop for stop in vehicle.pitstops if stop.consumable_id == consumable.id] + pitstop_count = len(pitstops) + + if pitstop_count > 0: + for pitstop in pitstops: + self.amount += pitstop.amount + self.average_amount_fuelled = self.amount / pitstop_count + + if pitstop_count > 1: + overall_distance = vehicle.pitstops[-1].odometer - vehicle.pitstops[0].odometer + self.average_distance = overall_distance / (pitstop_count - 1) + self.average_amount_used = 100 * (self.amount - pitstops[0].amount) / overall_distance + for index in range(1, pitstop_count): + last_ps = pitstops[index - 1] + current_ps = pitstops[index] + self.average_amount.append( + StatsEvent( + current_ps.date, + round(100 * current_ps.amount/(current_ps.odometer - last_ps.odometer), 2))) + 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 - self.litres = [] - self.average_litres = [] - self.odometers = [] - self.costsPerLitre = [] - self.costs = [] self.overall_costs = 0 - self.average_costs_per_litre = 0 - cost_count = 0; + self.consumables = [] + self.odometers = [] - if self.pitstop_count > 0: - for pitstop in vehicle.pitstops: - self.overall_litres += pitstop.litres - self.litres.append(StatsEvent(pitstop.date, pitstop.litres)) - self.odometers.append(StatsEvent(pitstop.date, pitstop.odometer)) - self.costsPerLitre.append(StatsEvent(pitstop.date, pitstop.costs / pitstop.litres)) - self.costs.append(StatsEvent(pitstop.date, pitstop.costs)) - self.overall_costs += pitstop.costs - self.average_costs_per_litre += (pitstop.costs / pitstop.litres) - if pitstop.costs > 0: - cost_count += 1 - self.average_litres_fuelled = self.overall_litres / self.pitstop_count - if cost_count > 0: - self.average_costs_per_litre = self.average_costs_per_litre / cost_count - else: - self.average_costs_per_litre = 0 + for consumable in vehicle.consumables: + self.consumables.append(ConsumableStats(vehicle, consumable)) - if self.pitstop_count > 1: +# 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) + + if pitstop_count > 0: + 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.costsPerLitre.append(StatsEvent(pitstop.date, pitstop.costs / pitstop.amount)) + # self.costs.append(StatsEvent(pitstop.date, 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: 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 - 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))) + # 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: