Initial curated EventLens source from 0d80d3b38dfb817285481665b9e71e4b8a0656a0
This commit is contained in:
commit
663ea8c829
85 files changed
+12395
No files matched your search
@@ -0,0 +1,82 @@
|
||||
import type { Analysis, EventItem, Incident, ParseIssue, ParseStats } from "../types";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function isTimestamp(value: unknown): value is string | null {
|
||||
return value === null || (typeof value === "string" && /(?:Z|[+-]\d{2}:\d{2})$/.test(value) && Number.isFinite(Date.parse(value)));
|
||||
}
|
||||
|
||||
function isFiniteNumber(value: unknown): value is number {
|
||||
return typeof value === "number" && Number.isFinite(value);
|
||||
}
|
||||
|
||||
function isNonnegativeInteger(value: unknown): value is number {
|
||||
return isFiniteNumber(value) && Number.isInteger(value) && value >= 0;
|
||||
}
|
||||
|
||||
function isStringArray(value: unknown): value is string[] {
|
||||
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
||||
}
|
||||
|
||||
function isEventItem(value: unknown): value is EventItem {
|
||||
if (!isRecord(value)) return false;
|
||||
if (typeof value.id !== "string" || !isTimestamp(value.timestamp)) return false;
|
||||
if (typeof value.provider !== "string" || !(value.event_id === null || (isNonnegativeInteger(value.event_id) && value.event_id <= 4_294_967_295))) return false;
|
||||
if (typeof value.level !== "string" || typeof value.category !== "string" || typeof value.summary !== "string") return false;
|
||||
if (value.channel !== undefined && typeof value.channel !== "string") return false;
|
||||
if (value.computer !== undefined && typeof value.computer !== "string") return false;
|
||||
if (value.fields !== undefined) {
|
||||
if (!isRecord(value.fields) || !Object.values(value.fields).every((item) => typeof item === "string")) return false;
|
||||
}
|
||||
if (typeof value.timestamp_valid !== "boolean" || !["event", "record", "missing", "invalid"].includes(String(value.timestamp_source))) return false;
|
||||
if (value.timestamp === null && value.timestamp_valid) return false;
|
||||
if (["event", "record"].includes(String(value.timestamp_source)) !== value.timestamp_valid) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isIncident(value: unknown): value is Incident {
|
||||
if (!isRecord(value)) return false;
|
||||
return typeof value.id === "string"
|
||||
&& typeof value.rule_id === "string"
|
||||
&& isTimestamp(value.start)
|
||||
&& isTimestamp(value.end)
|
||||
&& typeof value.title === "string"
|
||||
&& typeof value.category === "string"
|
||||
&& typeof value.severity === "string"
|
||||
&& isStringArray(value.reasons)
|
||||
&& isStringArray(value.evidence_event_ids)
|
||||
&& isNonnegativeInteger(value.original_count)
|
||||
&& value.original_count > 0
|
||||
&& isNonnegativeInteger(value.compressed_count)
|
||||
&& value.compressed_count > 0;
|
||||
}
|
||||
|
||||
function isParseIssue(value: unknown): value is ParseIssue {
|
||||
if (!isRecord(value) || typeof value.code !== "string" || typeof value.message !== "string") return false;
|
||||
if (value.record_id !== undefined && value.record_id !== null && !isNonnegativeInteger(value.record_id)) return false;
|
||||
if (value.skipped_record !== undefined && typeof value.skipped_record !== "boolean") return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isParseStats(value: unknown): value is ParseStats {
|
||||
if (!isRecord(value)) return false;
|
||||
if (typeof value.parser !== "string" || !["complete", "partial"].includes(String(value.status))) return false;
|
||||
for (const key of ["parsed_events", "records_seen", "skipped_records", "issue_count", "duration_ms"] as const) {
|
||||
if (!isNonnegativeInteger(value[key])) return false;
|
||||
}
|
||||
if (typeof value.truncated !== "boolean") return false;
|
||||
if (value.truncation_reason !== undefined && value.truncation_reason !== null && typeof value.truncation_reason !== "string") return false;
|
||||
return Array.isArray(value.issues) && value.issues.every(isParseIssue) && value.issue_count === value.issues.length;
|
||||
}
|
||||
|
||||
export function isAnalysis(value: unknown): value is Analysis {
|
||||
if (!isRecord(value) || typeof value.source !== "string") return false;
|
||||
if (!Array.isArray(value.events) || !value.events.every(isEventItem)) return false;
|
||||
if (!Array.isArray(value.incidents) || !value.incidents.every(isIncident)) return false;
|
||||
const eventIds = new Set(value.events.map((event: EventItem) => event.id));
|
||||
if (eventIds.size !== value.events.length) return false;
|
||||
if (!value.incidents.every((incident: Incident) => incident.evidence_event_ids.every((id) => eventIds.has(id)))) return false;
|
||||
return value.parse === undefined || value.parse === null || (isParseStats(value.parse) && value.parse.parsed_events === value.events.length);
|
||||
}
|
||||
Reference in new issue
Block a user