@cosmicdrift/kumiko-samples 0.348.1 → 0.349.0
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/package.json +1 -1
- package/packages/bundled-features/package.json +1 -1
- package/packages/bundled-features/src/metrics/changes.json +6 -0
- package/packages/bundled-features/src/metrics/compute.ts +5 -2
- package/packages/bundled-features/src/metrics/feature.ts +24 -9
- package/packages/bundled-features/src/metrics/filters.ts +100 -0
- package/packages/bundled-features/src/metrics/index.ts +1 -0
- package/packages/bundled-features/src/metrics/types.ts +14 -0
- package/packages/bundled-features/src/metrics/validate.ts +34 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-samples",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.349.0",
|
|
4
4
|
"description": "Source trees of the Kumiko sample recipes, sample apps and bundled features in repo layout, for tooling such as few-shot corpus builds.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-bundled-features",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.349.0",
|
|
4
4
|
"description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "0.349.0",
|
|
4
|
+
"type": "improvement",
|
|
5
|
+
"title": "Metrics accept payload filters that narrow the aggregate without replacing the tenant scope",
|
|
6
|
+
"detail": "metrics: payload filters on a metric\n`MetricDefinition.filters` maps payload keys to source columns, e.g. `{ folderId: \"folderId\" }`. The metric query then accepts that key and narrows the aggregate to the value, for the current and previous window and for groups, stacks and series. The tenant scope is applied last, so a filter value from another tenant yields 0, never that tenant's numbers. `\"\"`, `null` and a missing key mean no filter. The value type follows the column (uuid, numeric, text); an invalid value is a 400. Definitions are rejected at boot for non-camelCase keys, the reserved keys `range`/`timeZone`/`tenantId`, unknown columns, the tenant column, columns already fixed by `where`, and column types other than uuid, text and numeric.\nA filter can also be a resolver `{ column, resolve(value, ctx), valueKind? }`: the payload value (e.g. a folder id) is mapped to the allowed values of `column` through the tenant handler context (IN filter, empty result = no rows), for assignments that live in another table. Resolvers are rejected on metrics with scope `system`."
|
|
7
|
+
},
|
|
2
8
|
{
|
|
3
9
|
"version": "0.335.0",
|
|
4
10
|
"type": "fix",
|
|
@@ -22,6 +22,8 @@ export type MetricRunParams = {
|
|
|
22
22
|
readonly range: MetricRange;
|
|
23
23
|
readonly timeZone: string;
|
|
24
24
|
readonly now: Temporal.Instant;
|
|
25
|
+
/** Payload-driven narrowing (MetricDefinition.filters); scopeWhere wins on conflicts. */
|
|
26
|
+
readonly filterWhere: WhereObject;
|
|
25
27
|
/** Extra filter that narrows the source to the caller's scope. */
|
|
26
28
|
readonly scopeWhere: WhereObject;
|
|
27
29
|
};
|
|
@@ -68,9 +70,10 @@ type MetricAggregates = {
|
|
|
68
70
|
function buildWheres(
|
|
69
71
|
metric: MetricDefinition,
|
|
70
72
|
window: MetricWindow | undefined,
|
|
73
|
+
filterWhere: WhereObject,
|
|
71
74
|
scopeWhere: WhereObject,
|
|
72
75
|
): MetricWheres {
|
|
73
|
-
const baseWhere: WhereObject = { ...metric.where, ...scopeWhere };
|
|
76
|
+
const baseWhere: WhereObject = { ...metric.where, ...filterWhere, ...scopeWhere };
|
|
74
77
|
const { timeField } = metric;
|
|
75
78
|
if (timeField === undefined || window === undefined) {
|
|
76
79
|
return { current: baseWhere, previous: undefined };
|
|
@@ -200,7 +203,7 @@ export async function runMetric(
|
|
|
200
203
|
? { field: timeField, bucket, timeZone: params.timeZone }
|
|
201
204
|
: undefined;
|
|
202
205
|
|
|
203
|
-
const wheres = buildWheres(metric, window, params.scopeWhere);
|
|
206
|
+
const wheres = buildWheres(metric, window, params.filterWhere, params.scopeWhere);
|
|
204
207
|
const aggregates = await fetchAggregates(db, metric, wheres, bucketDimension);
|
|
205
208
|
|
|
206
209
|
const value = aggregates.total[0]?.value ?? null;
|
|
@@ -15,6 +15,11 @@ import {
|
|
|
15
15
|
METRICS_SYSTEM_FEATURE,
|
|
16
16
|
type MetricScope,
|
|
17
17
|
} from "./constants.js";
|
|
18
|
+
import {
|
|
19
|
+
metricFilterPayloadShape,
|
|
20
|
+
metricFilterWhere,
|
|
21
|
+
resolveMetricFilterWhere,
|
|
22
|
+
} from "./filters.js";
|
|
18
23
|
import { METRICS_I18N } from "./i18n.js";
|
|
19
24
|
import type { MetricDefinition, MetricsFeatureOptions } from "./types.js";
|
|
20
25
|
import { metricHasTenantColumn, validateMetrics } from "./validate.js";
|
|
@@ -33,13 +38,21 @@ const invalidTimeZone = {
|
|
|
33
38
|
message: "Unknown IANA time zone",
|
|
34
39
|
};
|
|
35
40
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
function tenantPayloadSchema(metric: MetricDefinition) {
|
|
42
|
+
return z
|
|
43
|
+
.object({ ...metricFilterPayloadShape(metric), ...windowFields })
|
|
44
|
+
.refine((payload) => isValidTimeZone(payload.timeZone), invalidTimeZone);
|
|
45
|
+
}
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
function systemPayloadSchema(metric: MetricDefinition) {
|
|
48
|
+
return z
|
|
49
|
+
.object({
|
|
50
|
+
...metricFilterPayloadShape(metric),
|
|
51
|
+
...windowFields,
|
|
52
|
+
tenantId: z.uuid().optional(),
|
|
53
|
+
})
|
|
54
|
+
.refine((payload) => isValidTimeZone(payload.timeZone), invalidTimeZone);
|
|
55
|
+
}
|
|
43
56
|
|
|
44
57
|
const defaultClock = (): Temporal.Instant => Temporal.Now.instant();
|
|
45
58
|
|
|
@@ -64,13 +77,14 @@ function createTenantMetricQuery(
|
|
|
64
77
|
return defineQueryHandler({
|
|
65
78
|
name: metric.id,
|
|
66
79
|
description: metric.description,
|
|
67
|
-
schema: tenantPayloadSchema,
|
|
80
|
+
schema: tenantPayloadSchema(metric),
|
|
68
81
|
access: { roles: access.admin },
|
|
69
|
-
handler: (query, ctx) =>
|
|
82
|
+
handler: async (query, ctx) =>
|
|
70
83
|
runMetric(ctx.db, metric, {
|
|
71
84
|
range: query.payload.range,
|
|
72
85
|
timeZone: query.payload.timeZone,
|
|
73
86
|
now: now(),
|
|
87
|
+
filterWhere: await resolveMetricFilterWhere(metric, query.payload, ctx),
|
|
74
88
|
// ctx.db already reads own-tenant + SYSTEM reference rows; this narrows to own-tenant only.
|
|
75
89
|
scopeWhere: { tenantId: query.user.tenantId },
|
|
76
90
|
}),
|
|
@@ -84,7 +98,7 @@ function createSystemMetricQuery(
|
|
|
84
98
|
return defineQueryHandler({
|
|
85
99
|
name: metric.id,
|
|
86
100
|
description: metric.description,
|
|
87
|
-
schema: systemPayloadSchema,
|
|
101
|
+
schema: systemPayloadSchema(metric),
|
|
88
102
|
access: { roles: access.systemAdmin },
|
|
89
103
|
handler: (query, ctx) => {
|
|
90
104
|
if (!ctx.systemDb) {
|
|
@@ -111,6 +125,7 @@ function createSystemMetricQuery(
|
|
|
111
125
|
range: query.payload.range,
|
|
112
126
|
timeZone: query.payload.timeZone,
|
|
113
127
|
now: now(),
|
|
128
|
+
filterWhere: metricFilterWhere(metric, query.payload),
|
|
114
129
|
scopeWhere: tenantId === undefined ? {} : { tenantId },
|
|
115
130
|
});
|
|
116
131
|
},
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { extractTableInfo, type WhereObject } from "@cosmicdrift/kumiko-framework/bun-db";
|
|
2
|
+
import type { HandlerContext } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
import type { MetricDefinition, MetricFilterResolver } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export const RESERVED_PAYLOAD_KEYS: readonly string[] = ["range", "timeZone", "tenantId"];
|
|
7
|
+
|
|
8
|
+
// Dashboard select options always send strings.
|
|
9
|
+
const NUMERIC_STRING = /^-?\d+(\.\d+)?$/;
|
|
10
|
+
const MAX_FILTER_STRING_LENGTH = 200;
|
|
11
|
+
const NUMERIC_PG_TYPES: ReadonlySet<string> = new Set([
|
|
12
|
+
"integer",
|
|
13
|
+
"int4",
|
|
14
|
+
"smallint",
|
|
15
|
+
"int2",
|
|
16
|
+
"bigint",
|
|
17
|
+
"serial",
|
|
18
|
+
"bigserial",
|
|
19
|
+
"double precision",
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
export type FilterValueKind = "uuid" | "number" | "string";
|
|
23
|
+
|
|
24
|
+
/** Undefined = the column type cannot be filtered by a payload value without risking a Postgres cast error. */
|
|
25
|
+
export function filterValueKindOf(pgType: string | undefined): FilterValueKind | undefined {
|
|
26
|
+
if (pgType === undefined) return undefined;
|
|
27
|
+
if (pgType === "uuid") return "uuid";
|
|
28
|
+
if (pgType === "text") return "string";
|
|
29
|
+
if (NUMERIC_PG_TYPES.has(pgType) || pgType.startsWith("numeric")) return "number";
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type MetricFilterEntry = {
|
|
34
|
+
readonly payloadKey: string;
|
|
35
|
+
readonly column: string;
|
|
36
|
+
readonly resolver?: MetricFilterResolver;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function metricFilterEntries(metric: MetricDefinition): readonly MetricFilterEntry[] {
|
|
40
|
+
return Object.entries(metric.filters ?? {}).map(([payloadKey, spec]) =>
|
|
41
|
+
typeof spec === "string"
|
|
42
|
+
? { payloadKey, column: spec }
|
|
43
|
+
: { payloadKey, column: spec.column, resolver: spec },
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function filterValueSchema(kind: FilterValueKind): z.ZodType {
|
|
48
|
+
if (kind === "uuid") return z.uuid();
|
|
49
|
+
if (kind === "number")
|
|
50
|
+
return z.union([z.number(), z.string().regex(NUMERIC_STRING).transform(Number)]);
|
|
51
|
+
return z.string().min(1).max(MAX_FILTER_STRING_LENGTH);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** "" and null mean "no filter" (the dashboard's "(all)" option). */
|
|
55
|
+
export function metricFilterPayloadShape(metric: MetricDefinition): Record<string, z.ZodType> {
|
|
56
|
+
const info = extractTableInfo(metric.source);
|
|
57
|
+
const shape: Record<string, z.ZodType> = {};
|
|
58
|
+
for (const { payloadKey, column, resolver } of metricFilterEntries(metric)) {
|
|
59
|
+
const kind =
|
|
60
|
+
resolver !== undefined
|
|
61
|
+
? (resolver.valueKind ?? "string")
|
|
62
|
+
: filterValueKindOf(info.pgTypeOf(info.columnOf(column)));
|
|
63
|
+
if (kind === undefined) continue;
|
|
64
|
+
shape[payloadKey] = z.union([z.literal(""), z.null(), filterValueSchema(kind)]).optional();
|
|
65
|
+
}
|
|
66
|
+
return shape;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function activeFilterValue(payload: Readonly<Record<string, unknown>>, key: string): unknown {
|
|
70
|
+
const value = payload[key];
|
|
71
|
+
return value === undefined || value === null || value === "" ? undefined : value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Column-equality filters only; resolver filters need resolveMetricFilterWhere. */
|
|
75
|
+
export function metricFilterWhere(
|
|
76
|
+
metric: MetricDefinition,
|
|
77
|
+
payload: Readonly<Record<string, unknown>>,
|
|
78
|
+
): WhereObject {
|
|
79
|
+
const where: WhereObject = {};
|
|
80
|
+
for (const { payloadKey, column, resolver } of metricFilterEntries(metric)) {
|
|
81
|
+
const value = activeFilterValue(payload, payloadKey);
|
|
82
|
+
if (resolver === undefined && value !== undefined) where[column] = value;
|
|
83
|
+
}
|
|
84
|
+
return where;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function resolveMetricFilterWhere(
|
|
88
|
+
metric: MetricDefinition,
|
|
89
|
+
payload: Readonly<Record<string, unknown>>,
|
|
90
|
+
ctx: HandlerContext,
|
|
91
|
+
): Promise<WhereObject> {
|
|
92
|
+
const where = metricFilterWhere(metric, payload);
|
|
93
|
+
for (const { payloadKey, column, resolver } of metricFilterEntries(metric)) {
|
|
94
|
+
const value = activeFilterValue(payload, payloadKey);
|
|
95
|
+
if (resolver === undefined || (typeof value !== "string" && typeof value !== "number"))
|
|
96
|
+
continue;
|
|
97
|
+
where[column] = await resolver.resolve(value, ctx);
|
|
98
|
+
}
|
|
99
|
+
return where;
|
|
100
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { EntityTableMeta } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import type { HandlerContext } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
3
|
import type {
|
|
3
4
|
AggregateMeasure,
|
|
4
5
|
AggregateTimeBucket,
|
|
@@ -8,6 +9,17 @@ import type { WhereObject } from "@cosmicdrift/kumiko-types/where-clause-types";
|
|
|
8
9
|
import type { Temporal } from "temporal-polyfill";
|
|
9
10
|
import type { MetricRange, MetricScope } from "./constants.js";
|
|
10
11
|
|
|
12
|
+
/** Maps a payload value (e.g. a folder id) to the values `column` may have (e.g. the folder and its subfolders' loan ids). Runs with the tenant handler context, so reads stay tenant-scoped; the result only narrows. An empty result yields no rows. */
|
|
13
|
+
export type MetricFilterResolver = {
|
|
14
|
+
readonly column: string;
|
|
15
|
+
readonly resolve: (
|
|
16
|
+
value: string | number,
|
|
17
|
+
ctx: HandlerContext,
|
|
18
|
+
) => Promise<readonly (string | number)[]>;
|
|
19
|
+
/** Type of the payload value, not of `column`. Default "string". */
|
|
20
|
+
readonly valueKind?: "uuid" | "number" | "string";
|
|
21
|
+
};
|
|
22
|
+
|
|
11
23
|
export type MetricDefinition = {
|
|
12
24
|
readonly id: string;
|
|
13
25
|
readonly description: string;
|
|
@@ -16,6 +28,8 @@ export type MetricDefinition = {
|
|
|
16
28
|
readonly requires?: string;
|
|
17
29
|
readonly measure: AggregateMeasure;
|
|
18
30
|
readonly where?: WhereObject;
|
|
31
|
+
/** Payload key → source column (equality) or a resolver (IN). e.g. `{ folderId: "folderId" }`. Narrows the result only; the tenant scope is always applied on top and never replaced. Resolvers need a metric without scope "system". */
|
|
32
|
+
readonly filters?: Readonly<Record<string, string | MetricFilterResolver>>;
|
|
19
33
|
/** timestamptz column; without it the metric is a snapshot with no window. */
|
|
20
34
|
readonly timeField?: string;
|
|
21
35
|
/** Fixed window, ignores the payload range. */
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { extractTableInfo, isTimestamptzType } from "@cosmicdrift/kumiko-framework/bun-db";
|
|
2
2
|
import { METRIC_RANGES } from "./constants.js";
|
|
3
|
+
import { filterValueKindOf, metricFilterEntries, RESERVED_PAYLOAD_KEYS } from "./filters.js";
|
|
3
4
|
import type { MetricDefinition } from "./types.js";
|
|
4
5
|
|
|
5
6
|
const KEBAB_ID = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
|
|
7
|
+
const FILTER_PAYLOAD_KEY = /^[a-z][a-zA-Z0-9]*$/;
|
|
6
8
|
const TENANT_ID_FIELD = "tenantId";
|
|
7
9
|
|
|
8
10
|
export function metricHasTenantColumn(metric: MetricDefinition): boolean {
|
|
@@ -32,6 +34,7 @@ function validateMetric(metric: MetricDefinition): void {
|
|
|
32
34
|
|
|
33
35
|
const info = extractTableInfo(metric.source);
|
|
34
36
|
validateColumns(metric, info, fail);
|
|
37
|
+
validateFilters(metric, info, fail);
|
|
35
38
|
validateTimeSettings(metric, info, fail);
|
|
36
39
|
validateShape(metric, fail);
|
|
37
40
|
if (metric.scopes.includes("tenant") && !metricHasTenantColumn(metric)) {
|
|
@@ -50,6 +53,37 @@ function validateColumns(metric: MetricDefinition, info: TableInfo, fail: Fail):
|
|
|
50
53
|
if (metric.timeField !== undefined) requireColumn(metric.timeField, "timeField");
|
|
51
54
|
}
|
|
52
55
|
|
|
56
|
+
function validateFilters(metric: MetricDefinition, info: TableInfo, fail: Fail): void {
|
|
57
|
+
const baseWhereColumns = new Set(Object.keys(metric.where ?? {}).map((f) => info.columnOf(f)));
|
|
58
|
+
for (const { payloadKey, column: field, resolver } of metricFilterEntries(metric)) {
|
|
59
|
+
if (resolver !== undefined && metric.scopes.includes("system")) {
|
|
60
|
+
fail(
|
|
61
|
+
`filter "${payloadKey}" uses a resolver, which runs with the tenant context and cannot be combined with scope "system"`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (!FILTER_PAYLOAD_KEY.test(payloadKey)) {
|
|
65
|
+
fail(`has filter key "${payloadKey}" that is not a camelCase identifier`);
|
|
66
|
+
}
|
|
67
|
+
if (RESERVED_PAYLOAD_KEYS.includes(payloadKey)) {
|
|
68
|
+
fail(`has filter key "${payloadKey}" that is reserved by the metric payload`);
|
|
69
|
+
}
|
|
70
|
+
if (!info.hasColumn(field))
|
|
71
|
+
fail(`filter "${payloadKey}" column "${field}" is not a column of ${info.name}`);
|
|
72
|
+
const column = info.columnOf(field);
|
|
73
|
+
if (column === info.columnOf(TENANT_ID_FIELD)) {
|
|
74
|
+
fail(`filter "${payloadKey}" targets the tenant column, which only the scope may set`);
|
|
75
|
+
}
|
|
76
|
+
if (baseWhereColumns.has(column)) {
|
|
77
|
+
fail(`filter "${payloadKey}" column "${field}" is already fixed by the metric where`);
|
|
78
|
+
}
|
|
79
|
+
if (filterValueKindOf(info.pgTypeOf(column)) === undefined) {
|
|
80
|
+
fail(
|
|
81
|
+
`filter "${payloadKey}" column "${field}" has type ${info.pgTypeOf(column)}, only uuid, text and numeric columns can be filtered`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
53
87
|
function validateTimeSettings(metric: MetricDefinition, info: TableInfo, fail: Fail): void {
|
|
54
88
|
const { timeField } = metric;
|
|
55
89
|
if (timeField === undefined) {
|