Publish EventLens v1.1.0 (#1)

This commit was merged in pull request #1.
This commit is contained in:
Jens committed 2026-11-20 12:20:18 +00:00
1 parent c0ade21029
commit 87432f3283
63 files changed
+7381 -1420

No files matched your search

+98 -22
View File
@@ -1,11 +1,14 @@
import type { Analysis, EventItem, Incident, ParseIssue, ParseStats } from "../types";
import type { Analysis, Campaign, EventItem, Incident, ParseIssue, ParseStats, SourceInfo } 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)));
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 {
@@ -23,14 +26,30 @@ function isStringArray(value: unknown): value is 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 (
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.outcome !== undefined &&
value.outcome !== null &&
value.outcome !== "success" &&
value.outcome !== "failure"
)
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 (
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;
@@ -38,19 +57,54 @@ function isEventItem(value: unknown): value is EventItem {
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;
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 &&
(value.campaign_id === undefined || value.campaign_id === null || typeof value.campaign_id === "string")
);
}
function isCampaign(value: unknown): value is Campaign {
if (!isRecord(value)) return false;
return (
typeof value.id === "string" &&
typeof value.rule_id === "string" &&
typeof value.title === "string" &&
typeof value.category === "string" &&
typeof value.severity === "string" &&
typeof value.start === "string" &&
isTimestamp(value.start) &&
typeof value.end === "string" &&
isTimestamp(value.end) &&
isStringArray(value.incident_ids) &&
value.incident_ids.length >= 2 &&
isNonnegativeInteger(value.event_count) &&
isStringArray(value.reasons)
);
}
function isSourceInfo(value: unknown): value is SourceInfo {
return (
isRecord(value) &&
typeof value.sha256 === "string" &&
/^[0-9a-f]{64}$/.test(value.sha256) &&
isNonnegativeInteger(value.size_bytes)
);
}
function isIssueCounts(value: unknown): value is Record<string, number> {
return isRecord(value) && Object.values(value).every(isNonnegativeInteger);
}
function isParseIssue(value: unknown): value is ParseIssue {
@@ -67,8 +121,18 @@ function isParseStats(value: unknown): value is ParseStats {
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;
if (
value.truncation_reason !== undefined &&
value.truncation_reason !== null &&
typeof value.truncation_reason !== "string"
)
return false;
if (value.issues_truncated !== undefined && typeof value.issues_truncated !== "boolean") return false;
if (value.issue_counts !== undefined && !isIssueCounts(value.issue_counts)) return false;
if (!Array.isArray(value.issues) || !value.issues.every(isParseIssue)) return false;
// The API caps the reported issue list; issue_count keeps the true total.
const issueCount = value.issue_count as number;
return value.issues_truncated === true ? issueCount >= value.issues.length : issueCount === value.issues.length;
}
export function isAnalysis(value: unknown): value is Analysis {
@@ -77,6 +141,18 @@ export function isAnalysis(value: unknown): value is Analysis {
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);
if (!value.incidents.every((incident: Incident) => incident.evidence_event_ids.every((id) => eventIds.has(id))))
return false;
if (value.campaigns !== undefined) {
if (!Array.isArray(value.campaigns) || !value.campaigns.every(isCampaign)) return false;
const incidentIds = new Set(value.incidents.map((incident: Incident) => incident.id));
if (!value.campaigns.every((campaign: Campaign) => campaign.incident_ids.every((id) => incidentIds.has(id))))
return false;
}
if (value.source_info !== undefined && value.source_info !== null && !isSourceInfo(value.source_info)) return false;
return (
value.parse === undefined ||
value.parse === null ||
(isParseStats(value.parse) && value.parse.parsed_events === value.events.length)
);
}