@diagrammo/dgmo 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/skills/dgmo-chart/SKILL.md +28 -0
- package/.claude/skills/dgmo-generate/SKILL.md +1 -0
- package/.claude/skills/dgmo-sequence/SKILL.md +24 -1
- package/.cursorrules +27 -2
- package/.github/copilot-instructions.md +36 -3
- package/.windsurfrules +27 -2
- package/README.md +12 -3
- package/dist/cli.cjs +197 -154
- package/dist/index.cjs +8647 -3447
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +503 -58
- package/dist/index.d.ts +503 -58
- package/dist/index.js +8379 -3200
- package/dist/index.js.map +1 -1
- package/docs/ai-integration.md +1 -1
- package/docs/language-reference.md +336 -17
- package/docs/migration-sequence-color-to-tags.md +98 -0
- package/package.json +1 -1
- package/src/c4/renderer.ts +1 -20
- package/src/class/renderer.ts +1 -11
- package/src/cli.ts +40 -0
- package/src/d3.ts +92 -2
- package/src/dgmo-router.ts +11 -0
- package/src/echarts.ts +74 -8
- package/src/er/parser.ts +29 -3
- package/src/er/renderer.ts +1 -15
- package/src/graph/flowchart-parser.ts +7 -30
- package/src/graph/flowchart-renderer.ts +62 -69
- package/src/graph/layout.ts +5 -0
- package/src/graph/state-parser.ts +388 -0
- package/src/graph/state-renderer.ts +496 -0
- package/src/graph/types.ts +4 -2
- package/src/index.ts +42 -1
- package/src/infra/compute.ts +1113 -0
- package/src/infra/layout.ts +578 -0
- package/src/infra/parser.ts +559 -0
- package/src/infra/renderer.ts +1553 -0
- package/src/infra/roles.ts +60 -0
- package/src/infra/serialize.ts +67 -0
- package/src/infra/types.ts +221 -0
- package/src/infra/validation.ts +192 -0
- package/src/initiative-status/layout.ts +56 -61
- package/src/initiative-status/renderer.ts +13 -13
- package/src/kanban/renderer.ts +1 -24
- package/src/org/layout.ts +28 -37
- package/src/org/parser.ts +16 -1
- package/src/org/renderer.ts +159 -121
- package/src/org/resolver.ts +90 -23
- package/src/palettes/color-utils.ts +30 -0
- package/src/render.ts +2 -0
- package/src/sequence/parser.ts +202 -42
- package/src/sequence/renderer.ts +576 -113
- package/src/sequence/tag-resolution.ts +163 -0
- package/src/sharing.ts +8 -0
- package/src/sitemap/collapse.ts +187 -0
- package/src/sitemap/layout.ts +738 -0
- package/src/sitemap/parser.ts +489 -0
- package/src/sitemap/renderer.ts +774 -0
- package/src/sitemap/types.ts +42 -0
- package/src/utils/tag-groups.ts +119 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Infra Chart Role Inference
|
|
3
|
+
// ============================================================
|
|
4
|
+
//
|
|
5
|
+
// Infers component roles from declared behavior properties.
|
|
6
|
+
// Each role maps to a specific color for badge rendering.
|
|
7
|
+
|
|
8
|
+
import type { InfraProperty } from './types';
|
|
9
|
+
|
|
10
|
+
export interface InfraRole {
|
|
11
|
+
name: string;
|
|
12
|
+
color: string; // hex color for badge
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** All recognized roles with their trigger keys. */
|
|
16
|
+
const ROLE_RULES: { keys: string[]; role: InfraRole }[] = [
|
|
17
|
+
{ keys: ['cache-hit'], role: { name: 'Cache', color: '#22c55e' } },
|
|
18
|
+
{ keys: ['firewall-block'], role: { name: 'Firewall', color: '#ef4444' } },
|
|
19
|
+
{ keys: ['ratelimit-rps'], role: { name: 'Rate Limiter', color: '#eab308' } },
|
|
20
|
+
{ keys: ['max-rps'], role: { name: 'Service', color: '#3b82f6' } },
|
|
21
|
+
{ keys: ['cb-error-threshold', 'cb-latency-threshold-ms'], role: { name: 'Circuit Breaker', color: '#a855f7' } },
|
|
22
|
+
{ keys: ['concurrency'], role: { name: 'Serverless', color: '#06b6d4' } },
|
|
23
|
+
{ keys: ['buffer'], role: { name: 'Queue', color: '#8b5cf6' } },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Infer roles from a component's properties.
|
|
28
|
+
* A component can have multiple roles (e.g., Cache + Rate Limiter).
|
|
29
|
+
*/
|
|
30
|
+
export function inferRoles(properties: InfraProperty[]): InfraRole[] {
|
|
31
|
+
const propKeys = new Set(properties.map((p) => p.key));
|
|
32
|
+
const roles: InfraRole[] = [];
|
|
33
|
+
|
|
34
|
+
for (const rule of ROLE_RULES) {
|
|
35
|
+
if (rule.keys.some((k) => propKeys.has(k))) {
|
|
36
|
+
roles.push(rule.role);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return roles;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Collect all unique roles present in the diagram (for legend).
|
|
45
|
+
*/
|
|
46
|
+
export function collectDiagramRoles(allProperties: InfraProperty[][]): InfraRole[] {
|
|
47
|
+
const seen = new Set<string>();
|
|
48
|
+
const roles: InfraRole[] = [];
|
|
49
|
+
|
|
50
|
+
for (const props of allProperties) {
|
|
51
|
+
for (const role of inferRoles(props)) {
|
|
52
|
+
if (!seen.has(role.name)) {
|
|
53
|
+
seen.add(role.name);
|
|
54
|
+
roles.push(role);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return roles;
|
|
60
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Infra Scenario Serializer
|
|
3
|
+
// ============================================================
|
|
4
|
+
//
|
|
5
|
+
// Converts interactive overrides into a `scenario:` DSL block.
|
|
6
|
+
// Only includes properties that differ from the base diagram.
|
|
7
|
+
|
|
8
|
+
import type { ParsedInfra, InfraComputeParams } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Serialize interactive overrides as a DSL `scenario:` block.
|
|
12
|
+
* Returns an empty string if nothing differs from the base diagram.
|
|
13
|
+
*/
|
|
14
|
+
export function serializeScenario(name: string, parsed: ParsedInfra, overrides: InfraComputeParams): string {
|
|
15
|
+
const lines: string[] = [];
|
|
16
|
+
|
|
17
|
+
// Edge RPS override
|
|
18
|
+
const edgeNode = parsed.nodes.find((n) => n.isEdge);
|
|
19
|
+
if (edgeNode && overrides.rps != null) {
|
|
20
|
+
const baseRps = edgeNode.properties.find((p) => p.key === 'rps');
|
|
21
|
+
const baseVal = baseRps ? (typeof baseRps.value === 'number' ? baseRps.value : parseFloat(String(baseRps.value)) || 0) : 0;
|
|
22
|
+
if (overrides.rps !== baseVal) {
|
|
23
|
+
lines.push(` ${edgeNode.id}`);
|
|
24
|
+
lines.push(` rps: ${overrides.rps}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Instance overrides and property overrides per node
|
|
29
|
+
const instanceOv = overrides.instanceOverrides ?? {};
|
|
30
|
+
const propOv = overrides.propertyOverrides ?? {};
|
|
31
|
+
|
|
32
|
+
for (const node of parsed.nodes) {
|
|
33
|
+
if (node.isEdge) continue;
|
|
34
|
+
|
|
35
|
+
const nodeLines: string[] = [];
|
|
36
|
+
|
|
37
|
+
// Instance override
|
|
38
|
+
if (instanceOv[node.id] != null) {
|
|
39
|
+
const baseProp = node.properties.find((p) => p.key === 'instances');
|
|
40
|
+
const baseVal = baseProp ? (typeof baseProp.value === 'number' ? baseProp.value : parseFloat(String(baseProp.value)) || 1) : 1;
|
|
41
|
+
if (instanceOv[node.id] !== baseVal) {
|
|
42
|
+
nodeLines.push(` instances: ${instanceOv[node.id]}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Property overrides
|
|
47
|
+
const nodePropOv = propOv[node.id];
|
|
48
|
+
if (nodePropOv) {
|
|
49
|
+
for (const [key, val] of Object.entries(nodePropOv)) {
|
|
50
|
+
const baseProp = node.properties.find((p) => p.key === key);
|
|
51
|
+
const baseVal = baseProp ? (typeof baseProp.value === 'number' ? baseProp.value : parseFloat(String(baseProp.value)) || 0) : 0;
|
|
52
|
+
if (val !== baseVal) {
|
|
53
|
+
nodeLines.push(` ${key}: ${val}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (nodeLines.length > 0) {
|
|
59
|
+
lines.push(` ${node.id}`);
|
|
60
|
+
lines.push(...nodeLines);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (lines.length === 0) return '';
|
|
65
|
+
|
|
66
|
+
return `scenario: ${name}\n${lines.join('\n')}\n`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Infra Chart Types
|
|
3
|
+
// ============================================================
|
|
4
|
+
|
|
5
|
+
import type { DgmoError } from '../diagnostics';
|
|
6
|
+
|
|
7
|
+
/** Namespaced behavior property keys recognized by the parser. */
|
|
8
|
+
export type InfraBehaviorKey =
|
|
9
|
+
| 'cache-hit'
|
|
10
|
+
| 'firewall-block'
|
|
11
|
+
| 'ratelimit-rps'
|
|
12
|
+
| 'latency-ms'
|
|
13
|
+
| 'uptime'
|
|
14
|
+
| 'instances'
|
|
15
|
+
| 'max-rps'
|
|
16
|
+
| 'cb-error-threshold'
|
|
17
|
+
| 'cb-latency-threshold-ms'
|
|
18
|
+
| 'concurrency'
|
|
19
|
+
| 'duration-ms'
|
|
20
|
+
| 'cold-start-ms'
|
|
21
|
+
| 'buffer'
|
|
22
|
+
| 'drain-rate'
|
|
23
|
+
| 'retention-hours'
|
|
24
|
+
| 'partitions';
|
|
25
|
+
|
|
26
|
+
/** All recognized property keys (behavior + structural). */
|
|
27
|
+
export const INFRA_BEHAVIOR_KEYS = new Set<string>([
|
|
28
|
+
'cache-hit',
|
|
29
|
+
'firewall-block',
|
|
30
|
+
'ratelimit-rps',
|
|
31
|
+
'latency-ms',
|
|
32
|
+
'uptime',
|
|
33
|
+
'instances',
|
|
34
|
+
'max-rps',
|
|
35
|
+
'cb-error-threshold',
|
|
36
|
+
'cb-latency-threshold-ms',
|
|
37
|
+
'concurrency',
|
|
38
|
+
'duration-ms',
|
|
39
|
+
'cold-start-ms',
|
|
40
|
+
'buffer',
|
|
41
|
+
'drain-rate',
|
|
42
|
+
'retention-hours',
|
|
43
|
+
'partitions',
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
/** The `rps` key is only valid on the `edge` component. */
|
|
47
|
+
export const EDGE_ONLY_KEYS = new Set<string>(['rps']);
|
|
48
|
+
|
|
49
|
+
export interface InfraProperty {
|
|
50
|
+
key: string;
|
|
51
|
+
value: string | number;
|
|
52
|
+
lineNumber: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface InfraNode {
|
|
56
|
+
id: string;
|
|
57
|
+
label: string;
|
|
58
|
+
properties: InfraProperty[];
|
|
59
|
+
groupId: string | null;
|
|
60
|
+
tags: Record<string, string>; // tagGroup -> tagValue
|
|
61
|
+
isEdge: boolean; // true for the `edge` entry-point component
|
|
62
|
+
lineNumber: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface InfraEdge {
|
|
66
|
+
sourceId: string;
|
|
67
|
+
targetId: string;
|
|
68
|
+
label: string;
|
|
69
|
+
split: number | null; // percentage 0-100, or null if not declared
|
|
70
|
+
lineNumber: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface InfraGroup {
|
|
74
|
+
id: string;
|
|
75
|
+
label: string;
|
|
76
|
+
/** Number of instances (or auto-scaling range "N-M") of this group as a unit. */
|
|
77
|
+
instances?: number | string;
|
|
78
|
+
/** Whether this group should be collapsed by default in the source. */
|
|
79
|
+
collapsed?: boolean;
|
|
80
|
+
lineNumber: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface InfraTagValue {
|
|
84
|
+
name: string;
|
|
85
|
+
color?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface InfraTagGroup {
|
|
89
|
+
name: string;
|
|
90
|
+
alias: string | null;
|
|
91
|
+
values: InfraTagValue[];
|
|
92
|
+
/** Value of the entry marked `default` (nodes without this tag get it automatically). */
|
|
93
|
+
defaultValue?: string;
|
|
94
|
+
lineNumber: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface InfraScenario {
|
|
98
|
+
name: string;
|
|
99
|
+
/** Node property overrides: nodeId -> { key: value } */
|
|
100
|
+
overrides: Record<string, Record<string, string | number>>;
|
|
101
|
+
lineNumber: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface ParsedInfra {
|
|
105
|
+
type: 'infra';
|
|
106
|
+
title: string | null;
|
|
107
|
+
titleLineNumber: number | null;
|
|
108
|
+
direction: 'LR' | 'TB';
|
|
109
|
+
nodes: InfraNode[];
|
|
110
|
+
edges: InfraEdge[];
|
|
111
|
+
groups: InfraGroup[];
|
|
112
|
+
tagGroups: InfraTagGroup[];
|
|
113
|
+
scenarios: InfraScenario[];
|
|
114
|
+
options: Record<string, string>;
|
|
115
|
+
diagnostics: DgmoError[];
|
|
116
|
+
error: string | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ============================================================
|
|
120
|
+
// Computed Model Types
|
|
121
|
+
// ============================================================
|
|
122
|
+
|
|
123
|
+
export interface InfraComputeParams {
|
|
124
|
+
rps?: number; // override edge rps (for slider)
|
|
125
|
+
instanceOverrides?: Record<string, number>; // nodeId -> instance count override
|
|
126
|
+
scenario?: InfraScenario | null; // apply a named scenario's overrides
|
|
127
|
+
/** Per-node property overrides: nodeId -> { propertyKey: numericValue }.
|
|
128
|
+
* Applied after scenario overrides. Lets sliders adjust cache-hit, etc. */
|
|
129
|
+
propertyOverrides?: Record<string, Record<string, number>>;
|
|
130
|
+
/** Set of group IDs that should be treated as collapsed (virtual nodes). */
|
|
131
|
+
collapsedGroups?: Set<string>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type InfraCbState = 'closed' | 'open' | 'half-open';
|
|
135
|
+
|
|
136
|
+
export interface ComputedInfraNode {
|
|
137
|
+
id: string;
|
|
138
|
+
label: string;
|
|
139
|
+
groupId: string | null;
|
|
140
|
+
isEdge: boolean;
|
|
141
|
+
computedRps: number;
|
|
142
|
+
overloaded: boolean;
|
|
143
|
+
/** True when inbound RPS exceeds the node's ratelimit-rps and traffic is being shed. */
|
|
144
|
+
rateLimited: boolean;
|
|
145
|
+
/** Cumulative latency from edge to this node (ms). */
|
|
146
|
+
computedLatencyMs: number;
|
|
147
|
+
/** Latency percentiles from this node through all downstream paths (ms). */
|
|
148
|
+
computedLatencyPercentiles: InfraLatencyPercentiles;
|
|
149
|
+
/** Component uptime (product of uptimes along path, 0-1). */
|
|
150
|
+
computedUptime: number;
|
|
151
|
+
/** Local availability at this node (0-1), factoring in uptime, overload shed, and rate-limit reject. */
|
|
152
|
+
computedAvailability: number;
|
|
153
|
+
/** Availability percentiles through all downstream paths from this node (0-1 fractions). */
|
|
154
|
+
computedAvailabilityPercentiles: InfraAvailabilityPercentiles;
|
|
155
|
+
/** Circuit breaker state. */
|
|
156
|
+
computedCbState: InfraCbState;
|
|
157
|
+
/** Computed instance count for auto-scaling (min-max) ranges. */
|
|
158
|
+
computedInstances: number;
|
|
159
|
+
/** For serverless nodes: estimated concurrent invocations (Little's Law: RPS × duration_ms / 1000). */
|
|
160
|
+
computedConcurrentInvocations: number;
|
|
161
|
+
/** For collapsed group virtual nodes: worst health state of any child.
|
|
162
|
+
* 'overloaded' > 'warning' > 'normal'. Undefined for regular nodes. */
|
|
163
|
+
childHealthState?: 'normal' | 'warning' | 'overloaded';
|
|
164
|
+
/** Queue metrics — only present when buffer property exists. */
|
|
165
|
+
queueMetrics?: {
|
|
166
|
+
/** Messages per second filling the buffer (inbound - drain-rate, clamped to 0). */
|
|
167
|
+
fillRate: number;
|
|
168
|
+
/** Seconds until buffer overflow at sustained fill rate. Infinity if not filling. */
|
|
169
|
+
timeToOverflow: number;
|
|
170
|
+
/** Queue wait time in ms (pending_messages / drain_rate * 1000). */
|
|
171
|
+
waitTimeMs: number;
|
|
172
|
+
};
|
|
173
|
+
properties: InfraProperty[];
|
|
174
|
+
tags: Record<string, string>;
|
|
175
|
+
lineNumber: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ComputedInfraEdge {
|
|
179
|
+
sourceId: string;
|
|
180
|
+
targetId: string;
|
|
181
|
+
label: string;
|
|
182
|
+
computedRps: number;
|
|
183
|
+
split: number; // resolved split (always 0-100)
|
|
184
|
+
lineNumber: number;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface InfraDiagnostic {
|
|
188
|
+
type: 'SPLIT_SUM' | 'CYCLE' | 'OVERLOAD' | 'RATE_LIMITED' | 'ORPHAN' | 'SYNTAX' | 'UPTIME';
|
|
189
|
+
line: number;
|
|
190
|
+
message: string;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface InfraLatencyPercentiles {
|
|
194
|
+
p50: number;
|
|
195
|
+
p90: number;
|
|
196
|
+
p99: number;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface InfraAvailabilityPercentiles {
|
|
200
|
+
p50: number;
|
|
201
|
+
p90: number;
|
|
202
|
+
p99: number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface ComputedInfraModel {
|
|
206
|
+
nodes: ComputedInfraNode[];
|
|
207
|
+
edges: ComputedInfraEdge[];
|
|
208
|
+
groups: InfraGroup[];
|
|
209
|
+
tagGroups: InfraTagGroup[];
|
|
210
|
+
title: string | null;
|
|
211
|
+
direction: 'LR' | 'TB';
|
|
212
|
+
/** Diagram-level options (e.g., default-latency-ms, default-uptime). */
|
|
213
|
+
options: Record<string, string>;
|
|
214
|
+
/** Latency percentiles at the edge entry point (weighted by traffic probability). */
|
|
215
|
+
edgeLatency: InfraLatencyPercentiles;
|
|
216
|
+
/** System uptime at edge (weighted average across all paths). */
|
|
217
|
+
systemUptime: number;
|
|
218
|
+
/** System availability at edge (weighted average of compound availability across all paths). */
|
|
219
|
+
systemAvailability: number;
|
|
220
|
+
diagnostics: InfraDiagnostic[];
|
|
221
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Infra Chart Validation
|
|
3
|
+
// ============================================================
|
|
4
|
+
//
|
|
5
|
+
// Validates structural correctness of the parsed infra model:
|
|
6
|
+
// - Split sums (FR19)
|
|
7
|
+
// - DAG validation — no cycles (FR20)
|
|
8
|
+
// - Orphan detection
|
|
9
|
+
|
|
10
|
+
import type { ParsedInfra, InfraDiagnostic, ComputedInfraModel } from './types';
|
|
11
|
+
|
|
12
|
+
// ============================================================
|
|
13
|
+
// Cycle Detection
|
|
14
|
+
// ============================================================
|
|
15
|
+
|
|
16
|
+
function detectCycles(parsed: ParsedInfra): InfraDiagnostic[] {
|
|
17
|
+
const diagnostics: InfraDiagnostic[] = [];
|
|
18
|
+
|
|
19
|
+
// Build adjacency list
|
|
20
|
+
const adj = new Map<string, string[]>();
|
|
21
|
+
const edgeLines = new Map<string, number>(); // edge key -> lineNumber
|
|
22
|
+
for (const edge of parsed.edges) {
|
|
23
|
+
const list = adj.get(edge.sourceId) ?? [];
|
|
24
|
+
list.push(edge.targetId);
|
|
25
|
+
adj.set(edge.sourceId, list);
|
|
26
|
+
edgeLines.set(`${edge.sourceId}->${edge.targetId}`, edge.lineNumber);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// DFS with coloring: 0=white, 1=grey, 2=black
|
|
30
|
+
const color = new Map<string, number>();
|
|
31
|
+
const parent = new Map<string, string | null>();
|
|
32
|
+
|
|
33
|
+
function dfs(nodeId: string): boolean {
|
|
34
|
+
color.set(nodeId, 1); // grey
|
|
35
|
+
const neighbors = adj.get(nodeId) ?? [];
|
|
36
|
+
for (const next of neighbors) {
|
|
37
|
+
const c = color.get(next) ?? 0;
|
|
38
|
+
if (c === 1) {
|
|
39
|
+
// Back edge — cycle found
|
|
40
|
+
const lineKey = `${nodeId}->${next}`;
|
|
41
|
+
diagnostics.push({
|
|
42
|
+
type: 'CYCLE',
|
|
43
|
+
line: edgeLines.get(lineKey) ?? 0,
|
|
44
|
+
message: `Cycle detected: ${nodeId} -> ${next} creates a circular reference.`,
|
|
45
|
+
});
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if (c === 0) {
|
|
49
|
+
parent.set(next, nodeId);
|
|
50
|
+
if (dfs(next)) return true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
color.set(nodeId, 2); // black
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const node of parsed.nodes) {
|
|
58
|
+
if ((color.get(node.id) ?? 0) === 0) {
|
|
59
|
+
dfs(node.id);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return diagnostics;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ============================================================
|
|
67
|
+
// Split Validation
|
|
68
|
+
// ============================================================
|
|
69
|
+
|
|
70
|
+
function validateSplits(parsed: ParsedInfra): InfraDiagnostic[] {
|
|
71
|
+
const diagnostics: InfraDiagnostic[] = [];
|
|
72
|
+
|
|
73
|
+
// Group edges by source
|
|
74
|
+
const outbound = new Map<string, typeof parsed.edges>();
|
|
75
|
+
for (const edge of parsed.edges) {
|
|
76
|
+
const list = outbound.get(edge.sourceId) ?? [];
|
|
77
|
+
list.push(edge);
|
|
78
|
+
outbound.set(edge.sourceId, list);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const [sourceId, edges] of outbound) {
|
|
82
|
+
if (edges.length <= 1) continue;
|
|
83
|
+
|
|
84
|
+
const declared = edges.filter((e) => e.split !== null);
|
|
85
|
+
if (declared.length === edges.length) {
|
|
86
|
+
// All declared — validate sum
|
|
87
|
+
const sum = declared.reduce((s, e) => s + (e.split ?? 0), 0);
|
|
88
|
+
if (Math.abs(sum - 100) > 0.01) {
|
|
89
|
+
diagnostics.push({
|
|
90
|
+
type: 'SPLIT_SUM',
|
|
91
|
+
line: declared[0].lineNumber,
|
|
92
|
+
message: `Splits from '${sourceId}' sum to ${sum}%, expected 100%.`,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
} else if (declared.length > 0) {
|
|
96
|
+
// Some declared — validate declared sum doesn't exceed 100
|
|
97
|
+
const declaredSum = declared.reduce((s, e) => s + (e.split ?? 0), 0);
|
|
98
|
+
if (declaredSum > 100) {
|
|
99
|
+
diagnostics.push({
|
|
100
|
+
type: 'SPLIT_SUM',
|
|
101
|
+
line: declared[0].lineNumber,
|
|
102
|
+
message: `Declared splits from '${sourceId}' sum to ${declaredSum}%, exceeding 100%.`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return diagnostics;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ============================================================
|
|
112
|
+
// Orphan Detection
|
|
113
|
+
// ============================================================
|
|
114
|
+
|
|
115
|
+
function detectOrphans(parsed: ParsedInfra): InfraDiagnostic[] {
|
|
116
|
+
const diagnostics: InfraDiagnostic[] = [];
|
|
117
|
+
|
|
118
|
+
// Nodes reachable from edge
|
|
119
|
+
const edgeNode = parsed.nodes.find((n) => n.isEdge);
|
|
120
|
+
if (!edgeNode) return diagnostics;
|
|
121
|
+
|
|
122
|
+
const reachable = new Set<string>();
|
|
123
|
+
const adj = new Map<string, string[]>();
|
|
124
|
+
for (const edge of parsed.edges) {
|
|
125
|
+
const list = adj.get(edge.sourceId) ?? [];
|
|
126
|
+
list.push(edge.targetId);
|
|
127
|
+
adj.set(edge.sourceId, list);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const queue = [edgeNode.id];
|
|
131
|
+
while (queue.length > 0) {
|
|
132
|
+
const id = queue.shift()!;
|
|
133
|
+
if (reachable.has(id)) continue;
|
|
134
|
+
reachable.add(id);
|
|
135
|
+
for (const next of adj.get(id) ?? []) {
|
|
136
|
+
queue.push(next);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Also mark group children as reachable if their group is reachable
|
|
141
|
+
for (const group of parsed.groups) {
|
|
142
|
+
if (reachable.has(group.id)) {
|
|
143
|
+
for (const node of parsed.nodes) {
|
|
144
|
+
if (node.groupId === group.id) reachable.add(node.id);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const node of parsed.nodes) {
|
|
150
|
+
if (!node.isEdge && !reachable.has(node.id)) {
|
|
151
|
+
diagnostics.push({
|
|
152
|
+
type: 'ORPHAN',
|
|
153
|
+
line: node.lineNumber,
|
|
154
|
+
message: `Component '${node.label}' is not reachable from the edge entry point.`,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return diagnostics;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ============================================================
|
|
163
|
+
// Main Validation
|
|
164
|
+
// ============================================================
|
|
165
|
+
|
|
166
|
+
export function validateInfra(parsed: ParsedInfra): InfraDiagnostic[] {
|
|
167
|
+
return [
|
|
168
|
+
...detectCycles(parsed),
|
|
169
|
+
...validateSplits(parsed),
|
|
170
|
+
...detectOrphans(parsed),
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Validate computed model (post-computation warnings).
|
|
176
|
+
* Call after computeInfra() to get uptime/SLA warnings.
|
|
177
|
+
*/
|
|
178
|
+
export function validateComputed(computed: ComputedInfraModel): InfraDiagnostic[] {
|
|
179
|
+
const diagnostics: InfraDiagnostic[] = [];
|
|
180
|
+
|
|
181
|
+
// Uptime warning: if system uptime is below 99%
|
|
182
|
+
if (computed.systemUptime > 0 && computed.systemUptime < 0.99) {
|
|
183
|
+
const pct = (computed.systemUptime * 100).toFixed(2);
|
|
184
|
+
diagnostics.push({
|
|
185
|
+
type: 'UPTIME',
|
|
186
|
+
line: 1,
|
|
187
|
+
message: `System uptime is ${pct}%, below 99% SLA threshold.`,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return diagnostics;
|
|
192
|
+
}
|