import { getCollection, type CollectionEntry } from "astro:content"; import { TOPOLOGY_PAGE_PROFILES } from "../config/topologyPageProfiles"; import { createPresentationTopology } from "../framework/topologyLayouts.ts"; import { createHexGrid, getGridBounds } from "./hex"; interface SystemArchitectureNode { id: string; label: string; subtitle?: string; q: number; r: number; variant: string; size: string; active: boolean; selected: boolean; disabled?: boolean; href?: string; } interface SystemArchitectureEdge { from: string; to: string; kind: string; active: boolean; muted: boolean; } type PagesEntry = CollectionEntry<"pages">; interface SystemArchitectureRecord { id: string; eyebrow: string; title: string; subtitle: string; summary: string; details: string[]; meta: string[]; group: string; kind: "page" | "anchor"; links: Array<{ id: string; label: string; subtitle: string }>; } interface RecordSeed { id: string; eyebrow: string; title: string; subtitle: string; summary: string; details: string[]; meta: string[]; group: string; kind: SystemArchitectureRecord["kind"]; relatedIds: string[]; } function toList(value: unknown) { return Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : []; } function pageRecord(entry: PagesEntry): RecordSeed { return { id: entry.data.id, eyebrow: entry.data.eyebrow ?? "Architecture", title: entry.data.title, subtitle: entry.data.subtitle ?? entry.data.summary ?? "Architecture overview", summary: entry.data.summary, details: toList(entry.data.details), meta: toList(entry.data.meta), group: "Root", kind: "page", relatedIds: ["edge", "delivery", "application", "data", "infrastructure", "external"], }; } function layerRecord( id: string, title: string, subtitle: string, summary: string, details: string[], relatedIds: string[], ): RecordSeed { return { id, eyebrow: "Layer", title, subtitle, summary, details, meta: ["Architecture", "Layer"], group: "Layers", kind: "anchor", relatedIds, }; } function buildLinks(seed: RecordSeed, index: Map) { return seed.relatedIds.map((id) => { const target = index.get(id); return { id: target?.id ?? id, label: target?.title ?? id, subtitle: target?.subtitle ?? "", }; }); } export async function createSystemArchitectureSceneTopology() { const architectureProfile = TOPOLOGY_PAGE_PROFILES.systemArchitecture; const architectureRadius = architectureProfile.scene.radius; const architectureCells = createHexGrid({ radius: architectureRadius, columns: architectureProfile.scene.columns, rows: architectureProfile.scene.rows, originQ: architectureProfile.scene.originQ, originR: architectureProfile.scene.originR, }); const architectureBounds = getGridBounds(architectureCells, architectureRadius); const entries = await getCollection("pages"); const entry = entries.find((candidate) => candidate.id === "system-architecture") ?? ({ id: "system-architecture", data: { id: "system-architecture", type: "page", title: "System Architecture", subtitle: "Layered operational topology", summary: "A reusable map of edge, delivery, application, data, infrastructure, and external systems.", status: "published", tags: ["architecture", "infrastructure", "delivery", "systems", "topology"], connections: ["page:overview", "page:operational-dashboard", "page:lab"], details: [ "Edge frames ingress, public surfaces, CDN boundaries, and other entry points.", "Delivery handles load balancing, routing, and traffic distribution.", "Application contains services, orchestration, and business logic.", "Data stores state in databases, replicas, and caches.", "Infrastructure provides runtime, compute, network, and platform services.", "External keeps third-party APIs, vendors, and upstream dependencies visible.", ], timelineMode: "career", timelineScale: "years", timelineEntries: [], meta: ["Architecture", "Preset", "Template"], q: -6, r: 1, variant: "primary", size: "lg", href: "/system-architecture", eyebrow: "Architecture", language: "en", }, body: "", } as PagesEntry); const rootNode: SystemArchitectureNode = { id: entry.data.id, label: entry.data.title, subtitle: entry.data.subtitle ?? entry.data.summary, q: 0, r: 0, variant: "root", size: "root", active: true, selected: true, }; const anchorNodes: SystemArchitectureNode[] = [ { id: "edge", label: "Edge", subtitle: "Entry surfaces", q: -3, r: 0, variant: "anchor", size: "lg", active: false, selected: false, }, { id: "delivery", label: "Delivery", subtitle: "Routing and distribution", q: 0, r: -3, variant: "anchor", size: "lg", active: false, selected: false, }, { id: "application", label: "Application", subtitle: "Business logic and services", q: 3, r: -3, variant: "anchor", size: "lg", active: false, selected: false, }, { id: "data", label: "Data", subtitle: "Storage and state", q: 3, r: 0, variant: "anchor", size: "lg", active: false, selected: false, }, { id: "infrastructure", label: "Infrastructure", subtitle: "Runtime and platform", q: 0, r: 3, variant: "anchor", size: "lg", active: false, selected: false, }, { id: "external", label: "External", subtitle: "Third-party and upstream", q: -3, r: 3, variant: "anchor", size: "md", active: false, selected: false, }, ]; const architectureLayout = createPresentationTopology({ id: entry.data.id, title: entry.data.title, subtitle: entry.data.subtitle ?? entry.data.summary, summary: entry.data.summary, status: entry.data.status ?? "ready", route: entry.data.href ?? "/system-architecture", role: "architecture", sceneLayout: architectureProfile.scene.layout, topologyLayout: architectureProfile.scene.topologyLayout, anchors: anchorNodes.map((node) => ({ id: node.id, label: node.label, subtitle: node.subtitle, children: [], })), }); const architectureLayoutNodes = architectureLayout.nodes; rootNode.q = architectureLayoutNodes[0]?.q ?? rootNode.q; rootNode.r = architectureLayoutNodes[0]?.r ?? rootNode.r; anchorNodes.forEach((node, index) => { const position = architectureLayoutNodes[1 + index]; node.q = position?.q ?? node.q; node.r = position?.r ?? node.r; }); const recordSeeds: RecordSeed[] = [ pageRecord(entry), layerRecord( "edge", "Edge", "Entry surfaces", "Public ingress and boundary handling.", [ "Front doors, CDNs, public routes, and other ingress points.", "The edge absorbs traffic before it reaches the internal layers.", ], ["system-architecture", "delivery"], ), layerRecord( "delivery", "Delivery", "Routing and distribution", "Traffic mediation and request distribution.", [ "Load balancers, API gateways, routing policies, and traffic mediation.", "The delivery layer decides where requests go next.", ], ["system-architecture", "edge", "application"], ), layerRecord( "application", "Application", "Business logic and services", "Service orchestration and domain behavior.", [ "Business services, application servers, and workflow orchestration.", "This layer holds the logic that turns requests into outcomes.", ], ["system-architecture", "delivery", "data"], ), layerRecord( "data", "Data", "Storage and state", "Stateful services and persistence boundaries.", [ "Primary databases, replicas, caches, and other durable stores.", "This layer preserves the state that the rest of the system depends on.", ], ["system-architecture", "application", "infrastructure"], ), layerRecord( "infrastructure", "Infrastructure", "Runtime and platform", "Compute, network, and platform runtime.", [ "Compute hosts, virtualization, network segments, and platform services.", "This layer provides the runtime substrate for everything above it.", ], ["system-architecture", "data", "external"], ), layerRecord( "external", "External", "Third-party and upstream", "External providers and integration dependencies.", [ "Identity providers, SaaS integrations, vendors, and upstream APIs.", "This layer keeps the boundaries with outside systems visible.", ], ["system-architecture", "infrastructure"], ), ]; const recordIndex = new Map(recordSeeds.map((seed) => [seed.id, seed])); const records: SystemArchitectureRecord[] = recordSeeds.map((seed) => ({ ...seed, links: buildLinks(seed, recordIndex), })); const recordsById = Object.fromEntries(records.map((record) => [record.id, record])) as Record; const topologyNodes: SystemArchitectureNode[] = [rootNode, ...anchorNodes]; const topologyEdges: SystemArchitectureEdge[] = [ ...anchorNodes.map((node) => ({ from: rootNode.id, to: node.id, kind: "contains", active: true, muted: false, })), ]; const selectorGroups = [ { label: "Root", options: [{ id: rootNode.id, label: rootNode.label, subtitle: rootNode.subtitle ?? "" }] }, { label: "Layers", options: anchorNodes.map((node) => ({ id: node.id, label: node.label, subtitle: node.subtitle ?? "" })) }, ]; const selectedId = rootNode.id; const systemArchitectureJson = JSON.stringify({ nodes: topologyNodes.map((node) => ({ id: node.id, q: node.q, r: node.r })), records, relationships: topologyEdges, radius: architectureRadius, bounds: { minX: architectureBounds.minX, maxX: architectureBounds.maxX, minY: architectureBounds.minY, maxY: architectureBounds.maxY, width: architectureBounds.width, height: architectureBounds.height, }, camera: architectureProfile.camera, cameraPersistence: architectureProfile.camera.persistence, cameraSafeAreaRatio: architectureProfile.camera.safeAreaRatio, cameraZoomStep: architectureProfile.camera.zoomStep, cameraTransitionDuration: architectureProfile.camera.transitionDuration, selectedId, }); return { architectureProfile, architectureRadius, architectureCells, architectureBounds, nodes: topologyNodes, edges: topologyEdges, records, recordsById, selectedId, selectedRecord: recordsById[selectedId] ?? records[0], systemArchitectureJson, selectorGroups, rootNode, anchorNodes, }; }