Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags to models #16

Merged
merged 10 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[comment]: < Account Signers Current -

{% docs account_signers_current %}

The `account_signers_current` table is a nightly snapshotted table that represents the current status of all account signers associated with an account. The table returns the latest account entry, which is defined as the highest `last_modified_ledger` per account signer on the Stellar Network. Deleted signers are included in the table. For full state history, please use the `account_signers` table.

**The `account_signers_current` table is only updated nightly. Intraday ledger state changes are not captured in the table.**

The `account_signers_current` table may be joined to the `accounts` table in order to find out more information about the originating account. The signers table has a many-to-one relationship with the accounts table, so you should expect multiple records to be returned for each `account_id`.

{% enddocs %}
11 changes: 11 additions & 0 deletions models/docs/marts/ledger_current_state/accounts_current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[comment]: < Accounts Current -

{% docs accounts_current %}

The `accounts_current` table is a nightly snapshotted table that represents the current state of account ledger entries. The table returns the latest account entry, which is defined as the highest `last_modified_ledger` per account on the Stellar Network. Deleted accounts are included in the table. For full state history, please use the `accounts` table.

**The `accounts_current` table is only updated nightly. Intraday ledger state changes are not captured in the table.**

As a reminder, account ledger entries store detailed information for a given account, including current account status, preconditions for transaction authorization, security settings and account balance. The balance reported in the accounts table reflects the account’s XLM balance only. All other asset balances are reported in the `trust_lines` table.

