Skip to content

Commit bad83e8

Browse files
Adds the current models model with recent run stats (#79)
* Enforce consistency across materialised models. * Adds the current models model with recent run stats * linting * linting * Update models/schemas.yml Co-authored-by: Niall Woodward <niall@niallrees.com> * Add new model to Readme.md Co-authored-by: Niall Woodward <niall@niallrees.com>
1 parent fe61e25 commit bad83e8

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Models included:
77
- `dim_dbt__seeds`
88
- `dim_dbt__snapshots`
99
- `dim_dbt__tests`
10+
- `dim_dbt__current_models`
1011
- `fct_dbt__critical_path`
1112
- `fct_dbt__latest_full_model_executions`
1213
- `fct_dbt__model_executions`

models/dim_dbt__current_models.sql

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
with run_results as (
2+
3+
select *
4+
from {{ ref('fct_dbt__run_results') }}
5+
6+
),
7+
8+
models as (
9+
10+
select *
11+
from {{ ref('dim_dbt__models') }}
12+
13+
),
14+
15+
model_executions as (
16+
17+
select *
18+
from {{ ref('fct_dbt__model_executions') }}
19+
20+
),
21+
22+
-- Get the most recent comile run
23+
latest_compile as (
24+
25+
select
26+
command_invocation_id,
27+
dbt_cloud_run_id
28+
from run_results
29+
where execution_command = 'run'
30+
order by artifact_generated_at desc
31+
limit 1
32+
33+
),
34+
35+
-- Models present in the most recent compile run
36+
latest_models as (
37+
38+
select models.*
39+
from models
40+
-- In a local deploy, the command id is sufficient, but not in cloud - that requires the cloud run id to achieve a match.
41+
inner join latest_compile
42+
on models.command_invocation_id = latest_compile.command_invocation_id
43+
or models.dbt_cloud_run_id = latest_compile.dbt_cloud_run_id
44+
45+
),
46+
47+
latest_model_runs as (
48+
49+
select
50+
latest_models.node_id,
51+
model_executions.query_completed_at,
52+
model_executions.total_node_runtime,
53+
model_executions.rows_affected,
54+
model_executions.was_full_refresh,
55+
-- Work out indices so we can get the most recent runs, both incremental and full.
56+
row_number() over (
57+
partition by latest_models.node_id, model_executions.was_full_refresh
58+
order by model_executions.query_completed_at desc
59+
) as run_idx
60+
from latest_models
61+
inner join model_executions
62+
on latest_models.node_id = model_executions.node_id
63+
-- Only successful runs
64+
where model_executions.status = 'success'
65+
66+
),
67+
68+
latest_model_stats as (
69+
select
70+
node_id,
71+
max(iff(not was_full_refresh, query_completed_at, null)) as last_incremental_run_completed_at,
72+
max(iff(not was_full_refresh, total_node_runtime, null)) as last_incremental_run_total_runtime,
73+
max(iff(not was_full_refresh, rows_affected, null)) as last_incremental_run_rows_affected,
74+
max(iff(was_full_refresh, query_completed_at, null)) as last_full_run_completed_at,
75+
max(iff(was_full_refresh, total_node_runtime, null)) as last_full_run_total_runtime,
76+
max(iff(was_full_refresh, rows_affected, null)) as last_full_run_rows_affected
77+
from latest_model_runs
78+
-- Only most recent runs (of each type)
79+
where run_idx = 1
80+
group by node_id
81+
82+
),
83+
84+
final as (
85+
86+
select
87+
latest_models.*,
88+
latest_model_stats.last_incremental_run_completed_at,
89+
latest_model_stats.last_incremental_run_total_runtime,
90+
latest_model_stats.last_incremental_run_rows_affected,
91+
latest_model_stats.last_full_run_completed_at,
92+
latest_model_stats.last_full_run_total_runtime,
93+
latest_model_stats.last_full_run_rows_affected
94+
from latest_models
95+
left join latest_model_stats
96+
on latest_models.node_id = latest_model_stats.node_id
97+
98+
)
99+
100+
select * from final

models/schemas.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,42 @@ models:
215215
description: Name of the database entity this source resolved to.
216216
- name: source_path
217217
description: Filepath of the source.
218+
219+
- name: dim_dbt__current_models
220+
description: A subset of the models found in `dim_models`, which were present in the manifest of the most recent run. This
221+
represents the models which are currently live in the dbt project.
222+
columns:
223+
- name: manifest_model_id
224+
description: Primary key generated from the command_invocation_id and checksum.
225+
tests:
226+
- unique
227+
- not_null
228+
- name: command_invocation_id
229+
description: The id of the command which resulted in the source artifact's generation.
230+
- name: artifact_generated_at
231+
description: Timestamp of when the source artifact was generated.
232+
- name: node_id
233+
description: Unique id for the node, in the form of model.[package_name].[model_name]
234+
- name: name
235+
description: The model name.
236+
- name: model_schema
237+
- name: depends_on_nodes
238+
description: List of node ids the model depends on.
239+
- name: package_name
240+
- name: model_path
241+
description: Filepath of the model.
242+
- name: checksum
243+
description: Unique identifier for the model. If a model is unchanged between separate executions this will remain the same.
244+
- name: model_materialization
245+
- name: last_incremental_run_completed_at
246+
description: The completion time from the last time this model was run during an increment run.
247+
- name: last_incremental_run_total_runtime
248+
description: The total runtime from the last time this model was run during an increment run.
249+
- name: last_incremental_run_rows_affected
250+
description: The number of rows affected from the last time this model was run during an increment run.
251+
- name: last_full_run_completed_at
252+
description: The completion time from the last time this model was run during a full refresh.
253+
- name: last_full_run_total_runtime
254+
description: The total runtime from the last time this model was run during a full refresh.
255+
- name: last_full_run_rows_affected
256+
description: The number of rows affected from the last time this model was run during a full refresh.

0 commit comments

Comments
 (0)