diff --git a/models/docs/marts/ledger_current_state/claimable_balances_current.md b/models/docs/marts/ledger_current_state/claimable_balances_current.md new file mode 100644 index 0000000..fbb6927 --- /dev/null +++ b/models/docs/marts/ledger_current_state/claimable_balances_current.md @@ -0,0 +1,7 @@ +[comment]: < Claimable Balances Current - + +{% docs claimable_balances_current %} + +The `claimable_balances_current` table is a nightly snapshotted table that represents the current state of claimable balances ledger entries. + +{% enddocs %} diff --git a/models/marts/ledger_current_state/claimable_balances_current.sql b/models/marts/ledger_current_state/claimable_balances_current.sql new file mode 100644 index 0000000..575fbfe --- /dev/null +++ b/models/marts/ledger_current_state/claimable_balances_current.sql @@ -0,0 +1,63 @@ +{{ + config( + tags = ["current_state"], + materialized = "incremental", + unique_key = "balance_id" + ) +}} +/* Returns the latest state of each claimable balance in the `claimable_balances` table. + + Rank all rows for a claimable balance by closed_at timestamp and pick the latest one.*/ + +with + current_balance as ( + select + cb.balance_id + , cb.claimants + , cb.asset_type + , cb.asset_code + , cb.asset_issuer + , cb.asset_amount + , cb.sponsor + , cb.flags + , cb.last_modified_ledger + , cb.ledger_entry_change + , cb.deleted + , cb.batch_id + , cb.batch_run_date + , cb.batch_insert_ts + , cb.closed_at + , cb.ledger_sequence + , row_number() + over ( + partition by cb.balance_id + order by cb.closed_at desc + ) as rn + from {{ ref('stg_claimable_balances') }} as cb + {% if is_incremental() %} + -- limit the number of partitions fetched incrementally + where + cb.batch_run_date >= date_sub(current_date(), interval 7 day) + {% endif %} + ) + +select + balance_id + , claimants + , asset_type + , asset_code + , asset_issuer + , asset_amount + , sponsor + , flags + , last_modified_ledger + , ledger_entry_change + , deleted + , batch_id + , batch_run_date + , closed_at + , ledger_sequence + , batch_insert_ts as upstream_insert_ts + , current_timestamp() as batch_insert_ts +from current_balance +where rn = 1 diff --git a/models/marts/ledger_current_state/claimable_balances_current.yml b/models/marts/ledger_current_state/claimable_balances_current.yml new file mode 100644 index 0000000..0aa123b --- /dev/null +++ b/models/marts/ledger_current_state/claimable_balances_current.yml @@ -0,0 +1,134 @@ +version: 2 + +models: + - name: claimable_balances_current + description: '{{ doc("claimable_balances_current") }}' + tests: + - dbt_utils.recency: + datepart: hour + field: closed_at + interval: 12 + config: + severity: warn + meta: + description: "Monitors the freshness of your table over time, as the expected time between data updates." + columns: + - name: balance_id + description: '{{ doc("balance_id") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: claimants + description: '{{ doc("claimants") }}' + + - name: asset_type + description: '{{ doc("asset_type") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: asset_code + description: '{{ doc("asset_code") }}' + tests: + - not_null: + config: + where: asset_type != 'native' + and batch_run_date > current_datetime - interval 2 day + + - name: asset_issuer + description: '{{ doc("asset_issuer") }}' + tests: + - not_null: + config: + where: asset_type != 'native' + and batch_run_date > current_datetime - interval 2 day + + - name: asset_amount + description: '{{ doc("asset_amount") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: sponsor + description: '{{ doc("sponsor") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: flags + description: '{{ doc("flags_accounts_balances") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: last_modified_ledger + description: '{{ doc("last_modified_ledger") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: ledger_entry_change + description: '{{ doc("ledger_entry_change") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + - accepted_values: + values: [0, 1, 2] + quote: false + + - name: deleted + description: '{{ doc("deleted") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: batch_id + description: '{{ doc("batch_id") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: batch_run_date + description: '{{ doc("batch_run_date") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: closed_at + description: '{{ doc("closed_at") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: ledger_sequence + description: '{{ doc("ledger_sequence") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: upstream_insert_ts + description: '{{ doc("upstream_insert_ts") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day + + - name: batch_insert_ts + description: '{{ doc("batch_insert_ts") }}' + tests: + - not_null: + config: + where: batch_run_date > current_datetime - interval 2 day