From 503027c06e45c340a808a0344a1c7e4e801eb263 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Sun, 1 Jun 2025 15:40:17 +1000 Subject: [PATCH] debug Online_History #1020 --- server/helper.py | 21 +++++++++++++++++++++ server/scan/session_events.py | 20 +++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/server/helper.py b/server/helper.py index e8c570a9..0f729f4b 100755 --- a/server/helper.py +++ b/server/helper.py @@ -634,6 +634,27 @@ def collect_lang_strings(json, pref, stringSqlParams): # Misc #------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- +def print_table_schema(db, table): + sql = db.sql + sql.execute(f"PRAGMA table_info({table})") + result = sql.fetchall() + + if not result: + mylog('none', f'[Schema] Table "{table}" not found or has no columns.') + return + + mylog('debug', f'[Schema] Structure for table: {table}') + header = f"{'cid':<4} {'name':<20} {'type':<10} {'notnull':<8} {'default':<10} {'pk':<2}" + mylog('debug', header) + mylog('debug', '-' * len(header)) + + for row in result: + # row = (cid, name, type, notnull, dflt_value, pk) + line = f"{row[0]:<4} {row[1]:<20} {row[2]:<10} {row[3]:<8} {str(row[4]):<10} {row[5]:<2}" + mylog('debug', line) + + #------------------------------------------------------------------------------- def checkNewVersion(): mylog('debug', [f"[Version check] Checking if new version available"]) diff --git a/server/scan/session_events.py b/server/scan/session_events.py index cb3811c5..842c2815 100755 --- a/server/scan/session_events.py +++ b/server/scan/session_events.py @@ -6,11 +6,14 @@ sys.path.extend([f"{INSTALL_PATH}/server"]) import conf from scan.device_handling import create_new_devices, print_scan_stats, save_scanned_devices, update_devices_data_from_scan, exclude_ignored_devices -from helper import timeNowTZ -from logger import mylog +from helper import timeNowTZ, print_table_schema, get_setting_value +from logger import mylog, Logger from messaging.reporting import skip_repeated_notifications +# Make sure log level is initialized correctly +Logger(get_setting_value('LOG_LEVEL')) + #=============================================================================== # SCAN NETWORK #=============================================================================== @@ -248,7 +251,7 @@ def insertOnlineHistory(db): # Query to fetch all relevant device counts in one go query = """ SELECT - COUNT(*) AS allDevics, + COUNT(*) AS allDevices, SUM(CASE WHEN devIsArchived = 1 THEN 1 ELSE 0 END) AS archivedDevices, SUM(CASE WHEN devPresentLastScan = 1 THEN 1 ELSE 0 END) AS onlineDevices, SUM(CASE WHEN devPresentLastScan = 0 AND devAlertDown = 1 THEN 1 ELSE 0 END) AS downDevices @@ -257,12 +260,12 @@ def insertOnlineHistory(db): deviceCounts = db.read(query)[0] # Assuming db.read returns a list of rows, take the first (and only) row - allDevics = deviceCounts['allDevics'] + allDevices = deviceCounts['allDevices'] archivedDevices = deviceCounts['archivedDevices'] onlineDevices = deviceCounts['onlineDevices'] downDevices = deviceCounts['downDevices'] - offlineDevices = allDevics - archivedDevices - onlineDevices + offlineDevices = allDevices - archivedDevices - onlineDevices # Prepare the insert query using parameterized inputs insert_query = """ @@ -270,10 +273,13 @@ def insertOnlineHistory(db): VALUES (?, ?, ?, ?, ?, ?) """ - mylog('debug', f'[Presence graph] Sql query: {insert_query} with values: {scanTimestamp}, {onlineDevices}, {downDevices}, {allDevics}, {archivedDevices}, {offlineDevices}') + mylog('debug', f'[Presence graph] Sql query: {insert_query} with values: {scanTimestamp}, {onlineDevices}, {downDevices}, {allDevices}, {archivedDevices}, {offlineDevices}') + + # Debug output + print_table_schema(db, "Online_History") # Insert the gathered data into the history table - sql.execute(insert_query, (scanTimestamp, onlineDevices, downDevices, allDevics, archivedDevices, offlineDevices)) + sql.execute(insert_query, (scanTimestamp, onlineDevices, downDevices, allDevices, archivedDevices, offlineDevices)) db.commitDB()