Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions docs/domain/metrics/specs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,16 @@ Rules:

## Builtin Seed Reconciliation

Builtin definitions are declared in one code registry
(`src/backend/services/analytics/src/domain/metric_definitions/builtin.rs`)
and converged into the DB by a startup reconciler, not by migrations.
Migrations own schema only.
Builtin definitions are declared in one declarative registry
(`src/backend/services/analytics/src/domain/metric_definitions/registry.yaml`:
one `sources` list and one `metrics` list) and converged into the DB by a
startup reconciler, not by migrations. Migrations own schema only. The registry
is embedded at build time (`include_str!`) and deserialized once into the seed
types in `builtin.rs`; the reconciler reads it through `builtin_sources()` /
`builtin_metrics()`. Registry invariants (key shape and uniqueness,
input/measure references, computation field combinations, presentation-complete
formats) are pinned by the `builtin.rs` tests, which parse the same embedded
registry — a malformed or drifted registry fails the build.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Rules:

Expand Down Expand Up @@ -588,14 +594,14 @@ one that applies.
### Case 1: metric over an existing measure

The measure already appears in a managed source. Check the source's `measures`
list in `builtin.rs`, the emitting evidence model, and the observation model
list in `registry.yaml`, the emitting evidence model, and the observation model
derived from it.

1. Add one `MetricSeed` to `BUILTIN_METRICS` in
`src/backend/services/analytics/src/domain/metric_definitions/builtin.rs`:
1. Add one entry to the `metrics` list in
`src/backend/services/analytics/src/domain/metric_definitions/registry.yaml`:
metric key (`namespace.metric_name`, lowercase snake case), label,
description, unit, format, direction, entity type, computation type,
input role mapping to the measure, allowed dimensions, peer cohort key.
description, unit, format, direction, entity type, computation, input role
mapping to the measure, allowed dimensions, peer cohort key.
2. Run `cargo test -p analytics` — the registry invariant tests validate
key shapes, input/measure references, and computation field combinations.

Expand Down Expand Up @@ -627,12 +633,12 @@ The source exists but does not emit the measure yet.
the population that explains it.
4. Add the `measure_key` to the observation model's `schema.yml`
`accepted_values` test.
5. Add the measure key to the source's `measures` list in `builtin.rs` and
5. Add the measure key to the source's `measures` list in `registry.yaml` and
classify its evidence granularity.
6. Add or reuse a source-measure presentation rule when the default
date-plus-value table is insufficient. Declare detail keys there and add
explicit column metadata only for fields that are not humanized strings.
7. Add the `MetricSeed` as in case 1.
7. Add the metric entry as in case 1.
8. Validate: `dbt parse` + `cargo test -p analytics` (see Validation
commands).

