Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(web): stop the FIM dashboard refetching on every render (#1258)

antigravitysoham-engbfe45ba06095
diff --git a/apps/web/src/components/fim/FimDashboard.test.tsx b/apps/web/src/components/fim/FimDashboard.test.tsx
new file mode 100644
index 000000000..74febe0df
--- /dev/null
+++ b/apps/web/src/components/fim/FimDashboard.test.tsx
@@ -0,0 +1,69 @@
+import { describe, expect, it, vi, beforeEach } from 'vitest';
+import { render, waitFor } from '@testing-library/react';
+import { SWRConfig } from 'swr';
+
+// The SWR keys used to embed `sinceToISO(since)`, an absolute timestamp
+// recomputed from `new Date()` on every render. Each render produced a new
+// key, so each render fetched again: an unbounded request loop, and with the
+// osquery service absent (the core profile) one error toast per attempt.
+
+const getFimEvents = vi.fn();
+const getFimSummary = vi.fn();
+vi.mock('@/lib/osquery-api', () => ({
+ getFimEvents: (...a: unknown[]) => getFimEvents(...a),
+ getFimSummary: (...a: unknown[]) => getFimSummary(...a),
+}));
+
+const toastError = vi.fn();
+vi.mock('react-hot-toast', () => ({ default: { error: (...a: unknown[]) => toastError(...a) } }));
+
+import { FimDashboard } from './FimDashboard';
+
+function renderDashboard() {
+ return render(
+ <SWRConfig value={{ provider: () => new Map(), dedupingInterval: 0, shouldRetryOnError: false }}>
+ <FimDashboard />
+ </SWRConfig>,
+ );
+}
+
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
+
+describe('FimDashboard', () => {
+ beforeEach(() => {
+ getFimEvents.mockReset();
+ getFimSummary.mockReset();
+ toastError.mockReset();
+ });
+
+ it('fetches once per window, not once per render, when the backend is down', async () => {
+ getFimEvents.mockRejectedValue(new Error('500'));
+ getFimSummary.mockRejectedValue(new Error('500'));
+ renderDashboard();
+ await waitFor(() => expect(getFimSummary).toHaveBeenCalled());
+ await sleep(400);
+ expect(getFimEvents.mock.calls.length).toBeLessThanOrEqual(1);
+ expect(getFimSummary.mock.calls.length).toBeLessThanOrEqual(1);
+ });
+
+ it('fetches once per window, not once per render, when the backend answers', async () => {
+ getFimEvents.mockResolvedValue({ events: [], total: 0, page: 1, page_size: 25 });
+ getFimSummary.mockResolvedValue({ total_events: 0, by_action: [], top_paths: [], active_nodes: 0 });
+ renderDashboard();
+ await waitFor(() => expect(getFimSummary).toHaveBeenCalled());
+ await sleep(400);
+ expect(getFimEvents.mock.calls.length).toBeLessThanOrEqual(1);
+ expect(getFimSummary.mock.calls.length).toBeLessThanOrEqual(1);
+ });
+
+ it('shows one toast per failing feed, not one per attempt', async () => {
+ getFimEvents.mockRejectedValue(new Error('500'));
+ getFimSummary.mockRejectedValue(new Error('500'));
+ renderDashboard();
+ await waitFor(() => expect(toastError).toHaveBeenCalled());
+ await sleep(400);
+ for (const call of toastError.mock.calls) {
+ expect(call[1]).toEqual(expect.objectContaining({ id: expect.any(String) }));
+ }
+ });
+});
diff --git a/apps/web/src/components/fim/FimDashboard.tsx b/apps/web/src/components/fim/FimDashboard.tsx
index 50f91e55a..2d82a0412 100644
--- a/apps/web/src/components/fim/FimDashboard.tsx
+++ b/apps/web/src/components/fim/FimDashboard.tsx
@@ -67,10 +67,13 @@ export function FimDashboard() {
const [pathPrefix, setPathPrefix] = useState('');
const [since, setSince] = useState('24h');
- const sinceISO = sinceToISO(since);
+ // The SWR keys carry the relative window ('24h'), never the absolute
+ // timestamp: sinceToISO reads the clock, so a key built from it changed on
+ // every render and refetched on every render. The timestamp is computed
+ // inside each fetcher instead, at the moment the request goes out.
// Events feed
- const eventsKey = ['fim-events', page, action, pathPrefix, sinceISO];
+ const eventsKey = ['fim-events', page, action, pathPrefix, since];
const {
data: eventsData,
error: eventsError,
@@ -83,25 +86,26 @@ export function FimDashboard() {
page_size: PAGE_SIZE,
action: action || undefined,
path_prefix: pathPrefix || undefined,
- since: sinceISO,
+ since: sinceToISO(since),
}),
{
- onError: () => toast.error('Failed to load FIM events'),
+ // A fixed id collapses repeat failures into one toast.
+ onError: () => toast.error('Failed to load FIM events', { id: 'fim-events-error' }),
refreshInterval: 30_000,
},
);
// Summary cards
- const summaryKey = ['fim-summary', sinceISO];
+ const summaryKey = ['fim-summary', since];
const {
data: summaryData,
error: summaryError,
isLoading: summaryLoading,
} = useSWR<FimSummary>(
summaryKey,
- () => getFimSummary({ since: sinceISO }),
+ () => getFimSummary({ since: sinceToISO(since) }),
{
- onError: () => toast.error('Failed to load FIM summary'),
+ onError: () => toast.error('Failed to load FIM summary', { id: 'fim-summary-error' }),
refreshInterval: 60_000,
},
);