feat: add src/correlate.mjs

This commit is contained in:
2026-09-25 11:57:05 +08:00
parent 53a61bf8db
commit 7602ec7af4

19
src/correlate.mjs Normal file
View File

@@ -0,0 +1,19 @@
import { normalizeEvent } from './model.mjs';
import { advance } from './state.mjs';
export function correlate(rawEvents) {
const rejected = [], valid = [];
for (const [index, raw] of rawEvents.entries()) {
const item = normalizeEvent(raw, index);
if (item.error) rejected.push(item);
else valid.push(item);
}
valid.sort((a, b) => a.time - b.time || a.index - b.index);
const groups = new Map();
for (const event of valid) {
const key = `${event.device}/${event.alarmId}`;
const initial = groups.get(key) ?? { open: false, firstAlarm: null, acknowledged: false, anomalies: [] };
groups.set(key, advance(initial, event));
}
return { groups, rejected };
}