From 6882300c98f5b9b2879b1e378016476a8412f81c Mon Sep 17 00:00:00 2001 From: DavisVT Date: Mon, 22 Jun 2026 13:27:54 +0100 Subject: [PATCH] Optimize dashboard stats: add missing indexes and fix events-server --- listener/src/api/events-server.ts | 10 ++-------- listener/src/database/schema.sql | 6 ++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/listener/src/api/events-server.ts b/listener/src/api/events-server.ts index 76fdb65..ddf4fb4 100644 --- a/listener/src/api/events-server.ts +++ b/listener/src/api/events-server.ts @@ -6,9 +6,8 @@ import { PreferencesUpdateInput } from '../types/preferences'; import { NotificationAPI } from '../services/notification-api'; import { NotificationType } from '../types/scheduled-notification'; import logger from '../utils/logger'; -import { generateRequestId } from '../utils/request-id'; -import { NotificationHistoryService } from '../services/notification-history'; import { generateRequestId, resolveCorrelationId } from '../utils/request-id'; +import { NotificationHistoryService } from '../services/notification-history'; import { verifySignature, extractSignature, @@ -410,7 +409,6 @@ export function createEventsServer(options: EventsServerOptions): http.Server { const endDate = url.searchParams.get('endDate'); logger.info('Handling GET /api/notifications/history', { - requestId, limit, offset, status, @@ -431,14 +429,13 @@ export function createEventsServer(options: EventsServerOptions): http.Server { logger.info('GET /api/notifications/history complete', { requestId, - returned: result.records.length, total: result.total, durationMs: Date.now() - startTime, }); }) .catch((error) => { logger.error('Failed to retrieve notification history', { error, requestId }); - res.writeHead(500, { 'Content-Type': 'application/json' }); + logger.error('Failed to retrieve notification history', { error, requestId }); res.end(JSON.stringify({ error: (error as Error).message })); }); return; @@ -454,9 +451,7 @@ export function createEventsServer(options: EventsServerOptions): http.Server { if (req.method === 'GET' && getPrefsMatch) { const userId = decodeURIComponent(getPrefsMatch[1]); const prefs = preferenceStore.get(userId); - res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(prefs)); - return; } // PUT /api/preferences/:userId @@ -483,7 +478,6 @@ export function createEventsServer(options: EventsServerOptions): http.Server { }); return; } - logger.warn('Unhandled request', { requestId, correlationId, method: req.method, url: req.url }); res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Not found' })); diff --git a/listener/src/database/schema.sql b/listener/src/database/schema.sql index b303b4a..4591ca0 100644 --- a/listener/src/database/schema.sql +++ b/listener/src/database/schema.sql @@ -38,6 +38,9 @@ CREATE TABLE IF NOT EXISTS scheduled_notifications ( ); -- Indexes for performance optimization +CREATE INDEX IF NOT EXISTS idx_scheduled_notifications_status + ON scheduled_notifications(status); + CREATE INDEX IF NOT EXISTS idx_scheduled_notifications_status_execute_at ON scheduled_notifications(status, execute_at) WHERE status = 'PENDING'; @@ -76,6 +79,9 @@ CREATE INDEX IF NOT EXISTS idx_execution_log_notification_id CREATE INDEX IF NOT EXISTS idx_execution_log_execution_time ON notification_execution_log(execution_time); +CREATE INDEX IF NOT EXISTS idx_execution_log_status_execution_time + ON notification_execution_log(status, execution_time); + -- Trigger to update updated_at timestamp CREATE TRIGGER IF NOT EXISTS update_scheduled_notifications_timestamp AFTER UPDATE ON scheduled_notifications