mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-26 10:17:18 +00:00
improve performance & debug
This commit is contained in:
+1
-2
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
@@ -43,7 +42,7 @@ class Abrp:
|
||||
logger.debug(response.text)
|
||||
return response.json()["status"] == "ok"
|
||||
except (AttributeError, IndexError, ValueError):
|
||||
logger.error(traceback.format_exc())
|
||||
logger.exception("abrp:")
|
||||
return False
|
||||
|
||||
def __iter__(self):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import sys
|
||||
import sqlite3
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from time import sleep
|
||||
|
||||
@@ -17,30 +16,37 @@ from utils import get_temp
|
||||
NEW_BATTERY_COLUMNS = [["price", "INTEGER"], ["charging_mode", "TEXT"]]
|
||||
NEW_POSITION_COLUMNS = [["level_fuel", "INTEGER"], ["altitude", "INTEGER"]]
|
||||
|
||||
DATE_FORMAT = "%Y-%m-%d %H:%M:%S+00:00"
|
||||
|
||||
|
||||
def convert_sql_res(rows):
|
||||
return list(map(dict, rows))
|
||||
|
||||
|
||||
DATE_FORMAT = "%Y-%m-%d %H:%M:%S+00:00"
|
||||
|
||||
|
||||
def new_convert_datetime_from_string(string):
|
||||
return datetime.fromisoformat(string)
|
||||
|
||||
|
||||
class Database:
|
||||
callback_fct: Callable[[], None] = lambda: None
|
||||
DEFAULT_DB_FILE = 'info.db'
|
||||
# pylint: disable=invalid-name
|
||||
db_initialized = False
|
||||
|
||||
@staticmethod
|
||||
def convert_datetime_from_bytes(bytes_string):
|
||||
return datetime.strptime(bytes_string.decode("utf-8"), DATE_FORMAT).replace(tzinfo=pytz.UTC)
|
||||
def convert_datetime_from_string(string):
|
||||
try:
|
||||
return datetime.strptime(string, DATE_FORMAT).replace(tzinfo=pytz.UTC)
|
||||
except ValueError:
|
||||
return datetime.strptime(string.replace("T", " "), DATE_FORMAT).replace(tzinfo=pytz.UTC)
|
||||
|
||||
@staticmethod
|
||||
def convert_datetime_from_string(st):
|
||||
return datetime.strptime(st, DATE_FORMAT).replace(tzinfo=pytz.UTC)
|
||||
def convert_datetime_from_bytes(bytes_string):
|
||||
return Database.convert_datetime_from_string(bytes_string.decode("utf-8"))
|
||||
|
||||
@staticmethod
|
||||
def convert_datetime_to_string(date: datetime):
|
||||
return date.replace(tzinfo=pytz.UTC).strftime(DATE_FORMAT)
|
||||
return date.replace(tzinfo=pytz.UTC).isoformat(timespec='seconds', sep=" ")
|
||||
|
||||
@staticmethod
|
||||
def update_callback():
|
||||
@@ -89,14 +95,16 @@ class Database:
|
||||
Database.clean_battery(conn)
|
||||
Database.add_altitude_to_db(conn)
|
||||
conn.commit()
|
||||
if sys.version_info >= (3, 7):
|
||||
Database.convert_datetime_from_string = new_convert_datetime_from_string
|
||||
sqlite3.register_converter("DATETIME", Database.convert_datetime_from_bytes)
|
||||
sqlite3.register_adapter(datetime, Database.convert_datetime_to_string)
|
||||
Database.db_initialized = True
|
||||
|
||||
@staticmethod
|
||||
def get_db(db_file=None, update_callback=True):
|
||||
if db_file is None:
|
||||
db_file = Database.DEFAULT_DB_FILE
|
||||
sqlite3.register_converter("DATETIME", Database.convert_datetime_from_bytes)
|
||||
sqlite3.register_adapter(datetime, Database.convert_datetime_to_string)
|
||||
conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
||||
conn.row_factory = sqlite3.Row
|
||||
if update_callback:
|
||||
@@ -179,7 +187,6 @@ class Database:
|
||||
break
|
||||
except (ValueError, KeyError, requests.exceptions.RequestException):
|
||||
logger.error("Can't get altitude from API")
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
@staticmethod
|
||||
def get_recorded_position():
|
||||
|
||||
+2
-8
@@ -1,9 +1,7 @@
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
|
||||
from typing import List
|
||||
|
||||
import pytz
|
||||
import dash_bootstrap_components as dbc
|
||||
import dash_table
|
||||
import numpy as np
|
||||
@@ -233,13 +231,9 @@ def __calculate_co2_per_kw(charging_data):
|
||||
return 0
|
||||
|
||||
|
||||
def dash_date_to_datetime(dash_date):
|
||||
return datetime.strptime(dash_date, "%Y-%m-%dT%H:%M:%S+00:00").replace(tzinfo=pytz.UTC)
|
||||
|
||||
|
||||
def get_battery_curve_fig(row: dict, car: Car):
|
||||
start_date = dash_date_to_datetime(row["start_at"])
|
||||
stop_at = dash_date_to_datetime(row["stop_at"])
|
||||
start_date = Database.convert_datetime_from_string(row["start_at"])
|
||||
stop_at = Database.convert_datetime_from_string(row["stop_at"])
|
||||
res = Database.get_battery_curve(Database.get_db(), start_date, car.vin)
|
||||
res.insert(0, {"level": row["start_level"], "date": start_date})
|
||||
res.append({"level": row["end_level"], "date": stop_at})
|
||||
|
||||
+28
-34
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
import traceback
|
||||
from datetime import datetime, timezone
|
||||
from typing import List
|
||||
|
||||
@@ -55,7 +54,7 @@ def diff_dashtable(data, data_previous, row_id_name="row_id"):
|
||||
return changes
|
||||
|
||||
|
||||
def create_callback():
|
||||
def create_callback(): # flake8: noqa: C901
|
||||
global CALLBACK_CREATED
|
||||
if not CALLBACK_CREATED:
|
||||
@dash_app.callback(Output('trips_map', 'figure'),
|
||||
@@ -95,42 +94,38 @@ def create_callback():
|
||||
diff_data = diff_dashtable(data, data_previous, "start_at")
|
||||
for changed_line in diff_data:
|
||||
if changed_line['column_name'] == 'price':
|
||||
if not Database.set_chargings_price(Database.get_db(),
|
||||
figures.dash_date_to_datetime(changed_line['start_at']),
|
||||
if not Database.set_chargings_price(Database.get_db(),changed_line['start_at'],
|
||||
changed_line['current_value']):
|
||||
logger.error("Can't find line to update in the database")
|
||||
return ""
|
||||
|
||||
@dash_app.callback([Output("tab_battery_popup_graph", "children"), Output("tab_battery_popup", "is_open"), ],
|
||||
[Input("battery-table", "active_cell"),
|
||||
Input("tab_battery_popup-close", "n_clicks")],
|
||||
[State('battery-table', 'data'),
|
||||
State("tab_battery_popup", "is_open")])
|
||||
def get_battery_curve(active_cell, close, data, is_open): # pylint: disable=unused-argument, unused-variable
|
||||
if is_open is None:
|
||||
is_open = False
|
||||
if active_cell is not None and active_cell["column_id"] in ["start_level", "end_level"] and not is_open:
|
||||
row = data[active_cell["row"]]
|
||||
return figures.get_battery_curve_fig(row, myp.vehicles_list[0]), True
|
||||
return "", False
|
||||
|
||||
@dash_app.callback([Output("tab_trips_popup_graph", "children"), Output("tab_trips_popup", "is_open"), ],
|
||||
[Input("trips-table", "active_cell"),
|
||||
Input("tab_trips_popup-close", "n_clicks")],
|
||||
State("tab_trips_popup", "is_open"))
|
||||
def get_altitude(active_cell, close, is_open): # pylint: disable=unused-argument, unused-variable
|
||||
if is_open is None:
|
||||
is_open = False
|
||||
if active_cell is not None and active_cell["column_id"] in ["altitude_diff"] and not is_open:
|
||||
return figures.get_altitude_fig(trips[active_cell["row_id"] - 1]), True
|
||||
return "", False
|
||||
|
||||
CALLBACK_CREATED = True
|
||||
|
||||
|
||||
@dash_app.callback([Output("tab_battery_popup_graph", "children"), Output("tab_battery_popup", "is_open"), ],
|
||||
[Input("battery-table", "active_cell"),
|
||||
Input("tab_battery_popup-close", "n_clicks")],
|
||||
[State('battery-table', 'data'),
|
||||
State("tab_battery_popup", "is_open")])
|
||||
def get_battery_curve(active_cell, close, data, is_open): # pylint: disable=unused-argument
|
||||
if is_open is None:
|
||||
is_open = False
|
||||
if active_cell is not None and active_cell["column_id"] in ["start_level", "end_level"] and not is_open:
|
||||
row = data[active_cell["row"]]
|
||||
return figures.get_battery_curve_fig(row, myp.vehicles_list[0]), True
|
||||
return "", False
|
||||
|
||||
|
||||
@dash_app.callback([Output("tab_trips_popup_graph", "children"), Output("tab_trips_popup", "is_open"), ],
|
||||
[Input("trips-table", "active_cell"),
|
||||
Input("tab_trips_popup-close", "n_clicks")],
|
||||
State("tab_trips_popup", "is_open"))
|
||||
def get_altitude(active_cell, close, is_open): # pylint: disable=unused-argument
|
||||
print("altitude")
|
||||
if is_open is None:
|
||||
is_open = False
|
||||
if active_cell is not None and active_cell["column_id"] in ["altitude_diff"] and not is_open:
|
||||
return figures.get_altitude_fig(trips[active_cell["row_id"] - 1]), True
|
||||
return "", False
|
||||
|
||||
|
||||
@dash_app.callback(Output({'role': ABRP_SWITCH + RESPONSE, 'vin': MATCH}, 'children'),
|
||||
Input({'role': ABRP_SWITCH, 'vin': MATCH}, 'id'),
|
||||
Input({'role': ABRP_SWITCH, 'vin': MATCH}, 'value'))
|
||||
@@ -267,7 +262,7 @@ def update_trips():
|
||||
marks = figures.get_marks_from_start_end(min_date, max_date)
|
||||
cached_layout = None # force regenerate layout
|
||||
except (ValueError, IndexError):
|
||||
logger.error("update_trips (slider): %s", traceback.format_exc())
|
||||
logger.error("update_trips (slider): %s", exc_info=True)
|
||||
return
|
||||
|
||||
|
||||
@@ -315,7 +310,6 @@ def serve_layout():
|
||||
summary_tab = figures.ERROR_DIV
|
||||
maps = figures.ERROR_DIV
|
||||
logger.warning("Failed to generate figure, there is probably not enough data yet")
|
||||
logger.debug(traceback.format_exc())
|
||||
range_slider = html.Div()
|
||||
data_div = html.Div([
|
||||
range_slider,
|
||||
@@ -371,6 +365,6 @@ try:
|
||||
Charging.set_default_price()
|
||||
update_trips()
|
||||
except (IndexError, TypeError):
|
||||
logger.debug("Failed to get trips, there is probably not enough data yet %s", traceback.format_exc())
|
||||
logger.debug("Failed to get trips, there is probably not enough data yet:", exc_info=True)
|
||||
|
||||
dash_app.layout = serve_layout
|
||||
|
||||
Reference in New Issue
Block a user