@actuarial-ts/core 0.5.0 → 0.6.1
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/README.md +65 -226
- package/dist/capping.d.ts.map +1 -1
- package/dist/capping.js +39 -6
- package/dist/capping.js.map +1 -1
- package/dist/casualtyDiagnostics.d.ts +19 -49
- package/dist/casualtyDiagnostics.d.ts.map +1 -1
- package/dist/casualtyDiagnostics.js +84 -116
- package/dist/casualtyDiagnostics.js.map +1 -1
- package/dist/diagnosticAggregation.d.ts +32 -0
- package/dist/diagnosticAggregation.d.ts.map +1 -0
- package/dist/diagnosticAggregation.js +73 -0
- package/dist/diagnosticAggregation.js.map +1 -0
- package/dist/diagnosticDefinitions.d.ts +289 -0
- package/dist/diagnosticDefinitions.d.ts.map +1 -0
- package/dist/diagnosticDefinitions.js +1688 -0
- package/dist/diagnosticDefinitions.js.map +1 -0
- package/dist/diagnosticDerivations.d.ts +35 -0
- package/dist/diagnosticDerivations.d.ts.map +1 -0
- package/dist/diagnosticDerivations.js +224 -0
- package/dist/diagnosticDerivations.js.map +1 -0
- package/dist/diagnosticExposure.d.ts +51 -0
- package/dist/diagnosticExposure.d.ts.map +1 -0
- package/dist/diagnosticExposure.js +273 -0
- package/dist/diagnosticExposure.js.map +1 -0
- package/dist/diagnosticExpressions.d.ts +56 -0
- package/dist/diagnosticExpressions.d.ts.map +1 -0
- package/dist/diagnosticExpressions.js +124 -0
- package/dist/diagnosticExpressions.js.map +1 -0
- package/dist/diagnosticFormulas.d.ts +198 -0
- package/dist/diagnosticFormulas.d.ts.map +1 -0
- package/dist/diagnosticFormulas.js +219 -0
- package/dist/diagnosticFormulas.js.map +1 -0
- package/dist/diagnosticIdentity.d.ts +251 -0
- package/dist/diagnosticIdentity.d.ts.map +1 -0
- package/dist/diagnosticIdentity.js +721 -0
- package/dist/diagnosticIdentity.js.map +1 -0
- package/dist/diagnosticOrdering.d.ts +8 -0
- package/dist/diagnosticOrdering.d.ts.map +1 -0
- package/dist/diagnosticOrdering.js +73 -0
- package/dist/diagnosticOrdering.js.map +1 -0
- package/dist/diagnosticPeriodAxis.d.ts +9 -0
- package/dist/diagnosticPeriodAxis.d.ts.map +1 -0
- package/dist/diagnosticPeriodAxis.js +69 -0
- package/dist/diagnosticPeriodAxis.js.map +1 -0
- package/dist/diagnosticPeriods.d.ts +15 -0
- package/dist/diagnosticPeriods.d.ts.map +1 -0
- package/dist/diagnosticPeriods.js +50 -0
- package/dist/diagnosticPeriods.js.map +1 -0
- package/dist/diagnosticPreparation.d.ts +131 -0
- package/dist/diagnosticPreparation.d.ts.map +1 -0
- package/dist/diagnosticPreparation.js +1539 -0
- package/dist/diagnosticPreparation.js.map +1 -0
- package/dist/diagnosticReview.d.ts +66 -0
- package/dist/diagnosticReview.d.ts.map +1 -0
- package/dist/diagnosticReview.js +490 -0
- package/dist/diagnosticReview.js.map +1 -0
- package/dist/diagnosticRules.d.ts +32 -0
- package/dist/diagnosticRules.d.ts.map +1 -0
- package/dist/diagnosticRules.js +32 -0
- package/dist/diagnosticRules.js.map +1 -0
- package/dist/diagnosticRunner.d.ts +86 -0
- package/dist/diagnosticRunner.d.ts.map +1 -0
- package/dist/diagnosticRunner.js +703 -0
- package/dist/diagnosticRunner.js.map +1 -0
- package/dist/diagnosticRuntime.d.ts +26 -0
- package/dist/diagnosticRuntime.d.ts.map +1 -0
- package/dist/diagnosticRuntime.js +243 -0
- package/dist/diagnosticRuntime.js.map +1 -0
- package/dist/diagnosticSourceOrdering.d.ts +6 -0
- package/dist/diagnosticSourceOrdering.d.ts.map +1 -0
- package/dist/diagnosticSourceOrdering.js +38 -0
- package/dist/diagnosticSourceOrdering.js.map +1 -0
- package/dist/index.d.ts +28 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/mack.d.ts +4 -0
- package/dist/mack.d.ts.map +1 -1
- package/dist/mack.js +32 -4
- package/dist/mack.js.map +1 -1
- package/dist/triangle.d.ts.map +1 -1
- package/dist/triangle.js +59 -9
- package/dist/triangle.js.map +1 -1
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +128 -0
- package/dist/types.js.map +1 -1
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +4 -3
- package/src/capping.ts +109 -18
- package/src/casualtyDiagnostics.ts +79 -189
- package/src/diagnosticAggregation.ts +128 -0
- package/src/diagnosticDefinitions.ts +3624 -0
- package/src/diagnosticDerivations.ts +321 -0
- package/src/diagnosticExposure.ts +482 -0
- package/src/diagnosticExpressions.ts +255 -0
- package/src/diagnosticFormulas.ts +350 -0
- package/src/diagnosticIdentity.ts +1267 -0
- package/src/diagnosticOrdering.ts +106 -0
- package/src/diagnosticPeriodAxis.ts +92 -0
- package/src/diagnosticPeriods.ts +102 -0
- package/src/diagnosticPreparation.ts +2475 -0
- package/src/diagnosticReview.ts +897 -0
- package/src/diagnosticRules.ts +69 -0
- package/src/diagnosticRunner.ts +1114 -0
- package/src/diagnosticRuntime.ts +264 -0
- package/src/diagnosticSourceOrdering.ts +50 -0
- package/src/index.ts +184 -1
- package/src/mack.ts +72 -9
- package/src/triangle.ts +103 -13
- package/src/types.ts +196 -0
- package/src/version.ts +2 -0
- package/dist/metricDiagnostics.d.ts +0 -212
- package/dist/metricDiagnostics.d.ts.map +0 -1
- package/dist/metricDiagnostics.js +0 -627
- package/dist/metricDiagnostics.js.map +0 -1
- package/src/metricDiagnostics.ts +0 -876
|
@@ -0,0 +1,1114 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DiagnosticDeepReadonly,
|
|
3
|
+
DiagnosticMeasureDefinition,
|
|
4
|
+
DiagnosticMetricPresentation,
|
|
5
|
+
DiagnosticsFilter,
|
|
6
|
+
JsonValue,
|
|
7
|
+
} from "./diagnosticDefinitions.js";
|
|
8
|
+
import { getCompiledDiagnosticDefinitionInternals } from "./diagnosticDefinitions.js";
|
|
9
|
+
import { finalizeDiagnosticContributions } from "./diagnosticAggregation.js";
|
|
10
|
+
import {
|
|
11
|
+
applyDiagnosticPresentation,
|
|
12
|
+
diagnosticRawRatio,
|
|
13
|
+
evaluateDiagnosticMeasureExpression,
|
|
14
|
+
evaluateDiagnosticRoleExpression,
|
|
15
|
+
type DiagnosticMetricFinding,
|
|
16
|
+
type DiagnosticQuantity,
|
|
17
|
+
type FinalizedDiagnosticMeasure,
|
|
18
|
+
} from "./diagnosticFormulas.js";
|
|
19
|
+
import {
|
|
20
|
+
classifyDiagnosticComparison,
|
|
21
|
+
diagnosticPredicateMatches,
|
|
22
|
+
type DiagnosticRuleEvaluation,
|
|
23
|
+
} from "./diagnosticRules.js";
|
|
24
|
+
import {
|
|
25
|
+
assertPreparedDiagnosticData,
|
|
26
|
+
type PreparedDiagnosticData,
|
|
27
|
+
} from "./diagnosticPreparation.js";
|
|
28
|
+
import type { DiagnosticMeasureStats } from "./diagnosticDefinitions.js";
|
|
29
|
+
import {
|
|
30
|
+
DiagnosticValidationError,
|
|
31
|
+
type DiagnosticValidationIssue,
|
|
32
|
+
} from "./types.js";
|
|
33
|
+
import { canonicalJson } from "./canonical.js";
|
|
34
|
+
import {
|
|
35
|
+
projectDiagnosticIdentity,
|
|
36
|
+
type DiagnosticIdentityProjection,
|
|
37
|
+
} from "./diagnosticIdentity.js";
|
|
38
|
+
import { compareDiagnosticFindings } from "./diagnosticOrdering.js";
|
|
39
|
+
import { normalizeDiagnosticPeriod } from "./diagnosticPeriods.js";
|
|
40
|
+
import { normalizeDiagnosticSourceLocations } from "./diagnosticSourceOrdering.js";
|
|
41
|
+
import {
|
|
42
|
+
diagnosticJsonPreflight,
|
|
43
|
+
hasDiagnosticOwn,
|
|
44
|
+
isDiagnosticPlainRecord,
|
|
45
|
+
isDiagnosticToken,
|
|
46
|
+
} from "./diagnosticRuntime.js";
|
|
47
|
+
|
|
48
|
+
export interface RunMetricDiagnosticsInput {
|
|
49
|
+
readonly prepared: PreparedDiagnosticData;
|
|
50
|
+
readonly groupMap?: Readonly<Record<string, string>>;
|
|
51
|
+
readonly groupDimensions?: Readonly<Record<string, JsonValue>>;
|
|
52
|
+
}
|
|
53
|
+
export interface DiagnosticMetricEvaluation {
|
|
54
|
+
readonly instanceId: string;
|
|
55
|
+
readonly instanceVersion: string;
|
|
56
|
+
readonly formulaId: string;
|
|
57
|
+
readonly formulaVersion: string;
|
|
58
|
+
readonly semanticReferences: {
|
|
59
|
+
readonly amountBasisIds: readonly string[];
|
|
60
|
+
readonly countPopulationIds: readonly string[];
|
|
61
|
+
readonly exposureBasisIds: readonly string[];
|
|
62
|
+
};
|
|
63
|
+
readonly formulaFingerprint: string;
|
|
64
|
+
readonly calculationFingerprint: string;
|
|
65
|
+
readonly definitionIntegrity: string;
|
|
66
|
+
readonly calculation: {
|
|
67
|
+
readonly numerator: DiagnosticQuantity;
|
|
68
|
+
readonly denominator: DiagnosticQuantity;
|
|
69
|
+
readonly value: number | null;
|
|
70
|
+
};
|
|
71
|
+
readonly presentation: DiagnosticMetricPresentation & {
|
|
72
|
+
readonly value: number | null;
|
|
73
|
+
};
|
|
74
|
+
readonly components: Readonly<Record<string, DiagnosticMeasureStats>>;
|
|
75
|
+
readonly rules: readonly DiagnosticRuleEvaluation[];
|
|
76
|
+
readonly findings: readonly DiagnosticMetricFinding[];
|
|
77
|
+
}
|
|
78
|
+
export interface DiagnosticEmergencePoint {
|
|
79
|
+
readonly group: string;
|
|
80
|
+
readonly sourceGroups: readonly string[];
|
|
81
|
+
readonly dimensions?: JsonValue;
|
|
82
|
+
readonly origin: string;
|
|
83
|
+
readonly valuation: string;
|
|
84
|
+
readonly developmentAge: number;
|
|
85
|
+
readonly ageUnit: string;
|
|
86
|
+
readonly components: Readonly<Record<string, DiagnosticMeasureStats>>;
|
|
87
|
+
readonly metrics: Readonly<Record<string, DiagnosticMetricEvaluation>>;
|
|
88
|
+
readonly findings: readonly DiagnosticMetricFinding[];
|
|
89
|
+
}
|
|
90
|
+
export interface DiagnosticMetricTriangleCell {
|
|
91
|
+
readonly origin: string;
|
|
92
|
+
readonly valuation: string;
|
|
93
|
+
readonly developmentAge: number;
|
|
94
|
+
readonly ageUnit: string;
|
|
95
|
+
readonly evaluation: DiagnosticMetricEvaluation;
|
|
96
|
+
}
|
|
97
|
+
export interface DiagnosticMetricTriangle {
|
|
98
|
+
readonly group: string;
|
|
99
|
+
readonly instanceId: string;
|
|
100
|
+
readonly origins: readonly string[];
|
|
101
|
+
readonly developmentAges: readonly number[];
|
|
102
|
+
readonly ageUnit: string;
|
|
103
|
+
readonly calculationValues: readonly (readonly (number | null)[])[];
|
|
104
|
+
readonly presentationValues: readonly (readonly (number | null)[])[];
|
|
105
|
+
readonly cells: readonly (readonly (DiagnosticMetricTriangleCell | null)[])[];
|
|
106
|
+
}
|
|
107
|
+
export interface MetricDiagnosticsResult {
|
|
108
|
+
readonly definitionIntegrity: string;
|
|
109
|
+
readonly preparationFingerprint: string;
|
|
110
|
+
readonly ageUnit: string;
|
|
111
|
+
readonly emergence: readonly DiagnosticEmergencePoint[];
|
|
112
|
+
readonly triangles: readonly DiagnosticMetricTriangle[];
|
|
113
|
+
readonly latestDiagonal: readonly DiagnosticEmergencePoint[];
|
|
114
|
+
readonly findings: readonly DiagnosticMetricFinding[];
|
|
115
|
+
}
|
|
116
|
+
export interface CommonMaturityResult {
|
|
117
|
+
readonly developmentAge: number | null;
|
|
118
|
+
readonly ageUnit: string;
|
|
119
|
+
readonly points: readonly DiagnosticDeepReadonly<DiagnosticEmergencePoint>[];
|
|
120
|
+
}
|
|
121
|
+
export type NormalizedDiagnosticResultIdentity = DiagnosticIdentityProjection<
|
|
122
|
+
DiagnosticDeepReadonly<MetricDiagnosticsResult>
|
|
123
|
+
>;
|
|
124
|
+
|
|
125
|
+
function codeUnit(a: string, b: string): number {
|
|
126
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
127
|
+
}
|
|
128
|
+
function pointer(path: string, segment: string | number): string {
|
|
129
|
+
return `${path}/${String(segment).replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
130
|
+
}
|
|
131
|
+
function deepFreeze<T>(
|
|
132
|
+
value: T,
|
|
133
|
+
seen = new WeakSet<object>(),
|
|
134
|
+
): DiagnosticDeepReadonly<T> {
|
|
135
|
+
if (value === null || typeof value !== "object" || seen.has(value))
|
|
136
|
+
return value as DiagnosticDeepReadonly<T>;
|
|
137
|
+
seen.add(value);
|
|
138
|
+
for (const child of Object.values(value as Record<string, unknown>))
|
|
139
|
+
deepFreeze(child, seen);
|
|
140
|
+
return Object.freeze(value) as DiagnosticDeepReadonly<T>;
|
|
141
|
+
}
|
|
142
|
+
function quantity(
|
|
143
|
+
measure: DiagnosticMeasureDefinition,
|
|
144
|
+
value: number | null,
|
|
145
|
+
): DiagnosticQuantity {
|
|
146
|
+
return {
|
|
147
|
+
kind: measure.kind,
|
|
148
|
+
unit: measure.unit,
|
|
149
|
+
...(measure.basisId ? { basisId: measure.basisId } : {}),
|
|
150
|
+
...(measure.countPopulationId
|
|
151
|
+
? { countPopulationId: measure.countPopulationId }
|
|
152
|
+
: {}),
|
|
153
|
+
...(measure.exposureBasisId
|
|
154
|
+
? { exposureBasisId: measure.exposureBasisId }
|
|
155
|
+
: {}),
|
|
156
|
+
value,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const READINESS_ORDER = [
|
|
161
|
+
"missing",
|
|
162
|
+
"imputed",
|
|
163
|
+
"non-finite",
|
|
164
|
+
"structural-ambiguity",
|
|
165
|
+
"aggregation-overflow",
|
|
166
|
+
"expression-overflow",
|
|
167
|
+
"tolerance-overflow",
|
|
168
|
+
] as const;
|
|
169
|
+
function readiness(
|
|
170
|
+
values: readonly (typeof READINESS_ORDER)[number][],
|
|
171
|
+
): (typeof READINESS_ORDER)[number][] {
|
|
172
|
+
return READINESS_ORDER.filter((value) => values.includes(value));
|
|
173
|
+
}
|
|
174
|
+
function expressionMeasureIds(
|
|
175
|
+
expression: import("./diagnosticExpressions.js").DiagnosticMeasureExpression,
|
|
176
|
+
): string[] {
|
|
177
|
+
if (expression.op === "measure") return [expression.measureId];
|
|
178
|
+
return [
|
|
179
|
+
...new Set(
|
|
180
|
+
(expression.op === "add"
|
|
181
|
+
? expression.terms
|
|
182
|
+
: [expression.left, expression.right]
|
|
183
|
+
).flatMap(expressionMeasureIds),
|
|
184
|
+
),
|
|
185
|
+
].sort(codeUnit);
|
|
186
|
+
}
|
|
187
|
+
function unionSources(
|
|
188
|
+
values: readonly import("./diagnosticDefinitions.js").DiagnosticSourceLocation[],
|
|
189
|
+
): import("./diagnosticDefinitions.js").DiagnosticSourceLocation[] {
|
|
190
|
+
return normalizeDiagnosticSourceLocations(values);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function mergeFindings(
|
|
194
|
+
values: readonly DiagnosticMetricFinding[],
|
|
195
|
+
): DiagnosticMetricFinding[] {
|
|
196
|
+
const merged = new Map<string, DiagnosticMetricFinding>();
|
|
197
|
+
for (const value of values) {
|
|
198
|
+
const key = canonicalJson({ ...value, sources: [] });
|
|
199
|
+
const previous = merged.get(key);
|
|
200
|
+
const sources = normalizeDiagnosticSourceLocations([
|
|
201
|
+
...(previous?.sources ?? []),
|
|
202
|
+
...value.sources,
|
|
203
|
+
]);
|
|
204
|
+
merged.set(key, { ...(previous ?? value), sources });
|
|
205
|
+
}
|
|
206
|
+
return [...merged.values()].sort(compareDiagnosticFindings);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function validateDiagnosticGroupingConfiguration(
|
|
210
|
+
input: RunMetricDiagnosticsInput,
|
|
211
|
+
): void {
|
|
212
|
+
assertPreparedDiagnosticData(input.prepared);
|
|
213
|
+
const boundaryIssues: DiagnosticValidationIssue[] = [];
|
|
214
|
+
for (const [value, path] of [
|
|
215
|
+
[input.groupMap, "$.groupMap"],
|
|
216
|
+
[input.groupDimensions, "$.groupDimensions"],
|
|
217
|
+
] as const) {
|
|
218
|
+
if (value === undefined) continue;
|
|
219
|
+
if (!isDiagnosticPlainRecord(value))
|
|
220
|
+
boundaryIssues.push({
|
|
221
|
+
domain: "configuration",
|
|
222
|
+
code: "invalid-type",
|
|
223
|
+
path,
|
|
224
|
+
message: "Grouping configuration must be a plain object",
|
|
225
|
+
});
|
|
226
|
+
else
|
|
227
|
+
boundaryIssues.push(
|
|
228
|
+
...diagnosticJsonPreflight(value, "configuration").map((issue) => ({
|
|
229
|
+
...issue,
|
|
230
|
+
path: issue.path === "$" ? path : `${path}${issue.path.slice(1)}`,
|
|
231
|
+
})),
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
if (boundaryIssues.length > 0)
|
|
235
|
+
throw new DiagnosticValidationError(boundaryIssues);
|
|
236
|
+
const auditedSourceGroups = input.prepared.inputAudit.flatMap((item) => {
|
|
237
|
+
return input.prepared.filter?.sourceGroups === undefined ||
|
|
238
|
+
input.prepared.filter.sourceGroups.includes(item.record.sourceGroup)
|
|
239
|
+
? [item.record.sourceGroup]
|
|
240
|
+
: [];
|
|
241
|
+
});
|
|
242
|
+
const sourceGroups = new Set([
|
|
243
|
+
...auditedSourceGroups,
|
|
244
|
+
...input.prepared.cells.map((cell) => cell.sourceGroup),
|
|
245
|
+
]);
|
|
246
|
+
for (const [source, target] of Object.entries(input.groupMap ?? {})) {
|
|
247
|
+
if (!sourceGroups.has(source))
|
|
248
|
+
throw new DiagnosticValidationError([
|
|
249
|
+
{
|
|
250
|
+
domain: "configuration",
|
|
251
|
+
code: "invalid-configuration",
|
|
252
|
+
path: `$.groupMap[${JSON.stringify(source)}]`,
|
|
253
|
+
message: "Group map contains an unused source group",
|
|
254
|
+
},
|
|
255
|
+
]);
|
|
256
|
+
if (!isDiagnosticToken(source))
|
|
257
|
+
throw new DiagnosticValidationError([
|
|
258
|
+
{
|
|
259
|
+
domain: "configuration",
|
|
260
|
+
code: "invalid-string",
|
|
261
|
+
path: `$.groupMap[${JSON.stringify(source)}]`,
|
|
262
|
+
message: "Source group must be a nonempty token",
|
|
263
|
+
},
|
|
264
|
+
]);
|
|
265
|
+
if (!isDiagnosticToken(target))
|
|
266
|
+
throw new DiagnosticValidationError([
|
|
267
|
+
{
|
|
268
|
+
domain: "configuration",
|
|
269
|
+
code: "invalid-string",
|
|
270
|
+
path: `$.groupMap[${JSON.stringify(source)}]`,
|
|
271
|
+
message: "Output group must be a nonempty token",
|
|
272
|
+
},
|
|
273
|
+
]);
|
|
274
|
+
}
|
|
275
|
+
const outputGroups = new Set(
|
|
276
|
+
[...sourceGroups].map((source) =>
|
|
277
|
+
input.groupMap !== undefined && hasDiagnosticOwn(input.groupMap, source)
|
|
278
|
+
? input.groupMap[source]!
|
|
279
|
+
: source,
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
for (const group of Object.keys(input.groupDimensions ?? {}))
|
|
283
|
+
if (!outputGroups.has(group))
|
|
284
|
+
throw new DiagnosticValidationError([
|
|
285
|
+
{
|
|
286
|
+
domain: "configuration",
|
|
287
|
+
code: "invalid-configuration",
|
|
288
|
+
path: `$.groupDimensions[${JSON.stringify(group)}]`,
|
|
289
|
+
message: "Dimensions contain an unused output group",
|
|
290
|
+
},
|
|
291
|
+
]);
|
|
292
|
+
for (const group of input.prepared.filter?.outputGroups ?? [])
|
|
293
|
+
if (!outputGroups.has(group))
|
|
294
|
+
throw new DiagnosticValidationError([
|
|
295
|
+
{
|
|
296
|
+
domain: "configuration",
|
|
297
|
+
code: "unknown-reference",
|
|
298
|
+
path: "$.prepared.filter.outputGroups",
|
|
299
|
+
message: `Unknown output group ${group}`,
|
|
300
|
+
},
|
|
301
|
+
]);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function mergeStats(
|
|
305
|
+
cells: readonly PreparedDiagnosticData["cells"][number][],
|
|
306
|
+
measure: { readonly id: string; readonly missing: "unknown" | "zero" },
|
|
307
|
+
): DiagnosticMeasureStats {
|
|
308
|
+
const contributions = cells.flatMap(
|
|
309
|
+
(cell) => cell.contributions[measure.id] ?? [],
|
|
310
|
+
);
|
|
311
|
+
const blockers = [
|
|
312
|
+
...new Map(
|
|
313
|
+
cells
|
|
314
|
+
.flatMap((cell) => cell.structuralBlockers[measure.id] ?? [])
|
|
315
|
+
.map((blocker) => [canonicalJson(blocker), blocker]),
|
|
316
|
+
).entries(),
|
|
317
|
+
]
|
|
318
|
+
.sort(([left], [right]) => codeUnit(left, right))
|
|
319
|
+
.map(([, blocker]) => blocker);
|
|
320
|
+
return finalizeDiagnosticContributions(
|
|
321
|
+
contributions,
|
|
322
|
+
measure.missing,
|
|
323
|
+
blockers,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function inferExpressionMeasure(
|
|
328
|
+
expression: import("./diagnosticExpressions.js").DiagnosticMeasureExpression,
|
|
329
|
+
measures: ReadonlyMap<string, DiagnosticMeasureDefinition>,
|
|
330
|
+
): DiagnosticMeasureDefinition {
|
|
331
|
+
const id =
|
|
332
|
+
expression.op === "measure"
|
|
333
|
+
? expression.measureId
|
|
334
|
+
: expression.op === "add"
|
|
335
|
+
? inferExpressionMeasure(expression.terms[0]!, measures).id
|
|
336
|
+
: inferExpressionMeasure(expression.left, measures).id;
|
|
337
|
+
return measures.get(id)!;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function inferRoleExpressionMeasure(
|
|
341
|
+
expression: import("./diagnosticExpressions.js").DiagnosticRoleExpression,
|
|
342
|
+
bindings: Readonly<
|
|
343
|
+
Record<
|
|
344
|
+
string,
|
|
345
|
+
import("./diagnosticExpressions.js").DiagnosticMeasureExpression
|
|
346
|
+
>
|
|
347
|
+
>,
|
|
348
|
+
measures: ReadonlyMap<string, DiagnosticMeasureDefinition>,
|
|
349
|
+
): DiagnosticMeasureDefinition {
|
|
350
|
+
const role =
|
|
351
|
+
expression.op === "role"
|
|
352
|
+
? expression.role
|
|
353
|
+
: inferRoleExpressionMeasure(
|
|
354
|
+
expression.op === "add" ? expression.terms[0]! : expression.left,
|
|
355
|
+
bindings,
|
|
356
|
+
measures,
|
|
357
|
+
).id;
|
|
358
|
+
if (expression.op !== "role") return measures.get(role)!;
|
|
359
|
+
return inferExpressionMeasure(bindings[role]!, measures);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function evaluatePoint(
|
|
363
|
+
prepared: PreparedDiagnosticData,
|
|
364
|
+
components: Readonly<Record<string, DiagnosticMeasureStats>>,
|
|
365
|
+
instanceId: string,
|
|
366
|
+
context: {
|
|
367
|
+
readonly group: string;
|
|
368
|
+
readonly origin: string;
|
|
369
|
+
readonly valuation: string;
|
|
370
|
+
readonly developmentAge: number;
|
|
371
|
+
readonly ageUnit: string;
|
|
372
|
+
},
|
|
373
|
+
preparedFindings: readonly DiagnosticMetricFinding[],
|
|
374
|
+
blockerFindingsByMeasure: Readonly<
|
|
375
|
+
Record<string, readonly DiagnosticMetricFinding[]>
|
|
376
|
+
>,
|
|
377
|
+
sourcesByMeasure: Readonly<
|
|
378
|
+
Record<
|
|
379
|
+
string,
|
|
380
|
+
readonly import("./diagnosticDefinitions.js").DiagnosticSourceLocation[]
|
|
381
|
+
>
|
|
382
|
+
>,
|
|
383
|
+
): DiagnosticMetricEvaluation {
|
|
384
|
+
const definition = prepared.definition;
|
|
385
|
+
const internals = getCompiledDiagnosticDefinitionInternals(definition);
|
|
386
|
+
const instance = definition.definition.instances.find(
|
|
387
|
+
(item) => item.id === instanceId,
|
|
388
|
+
)!;
|
|
389
|
+
const formula = definition.definition.formulas.find(
|
|
390
|
+
(item) => item.id === instance.formulaId,
|
|
391
|
+
)!;
|
|
392
|
+
const instanceIndex = definition.definition.instances.findIndex(
|
|
393
|
+
(item) => item.id === instance.id,
|
|
394
|
+
);
|
|
395
|
+
const formulaIndex = definition.definition.formulas.findIndex(
|
|
396
|
+
(item) => item.id === formula.id,
|
|
397
|
+
);
|
|
398
|
+
const measureStates: Record<string, FinalizedDiagnosticMeasure> =
|
|
399
|
+
Object.create(null);
|
|
400
|
+
for (const [measureId, stats] of Object.entries(components)) {
|
|
401
|
+
const measure = internals.measuresById.get(measureId)!;
|
|
402
|
+
const readiness = [
|
|
403
|
+
...(stats.missing > 0
|
|
404
|
+
? [stats.imputedZero > 0 ? ("imputed" as const) : ("missing" as const)]
|
|
405
|
+
: []),
|
|
406
|
+
...(stats.nonFinite > 0 ? ["non-finite" as const] : []),
|
|
407
|
+
...(stats.structural > 0 ? ["structural-ambiguity" as const] : []),
|
|
408
|
+
...(stats.sum === null && stats.nonFinite === 0 && stats.structural === 0
|
|
409
|
+
? ["aggregation-overflow" as const]
|
|
410
|
+
: []),
|
|
411
|
+
];
|
|
412
|
+
measureStates[measureId] = {
|
|
413
|
+
quantity: quantity(measure, stats.value),
|
|
414
|
+
stats,
|
|
415
|
+
readiness,
|
|
416
|
+
sources: sourcesByMeasure[measureId] ?? [],
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
const expressionSources = (
|
|
420
|
+
expression: import("./diagnosticExpressions.js").DiagnosticMeasureExpression,
|
|
421
|
+
) =>
|
|
422
|
+
unionSources(
|
|
423
|
+
expressionMeasureIds(expression).flatMap(
|
|
424
|
+
(measureId) => sourcesByMeasure[measureId] ?? [],
|
|
425
|
+
),
|
|
426
|
+
);
|
|
427
|
+
const withOverflowSources = <
|
|
428
|
+
T extends {
|
|
429
|
+
readonly value: number | null;
|
|
430
|
+
readonly reasons: readonly (typeof READINESS_ORDER)[number][];
|
|
431
|
+
readonly overflows: readonly import("./diagnosticRules.js").DiagnosticExpressionOverflow[];
|
|
432
|
+
},
|
|
433
|
+
>(
|
|
434
|
+
result: T,
|
|
435
|
+
sources: readonly import("./diagnosticDefinitions.js").DiagnosticSourceLocation[],
|
|
436
|
+
) => ({
|
|
437
|
+
...result,
|
|
438
|
+
overflows: result.overflows.map((overflow) => ({
|
|
439
|
+
...overflow,
|
|
440
|
+
sources: overflow.sources.length > 0 ? overflow.sources : sources,
|
|
441
|
+
})),
|
|
442
|
+
});
|
|
443
|
+
const bindings = Object.fromEntries(
|
|
444
|
+
Object.entries(instance.bindings).map(([role, expression]) => {
|
|
445
|
+
const sources = expressionSources(expression);
|
|
446
|
+
const path = pointer(
|
|
447
|
+
pointer(pointer(pointer("", "instances"), instanceIndex), "bindings"),
|
|
448
|
+
role,
|
|
449
|
+
);
|
|
450
|
+
return [
|
|
451
|
+
role,
|
|
452
|
+
withOverflowSources(
|
|
453
|
+
evaluateDiagnosticMeasureExpression(expression, measureStates, path),
|
|
454
|
+
sources,
|
|
455
|
+
),
|
|
456
|
+
];
|
|
457
|
+
}),
|
|
458
|
+
);
|
|
459
|
+
const calculationSources = unionSources(
|
|
460
|
+
(
|
|
461
|
+
internals.calculationDependenciesByInstanceId.get(instance.id) ?? []
|
|
462
|
+
).flatMap((measureId) => sourcesByMeasure[measureId] ?? []),
|
|
463
|
+
);
|
|
464
|
+
const roleExpressionSources = (
|
|
465
|
+
expression: import("./diagnosticExpressions.js").DiagnosticRoleExpression,
|
|
466
|
+
): import("./diagnosticDefinitions.js").DiagnosticSourceLocation[] => {
|
|
467
|
+
if (expression.op === "role")
|
|
468
|
+
return expressionSources(instance.bindings[expression.role]!);
|
|
469
|
+
return unionSources(
|
|
470
|
+
(expression.op === "add"
|
|
471
|
+
? expression.terms
|
|
472
|
+
: [expression.left, expression.right]
|
|
473
|
+
).flatMap(roleExpressionSources),
|
|
474
|
+
);
|
|
475
|
+
};
|
|
476
|
+
const numeratorSources = roleExpressionSources(formula.numerator);
|
|
477
|
+
const denominatorSources = roleExpressionSources(formula.denominator);
|
|
478
|
+
const formulaPath = pointer(pointer("", "formulas"), formulaIndex);
|
|
479
|
+
const numeratorResult = withOverflowSources(
|
|
480
|
+
evaluateDiagnosticRoleExpression(
|
|
481
|
+
formula.numerator,
|
|
482
|
+
bindings,
|
|
483
|
+
pointer(formulaPath, "numerator"),
|
|
484
|
+
),
|
|
485
|
+
numeratorSources,
|
|
486
|
+
);
|
|
487
|
+
const denominatorResult = withOverflowSources(
|
|
488
|
+
evaluateDiagnosticRoleExpression(
|
|
489
|
+
formula.denominator,
|
|
490
|
+
bindings,
|
|
491
|
+
pointer(formulaPath, "denominator"),
|
|
492
|
+
),
|
|
493
|
+
denominatorSources,
|
|
494
|
+
);
|
|
495
|
+
const raw = diagnosticRawRatio(
|
|
496
|
+
numeratorResult.value,
|
|
497
|
+
denominatorResult.value,
|
|
498
|
+
);
|
|
499
|
+
const presented = applyDiagnosticPresentation(raw, instance.presentation);
|
|
500
|
+
const numeratorMeasure = inferRoleExpressionMeasure(
|
|
501
|
+
formula.numerator,
|
|
502
|
+
instance.bindings,
|
|
503
|
+
internals.measuresById,
|
|
504
|
+
);
|
|
505
|
+
const denominatorMeasure = inferRoleExpressionMeasure(
|
|
506
|
+
formula.denominator,
|
|
507
|
+
instance.bindings,
|
|
508
|
+
internals.measuresById,
|
|
509
|
+
);
|
|
510
|
+
const ruleEvaluations: DiagnosticRuleEvaluation[] = [];
|
|
511
|
+
const deps =
|
|
512
|
+
internals.evaluationDependenciesByInstanceId.get(instance.id) ?? [];
|
|
513
|
+
const findingContext = {
|
|
514
|
+
instanceId: instance.id,
|
|
515
|
+
...context,
|
|
516
|
+
sources: calculationSources,
|
|
517
|
+
};
|
|
518
|
+
const findings: DiagnosticMetricFinding[] = mergeFindings([
|
|
519
|
+
...preparedFindings.filter(
|
|
520
|
+
(finding) =>
|
|
521
|
+
finding.category !== "structural" &&
|
|
522
|
+
(finding.measureId === undefined || deps.includes(finding.measureId)),
|
|
523
|
+
),
|
|
524
|
+
...deps.flatMap((measureId) => blockerFindingsByMeasure[measureId] ?? []),
|
|
525
|
+
]);
|
|
526
|
+
for (const overflow of [
|
|
527
|
+
...numeratorResult.overflows,
|
|
528
|
+
...denominatorResult.overflows,
|
|
529
|
+
])
|
|
530
|
+
findings.push({
|
|
531
|
+
code: "diagnostic-expression-overflow",
|
|
532
|
+
message: "Measure expression overflowed",
|
|
533
|
+
severity: "fail",
|
|
534
|
+
category: "aggregation",
|
|
535
|
+
expressionPath: overflow.expressionPath,
|
|
536
|
+
...findingContext,
|
|
537
|
+
sources: overflow.sources,
|
|
538
|
+
});
|
|
539
|
+
if (numeratorResult.value === null)
|
|
540
|
+
findings.push({
|
|
541
|
+
code: "diagnostic-numerator-unavailable",
|
|
542
|
+
message: "Calculation numerator is unavailable",
|
|
543
|
+
severity: "warning",
|
|
544
|
+
category: "calculation",
|
|
545
|
+
...findingContext,
|
|
546
|
+
});
|
|
547
|
+
if (denominatorResult.value === null)
|
|
548
|
+
findings.push({
|
|
549
|
+
code: "diagnostic-denominator-unavailable",
|
|
550
|
+
message: "Calculation denominator is unavailable",
|
|
551
|
+
severity: "warning",
|
|
552
|
+
category: "calculation",
|
|
553
|
+
...findingContext,
|
|
554
|
+
});
|
|
555
|
+
else if (denominatorResult.value <= 0)
|
|
556
|
+
findings.push({
|
|
557
|
+
code: "diagnostic-denominator-not-positive",
|
|
558
|
+
message: "Calculation denominator is not strictly positive",
|
|
559
|
+
severity: "warning",
|
|
560
|
+
category: "calculation",
|
|
561
|
+
...findingContext,
|
|
562
|
+
});
|
|
563
|
+
else if (numeratorResult.value !== null && raw === null)
|
|
564
|
+
findings.push({
|
|
565
|
+
code: "diagnostic-calculation-overflow",
|
|
566
|
+
message: "Calculation result overflowed",
|
|
567
|
+
severity: "fail",
|
|
568
|
+
category: "calculation",
|
|
569
|
+
...findingContext,
|
|
570
|
+
});
|
|
571
|
+
if (presented.finding)
|
|
572
|
+
findings.push({ ...presented.finding, ...findingContext });
|
|
573
|
+
type Operand = {
|
|
574
|
+
readonly value: number | null;
|
|
575
|
+
readonly reasons: readonly (typeof READINESS_ORDER)[number][];
|
|
576
|
+
readonly overflows: readonly import("./diagnosticRules.js").DiagnosticExpressionOverflow[];
|
|
577
|
+
readonly sources: readonly import("./diagnosticDefinitions.js").DiagnosticSourceLocation[];
|
|
578
|
+
};
|
|
579
|
+
const operand = (
|
|
580
|
+
item: (typeof instance.rules)[number]["when"]["left"],
|
|
581
|
+
path: string,
|
|
582
|
+
): Operand => {
|
|
583
|
+
if (item.source === "constant")
|
|
584
|
+
return { value: item.value, reasons: [], overflows: [], sources: [] };
|
|
585
|
+
if (item.source === "calculation") {
|
|
586
|
+
const result =
|
|
587
|
+
item.field === "numerator" ? numeratorResult : denominatorResult;
|
|
588
|
+
return {
|
|
589
|
+
...result,
|
|
590
|
+
sources:
|
|
591
|
+
item.field === "numerator" ? numeratorSources : denominatorSources,
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
const sources = expressionSources(item.expression);
|
|
595
|
+
return {
|
|
596
|
+
...withOverflowSources(
|
|
597
|
+
evaluateDiagnosticMeasureExpression(
|
|
598
|
+
item.expression,
|
|
599
|
+
measureStates,
|
|
600
|
+
pointer(path, "expression"),
|
|
601
|
+
),
|
|
602
|
+
sources,
|
|
603
|
+
),
|
|
604
|
+
sources,
|
|
605
|
+
};
|
|
606
|
+
};
|
|
607
|
+
for (const [ruleIndex, rule] of instance.rules.entries()) {
|
|
608
|
+
const rulePath = pointer(
|
|
609
|
+
pointer(
|
|
610
|
+
pointer(pointer(pointer("", "instances"), instanceIndex), "rules"),
|
|
611
|
+
ruleIndex,
|
|
612
|
+
),
|
|
613
|
+
"when",
|
|
614
|
+
);
|
|
615
|
+
const left = operand(rule.when.left, pointer(rulePath, "left"));
|
|
616
|
+
const right = operand(rule.when.right, pointer(rulePath, "right"));
|
|
617
|
+
const ruleSources = unionSources([...left.sources, ...right.sources]);
|
|
618
|
+
const expressionOverflows = [
|
|
619
|
+
...new Map(
|
|
620
|
+
[...left.overflows, ...right.overflows].map((overflow) => [
|
|
621
|
+
canonicalJson(overflow),
|
|
622
|
+
overflow,
|
|
623
|
+
]),
|
|
624
|
+
).entries(),
|
|
625
|
+
]
|
|
626
|
+
.sort(([a], [b]) => codeUnit(a, b))
|
|
627
|
+
.map(([, overflow]) => overflow);
|
|
628
|
+
const reasons = readiness([
|
|
629
|
+
...left.reasons,
|
|
630
|
+
...right.reasons,
|
|
631
|
+
...(expressionOverflows.length > 0
|
|
632
|
+
? ["expression-overflow" as const]
|
|
633
|
+
: []),
|
|
634
|
+
]);
|
|
635
|
+
const classified =
|
|
636
|
+
reasons.length > 0
|
|
637
|
+
? null
|
|
638
|
+
: classifyDiagnosticComparison(
|
|
639
|
+
left.value,
|
|
640
|
+
right.value,
|
|
641
|
+
rule.when.tolerance,
|
|
642
|
+
);
|
|
643
|
+
if (classified === null || classified.status === "not-evaluated") {
|
|
644
|
+
const notEvaluatedReasons = readiness([
|
|
645
|
+
...reasons,
|
|
646
|
+
...(classified?.status === "not-evaluated" ? [classified.reason] : []),
|
|
647
|
+
...((left.value === null || right.value === null) &&
|
|
648
|
+
reasons.length === 0
|
|
649
|
+
? ["missing" as const]
|
|
650
|
+
: []),
|
|
651
|
+
]);
|
|
652
|
+
ruleEvaluations.push({
|
|
653
|
+
ruleId: rule.id,
|
|
654
|
+
status: "not-evaluated",
|
|
655
|
+
severity: rule.severity,
|
|
656
|
+
left: left.value,
|
|
657
|
+
right: right.value,
|
|
658
|
+
relation: null,
|
|
659
|
+
notEvaluatedReasons,
|
|
660
|
+
expressionOverflows,
|
|
661
|
+
code: "diagnostic-rule-not-evaluated",
|
|
662
|
+
message: "Diagnostic metric rule was not evaluated",
|
|
663
|
+
});
|
|
664
|
+
for (const overflow of expressionOverflows)
|
|
665
|
+
findings.push({
|
|
666
|
+
code: "diagnostic-expression-overflow",
|
|
667
|
+
message: "Measure expression overflowed",
|
|
668
|
+
severity: "fail",
|
|
669
|
+
category: "aggregation",
|
|
670
|
+
ruleId: rule.id,
|
|
671
|
+
expressionPath: overflow.expressionPath,
|
|
672
|
+
...findingContext,
|
|
673
|
+
sources: overflow.sources,
|
|
674
|
+
});
|
|
675
|
+
findings.push({
|
|
676
|
+
code: "diagnostic-rule-not-evaluated",
|
|
677
|
+
message: "Diagnostic metric rule was not evaluated",
|
|
678
|
+
severity: "info",
|
|
679
|
+
category: "rule",
|
|
680
|
+
ruleId: rule.id,
|
|
681
|
+
...findingContext,
|
|
682
|
+
sources: ruleSources,
|
|
683
|
+
});
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
const triggered = diagnosticPredicateMatches(
|
|
687
|
+
rule.when.operator,
|
|
688
|
+
classified.relation,
|
|
689
|
+
);
|
|
690
|
+
ruleEvaluations.push({
|
|
691
|
+
ruleId: rule.id,
|
|
692
|
+
status: triggered ? "triggered" : "pass",
|
|
693
|
+
severity: rule.severity,
|
|
694
|
+
left: left.value,
|
|
695
|
+
right: right.value,
|
|
696
|
+
relation: classified.relation,
|
|
697
|
+
notEvaluatedReasons: [],
|
|
698
|
+
expressionOverflows: [],
|
|
699
|
+
code: triggered ? rule.code : null,
|
|
700
|
+
message: triggered ? rule.message : null,
|
|
701
|
+
});
|
|
702
|
+
if (triggered)
|
|
703
|
+
findings.push({
|
|
704
|
+
code: rule.code,
|
|
705
|
+
message: rule.message,
|
|
706
|
+
severity: rule.severity,
|
|
707
|
+
category: "rule",
|
|
708
|
+
ruleId: rule.id,
|
|
709
|
+
...findingContext,
|
|
710
|
+
sources: ruleSources,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
const refs = {
|
|
714
|
+
amountBasisIds: [
|
|
715
|
+
...new Set(
|
|
716
|
+
deps.flatMap((id) =>
|
|
717
|
+
internals.measuresById.get(id)?.basisId
|
|
718
|
+
? [internals.measuresById.get(id)!.basisId!]
|
|
719
|
+
: [],
|
|
720
|
+
),
|
|
721
|
+
),
|
|
722
|
+
].sort(codeUnit),
|
|
723
|
+
countPopulationIds: [
|
|
724
|
+
...new Set(
|
|
725
|
+
deps.flatMap((id) =>
|
|
726
|
+
internals.measuresById.get(id)?.countPopulationId
|
|
727
|
+
? [internals.measuresById.get(id)!.countPopulationId!]
|
|
728
|
+
: [],
|
|
729
|
+
),
|
|
730
|
+
),
|
|
731
|
+
].sort(codeUnit),
|
|
732
|
+
exposureBasisIds: [
|
|
733
|
+
...new Set(
|
|
734
|
+
deps.flatMap((id) =>
|
|
735
|
+
internals.measuresById.get(id)?.exposureBasisId
|
|
736
|
+
? [internals.measuresById.get(id)!.exposureBasisId!]
|
|
737
|
+
: [],
|
|
738
|
+
),
|
|
739
|
+
),
|
|
740
|
+
].sort(codeUnit),
|
|
741
|
+
};
|
|
742
|
+
return deepFreeze({
|
|
743
|
+
instanceId: instance.id,
|
|
744
|
+
instanceVersion: instance.version,
|
|
745
|
+
formulaId: formula.id,
|
|
746
|
+
formulaVersion: formula.version,
|
|
747
|
+
semanticReferences: refs,
|
|
748
|
+
formulaFingerprint: definition.formulaFingerprints[formula.id]!,
|
|
749
|
+
calculationFingerprint: definition.calculationFingerprints[instance.id]!,
|
|
750
|
+
definitionIntegrity: definition.definitionIntegrity,
|
|
751
|
+
calculation: {
|
|
752
|
+
numerator: quantity(numeratorMeasure, numeratorResult.value),
|
|
753
|
+
denominator: quantity(denominatorMeasure, denominatorResult.value),
|
|
754
|
+
value: raw,
|
|
755
|
+
},
|
|
756
|
+
presentation: { ...instance.presentation, value: presented.value },
|
|
757
|
+
components: Object.fromEntries(deps.map((id) => [id, components[id]!])),
|
|
758
|
+
rules: ruleEvaluations,
|
|
759
|
+
findings: mergeFindings(findings),
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
export function runMetricDiagnostics(
|
|
764
|
+
input: RunMetricDiagnosticsInput,
|
|
765
|
+
): DiagnosticDeepReadonly<MetricDiagnosticsResult> {
|
|
766
|
+
validateDiagnosticGroupingConfiguration(input);
|
|
767
|
+
const prepared = input.prepared;
|
|
768
|
+
const buckets = new Map<string, typeof prepared.cells>();
|
|
769
|
+
for (const cell of prepared.cells) {
|
|
770
|
+
const group =
|
|
771
|
+
input.groupMap !== undefined &&
|
|
772
|
+
hasDiagnosticOwn(input.groupMap, cell.sourceGroup)
|
|
773
|
+
? input.groupMap[cell.sourceGroup]!
|
|
774
|
+
: cell.sourceGroup;
|
|
775
|
+
if (
|
|
776
|
+
prepared.filter?.outputGroups !== undefined &&
|
|
777
|
+
!prepared.filter.outputGroups.includes(group)
|
|
778
|
+
)
|
|
779
|
+
continue;
|
|
780
|
+
const key = canonicalJson([group, cell.origin, cell.valuation]);
|
|
781
|
+
buckets.set(key, [...(buckets.get(key) ?? []), cell]);
|
|
782
|
+
}
|
|
783
|
+
const selectedInstances = prepared.definition.definition.instances.filter(
|
|
784
|
+
(instance) =>
|
|
785
|
+
prepared.filter?.instanceIds === undefined ||
|
|
786
|
+
prepared.filter.instanceIds.includes(instance.id),
|
|
787
|
+
);
|
|
788
|
+
const emergence: DiagnosticEmergencePoint[] = [];
|
|
789
|
+
for (const [key, cells] of buckets) {
|
|
790
|
+
const [group, origin, valuation] = JSON.parse(key) as [
|
|
791
|
+
string,
|
|
792
|
+
string,
|
|
793
|
+
string,
|
|
794
|
+
];
|
|
795
|
+
const components = Object.fromEntries(
|
|
796
|
+
prepared.definition.definition.measures.map((measure) => [
|
|
797
|
+
measure.id,
|
|
798
|
+
mergeStats(cells, measure),
|
|
799
|
+
]),
|
|
800
|
+
);
|
|
801
|
+
const sourcesByMeasure = Object.fromEntries(
|
|
802
|
+
prepared.definition.definition.measures.map((measure) => [
|
|
803
|
+
measure.id,
|
|
804
|
+
unionSources(
|
|
805
|
+
cells.flatMap((cell) => [
|
|
806
|
+
...(cell.contributions[measure.id] ?? []).flatMap(
|
|
807
|
+
(item) => item.sources,
|
|
808
|
+
),
|
|
809
|
+
...(cell.structuralBlockers[measure.id] ?? []).flatMap(
|
|
810
|
+
(item) => item.sources,
|
|
811
|
+
),
|
|
812
|
+
]),
|
|
813
|
+
),
|
|
814
|
+
]),
|
|
815
|
+
);
|
|
816
|
+
const context = {
|
|
817
|
+
group,
|
|
818
|
+
origin,
|
|
819
|
+
valuation,
|
|
820
|
+
developmentAge: cells[0]!.developmentAge,
|
|
821
|
+
ageUnit: cells[0]!.ageUnit,
|
|
822
|
+
};
|
|
823
|
+
const mappedOverflowFindings =
|
|
824
|
+
prepared.definition.definition.measures.flatMap(
|
|
825
|
+
(measure): DiagnosticMetricFinding[] => {
|
|
826
|
+
const stats = components[measure.id]!;
|
|
827
|
+
return stats.sum === null &&
|
|
828
|
+
stats.nonFinite === 0 &&
|
|
829
|
+
stats.structural === 0
|
|
830
|
+
? [
|
|
831
|
+
{
|
|
832
|
+
code: "diagnostic-measure-overflow",
|
|
833
|
+
message: "Measure aggregation overflowed",
|
|
834
|
+
severity: "fail",
|
|
835
|
+
category: "aggregation",
|
|
836
|
+
measureId: measure.id,
|
|
837
|
+
...context,
|
|
838
|
+
sources: sourcesByMeasure[measure.id] ?? [],
|
|
839
|
+
},
|
|
840
|
+
]
|
|
841
|
+
: [];
|
|
842
|
+
},
|
|
843
|
+
);
|
|
844
|
+
const mappedDeduplicationFindings =
|
|
845
|
+
prepared.definition.definition.measures.flatMap(
|
|
846
|
+
(measure): DiagnosticMetricFinding[] => {
|
|
847
|
+
const stats = components[measure.id]!;
|
|
848
|
+
return stats.deduplicated > 0
|
|
849
|
+
? [
|
|
850
|
+
{
|
|
851
|
+
code: "diagnostic-exposure-deduplicated",
|
|
852
|
+
message: `${stats.deduplicated} repeated exposure observation(s) were counted once by stable key`,
|
|
853
|
+
severity: "info",
|
|
854
|
+
category: "aggregation",
|
|
855
|
+
measureId: measure.id,
|
|
856
|
+
...context,
|
|
857
|
+
sources: sourcesByMeasure[measure.id] ?? [],
|
|
858
|
+
},
|
|
859
|
+
]
|
|
860
|
+
: [];
|
|
861
|
+
},
|
|
862
|
+
);
|
|
863
|
+
const mappedFindings = mergeFindings([
|
|
864
|
+
...cells.flatMap((cell) =>
|
|
865
|
+
cell.findings.map((finding) => ({ ...finding, group })),
|
|
866
|
+
),
|
|
867
|
+
...mappedOverflowFindings,
|
|
868
|
+
...mappedDeduplicationFindings,
|
|
869
|
+
]);
|
|
870
|
+
const blockerFindingsByMeasure = Object.fromEntries(
|
|
871
|
+
prepared.definition.definition.measures.map((measure) => [
|
|
872
|
+
measure.id,
|
|
873
|
+
mergeFindings(
|
|
874
|
+
cells.flatMap((cell) =>
|
|
875
|
+
(cell.structuralBlockers[measure.id] ?? []).flatMap((blocker) =>
|
|
876
|
+
blocker.finding ? [{ ...blocker.finding, group }] : [],
|
|
877
|
+
),
|
|
878
|
+
),
|
|
879
|
+
),
|
|
880
|
+
]),
|
|
881
|
+
);
|
|
882
|
+
const metrics = Object.fromEntries(
|
|
883
|
+
selectedInstances.map((instance) => [
|
|
884
|
+
instance.id,
|
|
885
|
+
evaluatePoint(
|
|
886
|
+
prepared,
|
|
887
|
+
components,
|
|
888
|
+
instance.id,
|
|
889
|
+
context,
|
|
890
|
+
mappedFindings,
|
|
891
|
+
blockerFindingsByMeasure,
|
|
892
|
+
sourcesByMeasure,
|
|
893
|
+
),
|
|
894
|
+
]),
|
|
895
|
+
);
|
|
896
|
+
emergence.push({
|
|
897
|
+
group,
|
|
898
|
+
sourceGroups: [...new Set(cells.map((cell) => cell.sourceGroup))].sort(
|
|
899
|
+
codeUnit,
|
|
900
|
+
),
|
|
901
|
+
...(input.groupDimensions &&
|
|
902
|
+
Object.prototype.hasOwnProperty.call(input.groupDimensions, group)
|
|
903
|
+
? { dimensions: input.groupDimensions[group] }
|
|
904
|
+
: {}),
|
|
905
|
+
origin,
|
|
906
|
+
valuation,
|
|
907
|
+
developmentAge: cells[0]!.developmentAge,
|
|
908
|
+
ageUnit: cells[0]!.ageUnit,
|
|
909
|
+
components,
|
|
910
|
+
metrics,
|
|
911
|
+
findings: mergeFindings([
|
|
912
|
+
...mappedFindings,
|
|
913
|
+
...Object.values(metrics).flatMap((metric) => metric.findings),
|
|
914
|
+
]),
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
const periodCoordinate = (side: "origin" | "valuation", label: string) =>
|
|
918
|
+
normalizeDiagnosticPeriod(prepared.definition, side, label).coordinate;
|
|
919
|
+
emergence.sort(
|
|
920
|
+
(a, b) =>
|
|
921
|
+
codeUnit(a.group, b.group) ||
|
|
922
|
+
periodCoordinate("origin", a.origin) -
|
|
923
|
+
periodCoordinate("origin", b.origin) ||
|
|
924
|
+
periodCoordinate("valuation", a.valuation) -
|
|
925
|
+
periodCoordinate("valuation", b.valuation) ||
|
|
926
|
+
codeUnit(a.origin, b.origin) ||
|
|
927
|
+
codeUnit(a.valuation, b.valuation),
|
|
928
|
+
);
|
|
929
|
+
const groups = [...new Set(emergence.map((point) => point.group))].sort(
|
|
930
|
+
codeUnit,
|
|
931
|
+
);
|
|
932
|
+
const triangles: DiagnosticMetricTriangle[] = [];
|
|
933
|
+
for (const group of groups)
|
|
934
|
+
for (const instance of selectedInstances) {
|
|
935
|
+
const points = emergence.filter((point) => point.group === group);
|
|
936
|
+
const origins = [...new Set(points.map((point) => point.origin))].sort(
|
|
937
|
+
(a, b) =>
|
|
938
|
+
periodCoordinate("origin", a) - periodCoordinate("origin", b) ||
|
|
939
|
+
codeUnit(a, b),
|
|
940
|
+
);
|
|
941
|
+
const ages = [
|
|
942
|
+
...new Set(points.map((point) => point.developmentAge)),
|
|
943
|
+
].sort((a, b) => a - b);
|
|
944
|
+
const cells = origins.map((origin) =>
|
|
945
|
+
ages.map((age) => {
|
|
946
|
+
const point = points.find(
|
|
947
|
+
(item) => item.origin === origin && item.developmentAge === age,
|
|
948
|
+
);
|
|
949
|
+
return point
|
|
950
|
+
? {
|
|
951
|
+
origin: point.origin,
|
|
952
|
+
valuation: point.valuation,
|
|
953
|
+
developmentAge: point.developmentAge,
|
|
954
|
+
ageUnit: point.ageUnit,
|
|
955
|
+
evaluation: point.metrics[instance.id]!,
|
|
956
|
+
}
|
|
957
|
+
: null;
|
|
958
|
+
}),
|
|
959
|
+
);
|
|
960
|
+
triangles.push({
|
|
961
|
+
group,
|
|
962
|
+
instanceId: instance.id,
|
|
963
|
+
origins,
|
|
964
|
+
developmentAges: ages,
|
|
965
|
+
ageUnit: prepared.definition.definition.periodAxis.ageUnit,
|
|
966
|
+
calculationValues: cells.map((row) =>
|
|
967
|
+
row.map((cell) => cell?.evaluation.calculation.value ?? null),
|
|
968
|
+
),
|
|
969
|
+
presentationValues: cells.map((row) =>
|
|
970
|
+
row.map((cell) => cell?.evaluation.presentation.value ?? null),
|
|
971
|
+
),
|
|
972
|
+
cells,
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
const latestDiagonal = groups.flatMap((group) => {
|
|
976
|
+
const groupPoints = emergence.filter((point) => point.group === group);
|
|
977
|
+
return [...new Set(groupPoints.map((point) => point.origin))].flatMap(
|
|
978
|
+
(origin) => {
|
|
979
|
+
const points = groupPoints.filter((point) => point.origin === origin);
|
|
980
|
+
return points.length
|
|
981
|
+
? [
|
|
982
|
+
points.reduce((latest, point) =>
|
|
983
|
+
point.developmentAge > latest.developmentAge ? point : latest,
|
|
984
|
+
),
|
|
985
|
+
]
|
|
986
|
+
: [];
|
|
987
|
+
},
|
|
988
|
+
);
|
|
989
|
+
});
|
|
990
|
+
const attachedPreparationFindings = new Set(
|
|
991
|
+
prepared.cells
|
|
992
|
+
.flatMap((cell) => cell.findings)
|
|
993
|
+
.map((finding) => canonicalJson(finding)),
|
|
994
|
+
);
|
|
995
|
+
const unattachedPreparationFindings = prepared.findings.filter(
|
|
996
|
+
(finding) => !attachedPreparationFindings.has(canonicalJson(finding)),
|
|
997
|
+
);
|
|
998
|
+
return deepFreeze({
|
|
999
|
+
definitionIntegrity: prepared.definition.definitionIntegrity,
|
|
1000
|
+
preparationFingerprint: prepared.preparationFingerprint,
|
|
1001
|
+
ageUnit: prepared.definition.definition.periodAxis.ageUnit,
|
|
1002
|
+
emergence,
|
|
1003
|
+
triangles,
|
|
1004
|
+
latestDiagonal,
|
|
1005
|
+
findings: mergeFindings([
|
|
1006
|
+
...unattachedPreparationFindings,
|
|
1007
|
+
...emergence.flatMap((point) => point.findings),
|
|
1008
|
+
]),
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
function viewGroups(
|
|
1013
|
+
result: DiagnosticDeepReadonly<MetricDiagnosticsResult>,
|
|
1014
|
+
outputGroups: readonly string[],
|
|
1015
|
+
path: string,
|
|
1016
|
+
): readonly string[] {
|
|
1017
|
+
if (!Array.isArray(outputGroups))
|
|
1018
|
+
throw new DiagnosticValidationError([
|
|
1019
|
+
{
|
|
1020
|
+
domain: "view",
|
|
1021
|
+
code: "invalid-type",
|
|
1022
|
+
path,
|
|
1023
|
+
message: "Output groups must be an array",
|
|
1024
|
+
},
|
|
1025
|
+
]);
|
|
1026
|
+
const produced = new Set(result.emergence.map((point) => point.group));
|
|
1027
|
+
const issues: DiagnosticValidationIssue[] = [];
|
|
1028
|
+
outputGroups.forEach((group, index) => {
|
|
1029
|
+
const itemPath = `${path}[${index}]`;
|
|
1030
|
+
if (!isDiagnosticToken(group))
|
|
1031
|
+
issues.push({
|
|
1032
|
+
domain: "view",
|
|
1033
|
+
code: "invalid-string",
|
|
1034
|
+
path: itemPath,
|
|
1035
|
+
message: "Output group must be a nonempty token",
|
|
1036
|
+
});
|
|
1037
|
+
else if (!produced.has(group))
|
|
1038
|
+
issues.push({
|
|
1039
|
+
domain: "view",
|
|
1040
|
+
code: "unknown-reference",
|
|
1041
|
+
path: itemPath,
|
|
1042
|
+
message: `Unknown output group ${group}`,
|
|
1043
|
+
});
|
|
1044
|
+
});
|
|
1045
|
+
if (issues.length > 0) throw new DiagnosticValidationError(issues);
|
|
1046
|
+
return [...new Set(outputGroups)].sort(codeUnit);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
export function sameMaturity(
|
|
1050
|
+
result: DiagnosticDeepReadonly<MetricDiagnosticsResult>,
|
|
1051
|
+
developmentAge: number,
|
|
1052
|
+
outputGroups?: readonly string[],
|
|
1053
|
+
): readonly DiagnosticDeepReadonly<DiagnosticEmergencePoint>[] {
|
|
1054
|
+
if (!Number.isSafeInteger(developmentAge) || developmentAge < 0)
|
|
1055
|
+
throw new DiagnosticValidationError([
|
|
1056
|
+
{
|
|
1057
|
+
domain: "view",
|
|
1058
|
+
code: "invalid-number",
|
|
1059
|
+
path: "$.developmentAge",
|
|
1060
|
+
message: "Development age must be a nonnegative safe integer",
|
|
1061
|
+
},
|
|
1062
|
+
]);
|
|
1063
|
+
const groups =
|
|
1064
|
+
outputGroups === undefined
|
|
1065
|
+
? null
|
|
1066
|
+
: new Set(viewGroups(result, outputGroups, "$.outputGroups"));
|
|
1067
|
+
return result.emergence.filter(
|
|
1068
|
+
(point) =>
|
|
1069
|
+
point.developmentAge === developmentAge &&
|
|
1070
|
+
(groups === null || groups.has(point.group)),
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
export function commonMaturity(
|
|
1075
|
+
result: DiagnosticDeepReadonly<MetricDiagnosticsResult>,
|
|
1076
|
+
outputGroups: readonly string[],
|
|
1077
|
+
): DiagnosticDeepReadonly<CommonMaturityResult> {
|
|
1078
|
+
const groups = viewGroups(result, outputGroups, "$.outputGroups");
|
|
1079
|
+
if (groups.length === 0)
|
|
1080
|
+
return deepFreeze({
|
|
1081
|
+
developmentAge: null,
|
|
1082
|
+
ageUnit: result.ageUnit,
|
|
1083
|
+
points: [],
|
|
1084
|
+
});
|
|
1085
|
+
const common = [
|
|
1086
|
+
...new Set(
|
|
1087
|
+
result.emergence
|
|
1088
|
+
.filter((point) => point.group === groups[0])
|
|
1089
|
+
.map((point) => point.developmentAge),
|
|
1090
|
+
),
|
|
1091
|
+
]
|
|
1092
|
+
.filter((age) =>
|
|
1093
|
+
groups.every((group) =>
|
|
1094
|
+
result.emergence.some(
|
|
1095
|
+
(point) => point.group === group && point.developmentAge === age,
|
|
1096
|
+
),
|
|
1097
|
+
),
|
|
1098
|
+
)
|
|
1099
|
+
.sort((left, right) => right - left);
|
|
1100
|
+
const developmentAge = common[0] ?? null;
|
|
1101
|
+
return deepFreeze({
|
|
1102
|
+
developmentAge,
|
|
1103
|
+
ageUnit: result.ageUnit,
|
|
1104
|
+
points:
|
|
1105
|
+
developmentAge === null
|
|
1106
|
+
? []
|
|
1107
|
+
: sameMaturity(result, developmentAge, groups),
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
export function getMetricDiagnosticsResultIdentity(
|
|
1111
|
+
result: DiagnosticDeepReadonly<MetricDiagnosticsResult>,
|
|
1112
|
+
): NormalizedDiagnosticResultIdentity {
|
|
1113
|
+
return projectDiagnosticIdentity(result);
|
|
1114
|
+
}
|