Expand All @@ -648,14 +654,15 @@ The metric family reads data no managed source covers.
the evidence contract; the observation model derives the aggregate-ready
observation contract from it. Document both in
`src/ingestion/gold/schema.yml`.
2. Add a `BuiltinSource` (source + measures + dimensions) to `builtin.rs`,
with `source_ref` and `evidence_ref` set to their relation names and every
measure assigned an evidence granularity. No backend enum or table-name
code changes are required: relation names are validated data and both
contracts are probed by the runtime schema validator.
2. Add a source entry (source + measures + dimensions) to the `sources` list
in `registry.yaml`, with `source_ref` and `evidence_ref` set to their
relation names and every measure assigned an evidence granularity. No
backend enum or table-name code changes are required: relation names are
validated data and both contracts are probed by the runtime schema
validator.
3. Add source-measure presentation rules for event shapes that need
human-facing detail columns.
4. Add `MetricSeed`s as in case 1.
4. Add metric entries as in case 1.
5. Validate: `dbt parse` + `cargo test -p analytics` (see Validation
commands). The runtime schema validator probes the new relation at
startup.
Expand Down
35 changes: 32 additions & 3 deletions docs/domain/presentation-layer/specs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- [4.1 Implementation Plan](#41-implementation-plan)
- [4.2 Promotion Ladder (FE)](#42-promotion-ladder-fe)
- [4.3 Open Decisions](#43-open-decisions)
- [4.4 Out of Scope (Phase B)](#44-out-of-scope-phase-b)
- [4.4 Phase B and Out of Scope](#44-phase-b-and-out-of-scope)
- [5. Traceability](#5-traceability)

<!-- /toc -->
Expand Down Expand Up @@ -58,6 +58,7 @@ Requirements that significantly influence architecture decisions.
| `cpt-presentation-fr-query-console` | Single stable FE app on the saved-query API: author, list, run, render table / auto-chart. Shipped (#1970) |
| `cpt-presentation-fr-preview-envs` | Path-based `/exp/<name>` on one host, one shared read-only backend, FE-only variation. Per-experiment bundle (`Deployment` + `Service` + one prefix-strip `Ingress`) shipped as the `insight-preview` chart at `deploy/preview/`, applied by hand (#1971). Experiments are a capability gated off on production by default (`cpt-presentation-constraint-experiments-off-prod`, #1973). Shipped |
| `cpt-presentation-fr-preview-auth` | Single fixed callback with a Redis-backed opaque `state` return path (already stashing `state -> { return_to, pkce_verifier, nonce }`, delete-on-read), extended so `return_to` is validated at store time against a configurable `/exp/` prefix. Shipped (#1972) |
| `cpt-presentation-fr-metric-registry` | Single declarative `registry.yaml` (a `sources` list and a `metrics` list) embedded at build time and reconciled into the service DB at boot; replaces the code-literal seed with no change to reconcile semantics; invariants pinned by tests that parse the same registry. Shipped (#1974) |

#### NFR Allocation

Expand Down Expand Up @@ -400,6 +401,32 @@ Serves many experimental FE builds under one host against one shared read-only b

---

#### Metric Registry

- [x] `p3` - **ID**: `cpt-presentation-component-metric-registry`

##### Why this component exists

Collapses the metric-definition seed into one declarative artifact so a metric can be added or changed without editing Rust, and so Phase B (semantic compiler, passports) has a single source of truth to build on. Shipped (#1974). The detailed metric contract is governed by the metrics DESIGN (`docs/domain/metrics/specs/DESIGN.md`).

##### Responsibility scope

- `registry.yaml` (`domain/metric_definitions/`): one `sources` list and one `metrics` list, embedded at build time (`include_str!`) and deserialized once into the seed types in `builtin.rs`, exposed as `builtin_sources()` / `builtin_metrics()`.
- The startup reconciler converges the registry into the service DB (`metric_definitions` and its source/measure/dimension/input tables) idempotently: additive upserts plus disable-missing for builtins dropped from the registry; tenant-owned rows untouched. Unchanged from the prior code-literal seed except its source.
- Registry invariants (key shape and uniqueness, input/measure references, computation field combinations, presentation-complete formats carry no unit) are pinned by tests that parse the same embedded registry, so a malformed or drifted registry fails the build.

##### Responsibility boundaries

- Does NOT define or drive the legacy `metric_catalog`/`metric_threshold` subsystem — that orphaned, frozen path is untouched; its retirement is tracked separately.
- Does NOT add the semantic raw-to-derived compiler, metric passports, or the drift test — those are later Phase B sub-issues (#1975, #1976).
- Does NOT change reconcile semantics or the metric-result runtime.

##### Related components (by ID)

- `cpt-presentation-component-metric-compiler` — consumes the reconciled definitions

---

### 3.3 API Contracts

- [x] `p2` - **ID**: `cpt-presentation-interface-saved-query-endpoints`
Expand Down Expand Up @@ -580,9 +607,11 @@ Tiers 1-2 need no deploy — the unit of change is a query row. Tier 3 is the pr
2. **CI-automated preview provisioning** — whether/when a CI job automates provisioning (best after the nginx-to-Envoy move), and whether tier-3 experiments live on branches of the real FE repo or a prototype repo.
3. **Read-only on v1** — confirm the presentation layer is strictly read-only for v1 (write-back out of scope).

### 4.4 Out of Scope (Phase B)
### 4.4 Phase B and Out of Scope

The declarative metric registry (`cpt-presentation-component-metric-registry`, #1974) lands as the first Phase B step: one YAML is the source of truth for the sanctioned metric-definition seed. The former "FE thresholds" collapse is already done — the FE renders from the `metric_definitions` catalog API and live peer percentiles, holding no per-metric thresholds.

Declarative metric registry (collapse `builtin.rs` + MariaDB `metrics` + FE thresholds into one YAML catalog with passports and a drift test); semantic raw-to-derived compiler; FE metric rework (catalog-driven thresholds, honest NULL-to-ComingSoon). Named here so Phase A does not encode choices that block them.
Still out of scope: metric passports plus their drift test (#1975); the semantic raw-to-derived compiler (#1976); FE metric rework (#1977-#1978). Also deferred: retirement of the orphaned, frozen legacy `metric_catalog`/`metric_threshold` subsystem — no live consumer reads it, so it is left untouched and its removal is tracked separately rather than perpetuated in the new registry.

## 5. Traceability

Expand Down
19 changes: 18 additions & 1 deletion docs/domain/presentation-layer/specs/PRD.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [5.3 Tenant Isolation (p1)](#53-tenant-isolation-p1)
- [5.4 Contract Stability (p2)](#54-contract-stability-p2)
- [5.5 FE Loop (p2)](#55-fe-loop-p2)
- [5.6 Metric Registry (p3)](#56-metric-registry-p3)
- [6. Non-Functional Requirements](#6-non-functional-requirements)
- [6.1 NFR Inclusions](#61-nfr-inclusions)
- [6.2 NFR Exclusions](#62-nfr-exclusions)
Expand Down Expand Up @@ -134,10 +135,12 @@ Phase A draws the contract and makes it safe by construction, so a new analytics
- Server-injected flat tenant filter replacing the no-op on every contract read.
- Contract surface documentation and a contract version stamp.
- A stable query console (FE) and path-based preview environments with an authenticated return path.
- Declarative metric registry: a single YAML as the source of truth for the sanctioned metric-definition seed (Phase B, #1974).

### 4.2 Out of Scope

- Declarative metric registry, semantic raw-to-derived compiler, and FE metric rework (Phase B, #1974-#1978).
- Semantic raw-to-derived compiler, metric passports plus their drift test, and FE metric rework (Phase B, #1975-#1978).
- Retirement of the orphaned legacy `metric_catalog`/`metric_threshold` subsystem (frozen, no live consumer; tracked separately).
- Tenant subtree/hierarchy scoping and a row-policy backstop (deferred pending benchmark).
- Physical relocation of legacy gold from `insight` to `presentation` (deferred, #1979-#1981).
- Write-back from presentation to the contract (presentation is read-only for v1).
Expand Down Expand Up @@ -275,6 +278,20 @@ The system **MUST** authenticate preview environments through a single fixed cal

**Actors**: `cpt-presentation-actor-fe-dev`, `cpt-presentation-actor-analytics-svc`

### 5.6 Metric Registry (p3)

#### Declarative Metric Registry

- [x] `p3` - **ID**: `cpt-presentation-fr-metric-registry`

The sanctioned metric definitions **MUST** be declared in a single declarative registry — one YAML document with a `sources` list and a `metrics` list — that is the single source of truth for the metric-definition seed, replacing the former code-literal seed. The registry **MUST** be reconciled into the service database at startup by the existing reconciler, keeping its idempotent, additive-upsert-plus-disable-missing semantics unchanged, and its invariants (key shape and uniqueness, input/measure references, computation field combinations, presentation-complete formats carrying no unit) **MUST** be enforced by tests that parse the same registry, so a malformed or drifted registry fails the build. (#1974.)

**Status**: Shipped for the sanctioned `metric_definitions` seed (#1974): the registry is embedded at build time and loaded once. The orphaned legacy `metric_catalog`/`metric_threshold` subsystem (no live consumer) is untouched; its retirement is tracked separately.

**Rationale**: One declarative source of truth for metric semantics, editable without a code change, is the foundation the Phase B semantic compiler and passports build on.

**Actors**: `cpt-presentation-actor-analytics-svc`, `cpt-presentation-actor-engineering`

## 6. Non-Functional Requirements

### 6.1 NFR Inclusions
Expand Down
1 change: 1 addition & 0 deletions src/backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/backend/services/analytics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ redis = { workspace = true }

serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
# OpenAPI schemas for request/response DTOs. Version pinned to 5.5 to match the
# `utoipa::ToSchema` the toolkit (`OperationBuilder::json_request` /
# `json_response_with_schema`) is compiled against (already in Cargo.lock via
Expand Down
Loading
Loading