{% enddocs %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[comment]: < Liquidity Pools Current -

{% docs liquidity_pools_current %}

The `liquidity_pools_current` table is a nightly snapshotted table that represents the current status of all liquidity pools. The table returns the latest pool entry, which is defined as the highest `last_modified_ledger` per liquidity pool on the Stellar Network. Deleted pools are included in the table. For full state history, please use the `liquidity_pools` table.

**The `liquidity_pools_current` table is only updated nightly. Intraday ledger state changes are not captured in the table.**

{% enddocs %}

{% docs asset_pair %}

A concatenated representation of the pair of assets in the liquidity pool, in the form `asset_a_code:asset_b_code`. In the case when the pool contains `XLM`, `XLM` is written as an asset code even though the native asset normally has a null asset code. Asset pair intends to make a human readable name for a pool, but does **not** represent a unique pool id. Different asset issuers can mint asset codes of the same name. In these cases, there will be duplicate `asset_pair` names. The pool ids are unique.

{% enddocs %}
11 changes: 11 additions & 0 deletions models/docs/marts/ledger_current_state/offers_current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[comment]: < Offers Current -

{% docs offers_current %}

The `offers_current` table is a nightly snapshotted table that represents the current orderbook of the Stellar Decentralized Exchange. The table returns the latest offer, which is defined as the highest `last_modified_ledger` per offer on the Stellar Network. Deleted offers are included in the table. An offer is deleted when the seller either updates their offer price to zero or an order is completely filled. For full orderbook history, please use the `offers` table.

**The `offers_current` table is only updated nightly. Intraday ledger state changes are not captured in the table.**

For more information on how the Stellar Decentralized Exchange works, please read [these docs](https://developers.stellar.org/docs/encyclopedia/liquidity-on-stellar-sdex-liquidity-pools#order-books).

{% enddocs %}
11 changes: 11 additions & 0 deletions models/docs/marts/ledger_current_state/trust_lines_current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[comment]: < Trust Lines Current -

{% docs trust_lines_current %}

The `trust_lines_current` table is a nightly snapshotted table that represents the current state of account trustlines. The table returns the latest trustline entry, per account, which is defined as the highest `last_modified_ledger` per trustline/account on the Stellar Network. Deleted trust lines are included in the table. For full state history, please use the `trust_lines` table.

**The `trust_lines_current` table is only updated nightly. Intraday ledger state changes are not captured in the table.**

As a reminder, trustline ledger entries store detailed information for assets trusted by a given account. The balance reported in the trustlines table reflects the account’s trusted asset balances, `XLM` balance is reported in the `accounts` table.

{% enddocs %}
62 changes: 62 additions & 0 deletions models/marts/ledger_current_state/account_signers_current.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{
config(
tags = ["current_state"],
materialized = "incremental",
unique_key = "unique_id",
cluster_by = "account_id"
)
}}

/* Finds the latest state of each account signer in the `account_signers` table.
Ranks each record (grain: one row per account) using
last modified ledger sequence number. View includes all account signers.
(Deleted and Existing). View matches the Horizon snapshotted state tables. */
with
current_signers as (
select
s.account_id
, s.signer
, s.weight
, s.sponsor
, s.last_modified_ledger
, l.closed_at
, s.ledger_entry_change
, s.deleted
-- table only has natural keys, creating a primary key
, concat(s.account_id, '-', s.signer
) as unique_id
, s.batch_run_date
, s.batch_insert_ts
, row_number()
over (
partition by s.account_id, s.signer
order by s.last_modified_ledger desc, s.ledger_entry_change desc
) as row_nr
from {{ ref('stg_account_signers') }} as s
join {{ ref('stg_history_ledgers') }} as l
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: I think we can remove the join against history_ledgers on the current state tables. Since the state tables now all have closed_at. We will need to validate that though and confirm that the closed_at field is backfilled appropriately.

on s.last_modified_ledger = l.sequence

{% if is_incremental() %}
-- limit the number of partitions fetched
where
s.batch_run_date >= date_sub(current_date(), interval 30 day)
-- fetch the last week of records loaded
and timestamp_add(s.batch_insert_ts, interval 7 day)
> (select max(t.upstream_insert_ts) from {{ this }} as t)
{% endif %}
)
select
account_id
, signer
, weight
, sponsor
, last_modified_ledger
, ledger_entry_change
, closed_at
, deleted
, unique_id
, batch_run_date
, batch_insert_ts as upstream_insert_ts
, current_timestamp() as batch_insert_ts
from current_signers
where row_nr = 1
121 changes: 121 additions & 0 deletions models/marts/ledger_current_state/account_signers_current.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
version: 2

models:
- name: account_signers_current
description: '{{ doc("account_signers_current") }}'
tests:
- dbt_utils.recency:
datepart: hour
field: cast(closed_at as datetime)
interval: 12
config:
severity: warn
meta:
description:
"Monitors the freshness of your table over time, as the expected time between data updates."
- elementary.volume_anomalies:
timestamp_column: closed_at
backfill_days: 90
time_bucket:
period: day
count: 1
meta:
description:
"Monitors the row count of your table over time."
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- account_id
- signer
- ledger_entry_change
- last_modified_ledger
config:
where: closed_at >= timestamp_trunc(timestamp_sub(current_timestamp(), interval 2 day), day)
and closed_at < timestamp_trunc(current_timestamp(), day)
meta:
description:
"Tests the uniqueness combination of: account_id, signer, ledger_entry_change and last_modified_ledger."
columns:
- name: account_id
description: '{{ doc("account_id") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: signer
description: '{{ doc("signer") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: weight
description: '{{ doc("weight") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: sponsor
description: '{{ doc("sponsor") }}'

- name: last_modified_ledger
description: '{{ doc("last_modified_ledger") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: ledger_entry_change
description: '{{ doc("ledger_entry_change") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day
- accepted_values:
values: [0, 1, 2]
quote: false

- name: closed_at
description: '{{ doc("closed_at") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: deleted
description: '{{ doc("deleted") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day
- accepted_values:
values: ["true", "false"]
quote: false

- name: batch_run_date
description: '{{ doc("batch_run_date") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: batch_insert_ts
description: '{{ doc("batch_insert_ts") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: upstream_insert_ts
description: '{{ doc("upstream_insert_ts") }}'
tests:
- not_null:
config:
where: closed_at > current_timestamp - interval 2 day

- name: unique_id
description: '{{ doc("unique_id") }}'
tests:
- not_null
- unique
91 changes: 91 additions & 0 deletions models/marts/ledger_current_state/accounts_current.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{{
config(
tags = ["current_state"],
materialized = "incremental",
unique_key = "account_id"
)
}}
/* Returns the latest state of each account in the `accounts` table.
Table includes all accounts. (Deleted and Existing).

Rank all rows for an account by last modified ledger and ledger entry type.
Deleted entry types reuse the last modified ledger sequence */

with
current_accts as (
select
a.account_id
, a.balance
, a.buying_liabilities
, a.selling_liabilities
, a.sequence_number
, a.num_subentries
, a.num_sponsoring
, a.num_sponsored
, a.inflation_destination
, a.flags
, a.home_domain
, a.master_weight
, a.threshold_low
, a.threshold_medium
, a.threshold_high
, a.last_modified_ledger
, a.ledger_entry_change
, l.closed_at
, a.deleted
, a.sponsor
, a.sequence_ledger
, a.sequence_time
, a.batch_run_date
, a.batch_insert_ts
, row_number()
over (
partition by a.account_id
order by
a.last_modified_ledger desc
, a.ledger_entry_change desc
)
as row_nr
from {{ ref('stg_accounts') }} as a
join {{ ref('stg_history_ledgers') }} as l
on a.last_modified_ledger = l.sequence

{% if is_incremental() %}
-- limit the number of partitions fetched
where
a.batch_run_date >= date_sub(current_date(), interval 30 day)
-- fetch the last week of records loaded
and timestamp_add(a.batch_insert_ts, interval 7 day)
> (select max(t.upstream_insert_ts) from {{ this }} as t)
{% endif %}
)

/* Return the same fields as the `accounts` table */
select
account_id
, balance
, buying_liabilities
, selling_liabilities
, sequence_number
, num_subentries
, num_sponsoring
, num_sponsored
, inflation_destination
, flags
, home_domain
, master_weight
, threshold_low
, threshold_medium
, threshold_high
, last_modified_ledger
, ledger_entry_change
, closed_at
, deleted
, sponsor
, sequence_ledger
, sequence_time
, batch_run_date
, batch_insert_ts as upstream_insert_ts
, current_timestamp() as batch_insert_ts
from current_accts
where row_nr = 1
Loading