From 6aded6d497eaf9805f7eea6dfb37d1ae210a819b Mon Sep 17 00:00:00 2001 From: Mike Stepanovic Date: Mon, 17 Mar 2025 16:30:20 -0600 Subject: [PATCH] + silver_stats --- models/silver/stats/silver_stats.yml | 73 +++++++++++++++++ .../silver_stats__core_metrics_hourly.sql | 78 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 models/silver/stats/silver_stats.yml create mode 100644 models/silver/stats/silver_stats__core_metrics_hourly.sql diff --git a/models/silver/stats/silver_stats.yml b/models/silver/stats/silver_stats.yml new file mode 100644 index 0000000..12763c0 --- /dev/null +++ b/models/silver/stats/silver_stats.yml @@ -0,0 +1,73 @@ +version: 2 +models: + - name: silver_stats__core_metrics_hourly + config: + contract: + enforced: true + tests: + - dbt_utils.sequential_values: + column_name: BLOCK_TIMESTAMP_HOUR + interval: 1 + config: + severity: error + error_if: ">0" + tags: ['test_recency'] + columns: + - name: BLOCK_TIMESTAMP_HOUR + data_type: TIMESTAMP_NTZ + tests: + - not_null: + tags: ['test_quality'] + - name: BLOCK_NUMBER_MIN + data_type: FLOAT + tests: + - not_null: + tags: ['test_quality'] + - name: BLOCK_NUMBER_MAX + data_type: FLOAT + tests: + - not_null: + tags: ['test_quality'] + - name: BLOCK_COUNT + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: TRANSACTION_COUNT + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: TRANSACTION_COUNT_SUCCESS + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: TRANSACTION_COUNT_FAILED + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: UNIQUE_SENDER_COUNT + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: UNIQUE_PAYLOAD_FUNCTION_COUNT + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: TOTAL_FEES + data_type: NUMBER + tests: + - not_null: + tags: ['test_quality'] + - name: CORE_METRICS_HOURLY_ID + data_type: VARCHAR + - name: INSERTED_TIMESTAMP + data_type: TIMESTAMP_NTZ + - name: MODIFIED_TIMESTAMP + data_type: TIMESTAMP_NTZ + - name: _INVOCATION_ID + data_type: VARCHAR diff --git a/models/silver/stats/silver_stats__core_metrics_hourly.sql b/models/silver/stats/silver_stats__core_metrics_hourly.sql new file mode 100644 index 0000000..58b87cd --- /dev/null +++ b/models/silver/stats/silver_stats__core_metrics_hourly.sql @@ -0,0 +1,78 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_timestamp_hour", + cluster_by = ['block_timestamp_hour::DATE'], + tags = ['noncore'] +) }} + +{% if is_incremental() %} + {% set min_block_timestamp_hour_query %} + SELECT + MIN(DATE_TRUNC('hour', block_timestamp)) as block_timestamp_hour + FROM + {{ ref('silver__transactions') }} + WHERE + inserted_timestamp >= ( + SELECT + MAX(inserted_timestamp) + FROM + {{ this }} + ) + {% endset %} + + {% set min_block_timestamp_hour = run_query(min_block_timestamp_hour_query).columns[0].values()[0] %} +{% endif %} + +SELECT + DATE_TRUNC( + 'hour', + block_timestamp + ) AS block_timestamp_hour, + MIN(block_number) :: FLOAT AS block_number_min, + MAX(block_number) :: FLOAT AS block_number_max, + COUNT( + DISTINCT block_number + ) AS block_count, + COUNT( + DISTINCT tx_hash + ) AS transaction_count, + COUNT( + DISTINCT CASE + WHEN success THEN tx_hash + END + ) AS transaction_count_success, + COUNT( + DISTINCT CASE + WHEN NOT success THEN tx_hash + END + ) AS transaction_count_failed, + COUNT( + DISTINCT sender + ) AS unique_sender_count, + COUNT( + DISTINCT payload_function + ) AS unique_payload_function_count, + SUM(COALESCE(gas_unit_price,0) * gas_used) AS total_fees, + {{ dbt_utils.generate_surrogate_key( + ['block_timestamp_hour'] + ) }} AS core_metrics_hourly_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + {{ ref('silver__transactions') }} +WHERE + block_timestamp_hour < DATE_TRUNC( + 'hour', + CURRENT_TIMESTAMP + ) + +{% if is_incremental() %} +AND DATE_TRUNC( + 'hour', + block_timestamp +) >= '{{ min_block_timestamp_hour }}' +{% endif %} +GROUP BY + 1 \ No newline at end of file