mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
feat: add scheduling anomaly drill-down and improve dev startup
This commit is contained in:
1 parent
d9e9e083ab
commit
2070931c82
21 files changed
+1185
-54
No files matched your search
@@ -3,7 +3,7 @@ const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const ts = require('typescript');
|
||||
|
||||
function dashboard() {
|
||||
function dashboard(anomaly) {
|
||||
const source = fs.readFileSync(require.resolve('../../src/pages/dashboard/index.tsx'), 'utf8');
|
||||
const ast = ts.createSourceFile('dashboard.tsx', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
||||
let effect;
|
||||
@@ -17,7 +17,8 @@ function dashboard() {
|
||||
const requests = [], results = [], failures = [], timers = new Set();
|
||||
const noop = () => {};
|
||||
const bindings = {
|
||||
config: { apiPrefix: '/api/' }, REFRESH_INTERVAL: 30000,
|
||||
performance: { now: () => 1000 },
|
||||
anomaly, config: { apiPrefix: '/api/' }, REFRESH_INTERVAL: 30000,
|
||||
setOverview: value => results.push(value), setOverviewFailed: value => failures.push(value),
|
||||
setLoading: noop, setTrend: noop, setSystem: noop, setTopTime: noop, setTopCount: noop, setLabels: noop, setRuntime: noop,
|
||||
setInterval(fn) { timers.add(fn); return fn; }, clearInterval(fn) { timers.delete(fn); },
|
||||
@@ -34,6 +35,50 @@ function dashboard() {
|
||||
}
|
||||
const flush = () => new Promise(resolve => setImmediate(resolve));
|
||||
|
||||
test('a first anomaly request that never succeeds still expires using the overview deadline', async () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100, deadline: 10000 };
|
||||
const close = v.start(selection, { page: 1 });
|
||||
v.tick(10000);
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: 'expired' }]);
|
||||
assert.equal(v.requests[0].signal.aborted, true);
|
||||
v.requests[0].resolve({ code: 200, data: { rows: ['yesterday'], summary: {} } });
|
||||
await flush();
|
||||
assert.deepEqual(v.received, []);
|
||||
close();
|
||||
});
|
||||
|
||||
test('a first failure and manual retry cannot reset the overview deadline', async () => {
|
||||
const v = anomalies(), selection = { source: 'system', kind: 'unconfirmed', at: 100, deadline: 10000 };
|
||||
const close = v.start(selection, { page: 1 });
|
||||
v.requests[0].reject(new Error('offline'));
|
||||
await flush();
|
||||
v.advance(7000);
|
||||
close();
|
||||
const cancel = v.start(selection, { page: 1 });
|
||||
v.tick(3000);
|
||||
assert.equal(v.requests[1].signal.aborted, true);
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: 'expired' }]);
|
||||
cancel();
|
||||
});
|
||||
|
||||
test('opening an already expired overview does not send an obsolete detail request', () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100, deadline: 1000 };
|
||||
v.advance(1001);
|
||||
const close = v.start(selection, { page: 1 });
|
||||
assert.equal(v.requests.length, 0);
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: 'expired' }]);
|
||||
close();
|
||||
});
|
||||
|
||||
test('overview deadlines use server time and request start, independent of the browser wall clock', async () => {
|
||||
const v = dashboard(), close = v.start();
|
||||
const serverNow = 1795017590000;
|
||||
v.requests('overview')[0].resolve({ code: 200, data: { serverNow, expiresAt: serverNow + 10000 } });
|
||||
await flush();
|
||||
assert.equal(v.results[0].anomalyDeadline, 11000);
|
||||
close();
|
||||
});
|
||||
|
||||
test('slow dashboard cards do not block crond counts, overlap a pending refresh or restart polling after unmount', async () => {
|
||||
const view = dashboard(), leave = view.start();
|
||||
view.tick();
|
||||
@@ -119,3 +164,242 @@ test('dashboard timing preserves millisecond precision, rounds long waits and di
|
||||
await intl.init({ currentLocale: 'en-US', locales });
|
||||
assert.equal(component.formatTiming(1703994), '28 min 24 s');
|
||||
});
|
||||
|
||||
function anomalies() {
|
||||
const filename = require.resolve('../../src/pages/dashboard/scheduleAnomalies.tsx');
|
||||
const source = fs.readFileSync(filename, 'utf8');
|
||||
const ast = ts.createSourceFile(filename, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
||||
let effect;
|
||||
function visit(node) {
|
||||
if (ts.isCallExpression(node) && node.expression.getText(ast) === 'useEffect')
|
||||
effect = node.arguments[0].getText(ast);
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(ast);
|
||||
const js = ts.transpile('const start = ' + effect, { target: ts.ScriptTarget.ES2022 });
|
||||
const requests = [], received = [], failed = [], summaries = [], invalidated = [], timers = new Map();
|
||||
let now = 0;
|
||||
const bindings = {
|
||||
expiresAt: { current: undefined },
|
||||
config: { apiPrefix: '/api/' },
|
||||
request: { get(url, options) {
|
||||
assert.equal(url, '/api/dashboard/schedule-anomalies');
|
||||
return new Promise((resolve, reject) => requests.push({ ...options, resolve, reject }));
|
||||
} },
|
||||
setResponse: data => received.push(data.result), setFailed: flag => failed.push(flag), setLoading() {},
|
||||
onUpdate: (selection, summary) => summaries.push(summary),
|
||||
onInvalidate: (selection, reason) => invalidated.push({ selection, reason }),
|
||||
performance: { now: () => now },
|
||||
setTimeout(fn, ms) { timers.set(fn, ms); return fn; }, clearTimeout(fn) { timers.delete(fn); },
|
||||
};
|
||||
return {
|
||||
start: (selection, query) => Function(...Object.keys(bindings), 'selection', 'query',
|
||||
js + '\nreturn start();')(...Object.values(bindings), selection, query),
|
||||
requests, received, failed, summaries, invalidated, timers,
|
||||
advance(ms) { now += ms; },
|
||||
tick(delay = 5000) { for (const [fn, ms] of [...timers]) if (ms === delay) { timers.delete(fn); fn(); } },
|
||||
};
|
||||
}
|
||||
|
||||
test('anomaly pagination pins its plan window, advances revisions after resets and stops old polling across source changes', async () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100 };
|
||||
const close = v.start(selection, { page: 2, revision: 'old' });
|
||||
assert.deepEqual(v.requests[0].params, { ...selection, live: true, page: 2, revision: 'old' });
|
||||
const updated = { rows: ['remaining'], page: 1, revision: 'new', total: 26, changed: true, summary: { audit: { missing: 26 } } };
|
||||
v.requests[0].resolve({ code: 200, data: updated });
|
||||
await flush();
|
||||
assert.deepEqual(v.received, [updated]);
|
||||
assert.deepEqual(v.summaries, [updated.summary]);
|
||||
v.tick(); v.tick();
|
||||
assert.equal(v.requests.length, 2);
|
||||
assert.deepEqual(v.requests[1].params, { ...selection, live: true, page: 1, revision: 'new' });
|
||||
close();
|
||||
assert.equal(v.requests[1].signal.aborted, true);
|
||||
const leave = v.start({ source: 'system', kind: 'unconfirmed', at: 200 }, { page: 1 });
|
||||
v.requests[1].resolve({ code: 200, data: { ...updated, total: 99 } });
|
||||
v.requests[2].reject(new Error('timeout'));
|
||||
await flush();
|
||||
assert.equal(v.failed.at(-1), true);
|
||||
assert.equal(v.received.length, 1);
|
||||
assert.equal(v.timers.size, 0);
|
||||
leave();
|
||||
const finish = v.start({ source: 'system', kind: 'unconfirmed', at: 200 }, { page: 1 });
|
||||
const empty = { rows: [], page: 1, revision: 'empty', total: 0, summary: { systemAudit: { actualUnconfirmed: 0 } } };
|
||||
v.requests[3].resolve({ code: 200, data: empty });
|
||||
await flush();
|
||||
assert.equal(v.failed.at(-1), false);
|
||||
assert.deepEqual(v.summaries, [updated.summary, empty.summary]);
|
||||
finish();
|
||||
v.tick();
|
||||
assert.equal(v.requests.length, 4);
|
||||
});
|
||||
|
||||
test('opening anomaly details cancels older overview responses and keeps its counts owned by the detail snapshot', async () => {
|
||||
const v = dashboard(), close = v.start();
|
||||
close();
|
||||
v.requests('overview')[0].resolve({ code: 200, data: { triggers: { audit: { missing: 55 } } } });
|
||||
await flush();
|
||||
assert.deepEqual(v.results, []);
|
||||
const opened = dashboard({ source: 'panel' }), leave = opened.start();
|
||||
opened.tick();
|
||||
assert.equal(opened.requests('overview').length, 0);
|
||||
assert.equal(opened.requests('runtime').length, 1);
|
||||
leave();
|
||||
});
|
||||
|
||||
for (const source of ['panel', 'system']) test(source + ' partial cleanup resets pagination and abandoned page replies cannot restore deleted rows after switching source', async () => {
|
||||
const v = anomalies(), selection = { source, kind: 'unconfirmed', at: 100, session: 1 };
|
||||
const stop = v.start(selection, { page: 3, revision: 'before-cleanup' });
|
||||
const retained = { page: 1, total: 40, revision: 'after-cleanup', changed: true,
|
||||
rows: [{ trigger: 'retained', hasLog: true }], summary: source === 'panel'
|
||||
? { audit: { unconfirmed: 40 } } : { systemAudit: { actualUnconfirmed: 40 } } };
|
||||
v.requests[0].resolve({ code: 200, data: retained });
|
||||
await flush();
|
||||
v.tick();
|
||||
assert.equal(v.requests[1].params.page, 1);
|
||||
assert.equal(v.requests[1].params.revision, 'after-cleanup');
|
||||
stop();
|
||||
const next = { ...selection, source: source === 'panel' ? 'system' : 'panel', session: 2 };
|
||||
const close = v.start(next, { page: 1 });
|
||||
const fresh = { ...retained, rows: [{ trigger: 'other-source', hasLog: true }], total: 1, revision: 'fresh',
|
||||
summary: next.source === 'panel' ? { audit: { unconfirmed: 1 } } : { systemAudit: { actualUnconfirmed: 1 } } };
|
||||
v.requests[2].resolve({ code: 200, data: fresh });
|
||||
await flush();
|
||||
v.requests[1].resolve({ code: 200, data: { ...retained, total: 60, rows: [{ trigger: 'deleted' }] } });
|
||||
await flush();
|
||||
assert.deepEqual(v.received, [retained, fresh]);
|
||||
assert.deepEqual(v.summaries, [retained.summary, fresh.summary]);
|
||||
close();
|
||||
assert.equal(v.timers.size, 0);
|
||||
});
|
||||
|
||||
test('midnight cancels a hung anomaly refresh and a late reply cannot publish yesterday counts', async () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100, session: 1 };
|
||||
const leave = v.start(selection, { page: 1 });
|
||||
const data = { page: 1, revision: 'before-midnight', rows: ['old'], summary: { audit: { missing: 1 } }, serverNow: 1000, expiresAt: 11000 };
|
||||
v.requests[0].resolve({ code: 200, data });
|
||||
await flush();
|
||||
v.tick();
|
||||
assert.equal(v.requests.length, 2);
|
||||
v.tick(10000);
|
||||
assert.equal(v.requests[1].signal.aborted, true);
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: 'expired' }]);
|
||||
v.requests[1].resolve({ code: 200, data });
|
||||
await flush();
|
||||
assert.deepEqual(v.summaries, [data.summary]);
|
||||
assert.equal(v.timers.size, 0);
|
||||
leave();
|
||||
});
|
||||
|
||||
test('pagination and retry preserve the midnight deadline while their requests remain hung', async () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100, session: 1 };
|
||||
const leave = v.start(selection, { page: 1 });
|
||||
const data = { page: 1, revision: 'old', rows: ['old'], summary: { audit: { missing: 30 } }, serverNow: 1000, expiresAt: 11000 };
|
||||
v.requests[0].resolve({ code: 200, data });
|
||||
await flush();
|
||||
v.advance(3000);
|
||||
leave();
|
||||
const cancelPage = v.start(selection, { page: 2, revision: 'old' });
|
||||
assert.ok([...v.timers.values()].includes(7000));
|
||||
v.requests[1].reject(new Error('timeout'));
|
||||
await flush();
|
||||
v.advance(2000);
|
||||
cancelPage();
|
||||
const cancelRetry = v.start(selection, { page: 2, revision: 'old' });
|
||||
v.tick(5000);
|
||||
assert.equal(v.requests[2].signal.aborted, true);
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: 'expired' }]);
|
||||
v.requests[2].resolve({ code: 200, data });
|
||||
await flush();
|
||||
assert.deepEqual(v.received, [data]);
|
||||
assert.equal(v.timers.size, 0);
|
||||
cancelRetry();
|
||||
});
|
||||
|
||||
for (const status of ['expired', 'unavailable']) test('anomaly ' + status + ' closes the old session without republishing old rows or retrying', async () => {
|
||||
const v = anomalies(), selection = { source: 'system', kind: 'unconfirmed', at: 100, session: 1 };
|
||||
const leave = v.start(selection, { page: 2, revision: 'old' });
|
||||
v.requests[0].resolve({ code: 200, data: { status } });
|
||||
await flush();
|
||||
assert.deepEqual(v.invalidated, [{ selection, reason: status }]);
|
||||
assert.deepEqual(v.received, []);
|
||||
assert.deepEqual(v.summaries, []);
|
||||
assert.equal(v.timers.size, 0);
|
||||
leave();
|
||||
const next = { ...selection, session: 2 }, close = v.start(next, { page: 1 });
|
||||
const data = { page: 1, revision: 'new', rows: [], total: 0, summary: { systemAudit: { actualUnconfirmed: 0 } } };
|
||||
v.requests[1].resolve({ code: 200, data });
|
||||
await flush();
|
||||
assert.deepEqual(v.received, [data]);
|
||||
close();
|
||||
});
|
||||
|
||||
test('an expired response that arrived across midnight cannot be rendered for even one poll', async () => {
|
||||
const v = anomalies(), selection = { source: 'panel', kind: 'missing', at: 100 };
|
||||
const close = v.start(selection, { page: 1 });
|
||||
v.requests[0].resolve({ code: 200, data: { serverNow: 1000, expiresAt: 999, summary: { audit: { missing: 7 } }, rows: ['old'] } });
|
||||
await flush();
|
||||
assert.equal(v.invalidated[0].reason, 'expired');
|
||||
assert.deepEqual(v.received, []);
|
||||
assert.deepEqual(v.summaries, []);
|
||||
close();
|
||||
});
|
||||
|
||||
test('a queued summary update is fenced even when closing and reopening happens before effect cleanup', () => {
|
||||
const file = require.resolve('../../src/pages/dashboard/index.tsx');
|
||||
const source = fs.readFileSync(file, 'utf8'), ast = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
||||
let callback;
|
||||
function visit(node) {
|
||||
if (ts.isVariableDeclaration(node) && node.name.getText(ast) === 'updateAnomalySummary')
|
||||
callback = node.initializer.arguments[0].getText(ast);
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(ast);
|
||||
assert.ok(callback);
|
||||
const old = { session: 1 }, next = { session: 2 }, anomalySession = { current: old }, queued = [];
|
||||
const js = ts.transpile('const update = ' + callback, { target: ts.ScriptTarget.ES2022 });
|
||||
const update = Function('anomalySession', 'setOverview', js + '\nreturn update;')(anomalySession, updater => queued.push(updater));
|
||||
update(old, { audit: { missing: 8 } });
|
||||
anomalySession.current = next;
|
||||
update(old, { audit: { missing: 9 } });
|
||||
assert.equal(queued.length, 1);
|
||||
const fresh = { triggers: { audit: { missing: 0 } } };
|
||||
assert.strictEqual(queued[0](fresh), fresh);
|
||||
});
|
||||
|
||||
test('an old invalidation cannot close a reopened session or clear its freshly queued summary', () => {
|
||||
const file = require.resolve('../../src/pages/dashboard/index.tsx');
|
||||
const source = fs.readFileSync(file, 'utf8'), ast = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
||||
let callback;
|
||||
function visit(node) {
|
||||
if (ts.isVariableDeclaration(node) && node.name.getText(ast) === 'invalidateAnomaly')
|
||||
callback = node.initializer.arguments[0].getText(ast);
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(ast);
|
||||
assert.ok(callback);
|
||||
const old = { session: 1 }, current = { session: 2 };
|
||||
const anomalySession = { current }, anomalyVersion = { current: 2 };
|
||||
const summaries = [], logs = [], messages = [];
|
||||
let closes = 0;
|
||||
const bindings = { anomalySession, anomalyVersion,
|
||||
closeAnomaly() { closes++; anomalySession.current = undefined; },
|
||||
setOverview: update => summaries.push(update), setLogCron: update => logs.push(update),
|
||||
message: { info: text => messages.push(text) }, intl: { get: text => text },
|
||||
};
|
||||
const js = ts.transpile('const invalidate = ' + callback, { target: ts.ScriptTarget.ES2022 });
|
||||
const invalidate = Function(...Object.keys(bindings), js + '\nreturn invalidate;')(...Object.values(bindings));
|
||||
invalidate(old, 'unavailable');
|
||||
assert.equal(closes, 0);
|
||||
assert.equal(summaries.length, 0);
|
||||
invalidate(current, 'expired');
|
||||
assert.equal(closes, 1);
|
||||
assert.equal(logs[0]({ anomalySession: 2 }), null);
|
||||
const next = { session: 3 };
|
||||
anomalySession.current = next;
|
||||
anomalyVersion.current = 3;
|
||||
const fresh = { triggers: { audit: { missing: 0 } } }, freshLog = { anomalySession: 3 };
|
||||
assert.strictEqual(summaries[0](fresh), fresh);
|
||||
assert.strictEqual(logs[0](freshLog), freshLog);
|
||||
assert.equal(messages.length, 1);
|
||||
});
|
||||
Reference in new issue
Block a user