Initial curated ServiceMap source from 7a0a61232d05ba9097432abef6f7f4102a64d0b8
This commit is contained in:
commit
105251b9c8
96 files changed
+23587
No files matched your search
@@ -0,0 +1,256 @@
|
||||
import { MarkerType, type Edge } from "@xyflow/react";
|
||||
|
||||
import type { HealthStatus, SemanticTopology } from "../api";
|
||||
import type { ServiceNode } from "./ServiceNode";
|
||||
|
||||
export type SemanticView = "domains" | "applications" | "components";
|
||||
export type SemanticFilterStatus = "all" | "unresolved" | HealthStatus;
|
||||
|
||||
/** Projects canonical services and relationships into the active semantic level. */
|
||||
export function semanticTopologyGraph(
|
||||
topology: SemanticTopology,
|
||||
view: SemanticView,
|
||||
scopeId: string | null,
|
||||
incidentOnly: boolean,
|
||||
search: string,
|
||||
statusFilter: SemanticFilterStatus,
|
||||
): { nodes: ServiceNode[]; edges: Edge[] } {
|
||||
const applicationById = new Map(
|
||||
topology.applications.map((item) => [item.id, item]),
|
||||
);
|
||||
const componentById = new Map(
|
||||
topology.components.map((item) => [item.service_id, item]),
|
||||
);
|
||||
const domainById = new Map(topology.domains.map((item) => [item.id, item]));
|
||||
const scopedApplications =
|
||||
view === "applications" && scopeId
|
||||
? topology.applications.filter((item) => item.domain_id === scopeId)
|
||||
: topology.applications;
|
||||
const scopedComponents =
|
||||
view === "components" && scopeId
|
||||
? topology.components.filter((item) => item.application_id === scopeId)
|
||||
: topology.components;
|
||||
const isIncidentVisible = <
|
||||
T extends { root_cause: boolean; affected: boolean },
|
||||
>(
|
||||
item: T,
|
||||
) => !incidentOnly || item.root_cause || item.affected;
|
||||
const nodes: ServiceNode[] =
|
||||
view === "domains"
|
||||
? topology.domains.filter(isIncidentVisible).map((domain) => ({
|
||||
id: domain.id,
|
||||
type: "service",
|
||||
position: { x: 0, y: 0 },
|
||||
selected: domain.root_cause,
|
||||
data: {
|
||||
name: domain.name,
|
||||
status: domain.status,
|
||||
kind: "http",
|
||||
latency: null,
|
||||
dependencyCount: 0,
|
||||
rootCause: domain.root_cause,
|
||||
impacted: domain.affected,
|
||||
group: domain.name,
|
||||
criticality: "medium",
|
||||
flapping: false,
|
||||
category: "domain",
|
||||
memberIds: domain.application_ids.flatMap(
|
||||
(id) => applicationById.get(id)?.component_ids ?? [],
|
||||
),
|
||||
nodeKind: "domain",
|
||||
explanation: `${domain.resolved_application_count}/${domain.application_count} resolved · ${domain.unresolved_application_count} unresolved${domain.incident_count ? ` · ${domain.incident_count} incident` : ""}`,
|
||||
},
|
||||
className: `service-node domain-node status-${domain.status}`,
|
||||
}))
|
||||
: view === "applications"
|
||||
? scopedApplications.filter(isIncidentVisible).map((app) => ({
|
||||
id: app.id,
|
||||
type: "service",
|
||||
position: { x: 0, y: 0 },
|
||||
selected: app.root_cause,
|
||||
data: {
|
||||
name: app.name,
|
||||
status: app.status,
|
||||
kind: "http",
|
||||
latency: null,
|
||||
dependencyCount: app.dependency_application_ids.length,
|
||||
rootCause: app.root_cause,
|
||||
impacted: app.affected,
|
||||
group: domainById.get(app.domain_id)?.name ?? null,
|
||||
criticality: "medium",
|
||||
flapping: false,
|
||||
category: "application",
|
||||
application: app.name,
|
||||
applicationNode: true,
|
||||
memberIds: app.component_ids,
|
||||
nodeKind: "application",
|
||||
roles: app.roles,
|
||||
explanation: app.status_explanation,
|
||||
monitoredComponents: app.resolved_components,
|
||||
unmonitoredComponents: app.unresolved_components,
|
||||
domainId: app.domain_id,
|
||||
},
|
||||
className: `service-node application-node status-${app.status}`,
|
||||
}))
|
||||
: scopedComponents.filter(isIncidentVisible).map((component) => ({
|
||||
id: component.service_id,
|
||||
type: "service",
|
||||
position: { x: 0, y: 0 },
|
||||
data: {
|
||||
name: component.name,
|
||||
status: component.effective_status,
|
||||
kind: component.kind,
|
||||
latency: component.latest_latency_ms,
|
||||
dependencyCount: component.dependency_ids.length,
|
||||
rootCause: component.root_cause,
|
||||
impacted: component.affected,
|
||||
group:
|
||||
applicationById.get(component.application_id)?.name ?? null,
|
||||
criticality: "medium",
|
||||
flapping: false,
|
||||
category: component.role,
|
||||
nodeKind: "component",
|
||||
role: component.role,
|
||||
explanation: component.unknown_reason
|
||||
? `${component.unknown_reason.replaceAll("_", " ")} · ${component.dependent_ids.length} dependents`
|
||||
: `${component.monitoring_state.replace("_", " ")} · ${component.dependent_ids.length} dependents`,
|
||||
},
|
||||
className: `service-node component-node status-${component.effective_status}`,
|
||||
}));
|
||||
const normalizedSearch = search.trim().toLocaleLowerCase();
|
||||
const visibleNodes = nodes.filter((node) => {
|
||||
const descendants = node.data.memberIds ?? [node.id];
|
||||
const descendantMatches = descendants.some((serviceId) =>
|
||||
componentById
|
||||
.get(serviceId)
|
||||
?.name.toLocaleLowerCase()
|
||||
.includes(normalizedSearch),
|
||||
);
|
||||
return (
|
||||
(!normalizedSearch ||
|
||||
`${node.data.name} ${node.data.explanation ?? ""}`
|
||||
.toLocaleLowerCase()
|
||||
.includes(normalizedSearch) ||
|
||||
descendantMatches) &&
|
||||
(statusFilter === "all" ||
|
||||
(statusFilter === "unresolved"
|
||||
? ["unknown", "unmonitored"].includes(node.data.status)
|
||||
: node.data.status === statusFilter))
|
||||
);
|
||||
});
|
||||
const nodeIds = new Set(visibleNodes.map((node) => node.id));
|
||||
const nodeIdFor = (serviceId: string): string | null => {
|
||||
const component = componentById.get(serviceId);
|
||||
if (!component) return null;
|
||||
if (view === "components") return component.service_id;
|
||||
if (view === "applications") return component.application_id;
|
||||
return component.domain_id;
|
||||
};
|
||||
const canonicalPairs = topology.components.flatMap((component) =>
|
||||
component.dependency_ids.map((target_service_id) => ({
|
||||
source_service_id: component.service_id,
|
||||
target_service_id,
|
||||
active: true,
|
||||
relation_type: "depends_on",
|
||||
})),
|
||||
);
|
||||
const relationshipPairs = topology.relations
|
||||
.filter((relation) => relation.active && relation.target_service_id)
|
||||
.map((relation) => ({
|
||||
source_service_id: relation.source_service_id,
|
||||
target_service_id: relation.target_service_id as string,
|
||||
active: relation.active,
|
||||
relation_type: relation.relation_type,
|
||||
}));
|
||||
const uniquePairs = new Map<
|
||||
string,
|
||||
{
|
||||
source_service_id: string;
|
||||
target_service_id: string;
|
||||
active: boolean;
|
||||
relation_type: string;
|
||||
}
|
||||
>();
|
||||
for (const relation of [...canonicalPairs, ...relationshipPairs]) {
|
||||
const key = `${relation.source_service_id}->${relation.target_service_id}`;
|
||||
if (!uniquePairs.has(key)) uniquePairs.set(key, relation);
|
||||
}
|
||||
const statusRank: Record<HealthStatus, number> = {
|
||||
down: 5,
|
||||
degraded: 4,
|
||||
recovering: 3,
|
||||
maintenance: 2,
|
||||
unknown: 1,
|
||||
unmonitored: 0,
|
||||
healthy: 0,
|
||||
};
|
||||
const projected = new Map<
|
||||
string,
|
||||
{
|
||||
id: string;
|
||||
source: string;
|
||||
target: string;
|
||||
status: HealthStatus;
|
||||
weight: number;
|
||||
relationType: string;
|
||||
}
|
||||
>();
|
||||
for (const relation of uniquePairs.values()) {
|
||||
if (!relation.active) continue;
|
||||
// A relation is stored dependent -> dependency; render dependency -> dependent.
|
||||
const source = nodeIdFor(relation.target_service_id);
|
||||
const target = nodeIdFor(relation.source_service_id);
|
||||
if (
|
||||
!source ||
|
||||
!target ||
|
||||
source === target ||
|
||||
!nodeIds.has(source) ||
|
||||
!nodeIds.has(target)
|
||||
)
|
||||
continue;
|
||||
const id = `${source}->${target}`;
|
||||
const status =
|
||||
componentById.get(relation.source_service_id)?.effective_status ??
|
||||
"unknown";
|
||||
const existing = projected.get(id);
|
||||
if (existing) {
|
||||
existing.weight += 1;
|
||||
if (statusRank[status] > statusRank[existing.status])
|
||||
existing.status = status;
|
||||
} else {
|
||||
projected.set(id, {
|
||||
id,
|
||||
source,
|
||||
target,
|
||||
status,
|
||||
weight: 1,
|
||||
relationType: relation.relation_type,
|
||||
});
|
||||
}
|
||||
}
|
||||
const projectedEdges = [...projected.values()].sort(
|
||||
(left, right) =>
|
||||
right.weight - left.weight || left.id.localeCompare(right.id),
|
||||
);
|
||||
const edgeLimit =
|
||||
view === "domains" ? 12 : view === "applications" ? 28 : Infinity;
|
||||
const highSignal =
|
||||
view === "components"
|
||||
? projectedEdges
|
||||
: projectedEdges.filter((edge) => edge.weight >= 2);
|
||||
const visibleEdges = (highSignal.length ? highSignal : projectedEdges).slice(
|
||||
0,
|
||||
edgeLimit,
|
||||
);
|
||||
return {
|
||||
nodes: visibleNodes,
|
||||
edges: visibleEdges.map((edge) => ({
|
||||
id: edge.id,
|
||||
source: edge.source,
|
||||
target: edge.target,
|
||||
markerEnd: { type: MarkerType.ArrowClosed },
|
||||
animated: edge.status === "down" || edge.status === "degraded",
|
||||
className: `edge-${edge.status} relation-${edge.relationType}`,
|
||||
})),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user