From 3e90bafbd1321e2b550c3b1e3b14dc3b8cebec03 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 28 May 2025 19:37:01 -0600 Subject: [PATCH 1/5] correct & simplify the DB logging --- src/db-sql/migration.ts | 2 +- src/db/databaseUtil.ts | 60 +++++------------------- src/services/AbsurdSqlDatabaseService.ts | 8 ++++ src/views/LogView.vue | 3 +- 4 files changed, 23 insertions(+), 50 deletions(-) diff --git a/src/db-sql/migration.ts b/src/db-sql/migration.ts index ce917472..7a99724f 100644 --- a/src/db-sql/migration.ts +++ b/src/db-sql/migration.ts @@ -107,7 +107,7 @@ const MIGRATIONS = [ CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(name); CREATE TABLE IF NOT EXISTS logs ( - date TEXT PRIMARY KEY, + date TEXT NOT NULL, message TEXT NOT NULL ); diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index 610d5f69..6ab32d4e 100644 --- a/src/db/databaseUtil.ts +++ b/src/db/databaseUtil.ts @@ -123,6 +123,8 @@ export async function retrieveSettingsForActiveAccount(): Promise { } } +let lastCleanupDate: string | null = null; + /** * Logs a message to the database with proper handling of concurrent writes * @param message - The message to log @@ -135,65 +137,29 @@ export async function logToDb(message: string): Promise { try { // Try to insert first, if it fails due to UNIQUE constraint, update instead - try { - await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [ - todayKey, - fullMessage, - ]); - } catch (error) { - // If insert fails due to UNIQUE constraint, update instead - if ( - error instanceof Error && - error.message.includes("UNIQUE constraint failed") - ) { - const result = await platform.dbQuery( - "SELECT message FROM logs WHERE date = ?", - [todayKey], - ); - - if (result && result.values.length > 0) { - const prevMessages = result.values[0][0] as string; - const updatedMessage = `${prevMessages}\n${fullMessage}`; - - await platform.dbExec("UPDATE logs SET message = ? WHERE date = ?", [ - updatedMessage, - todayKey, - ]); - } - } else { - // If it's a different error, rethrow it - throw error; - } - } + await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [ + todayKey, + fullMessage, + ]); // Clean up old logs (keep only last 7 days) - do this less frequently // Only clean up if the date is different from the last cleanup - const lastCleanupKey = "last_log_cleanup"; - const result = await platform.dbQuery( - "SELECT value FROM settings WHERE key = ?", - [lastCleanupKey], - ); - - const lastCleanup = result?.values[0]?.[0] as string; - if (!lastCleanup || lastCleanup !== todayKey) { + if (!lastCleanupDate || lastCleanupDate !== todayKey) { const sevenDaysAgo = new Date(); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); await platform.dbExec("DELETE FROM logs WHERE date < ?", [ sevenDaysAgo.toDateString(), ]); - - // Update last cleanup date - await platform.dbExec( - "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)", - [lastCleanupKey, todayKey], - ); } } catch (error) { // Log to console as fallback // eslint-disable-next-line no-console - console.error("Failed to log to database:", error); - // eslint-disable-next-line no-console - console.error("Original message:", message); + console.error( + "Error logging to database:", + error, + " ... for original message:", + message, + ); } } diff --git a/src/services/AbsurdSqlDatabaseService.ts b/src/services/AbsurdSqlDatabaseService.ts index e98d6662..a8df16e3 100644 --- a/src/services/AbsurdSqlDatabaseService.ts +++ b/src/services/AbsurdSqlDatabaseService.ts @@ -143,6 +143,14 @@ class AbsurdSqlDatabaseService implements DatabaseService { } operation.resolve(result); } catch (error) { + console.log( + "Error while processing SQL queue:", + error, + " ... for sql:", + operation.sql, + " ... with params:", + operation.params, + ); operation.reject(error); } } diff --git a/src/views/LogView.vue b/src/views/LogView.vue index e90231e6..bdfe7297 100644 --- a/src/views/LogView.vue +++ b/src/views/LogView.vue @@ -35,8 +35,7 @@ No logs found.
-
-

{{ log.date }}

+
{{ log.message }}
Date: Wed, 28 May 2025 19:44:16 -0600 Subject: [PATCH 2/5] fix log cleanup check to actually pay attention to limit --- src/db/databaseUtil.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index 6ab32d4e..d69b8154 100644 --- a/src/db/databaseUtil.ts +++ b/src/db/databaseUtil.ts @@ -150,6 +150,7 @@ export async function logToDb(message: string): Promise { await platform.dbExec("DELETE FROM logs WHERE date < ?", [ sevenDaysAgo.toDateString(), ]); + lastCleanupDate = todayKey; } } catch (error) { // Log to console as fallback From 01c33069c41ea85c1a9072bb46f38016023d55d4 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 28 May 2025 20:08:09 -0600 Subject: [PATCH 3/5] fix more of the logging & log display --- src/db/databaseUtil.ts | 6 +++--- src/views/LogView.vue | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index d69b8154..0dd5fb06 100644 --- a/src/db/databaseUtil.ts +++ b/src/db/databaseUtil.ts @@ -133,13 +133,13 @@ let lastCleanupDate: string | null = null; export async function logToDb(message: string): Promise { const platform = PlatformServiceFactory.getInstance(); const todayKey = new Date().toDateString(); - const fullMessage = `${new Date().toISOString()} ${message}`; + const nowKey = new Date().toISOString(); try { // Try to insert first, if it fails due to UNIQUE constraint, update instead await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [ - todayKey, - fullMessage, + nowKey, + message, ]); // Clean up old logs (keep only last 7 days) - do this less frequently diff --git a/src/views/LogView.vue b/src/views/LogView.vue index bdfe7297..deecbb56 100644 --- a/src/views/LogView.vue +++ b/src/views/LogView.vue @@ -38,7 +38,7 @@
{{ log.message }}
{{ log.date }} {{ log.message }}
@@ -81,21 +81,22 @@ export default class LogView extends Vue { let allLogs: Log[] = []; const platformService = PlatformServiceFactory.getInstance(); - const queryResult = await platformService.dbQuery("SELECT * FROM logs"); - allLogs = databaseUtil.mapQueryResultToValues( + const queryResult = await platformService.dbQuery( + "SELECT * FROM logs ORDER BY date DESC", + ); + this.logs = databaseUtil.mapQueryResultToValues( queryResult, ) as unknown as Log[]; if (USE_DEXIE_DB) { await db.open(); allLogs = await db.logs.toArray(); + // Sort by date in reverse chronological order + this.logs = allLogs.sort((a, b) => { + const dateA = new Date(a.date); + const dateB = new Date(b.date); + return dateB.getTime() - dateA.getTime(); + }); } - - // Sort by date in reverse chronological order - this.logs = allLogs.sort((a, b) => { - const dateA = new Date(a.date); - const dateB = new Date(b.date); - return dateB.getTime() - dateA.getTime(); - }); } catch (error) { logger.error("Error loading logs:", error); this.error = From ec1f27bab196a6b641c1ec22d37cca3ae04083e8 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 28 May 2025 20:20:09 -0600 Subject: [PATCH 4/5] fix more logging cleanup errors --- src/db/databaseUtil.ts | 3 +-- src/utils/logger.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index 0dd5fb06..6dddecb9 100644 --- a/src/db/databaseUtil.ts +++ b/src/db/databaseUtil.ts @@ -145,8 +145,7 @@ export async function logToDb(message: string): Promise { // Clean up old logs (keep only last 7 days) - do this less frequently // Only clean up if the date is different from the last cleanup if (!lastCleanupDate || lastCleanupDate !== todayKey) { - const sevenDaysAgo = new Date(); - sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); + const sevenDaysAgo = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000); await platform.dbExec("DELETE FROM logs WHERE date < ?", [ sevenDaysAgo.toDateString(), ]); diff --git a/src/utils/logger.ts b/src/utils/logger.ts index fa4368bd..de4804e2 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -25,7 +25,7 @@ export const logger = { // eslint-disable-next-line no-console console.debug(message, ...args); const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; - logToDb(message + argsString); + // logToDb(message + argsString); } }, log: (message: string, ...args: unknown[]) => { From ef3bfcdbd2e398678a2564ed9d2799e175ef1c6c Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 28 May 2025 20:30:00 -0600 Subject: [PATCH 5/5] fix linting --- src/db/databaseUtil.ts | 4 +++- src/services/AbsurdSqlDatabaseService.ts | 2 +- src/utils/logger.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index 6dddecb9..f8079c97 100644 --- a/src/db/databaseUtil.ts +++ b/src/db/databaseUtil.ts @@ -145,7 +145,9 @@ export async function logToDb(message: string): Promise { // Clean up old logs (keep only last 7 days) - do this less frequently // Only clean up if the date is different from the last cleanup if (!lastCleanupDate || lastCleanupDate !== todayKey) { - const sevenDaysAgo = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000); + const sevenDaysAgo = new Date( + new Date().getTime() - 7 * 24 * 60 * 60 * 1000, + ); await platform.dbExec("DELETE FROM logs WHERE date < ?", [ sevenDaysAgo.toDateString(), ]); diff --git a/src/services/AbsurdSqlDatabaseService.ts b/src/services/AbsurdSqlDatabaseService.ts index a8df16e3..878dbea5 100644 --- a/src/services/AbsurdSqlDatabaseService.ts +++ b/src/services/AbsurdSqlDatabaseService.ts @@ -143,7 +143,7 @@ class AbsurdSqlDatabaseService implements DatabaseService { } operation.resolve(result); } catch (error) { - console.log( + logger.error( "Error while processing SQL queue:", error, " ... for sql:", diff --git a/src/utils/logger.ts b/src/utils/logger.ts index de4804e2..2cbb228b 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -24,7 +24,7 @@ export const logger = { if (process.env.NODE_ENV !== "production") { // eslint-disable-next-line no-console console.debug(message, ...args); - const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; + // const argsString = args.length > 0 ? " - " + safeStringify(args) : ""; // logToDb(message + argsString); } },