feat(analytics): BI-P022 — dbt canonical transformation CONTRACT and reference project
CI (SIAX Cloud) / security (push) Successful in 22s
CI (SIAX Cloud) / integration (push) Successful in 38s
CI (SIAX Cloud) / quality (push) Successful in 52s
Security Scan (SIAX Cloud) / security (push) Successful in 20s
CI (SIAX Cloud) / security (pull_request) Successful in 22s
CI (SIAX Cloud) / integration (pull_request) Successful in 37s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 20s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m1s
CI (SIAX Cloud) / security (push) Successful in 22s
CI (SIAX Cloud) / integration (push) Successful in 38s
CI (SIAX Cloud) / quality (push) Successful in 52s
Security Scan (SIAX Cloud) / security (push) Successful in 20s
CI (SIAX Cloud) / security (pull_request) Successful in 22s
CI (SIAX Cloud) / integration (pull_request) Successful in 37s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 20s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m1s
Scope stated in the module's first paragraph: this defines the contract, it
does not run dbt, and nothing in this repo does. Execution is N0D's (n0d#38),
which consumes this from the other side. There is deliberately no DbtExecutor
implementation here — only the interface — so "0pt can run dbt" cannot become
true by accident and the two repos share one versioned shape.
`analytics/dbt/` is a real reference project covering raw -> staging ->
curated -> serving, with the four layers mapped onto BI-P003's existing tiers
rather than inventing a fifth vocabulary. The tenant predicate is applied at
the FIRST modelled layer so every downstream model inherits it and no later
model has to remember — the same forced-predicate rule as the query compiler's,
one layer up.
Two singular tests ship with it, both aimed at false greens:
* `assert_mart_is_not_empty` — without it every column test passes trivially
over zero rows and the whole suite reports green while the pipeline is dead;
* `assert_no_cross_tenant_rows` — which should be impossible to fail, and
exists precisely for that reason. An isolation guarantee nobody tests is the
one that turns out to have four holes rather than the one reported.
BI-P022's version rule is a GATE. `DBT_RUNTIME_PIN` records dbt Core 1.10.13
(stable) with dbt-clickhouse 1.9.2 and BOTH review flags FALSE — no licence or
supply-chain review has been performed in this repo and none is claimed.
`assertDbtRuntimeCertified` refuses a production run until they are true, and
refuses an alpha line in production without a separate certification reference.
A version constant on its own would be the documented-but-unenforced pattern;
a test pins the flags at false so flipping them is a visible deliberate act.
The result is treated as UNTRUSTED INPUT, because it arrives from another
repo's executor and this estate has been burned by green statuses that meant
nothing. Refused: `status: success` with ZERO nodes executed (a dbt selector
matching nothing exits 0 — the dbt-shaped false green); an observed runtime
different from the pin (the models mean something else and nothing says so); a
revision that is not an exact 40-char SHA (a branch name is not reproducible,
and reproducibility is the DoD); a missing manifest.json or run_results.json,
without which the run cannot be audited or its tests ingested by BI-P025.
Layer dependencies are checked against the manifest GRAPH, not asserted as a
convention: a mart reading a source directly, or skipping a layer, or a
staging model reading a mart, are each refused.
Selectors are validated rather than passed through — a free-form argument list
is a shell surface, and a shell surface on a job holding warehouse credentials
is the whole problem. The job also cannot name an arbitrary target schema: it
must be scoped to the job's tenant.
39 tests. typecheck + eslint clean.
Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
-- CURATED (BI-P003). Joins and conformed entities. Reads STAGING only.
|
||||
{{ config(materialized='table', tags=['tier:CURATED']) }}
|
||||
|
||||
select
|
||||
e.event_id,
|
||||
e.tenant_id,
|
||||
e.customer_id,
|
||||
e.amount_sek,
|
||||
e.channel,
|
||||
coalesce(c.country_code, e.country_code) as country_code,
|
||||
c.segment,
|
||||
e.invoiced_at
|
||||
from {{ ref('stg_sales_events') }} e
|
||||
left join {{ ref('stg_customers') }} c
|
||||
on e.customer_id = c.customer_id
|
||||
and e.tenant_id = c.tenant_id -- the join itself stays tenant-scoped
|
||||
@@ -0,0 +1,17 @@
|
||||
-- SERVING (BI-P003). What a dashboard or a BI-P028 metric reads.
|
||||
--
|
||||
-- Reads INTERMEDIATE only. Reading a source here would skip two layers, and
|
||||
-- assertLayerDependenciesLegal refuses that against the manifest graph.
|
||||
{{ config(materialized='table', tags=['tier:SERVING']) }}
|
||||
|
||||
select
|
||||
tenant_id,
|
||||
date_trunc('day', invoiced_at) as day,
|
||||
country_code,
|
||||
channel,
|
||||
segment,
|
||||
count(*) as order_count,
|
||||
sum(amount_sek) as net_revenue_sek
|
||||
from {{ ref('int_sales_enriched') }}
|
||||
where channel != 'internal' -- matches the net_revenue metric's baked filter
|
||||
group by 1, 2, 3, 4, 5
|
||||
@@ -0,0 +1,19 @@
|
||||
version: 2
|
||||
|
||||
models:
|
||||
- name: mart_sales_daily
|
||||
description: >
|
||||
SERVING projection behind the `net_revenue` and `orders` metrics.
|
||||
A blocking test failure here quarantines the serving projection
|
||||
(BI-P025); the quarantine is enforced on the READ side.
|
||||
columns:
|
||||
- name: tenant_id
|
||||
tests: [not_null]
|
||||
- name: day
|
||||
tests: [not_null]
|
||||
- name: net_revenue_sek
|
||||
tests: [not_null]
|
||||
- name: order_count
|
||||
tests:
|
||||
- not_null
|
||||
- dbt_utils.accepted_range: { min_value: 1, inclusive: true }
|
||||
@@ -0,0 +1,34 @@
|
||||
version: 2
|
||||
|
||||
# RAW. Declared, never modelled. A source has no SQL of its own — anything that
|
||||
# transforms belongs in staging, where BI-P003 assigns it NORMALIZED.
|
||||
sources:
|
||||
- name: opt_raw
|
||||
description: >
|
||||
Landed, untransformed data as delivered by the 0pt sourcing engine.
|
||||
RAW tier: no BI surface may read this directly without a recorded
|
||||
BI-P003 exception.
|
||||
schema: "{{ var('siax_tenant_id') }}_raw"
|
||||
loaded_at_field: ingested_at
|
||||
freshness:
|
||||
# BI-P026's DELAYED/STALE thresholds, expressed where dbt can act on them.
|
||||
warn_after: { count: 90, period: minute }
|
||||
error_after: { count: 180, period: minute }
|
||||
tables:
|
||||
- name: sales_events
|
||||
description: One row per invoiced sales line, as landed.
|
||||
columns:
|
||||
- name: event_id
|
||||
tests: [not_null, unique]
|
||||
- name: tenant_id
|
||||
description: Present on every raw row; the tier boundary depends on it.
|
||||
tests: [not_null]
|
||||
- name: ingested_at
|
||||
tests: [not_null]
|
||||
- name: customers
|
||||
description: Customer master as landed.
|
||||
columns:
|
||||
- name: customer_id
|
||||
tests: [not_null, unique]
|
||||
- name: tenant_id
|
||||
tests: [not_null]
|
||||
@@ -0,0 +1,26 @@
|
||||
version: 2
|
||||
|
||||
models:
|
||||
- name: stg_sales_events
|
||||
description: Normalized sales events — one row per event_id, latest landing wins.
|
||||
columns:
|
||||
- name: event_id
|
||||
tests: [not_null, unique]
|
||||
- name: tenant_id
|
||||
tests: [not_null]
|
||||
- name: amount_sek
|
||||
tests:
|
||||
- not_null
|
||||
- dbt_utils.accepted_range:
|
||||
min_value: 0
|
||||
inclusive: true
|
||||
- name: channel
|
||||
tests:
|
||||
- accepted_values:
|
||||
values: ['web', 'retail', 'partner', 'internal']
|
||||
- name: stg_customers
|
||||
columns:
|
||||
- name: customer_id
|
||||
tests: [not_null, unique]
|
||||
- name: tenant_id
|
||||
tests: [not_null]
|
||||
@@ -0,0 +1,11 @@
|
||||
-- NORMALIZED (BI-P003).
|
||||
{{ config(materialized='view', tags=['tier:NORMALIZED']) }}
|
||||
|
||||
select
|
||||
cast(customer_id as varchar) as customer_id,
|
||||
cast(tenant_id as varchar) as tenant_id,
|
||||
cast(name as varchar) as customer_name,
|
||||
cast(segment as varchar) as segment,
|
||||
cast(country as varchar) as country_code
|
||||
from {{ source('opt_raw', 'customers') }}
|
||||
where tenant_id = '{{ var("siax_tenant_id") }}'
|
||||
@@ -0,0 +1,30 @@
|
||||
-- NORMALIZED (BI-P003). Rename, cast, deduplicate. No joins, no business logic.
|
||||
--
|
||||
-- The tenant predicate is applied HERE, at the first modelled layer, so every
|
||||
-- downstream model inherits it and no later model has to remember. It is the
|
||||
-- same forced-predicate rule as the query compiler's, one layer up.
|
||||
{{ config(materialized='view', tags=['tier:NORMALIZED']) }}
|
||||
|
||||
with source as (
|
||||
select * from {{ source('opt_raw', 'sales_events') }}
|
||||
where tenant_id = '{{ var("siax_tenant_id") }}'
|
||||
),
|
||||
|
||||
deduplicated as (
|
||||
select
|
||||
*,
|
||||
row_number() over (partition by event_id order by ingested_at desc) as _rn
|
||||
from source
|
||||
)
|
||||
|
||||
select
|
||||
cast(event_id as varchar) as event_id,
|
||||
cast(tenant_id as varchar) as tenant_id,
|
||||
cast(customer_id as varchar) as customer_id,
|
||||
cast(amount as double) as amount_sek,
|
||||
cast(channel as varchar) as channel,
|
||||
cast(country as varchar) as country_code,
|
||||
cast(invoiced_at as timestamp) as invoiced_at,
|
||||
cast(ingested_at as timestamp) as ingested_at
|
||||
from deduplicated
|
||||
where _rn = 1
|
||||
Reference in New Issue
Block a user