Skip to content

Commit

Permalink
[ENH] Add duration in Charge table
Browse files Browse the repository at this point in the history
- Add 2 new columns duration (string + min) in Charge tab
- Data calculated on the fly to display in the table (No fields in DB)
  • Loading branch information
Mikado8231 committed Nov 12, 2023
1 parent 56d96d5 commit 7b7585d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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")).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 @@ -292,6 +292,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
3 changes: 3 additions & 0 deletions 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 Down

0 comments on commit 7b7585d

Please sign in to comment.