mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
212 lines
6.6 KiB
TypeScript
212 lines
6.6 KiB
TypeScript
import { viewParentIdentity } from "./viewParentIdentity.js";
|
|
import { analysisViewPageFields } from "./analysisViewPageFields.js";
|
|
import { z } from "zod";
|
|
import type { StaticObservedEndpointComparison } from "../javascript/staticObservedEndpointSchemas.js";
|
|
import {
|
|
comparedStaticEndpointSchema,
|
|
comparedObservedGroupSchema,
|
|
endpointMatchSchema,
|
|
staticObservedEndpointComparisonSchema,
|
|
} from "../javascript/staticObservedEndpointSchemas.js";
|
|
import {
|
|
createAnalysisViewResultReader,
|
|
type AnalysisViewParent,
|
|
} from "./analysisViewParent.js";
|
|
import {
|
|
ownedView,
|
|
type AdmittedView,
|
|
type SummaryAnalysisView,
|
|
} from "./analysisViewShape.js";
|
|
import {
|
|
boundedViewLimitations,
|
|
completeWithinViewCoverage,
|
|
selectedViewPage,
|
|
} from "./analysisViewCoverage.js";
|
|
import { pickFields, selectViewRows, filterSet } from "./selectViewRows.js";
|
|
import { invalidAnalysisView } from "./analysisViewErrors.js";
|
|
import { ok } from "../result.js";
|
|
|
|
const page = analysisViewPageFields("endpoint-comparison");
|
|
export const endpointComparisonViewRequestSchema = z.discriminatedUnion(
|
|
"collection",
|
|
[
|
|
z.strictObject({
|
|
...page,
|
|
collection: z.literal("static-endpoints"),
|
|
endpoint_ids: z
|
|
.array(comparedStaticEndpointSchema.shape.endpoint_id)
|
|
.optional(),
|
|
statuses: z.array(comparedStaticEndpointSchema.shape.status).optional(),
|
|
fields: z.array(comparedStaticEndpointSchema.keyof()).optional(),
|
|
}),
|
|
z.strictObject({
|
|
...page,
|
|
collection: z.literal("observed-groups"),
|
|
group_ids: z.array(comparedObservedGroupSchema.shape.group_id).optional(),
|
|
statuses: z.array(comparedObservedGroupSchema.shape.status).optional(),
|
|
fields: z.array(comparedObservedGroupSchema.keyof()).optional(),
|
|
}),
|
|
z.strictObject({
|
|
...page,
|
|
collection: z.literal("matches"),
|
|
endpoint_ids: z.array(endpointMatchSchema.shape.endpoint_id).optional(),
|
|
group_ids: z.array(endpointMatchSchema.shape.group_id).optional(),
|
|
fields: z.array(endpointMatchSchema.keyof()).optional(),
|
|
}),
|
|
],
|
|
);
|
|
export const endpointComparisonViewItemsSchema = z.union([
|
|
z.array(
|
|
comparedStaticEndpointSchema
|
|
.partial()
|
|
.required({ endpoint_id: true, node_id: true, observation_id: true }),
|
|
),
|
|
z.array(comparedObservedGroupSchema.partial().required({ group_id: true })),
|
|
z.array(
|
|
endpointMatchSchema
|
|
.partial()
|
|
.required({ match_id: true, endpoint_id: true, group_id: true }),
|
|
),
|
|
]);
|
|
export const endpointComparisonViewContextSchema =
|
|
staticObservedEndpointComparisonSchema.omit({
|
|
static_endpoints: true,
|
|
observed_groups: true,
|
|
matches: true,
|
|
limitations: true,
|
|
});
|
|
const readReport = createAnalysisViewResultReader(
|
|
staticObservedEndpointComparisonSchema,
|
|
);
|
|
const selectComparisonRows = (
|
|
report: StaticObservedEndpointComparison,
|
|
view: z.output<typeof endpointComparisonViewRequestSchema>,
|
|
) => {
|
|
const { static_endpoints, observed_groups, matches } = report;
|
|
let selected: {
|
|
rows: z.output<typeof endpointComparisonViewItemsSchema>;
|
|
total: number;
|
|
};
|
|
if (view.collection === "static-endpoints") {
|
|
const ids = filterSet(view.endpoint_ids),
|
|
statuses = filterSet(view.statuses);
|
|
const fields = view.fields ?? [
|
|
"method",
|
|
"method_basis",
|
|
"origin",
|
|
"origin_basis",
|
|
"path",
|
|
"status",
|
|
"reason",
|
|
];
|
|
selected = selectViewRows(
|
|
static_endpoints,
|
|
view,
|
|
ids === undefined && statuses === undefined
|
|
? undefined
|
|
: (row) =>
|
|
(ids === undefined || ids.has(row.endpoint_id)) &&
|
|
(statuses === undefined || statuses.has(row.status)),
|
|
(row) => ({
|
|
...pickFields(row, fields),
|
|
endpoint_id: row.endpoint_id,
|
|
node_id: row.node_id,
|
|
observation_id: row.observation_id,
|
|
}),
|
|
);
|
|
} else if (view.collection === "observed-groups") {
|
|
const ids = filterSet(view.group_ids),
|
|
statuses = filterSet(view.statuses);
|
|
const fields = view.fields ?? [
|
|
"method",
|
|
"origin",
|
|
"path_template",
|
|
"status",
|
|
"reason",
|
|
];
|
|
selected = selectViewRows(
|
|
observed_groups,
|
|
view,
|
|
ids === undefined && statuses === undefined
|
|
? undefined
|
|
: (row) =>
|
|
(ids === undefined || ids.has(row.group_id)) &&
|
|
(statuses === undefined || statuses.has(row.status)),
|
|
(row) => ({
|
|
...pickFields(row, fields),
|
|
group_id: row.group_id,
|
|
}),
|
|
);
|
|
} else {
|
|
const endpoints = filterSet(view.endpoint_ids),
|
|
groups = filterSet(view.group_ids);
|
|
const fields = view.fields ?? ["basis", "supporting_record_ordinal"];
|
|
selected = selectViewRows(
|
|
matches,
|
|
view,
|
|
endpoints === undefined && groups === undefined
|
|
? undefined
|
|
: (row) =>
|
|
(endpoints === undefined || endpoints.has(row.endpoint_id)) &&
|
|
(groups === undefined || groups.has(row.group_id)),
|
|
(row) => ({
|
|
...pickFields(row, fields),
|
|
match_id: row.match_id,
|
|
endpoint_id: row.endpoint_id,
|
|
group_id: row.group_id,
|
|
}),
|
|
);
|
|
}
|
|
return selected;
|
|
};
|
|
|
|
/** Page completed classifications; filters never reclassify a subset or upgrade source coverage. */
|
|
export const projectEndpointComparisonView = (
|
|
parent: AnalysisViewParent,
|
|
admitted: AdmittedView,
|
|
) => {
|
|
const view = ownedView<
|
|
SummaryAnalysisView | z.output<typeof endpointComparisonViewRequestSchema>
|
|
>(admitted, "summary", "endpoint-comparison");
|
|
if (view === undefined)
|
|
return invalidAnalysisView(
|
|
"Endpoint comparisons support summary and endpoint-comparison pages",
|
|
);
|
|
const parsed = readReport(parent);
|
|
if (!parsed.ok) return parsed;
|
|
const {
|
|
static_endpoints: _static,
|
|
observed_groups: _observed,
|
|
matches: _matches,
|
|
limitations,
|
|
...context
|
|
} = parsed.value;
|
|
const shared = {
|
|
...viewParentIdentity(parent, "compare_static_and_observed_endpoints"),
|
|
artifact: null,
|
|
limitations: boundedViewLimitations(parent, limitations),
|
|
unknowns:
|
|
parsed.value.summary.unknown > 0
|
|
? [
|
|
"Some static endpoint observations cannot be classified; page static-endpoints with status unknown for their reasons.",
|
|
]
|
|
: [],
|
|
};
|
|
if (view.kind === "summary")
|
|
return ok({
|
|
...shared,
|
|
kind: "summary" as const,
|
|
view,
|
|
summary: context,
|
|
coverage: completeWithinViewCoverage(1),
|
|
});
|
|
const selected = selectComparisonRows(parsed.value, view);
|
|
return ok({
|
|
...shared,
|
|
...context,
|
|
kind: "endpoint-comparison" as const,
|
|
view,
|
|
...selectedViewPage(view.offset, selected),
|
|
});
|
|
};
|