Skip to content

Commit

Permalink
Add model for current claimable balances (#66)
Browse files Browse the repository at this point in the history
* Add model for current claimable balances

* add doc for claimable balances current

* add docs docs

lint

lint

* Add tests for fields in claimable balance current model

* remove asset_id

* use batch_run_date to filter data

* use date functions
  • Loading branch information
amishas157 authored Jul 24, 2024
1 parent f3bf424 commit d6606b1
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 %}
63 changes: 63 additions & 0 deletions models/marts/ledger_current_state/claimable_balances_current.sql
Original file line number Diff line number Diff line change
@@ -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
134 changes: 134 additions & 0 deletions models/marts/ledger_current_state/claimable_balances_current.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d6606b1

Please sign in to comment.