127 lines
5.5 KiB
JavaScript
127 lines
5.5 KiB
JavaScript
const M3_TRUSTED_ROUTE = Object.freeze({
|
|
fromResourceId: "res_boxsimu_1",
|
|
fromPort: "DO1",
|
|
patchPanelServiceId: "hwlab-patch-panel",
|
|
toResourceId: "res_boxsimu_2",
|
|
toPort: "DI1"
|
|
});
|
|
|
|
const LIVE_M3_ID_FIELDS = Object.freeze(["operationId", "traceId", "auditId", "evidenceId"]);
|
|
const LIVE_M3_PASS_STATUSES = Object.freeze(["pass", "passed", "verified", "succeeded", "trusted", "completed"]);
|
|
const NON_LIVE_ID_VALUES = Object.freeze(["", "n/a", "none", "null", "undefined", "not_observed", "not-observed"]);
|
|
|
|
export function wiringPresentationFromSummary(summary = null, { liveM3Evidence = null } = {}) {
|
|
const m3Status = summary?.contractVersion === "m3-status-v1" ? summary : null;
|
|
const evidence = m3Status ? trustedM3StatusEvidenceFrom(m3Status) : liveM3Evidence;
|
|
const m3Live = hasTrustedM3LiveEvidence(evidence);
|
|
const connectionActive = m3Status?.patchPanel?.connectionActive === true;
|
|
const blockedTrust = m3Status?.trust?.blocker;
|
|
const durableBlocked = m3Status && (m3Status.trust?.durableStatus !== "green" || blockedTrust != null);
|
|
const traceEvidence = m3Live
|
|
? [evidence.operationId, evidence.traceId, evidence.auditId, evidence.evidenceId].join(" / ")
|
|
: m3Status
|
|
? [m3Status.trust?.operationId, m3Status.trust?.traceId, m3Status.trust?.auditId, m3Status.trust?.evidenceId].filter(Boolean).join(" / ") || `durable blocked: ${blockedTrust ?? "unverified"}`
|
|
: "等待可信记录";
|
|
const stateLabel = m3Live ? "实况已验证" : connectionActive && durableBlocked ? "接线实况 / 持久化受阻" : "待可信记录";
|
|
const sourceLabel = m3Live ? "/v1/m3/status 聚合 / 实况可信记录" : m3Status ? "/v1/m3/status 聚合" : "来源拓扑";
|
|
|
|
return {
|
|
m3Live,
|
|
liveM3Evidence: evidence,
|
|
connectionActive,
|
|
stateLabel,
|
|
sourceLabel,
|
|
traceEvidence,
|
|
rowSourceKind: m3Live ? "DEV-LIVE" : "SOURCE",
|
|
badgeTone: m3Live ? "dev-live" : "blocked"
|
|
};
|
|
}
|
|
|
|
export function trustedM3StatusEvidenceFrom(status) {
|
|
if (status?.contractVersion !== "m3-status-v1") return null;
|
|
const trust = status.trust ?? {};
|
|
const sourceKind = String(status.sourceKind ?? "").toUpperCase();
|
|
if (
|
|
status.status !== "live" ||
|
|
sourceKind !== "DEV-LIVE" ||
|
|
status.patchPanel?.connectionActive !== true ||
|
|
status.patchPanel?.serviceId !== M3_TRUSTED_ROUTE.patchPanelServiceId ||
|
|
trust.durableStatus !== "green" ||
|
|
trust.blocker != null ||
|
|
!LIVE_M3_ID_FIELDS.every((field) => isLiveIdentifier(trust[field]))
|
|
) {
|
|
return null;
|
|
}
|
|
return {
|
|
status: "verified",
|
|
sourceKind: "DEV-LIVE",
|
|
live: true,
|
|
operationId: trust.operationId,
|
|
traceId: trust.traceId,
|
|
auditId: trust.auditId,
|
|
evidenceId: trust.evidenceId,
|
|
patchPanelLiveReport: {
|
|
serviceId: M3_TRUSTED_ROUTE.patchPanelServiceId,
|
|
sourceKind: "DEV-LIVE",
|
|
live: true,
|
|
reportId: trust.evidenceId,
|
|
activeConnections: [
|
|
{
|
|
fromResourceId: M3_TRUSTED_ROUTE.fromResourceId,
|
|
fromPort: M3_TRUSTED_ROUTE.fromPort,
|
|
toResourceId: M3_TRUSTED_ROUTE.toResourceId,
|
|
toPort: M3_TRUSTED_ROUTE.toPort,
|
|
patchPanelServiceId: M3_TRUSTED_ROUTE.patchPanelServiceId
|
|
}
|
|
]
|
|
}
|
|
};
|
|
}
|
|
|
|
function hasTrustedM3LiveEvidence(candidate) {
|
|
if (!candidate || typeof candidate !== "object") return false;
|
|
if (candidate.dryRun === true || candidate.fixture === true || candidate.sourceKind === "SOURCE" || candidate.sourceKind === "DRY-RUN") return false;
|
|
if (!hasLiveSourceKind(candidate)) return false;
|
|
if (!LIVE_M3_PASS_STATUSES.includes(String(candidate.status ?? candidate.outcome ?? "verified").toLowerCase())) return false;
|
|
if (!LIVE_M3_ID_FIELDS.every((field) => isLiveIdentifier(candidate[field] ?? candidate.operation?.[field] ?? candidate.identifiers?.[field]))) return false;
|
|
const patchPanelLiveReport = candidate.patchPanelLiveReport ?? candidate.patchPanelReport;
|
|
return hasTrustedPatchPanelLiveReport(patchPanelLiveReport);
|
|
}
|
|
|
|
function hasTrustedPatchPanelLiveReport(report) {
|
|
if (!report || typeof report !== "object") return false;
|
|
if (report.dryRun === true || report.fixture === true || report.sourceKind === "SOURCE" || report.sourceKind === "DRY-RUN") return false;
|
|
if (!hasLiveSourceKind(report)) return false;
|
|
if (report.serviceId !== M3_TRUSTED_ROUTE.patchPanelServiceId) return false;
|
|
if (!isLiveIdentifier(report.reportId ?? report.patchPanelStatusId ?? report.statusId ?? report.evidenceId)) return false;
|
|
return (report.activeConnections ?? report.connections ?? []).some((link) => trustedM3Link([link]));
|
|
}
|
|
|
|
function trustedM3Link(connections) {
|
|
return (connections ?? []).find(
|
|
(link) =>
|
|
String(link.fromResourceId ?? "") === M3_TRUSTED_ROUTE.fromResourceId &&
|
|
normalizePort(link.fromPort) === M3_TRUSTED_ROUTE.fromPort &&
|
|
String(link.toResourceId ?? "") === M3_TRUSTED_ROUTE.toResourceId &&
|
|
normalizePort(link.toPort) === M3_TRUSTED_ROUTE.toPort &&
|
|
connectionPatchPanelServiceId(link) === M3_TRUSTED_ROUTE.patchPanelServiceId
|
|
);
|
|
}
|
|
|
|
function connectionPatchPanelServiceId(link) {
|
|
return link.patchPanelServiceId ?? link.patchPanel?.serviceId ?? link.serviceId ?? M3_TRUSTED_ROUTE.patchPanelServiceId;
|
|
}
|
|
|
|
function hasLiveSourceKind(value) {
|
|
return value.live === true || value.sourceKind === "DEV-LIVE" || value.evidenceLevel === "DEV-LIVE";
|
|
}
|
|
|
|
function isLiveIdentifier(value) {
|
|
const text = String(value ?? "").trim();
|
|
return text !== "" && !NON_LIVE_ID_VALUES.includes(text.toLowerCase());
|
|
}
|
|
|
|
function normalizePort(port) {
|
|
return String(port ?? "").trim().toUpperCase();
|
|
}
|