Skip to content

Commit

Permalink
web: Add updated column in daily chart to debug why interter send sum…
Browse files Browse the repository at this point in the history
…mary of yesterday instead of today
  • Loading branch information
hoang-rio committed Nov 29, 2024
1 parent 23ab0fe commit a17a68a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a17a68a

Please sign in to comment.