Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add duration in Charge table #662

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions psa_car_controller/psacc/application/charging.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class Charging:

@staticmethod
def get_chargings() -> List[dict]:
conn = Database.get_db()
res = conn.execute("select * from battery ORDER BY start_at").fetchall()
conn.close()
return list(map(dict, res))
charge_db = Database.get_all_charge()
charge_list = list(map(dict, charge_db))
Charging._calculated_fields(charge_list)
return charge_list

@staticmethod
def get_battery_curve(conn, charge, car) -> List[BatteryChargeCurve]:
Expand Down Expand Up @@ -97,3 +97,15 @@ def record_charging(car: Car, charging_status, charge_date: datetime, level, lat
Charging.update_chargings(conn, last_charge, car)
conn.commit()
conn.close()

@staticmethod
def _calculated_fields(charge_list: list):
for c in charge_list:
if c.get("stop_at") and c.get("start_at"):
c.update(
{
"duration_min": (c.get("stop_at") - c.get("start_at")).total_seconds()
/ 60,
"duration_str": str((c.get("stop_at") - c.get("start_at"))),
}
)
7 changes: 7 additions & 0 deletions psa_car_controller/psacc/repository/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ def get_all_charge_without_price(conn) -> List[Charge]:
charges.append(Charge(**dict_key_to_lower_case(**row)))
return charges

@staticmethod
def get_all_charge() -> List[Charge]:
conn = Database.get_db()
res = conn.execute("select * from battery ORDER BY start_at").fetchall()
conn.close()
return res

@staticmethod
def update_charge(charge: Charge):
# we don't need to update mileage, since it should be inserted at beginning of charge,
Expand Down
5 changes: 4 additions & 1 deletion psa_car_controller/web/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def get_figures(car: Car):
sort_by=[{'column_id': 'start_at_str', 'direction': 'desc'}],
columns=[{'id': 'start_at_str', 'name': 'start at', 'type': 'datetime'},
{'id': 'stop_at_str', 'name': 'stop at', 'type': 'datetime'},
{'id': 'duration_str', 'name': 'duration', 'type': 'text'},
{'id': 'duration_min', 'name': 'duration (min)', 'type': 'numeric',
'format': deepcopy(nb_format).symbol_suffix(" min").precision(0)},
{'id': 'start_level', 'name': 'start level', 'type': 'numeric'},
{'id': 'end_level', 'name': 'end level', 'type': 'numeric'},
{'id': 'co2', 'name': 'CO2', 'type': 'numeric',
Expand All @@ -147,7 +150,7 @@ def get_figures(car: Car):
'format': deepcopy(nb_format).symbol_suffix(" kWh").precision(2)},
{'id': 'price', 'name': 'price', 'type': 'numeric',
'format': deepcopy(nb_format).symbol_suffix(" " + CURRENCY).precision(2), 'editable': True},
{'id': 'charging_mode', 'name': 'charging mode', 'type': 'string'},
{'id': 'charging_mode', 'name': 'charging mode', 'type': 'text'},
{'id': 'mileage', 'name': 'mileage', 'type': 'numeric', 'format': nb_format},
],
data=[],
Expand Down
4 changes: 3 additions & 1 deletion tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from psa_car_controller.psacc.utils.utils import get_temp
from tests.data.car_status import FUEL_CAR_STATUS, ELECTRIC_CAR_STATUS, ELECTRIC_CAR_STATUS_V2
from tests.utils import DATA_DIR, record_position, latitude, longitude, date0, date1, date2, date3, record_charging, \
vehicule_list, get_new_test_db, get_date, date4, compare_dict
vehicule_list, get_new_test_db, get_date, date4, compare_dict, duration_min, duration_str
from psa_car_controller.web.figures import get_figures, get_battery_curve_fig, get_altitude_fig

dummy_value = 0
Expand Down Expand Up @@ -237,6 +237,8 @@ def test_record_position_charging(self):
assert isinstance(co2, float)
assert compare_dict(chargings, [{'start_at': date0,
'stop_at': date3,
'duration_str': duration_str,
'duration_min' :duration_min,
'VIN': 'VR3UHZKX',
'start_level': 40,
'end_level': 85,
Expand Down
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
date1 = date3 - timedelta(minutes=40)
date0 = date3 - timedelta(minutes=60)
date4 = date3 + timedelta(minutes=1)
duration_min = (date3 - date0).seconds / 60
duration_str = str(date3 - date0)

vehicule_list = Cars()
vehicule_list.extend(
Expand Down
Loading