-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model for current claimable balances (#66)
* 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
1 parent
f3bf424
commit d6606b1
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
models/docs/marts/ledger_current_state/claimable_balances_current.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
models/marts/ledger_current_state/claimable_balances_current.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
134
models/marts/ledger_current_state/claimable_balances_current.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |