forked from trent_larson/crowd-funder-for-time-pwa
correct & simplify the DB logging
This commit is contained in:
@@ -107,7 +107,7 @@ const MIGRATIONS = [
|
|||||||
CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(name);
|
CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(name);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS logs (
|
CREATE TABLE IF NOT EXISTS logs (
|
||||||
date TEXT PRIMARY KEY,
|
date TEXT NOT NULL,
|
||||||
message TEXT NOT NULL
|
message TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let lastCleanupDate: string | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs a message to the database with proper handling of concurrent writes
|
* Logs a message to the database with proper handling of concurrent writes
|
||||||
* @param message - The message to log
|
* @param message - The message to log
|
||||||
@@ -135,65 +137,29 @@ export async function logToDb(message: string): Promise<void> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to insert first, if it fails due to UNIQUE constraint, update instead
|
// Try to insert first, if it fails due to UNIQUE constraint, update instead
|
||||||
try {
|
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
||||||
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
todayKey,
|
||||||
todayKey,
|
fullMessage,
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up old logs (keep only last 7 days) - do this less frequently
|
// 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
|
// Only clean up if the date is different from the last cleanup
|
||||||
const lastCleanupKey = "last_log_cleanup";
|
if (!lastCleanupDate || lastCleanupDate !== todayKey) {
|
||||||
const result = await platform.dbQuery(
|
|
||||||
"SELECT value FROM settings WHERE key = ?",
|
|
||||||
[lastCleanupKey],
|
|
||||||
);
|
|
||||||
|
|
||||||
const lastCleanup = result?.values[0]?.[0] as string;
|
|
||||||
if (!lastCleanup || lastCleanup !== todayKey) {
|
|
||||||
const sevenDaysAgo = new Date();
|
const sevenDaysAgo = new Date();
|
||||||
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
|
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
|
||||||
await platform.dbExec("DELETE FROM logs WHERE date < ?", [
|
await platform.dbExec("DELETE FROM logs WHERE date < ?", [
|
||||||
sevenDaysAgo.toDateString(),
|
sevenDaysAgo.toDateString(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Update last cleanup date
|
|
||||||
await platform.dbExec(
|
|
||||||
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
|
|
||||||
[lastCleanupKey, todayKey],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log to console as fallback
|
// Log to console as fallback
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error("Failed to log to database:", error);
|
console.error(
|
||||||
// eslint-disable-next-line no-console
|
"Error logging to database:",
|
||||||
console.error("Original message:", message);
|
error,
|
||||||
|
" ... for original message:",
|
||||||
|
message,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -143,6 +143,14 @@ class AbsurdSqlDatabaseService implements DatabaseService {
|
|||||||
}
|
}
|
||||||
operation.resolve(result);
|
operation.resolve(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(
|
||||||
|
"Error while processing SQL queue:",
|
||||||
|
error,
|
||||||
|
" ... for sql:",
|
||||||
|
operation.sql,
|
||||||
|
" ... with params:",
|
||||||
|
operation.params,
|
||||||
|
);
|
||||||
operation.reject(error);
|
operation.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,7 @@
|
|||||||
No logs found.
|
No logs found.
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<div v-for="(log, index) in logs" :key="index" class="mb-8">
|
<div v-for="(log, index) in logs" :key="index" class="mb-2">
|
||||||
<h2 class="text-lg font-semibold mb-2">{{ log.date }}</h2>
|
|
||||||
<pre
|
<pre
|
||||||
class="bg-slate-100 p-4 rounded-md overflow-x-auto whitespace-pre-wrap"
|
class="bg-slate-100 p-4 rounded-md overflow-x-auto whitespace-pre-wrap"
|
||||||
>{{ log.message }}</pre
|
>{{ log.message }}</pre
|
||||||
|
|||||||
Reference in New Issue
Block a user