mirror of
https://github.com/FlipsideCrypto/near-models.git
synced 2026-02-06 14:11:54 +00:00
tags
This commit is contained in:
parent
9da6fb9d8f
commit
1afde852d7
71
README.md
71
README.md
@ -102,56 +102,47 @@ Documentation is automatically generated and hosted using Netlify.
|
||||
|
||||
`/tests` - custom SQL tests that can be attached to tables.
|
||||
|
||||
## Background on Data
|
||||
## Applying Model Tags
|
||||
|
||||
`CHAINWALKERS.PROD.NEAR_BLOCKS` - Near blocks
|
||||
`CHAINWALKERS.PROD.NEAR_TXS` - Near txs
|
||||
### Database / Schema level tags
|
||||
|
||||
Blocks and transactions are fed into the above two Near tables utilizing the Chainwalkers Framework. Details on the data:
|
||||
Database and schema tags are applied via the `add_database_or_schema_tags` macro. These tags are inherited by their downstream objects. To add/modify tags call the appropriate tag set function within the macro.
|
||||
|
||||
1. This is near-real time. Blocks land in this table within 3-5 minutes of being minted.
|
||||
2. The table is a read-only data share in the Metrics DAO Snowflake account under the database `FLIPSIDE`.
|
||||
3. The table is append-only, meaning that duplicates can exist if blocks are re-processed. The injested_at timestamp should be used to retrieve only the most recent block. Macros exist `macros/dedupe_utils.sql` to handle this. See `models/core/blocks.sql` or `/models/core/txs.sql` for an example.
|
||||
4. Tx logs are decoded where an ABI exists.
|
||||
```
|
||||
{{ set_database_tag_value('SOME_DATABASE_TAG_KEY','SOME_DATABASE_TAG_VALUE') }}
|
||||
{{ set_schema_tag_value('SOME_SCHEMA_TAG_KEY','SOME_SCHEMA_TAG_VALUE') }}
|
||||
```
|
||||
|
||||
### Table Structures:
|
||||
### Model tags
|
||||
|
||||
`CHAINWALKERS.PROD.NEAR_BLOCKS` - Near Blocks
|
||||
To add/update a model's snowflake tags, add/modify the `meta` model property under `config`. Only table level tags are supported at this time via DBT.
|
||||
|
||||
| Column | Type | Description |
|
||||
| --------------- | ------------ | ---------------------------------------------------------------- |
|
||||
| record_id | VARCHAR | A unique id for the record generated by Chainwalkers |
|
||||
| offset_id | NUMBER(38, 0) | Synonmous with block_id for Near |
|
||||
| block_id | NUMBER(38, 0) | The height of the chain this block corresponds with |
|
||||
| block_timestamp | TIMESTAMP | The time the block was minted |
|
||||
| network | VARCHAR | The blockchain network (i.e. mainnet, testnet, etc.) |
|
||||
| chain_id | VARCHAR | Synonmous with blockchain name for Near |
|
||||
| tx_count | NUMBER(38, 0) | The number of transactions in the block |
|
||||
| header | json variant | A json queryable column containing the blocks header information |
|
||||
| ingested_at | TIMESTAMP | The time this data was ingested into the table by Snowflake |
|
||||
```
|
||||
{{ config(
|
||||
...,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'SOME_PURPOSE'
|
||||
}
|
||||
}
|
||||
},
|
||||
...
|
||||
) }}
|
||||
```
|
||||
|
||||
`CHAINWALKERS.PROD.NEAR_TXS` - Near Transactions
|
||||
By default, model tags are pushed to Snowflake on each DBT run. You can disable this by setting the `UPDATE_SNOWFLAKE_TAGS` project variable to `False` during a run.
|
||||
|
||||
| Column | Type | Description |
|
||||
| --------------- | ------------ | ---------------------------------------------------------------------- |
|
||||
| record_id | VARCHAR | A unique id for the record generated by Chainwalkers |
|
||||
| tx_id | VARCHAR | A unique on chain identifier for the transaction |
|
||||
| tx_block_index | NUMBER(38, 0) | The index of the transaction within the block. Starts at 0. |
|
||||
| offset_id | NUMBER(38, 0) | Synonmous with block_id for Near |
|
||||
| block_id | NUMBER(38, 0) | The height of the chain this block corresponds with |
|
||||
| block_timestamp | TIMESTAMP | The time the block was minted |
|
||||
| network | VARCHAR | The blockchain network (i.e. mainnet, testnet, etc.) |
|
||||
| chain_id | VARCHAR | Synonmous with blockchain name for Near |
|
||||
| tx_count | NUMBER(38, 0) | The number of transactions in the block |
|
||||
| header | json variant | A json queryable column containing the blocks header information |
|
||||
| tx | array | An array of json queryable objects containing each tx and decoded logs |
|
||||
| ingested_at | TIMESTAMP | The time this data was ingested into the table by Snowflake |
|
||||
```
|
||||
dbt run --var '{"UPDATE_SNOWFLAKE_TAGS":False}' -s models/core/core__ez_dex_swaps.sql
|
||||
```
|
||||
|
||||
## Target Database, Schemas and Tables
|
||||
### Querying for existing tags on a model in snowflake
|
||||
|
||||
Data in this DBT project is written to the `NEAR` database in MetricsDAO.
|
||||
|
||||
This database has 2 schemas, one for `DEV` and one for `PROD` . As a contributer you have full permission to write to the `DEV` schema. However the `PROD` schema can only be written to by Metric DAO's DBT Cloud account. The DBT Cloud account controls running / scheduling models against the `PROD` schema.
|
||||
```
|
||||
select *
|
||||
from table(near.information_schema.tag_references('near.core.ez_dex_swaps', 'table'));
|
||||
```
|
||||
|
||||
## Branching / PRs
|
||||
|
||||
|
||||
@ -29,6 +29,9 @@ on-run-start:
|
||||
- "{{create_sps()}}"
|
||||
- "{{create_get_nearblocks_fts()}}"
|
||||
|
||||
on-run-end:
|
||||
- '{{ apply_meta_as_tags(results) }}'
|
||||
|
||||
# Configuring models
|
||||
# Full documentation: https://docs.getdbt.com/docs/configuring-models
|
||||
|
||||
@ -55,3 +58,4 @@ vars:
|
||||
STREAMLINE_INVOKE_STREAMS: False
|
||||
STREAMLINE_USE_DEV_FOR_EXTERNAL_TABLES: False
|
||||
UPDATE_UDFS_AND_SPS: False
|
||||
UPDATE_SNOWFLAKE_TAGS: True
|
||||
3
macros/tags/add_database_or_schema_tags.sql
Normal file
3
macros/tags/add_database_or_schema_tags.sql
Normal file
@ -0,0 +1,3 @@
|
||||
{% macro add_database_or_schema_tags() %}
|
||||
{{ set_database_tag_value('BLOCKCHAIN_NAME','NEAR') }}
|
||||
{% endmacro %}
|
||||
127
macros/tags/snowflake_tagging.sql
Normal file
127
macros/tags/snowflake_tagging.sql
Normal file
@ -0,0 +1,127 @@
|
||||
{% macro apply_meta_as_tags(results) %}
|
||||
{% if var("UPDATE_SNOWFLAKE_TAGS") %}
|
||||
{{ log('apply_meta_as_tags', info=False) }}
|
||||
{{ log(results, info=False) }}
|
||||
{% if execute %}
|
||||
|
||||
{%- set tags_by_schema = {} -%}
|
||||
{% for res in results -%}
|
||||
{% if res.node.meta.database_tags %}
|
||||
|
||||
{%- set model_database = res.node.database -%}
|
||||
{%- set model_schema = res.node.schema -%}
|
||||
{%- set model_schema_full = model_database+'.'+model_schema -%}
|
||||
{%- set model_alias = res.node.alias -%}
|
||||
|
||||
{% if model_schema_full not in tags_by_schema.keys() %}
|
||||
{{ log('need to fetch tags for schema '+model_schema_full, info=False) }}
|
||||
{%- call statement('main', fetch_result=True) -%}
|
||||
show tags in {{model_database}}.{{model_schema}}
|
||||
{%- endcall -%}
|
||||
{%- set _ = tags_by_schema.update({model_schema_full: load_result('main')['table'].columns.get('name').values()|list}) -%}
|
||||
{{ log('Added tags to cache', info=False) }}
|
||||
{% else %}
|
||||
{{ log('already have tag info for schema', info=False) }}
|
||||
{% endif %}
|
||||
|
||||
{%- set current_tags_in_schema = tags_by_schema[model_schema_full] -%}
|
||||
{{ log('current_tags_in_schema:', info=False) }}
|
||||
{{ log(current_tags_in_schema, info=False) }}
|
||||
{{ log("========== Processing tags for "+model_schema_full+"."+model_alias+" ==========", info=False) }}
|
||||
|
||||
{% set line -%}
|
||||
node: {{ res.node.unique_id }}; status: {{ res.status }} (message: {{ res.message }})
|
||||
node full: {{ res.node}}
|
||||
meta: {{ res.node.meta}}
|
||||
materialized: {{ res.node.config.materialized }}
|
||||
{%- endset %}
|
||||
{{ log(line, info=False) }}
|
||||
|
||||
{%- call statement('main', fetch_result=True) -%}
|
||||
select LEVEL,UPPER(TAG_NAME) as TAG_NAME,TAG_VALUE from table(information_schema.tag_references_all_columns('{{model_schema}}.{{model_alias}}', 'table'))
|
||||
{%- endcall -%}
|
||||
{%- set existing_tags_for_table = load_result('main')['data'] -%}
|
||||
{{ log('Existing tags for table:', info=False) }}
|
||||
{{ log(existing_tags_for_table, info=False) }}
|
||||
|
||||
{{ log('--', info=False) }}
|
||||
{% for table_tag in res.node.meta.database_tags.table %}
|
||||
|
||||
{{ create_tag_if_missing(current_tags_in_schema,table_tag|upper) }}
|
||||
{% set desired_tag_value = res.node.meta.database_tags.table[table_tag] %}
|
||||
|
||||
{{set_table_tag_value_if_different(model_schema,model_alias,table_tag,desired_tag_value,existing_tags_for_table)}}
|
||||
{% endfor %}
|
||||
{{ log("========== Finished processing tags for "+model_alias+" ==========", info=False) }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
|
||||
{% macro create_tag_if_missing(all_tag_names,table_tag) %}
|
||||
{% if table_tag not in all_tag_names %}
|
||||
{{ log('Creating missing tag '+table_tag, info=False) }}
|
||||
{%- call statement('main', fetch_result=True) -%}
|
||||
create tag if not exists silver.{{table_tag}}
|
||||
{%- endcall -%}
|
||||
{{ log(load_result('main').data, info=False) }}
|
||||
{% else %}
|
||||
{{ log('Tag already exists: '+table_tag, info=False) }}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro set_table_tag_value_if_different(model_schema,table_name,tag_name,desired_tag_value,existing_tags) %}
|
||||
{{ log('Ensuring tag '+tag_name+' has value '+desired_tag_value+' at table level', info=False) }}
|
||||
{%- set existing_tag_for_table = existing_tags|selectattr('0','equalto','TABLE')|selectattr('1','equalto',tag_name|upper)|list -%}
|
||||
{{ log('Filtered tags for table:', info=False) }}
|
||||
{{ log(existing_tag_for_table[0], info=False) }}
|
||||
{% if existing_tag_for_table|length > 0 and existing_tag_for_table[0][2]==desired_tag_value %}
|
||||
{{ log('Correct tag value already exists', info=False) }}
|
||||
{% else %}
|
||||
{{ log('Setting tag value for '+tag_name+' to value '+desired_tag_value, info=False) }}
|
||||
{%- call statement('main', fetch_result=True) -%}
|
||||
alter table {{model_schema}}.{{table_name}} set tag {{tag_name}} = '{{desired_tag_value}}'
|
||||
{%- endcall -%}
|
||||
{{ log(load_result('main').data, info=False) }}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro set_column_tag_value_if_different(table_name,column_name,tag_name,desired_tag_value,existing_tags) %}
|
||||
{{ log('Ensuring tag '+tag_name+' has value '+desired_tag_value+' at column level', info=False) }}
|
||||
{%- set existing_tag_for_column = existing_tags|selectattr('0','equalto','COLUMN')|selectattr('1','equalto',tag_name|upper)|list -%}
|
||||
{{ log('Filtered tags for column:', info=False) }}
|
||||
{{ log(existing_tag_for_column[0], info=False) }}
|
||||
{% if existing_tag_for_column|length > 0 and existing_tag_for_column[0][2]==desired_tag_value %}
|
||||
{{ log('Correct tag value already exists', info=False) }}
|
||||
{% else %}
|
||||
{{ log('Setting tag value for '+tag_name+' to value '+desired_tag_value, info=False) }}
|
||||
{%- call statement('main', fetch_result=True) -%}
|
||||
alter table {{table_name}} modify column {{column_name}} set tag {{tag_name}} = '{{desired_tag_value}}'
|
||||
{%- endcall -%}
|
||||
{{ log(load_result('main').data, info=False) }}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro set_database_tag_value(tag_name,tag_value) %}
|
||||
{% set query %}
|
||||
create tag if not exists silver.{{tag_name}}
|
||||
{% endset %}
|
||||
{% do run_query(query) %}
|
||||
{% set query %}
|
||||
alter database {{target.database}} set tag {{target.database}}.silver.{{tag_name}} = '{{tag_value}}'
|
||||
{% endset %}
|
||||
{% do run_query(query) %}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro set_schema_tag_value(target_schema,tag_name,tag_value) %}
|
||||
{% set query %}
|
||||
create tag if not exists silver.{{tag_name}}
|
||||
{% endset %}
|
||||
{% do run_query(query) %}
|
||||
{% set query %}
|
||||
alter schema {{target.database}}.{{target_schema}} set tag {{target.database}}.silver.{{tag_name}} = '{{tag_value}}'
|
||||
{% endset %}
|
||||
{% do run_query(query) %}
|
||||
{% endmacro %}
|
||||
@ -1,6 +1,13 @@
|
||||
{{ config(
|
||||
materialized = 'view',
|
||||
secure = true
|
||||
secure = true,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'STAKING'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
with staking_actions as (
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
{{ config(
|
||||
materialized = 'view',
|
||||
secure = true
|
||||
secure = true,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'STAKING'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
WITH staking_pools AS (
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
{{ config(
|
||||
materialized = 'view',
|
||||
secure = true
|
||||
secure = true,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'DEFI, SWAPS'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
WITH dex_swaps AS (
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
{{ config(
|
||||
materialized = 'view',
|
||||
secure = true,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'NFT'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
with mints as (
|
||||
select *
|
||||
from {{ ref('silver__nft_mints_s3') }}
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
{{ config(
|
||||
materialized = 'view',
|
||||
secure = true
|
||||
secure = true,
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'PRICES'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
WITH oracle_prices AS (
|
||||
|
||||
@ -1,5 +1,12 @@
|
||||
{{ config(
|
||||
materialized = 'view'
|
||||
materialized = 'view',
|
||||
meta={
|
||||
'database_tags':{
|
||||
'table': {
|
||||
'PURPOSE': 'DEFI, TOKENS'
|
||||
}
|
||||
}
|
||||
}
|
||||
) }}
|
||||
|
||||
WITH nearblocks_ft_api AS (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user