diff --git a/frontend/src/lib/api/sse.ts b/frontend/src/lib/api/sse.ts index 5ace487..119a193 100644 --- a/frontend/src/lib/api/sse.ts +++ b/frontend/src/lib/api/sse.ts @@ -1,4 +1,5 @@ import { metrics, connected, historyData } from '$lib/stores/metrics'; +import { settings } from '$lib/stores/settings'; import { browser } from '$app/environment'; import type { AllMetrics, HistoryData } from '$lib/types/metrics'; @@ -35,6 +36,8 @@ export function connectSSE() { eventSource.onopen = () => { connected.set(true); console.log('SSE connected'); + // Sync stored settings to backend (in case container was restarted) + settings.syncToBackend(); }; eventSource.onmessage = (event) => { diff --git a/frontend/src/lib/stores/settings.ts b/frontend/src/lib/stores/settings.ts index 8ab2fbe..b04e856 100644 --- a/frontend/src/lib/stores/settings.ts +++ b/frontend/src/lib/stores/settings.ts @@ -15,21 +15,29 @@ function createSettingsStore() { const { subscribe, set, update } = writable(initial); + // Helper to push refresh rate to backend + async function pushRefreshRate(rate: number) { + try { + await fetch('/api/v1/settings/refresh', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ interval: rate }) + }); + } catch (e) { + console.error('Failed to update refresh rate:', e); + } + } + return { subscribe, setRefreshRate: async (rate: number) => { update((s) => ({ ...s, refreshRate: rate })); - - // Notify backend - try { - await fetch('/api/v1/settings/refresh', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ interval: rate }) - }); - } catch (e) { - console.error('Failed to update refresh rate:', e); - } + await pushRefreshRate(rate); + }, + // Sync stored settings to backend (call on SSE connect) + syncToBackend: async () => { + const current = stored ? JSON.parse(stored) : defaultSettings; + await pushRefreshRate(current.refreshRate); } }; }