-
Notifications
You must be signed in to change notification settings - Fork 17
chore: add schema for UC Metric Views on Analytics plugin #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+271
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a559e99
chore(shared): add metric.json schema (Zod source + generated JSON Sc…
atilafassina 2e038a2
test(shared): add metric.json schema validation tests
atilafassina fa05d05
docs(shared): tidy metric-source schema comments
atilafassina 1e141cd
test(shared): drop obo from the SP-only schema case
atilafassina 917242a
chore(shared): reshape metric.json to entity-first metrics map with e…
atilafassina 5fc4a00
chore(shared): rename executor value service_principal to app_service…
atilafassina aa5d7fb
chore(shared): rename metric.json root key metrics to metricViews
atilafassina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { metricSourceSchema } from "./metric-source"; | ||
|
|
||
| describe("metricSourceSchema", () => { | ||
| test("accepts a minimal configuration and defaults executor to app_service_principal", () => { | ||
| const config = { | ||
| $schema: | ||
| "https://databricks.github.io/appkit/schemas/metric-source.schema.json", | ||
| metricViews: { | ||
| revenue: { source: "appkit_demo.public.revenue_metrics" }, | ||
| }, | ||
| }; | ||
| const result = metricSourceSchema.safeParse(config); | ||
| expect(result.success).toBe(true); | ||
| expect(result.data?.metricViews?.revenue.executor).toBe( | ||
| "app_service_principal", | ||
| ); | ||
| }); | ||
|
|
||
| test("accepts explicit executor values", () => { | ||
| const config = { | ||
| metricViews: { | ||
| revenue: { | ||
| source: "demo.public.revenue", | ||
| executor: "app_service_principal", | ||
| }, | ||
| my_orders: { source: "main.sales.orders_by_user", executor: "user" }, | ||
| }, | ||
| }; | ||
| const result = metricSourceSchema.safeParse(config); | ||
| expect(result.success).toBe(true); | ||
| expect(result.data?.metricViews?.my_orders.executor).toBe("user"); | ||
| }); | ||
|
|
||
| test("accepts an empty configuration", () => { | ||
| expect(metricSourceSchema.safeParse({}).success).toBe(true); | ||
| expect(metricSourceSchema.safeParse({ metricViews: {} }).success).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test("accepts metric keys with underscores", () => { | ||
| const config = { | ||
| metricViews: { | ||
| customer_metrics: { source: "demo.public.customer_metrics" }, | ||
| }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(true); | ||
| }); | ||
|
|
||
| test("rejects the legacy sp/obo lane shape", () => { | ||
| const config = { | ||
| sp: { revenue: { source: "demo.public.revenue" } }, | ||
| obo: { my_orders: { source: "main.sales.orders_by_user" } }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects invalid executor values", () => { | ||
| for (const executor of ["sp", "obo", "service_principal", "USER"]) { | ||
| const config = { | ||
| metricViews: { revenue: { source: "a.b.c", executor } }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| test("rejects a bare-string entry (must be an object)", () => { | ||
| const config = { | ||
| metricViews: { revenue: "demo.public.revenue" }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects an entry without source", () => { | ||
| const config = { | ||
| metricViews: { revenue: {} }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects unknown fields on entries", () => { | ||
| const config = { | ||
| metricViews: { | ||
| revenue: { | ||
| source: "a.b.c", | ||
| ttl: 5, // future option, not in v1 | ||
| }, | ||
| }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects unknown top-level keys", () => { | ||
| expect(metricSourceSchema.safeParse({ foo: 1 }).success).toBe(false); | ||
| expect( | ||
| metricSourceSchema.safeParse({ metricViews: {}, unknown: {} }).success, | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects metric keys that start with a digit", () => { | ||
| const config = { | ||
| metricViews: { "1bad": { source: "a.b.c" } }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects metric keys containing a hyphen", () => { | ||
| const config = { | ||
| metricViews: { "bad-key": { source: "a.b.c" } }, | ||
| }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects a non-three-part FQN", () => { | ||
| const cases = [ | ||
| "revenue", // single token | ||
| "demo.revenue", // two parts | ||
| "four.parts.really.bad", | ||
| ".starts.with.dot", | ||
| "ends.with.dot.", | ||
| ]; | ||
| for (const source of cases) { | ||
| const config = { metricViews: { revenue: { source } } }; | ||
| expect(metricSourceSchema.safeParse(config).success).toBe(false); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * AppKit metric-source schema. | ||
| * | ||
| * Single source of truth for `metric.json` | ||
| * the config that activates the Analytics' metric-view path. | ||
| * | ||
| * `metric.json` declares UC Metric Views under a single `metricViews` map. | ||
| * Each entry binds a metric key to a UC metric view FQN plus the executor | ||
| * the query runs as: | ||
| * - `executor: "app_service_principal"` (default) — queried as the app service | ||
| * principal (cache scope shared across all users). | ||
| * - `executor: "user"` — queried as the requesting user (on-behalf-of; | ||
| * cache scope per-user). | ||
| * | ||
| * A single map (rather than per-executor sections) makes metric keys unique | ||
| * by construction — the same key cannot be declared twice with different | ||
| * executors. | ||
| */ | ||
|
|
||
| import { z } from "zod"; | ||
|
|
||
| export const metricKeySchema = z | ||
| .string() | ||
| .regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/) | ||
| .describe( | ||
| "Metric key. Must be a valid identifier (letters, digits, underscores; cannot start with a digit). Becomes the route key in POST /api/analytics/metric/:key, the hook argument in useMetricView('<key>', ...), and the MetricRegistry augmentation key.", | ||
| ); | ||
|
|
||
| export const metricExecutorSchema = z | ||
| .enum(["app_service_principal", "user"]) | ||
| .describe( | ||
| "Who the metric view is queried as. 'app_service_principal' (default) runs as the app service principal with a cache shared across all users; 'user' runs on-behalf-of the requesting user with a per-user cache.", | ||
| ); | ||
|
|
||
| /** | ||
| * @note Entries are objects (rather than bare strings) at v1 so future per-entry | ||
| * options (cacheTtl, defaultFilter, allowlists) can ship as additive | ||
| * properties without a breaking change. `executor` is the first such option. | ||
| */ | ||
| export const metricEntrySchema = z | ||
| .object({ | ||
| source: z | ||
| .string() | ||
| .regex( | ||
| /^[a-zA-Z0-9_][a-zA-Z0-9_-]*\.[a-zA-Z0-9_][a-zA-Z0-9_-]*\.[a-zA-Z0-9_][a-zA-Z0-9_-]*$/, | ||
| ) | ||
| .describe( | ||
| "Three-part Unity Catalog FQN of the metric view: <catalog>.<schema>.<metric_view>", | ||
| ) | ||
| .meta({ | ||
| examples: [ | ||
| "appkit_demo.public.revenue_metrics", | ||
| "main.analytics.customer_metrics", | ||
| ], | ||
| }), | ||
| executor: metricExecutorSchema.default("app_service_principal"), | ||
| }) | ||
| .strict() | ||
| .describe( | ||
| "A single metric view source declaration: the UC FQN to query and the executor to query it as. Future per-entry options (cacheTtl, defaultFilter, allowlists) ship as additive properties.", | ||
| ); | ||
|
|
||
| export const metricSourceSchema = z | ||
| .object({ | ||
| $schema: z | ||
| .string() | ||
| .optional() | ||
| .describe("Reference to the JSON Schema for validation"), | ||
| metricViews: z | ||
| .record(metricKeySchema, metricEntrySchema) | ||
| .optional() | ||
| .describe( | ||
| "Metric view declarations, keyed by metric key. Each entry names the UC metric view to query and the executor it runs as.", | ||
| ), | ||
| }) | ||
| .strict() | ||
| .describe( | ||
| "Schema for AppKit metric.json — declares Unity Catalog Metric View sources for the analytics plugin's metric-view path. Each entry under 'metricViews' binds a metric key to a UC metric view FQN and an executor ('app_service_principal' shared cache, or 'user' per-user cache). Object form (rather than bare string) at v1 enables future per-entry option growth without breaking changes.", | ||
| ); | ||
|
|
||
| export type MetricKey = z.infer<typeof metricKeySchema>; | ||
| export type MetricExecutor = z.infer<typeof metricExecutorSchema>; | ||
| export type MetricEntry = z.infer<typeof metricEntrySchema>; | ||
| export type MetricSource = z.infer<typeof metricSourceSchema>; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.