rollerverbrauch/app/tools.py

85 lines
3.2 KiB
Python
Raw Normal View History

import logging
2016-04-25 22:28:35 +02:00
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
2016-05-02 06:42:43 +02:00
self.litres = []
2016-05-02 07:59:31 +02:00
self.average_litres = []
2016-05-02 06:42:43 +02:00
self.odometers = []
self.costsPerLitre = []
self.costs = []
self.overall_costs = 0
self.average_costs_per_litre = 0
cost_count = 0;
2016-05-02 06:42:43 +02:00
2016-04-25 22:28:35 +02:00
if self.pitstop_count > 0:
for pitstop in vehicle.pitstops:
self.overall_litres += pitstop.litres
2016-05-02 06:42:43 +02:00
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
2016-04-25 22:28:35 +02:00
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
2016-04-25 22:28:35 +02:00
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
2016-05-02 07:59:31 +02:00
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)))
2016-05-02 06:42:43 +02:00
class StatsEvent:
def __init__(self, date, value):
self.date = date
self.value = value
def db_log_add(entity):
logging.info('db_add: %s' % str(entity))
def db_log_delete(entity):
logging.info('db_delete: %s' % str(entity))
def db_log_update(entity):
logging.info('db_update: %s' % str(entity))
def check_vehicle_name_is_unique(current_user, name_field):
"""
Checks if the vehicle name given in the name_field is unique for the vehicles of the current user. An error is added
to the field it the name is not unique.
:param current_user: the user currently logged in
:param name_field: the form field to enter the name to
:return: True if the name is unique, False otherwise.
"""
vehicle_name = name_field.data
for vehicle in current_user.vehicles:
if vehicle.name == vehicle_name:
name_field.default = vehicle_name
name_field.errors.append('Vehicle "%s" already exists.' % vehicle_name)
return False
return True