diff --git a/app.py b/app.py index 315c561..5e7a8f6 100644 --- a/app.py +++ b/app.py @@ -190,22 +190,24 @@ def insert_daly_chart(db_connection: sqlite3.Connection, inverter_data: dict): "grid_import": inverter_data["e_to_user_day"], "grid_export": inverter_data["e_to_grid_day"], "consumption": round(consumption, 1), + "updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } is_exist = cursor.execute( "SELECT id, consumption FROM daily_chart WHERE id = ?", (item_id,) ).fetchone() if is_exist is None: cursor.execute( - "INSERT INTO daily_chart (id, year, month, date, pv, battery_charged, battery_discharged, grid_import, grid_export, consumption) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO daily_chart (id, year, month, date, pv, battery_charged, battery_discharged, grid_import, grid_export, consumption, updated) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (item_id, daily_chart_item["year"], daily_chart_item["month"], daily_chart_item["date"], daily_chart_item["pv"], daily_chart_item["battery_charged"], daily_chart_item["battery_discharged"], daily_chart_item["grid_import"], daily_chart_item["grid_export"], - daily_chart_item["consumption"]), + daily_chart_item["consumption"], daily_chart_item["updated"]), ) else: + logger.warning("Daily chart info", daily_chart_item, device_time) _, current_consumption = is_exist if consumption >= current_consumption: cursor.execute( - "UPDATE daily_chart SET year = ?, month = ?, date = ?, pv = ?, battery_charged = ?, battery_discharged = ?, grid_import = ?, grid_export = ?, consumption = ? WHERE id = ?", + "UPDATE daily_chart SET year = ?, month = ?, date = ?, pv = ?, battery_charged = ?, battery_discharged = ?, grid_import = ?, grid_export = ?, consumption = ?, updated =? WHERE id = ?", ( daily_chart_item["year"], daily_chart_item["month"], @@ -216,6 +218,7 @@ def insert_daly_chart(db_connection: sqlite3.Connection, inverter_data: dict): daily_chart_item["grid_import"], daily_chart_item["grid_export"], daily_chart_item["consumption"], + daily_chart_item["updated"], item_id) ) cursor.close() diff --git a/migration.py b/migration.py index cd9af0e..38f4a5c 100644 --- a/migration.py +++ b/migration.py @@ -16,6 +16,7 @@ "CREATE TABLE IF NOT EXISTS migration (id INTEGER PRIMARY KEY, applied_at TEXT)", "CREATE TABLE IF NOT EXISTS hourly_chart (id VARCHAR PRIMARY KEY, datetime TEXT, pv INTEGER, battery INTEGER, grid INTEGER, consumption INTEGER, soc INTERGER)", "CREATE TABLE IF NOT EXISTS daily_chart (id VARCHAR PRIMARY KEY, year INTEGER, month INTEGER, date TEXT, pv INTEGER, battery_charged INTEGER, battery_discharged INTEGER, grid_import INTEGER, grid_export INTEGER, consumption INTEGER)", + "ALTER TABLE daily_chart ADD COLUMN updated TEXT" ] def execute_migration_sql(id: int, sql: str, cursor: sqlite3.Cursor) -> None: global logger