|
| 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 |
0 commit comments