-
Notifications
You must be signed in to change notification settings - Fork 4
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
Move tests from scheduled queries / business queries to DBT #83
Merged
amishas157
merged 9 commits into
master
from
patch/hubble-520/move-scheduled-queries-to-dbt-tests
Sep 12, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
88d0fed
Move tests from scheduled queries
amishas157 a05afc4
Move tests from scheduled queries and set freshness check to error in…
amishas157 8dc3b54
Update generic tests
amishas157 acfc893
remove trailing whitespace
amishas157 c62a947
Move adhoc business queries
amishas157 a86ea70
remove semicolon
amishas157 228f0dc
Fix the reference
amishas157 6bd6abf
feedback
amishas157 74dad5a
Use staging tables in test instead of source to handle test env
amishas157 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
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,17 @@ | ||
{{ config( | ||
severity="error" | ||
, tags=["singular_test"] | ||
) | ||
}} | ||
|
||
with bucketlist_db_size as ( | ||
select sequence, | ||
closed_at, | ||
total_byte_size_of_bucket_list / 1000000000 as bl_db_gb | ||
from {{ ref('stg_history_ledgers') }} | ||
where closed_at >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 HOUR ) | ||
-- alert when the bucketlist has grown larger than 12 gb | ||
and total_byte_size_of_bucket_list / 1000000000 >= 12 | ||
) | ||
|
||
select * from bucketlist_db_size |
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,36 @@ | ||
{{ config( | ||
severity="error" | ||
, tags=["singular_test"] | ||
) | ||
}} | ||
|
||
-- Enriched_history_operations table is dependent on the | ||
-- history_operations table to load. It is assumed that | ||
-- any id present in the upstream table should be loaded in | ||
-- the downstream. If records are not present, alert the team. | ||
WITH find_missing AS ( | ||
SELECT op.id, | ||
op.batch_run_date, | ||
op.batch_id | ||
FROM {{ ref('stg_history_operations') }} op | ||
LEFT OUTER JOIN {{ ref('enriched_history_operations') }} eho | ||
ON op.id = eho.op_id | ||
WHERE eho.op_id IS NULL | ||
-- Scan only the last 24 hours of data. Alert runs intraday so failures | ||
-- are caught and resolved quickly. | ||
AND TIMESTAMP(op.batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
), | ||
find_max_batch AS ( | ||
SELECT MAX(batch_run_date) AS max_batch | ||
FROM {{ ref('stg_history_operations') }} | ||
WHERE TIMESTAMP(batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
) | ||
SELECT batch_run_date, | ||
batch_id, | ||
count(*) | ||
FROM find_missing | ||
-- Account for delay in loading history_operations table prior to | ||
-- enriched_history_operations table being loaded. | ||
WHERE batch_run_date != (SELECT max_batch FROM find_max_batch) | ||
GROUP BY 1, 2 | ||
ORDER BY 1 |
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ with | |
, batch_id | ||
, closed_at | ||
, max(sequence) as max_sequence | ||
from {{ source('crypto_stellar', 'history_ledgers') }} | ||
from {{ ref('stg_history_ledgers') }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙏 |
||
where closed_at > TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 7 DAY ) | ||
group by id, batch_id, closed_at | ||
) | ||
|
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,65 @@ | ||
{{ config( | ||
severity="error" | ||
, tags=["singular_test"] | ||
) | ||
}} | ||
|
||
-- Query studies the number of reported transactions and operations | ||
-- reported and committed per ledger in history_ledgers with the | ||
-- actual transaction count and operation count in the ledger. | ||
-- If the counts mismatch, there was a batch processing error | ||
-- and transactions or operations were dropped from the dataset. | ||
-- Get the actual count of transactions per ledger | ||
WITH txn_count AS ( | ||
SELECT ledger_sequence, COUNT(id) as txn_transaction_count | ||
FROM {{ ref('stg_history_transactions') }} | ||
--Take all ledgers committed in the last 36 hours to validate newly written data | ||
-- Alert runs at 12pm UTC in GCP which creates the 36 hour interval | ||
WHERE TIMESTAMP(batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
GROUP BY ledger_sequence | ||
), | ||
-- Get the actual count of operations per ledger | ||
operation_count AS ( | ||
SELECT A.ledger_sequence, COUNT(B.id) AS op_operation_count | ||
FROM {{ ref('stg_history_transactions') }} A | ||
JOIN {{ ref('stg_history_operations') }} B | ||
ON A.id = B.transaction_id | ||
WHERE TIMESTAMP(A.batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
AND TIMESTAMP(B.batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
GROUP BY A.ledger_sequence | ||
), | ||
-- compare actual counts with the counts reported in the ledgers table | ||
final_counts AS ( | ||
SELECT A.sequence, A.closed_at, A.batch_id, | ||
A.tx_set_operation_count as expected_operation_count, | ||
A.operation_count, | ||
(A.failed_transaction_count + A.successful_transaction_count) as expected_transaction_count, | ||
COALESCE(B.txn_transaction_count, 0) as actual_transaction_count, | ||
COALESCE(C.op_operation_count, 0) as actual_operation_count | ||
FROM {{ ref('stg_history_ledgers') }} A | ||
LEFT OUTER JOIN txn_count B | ||
ON A.sequence = B.ledger_sequence | ||
LEFT OUTER JOIN operation_count C | ||
ON A.sequence = C.ledger_sequence | ||
WHERE TIMESTAMP(A.batch_run_date) >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 DAY ) | ||
) | ||
, raw_values AS ( | ||
SELECT sequence, closed_at, batch_id, | ||
expected_transaction_count, actual_transaction_count, | ||
expected_operation_count, actual_operation_count | ||
FROM final_counts | ||
WHERE | ||
((expected_transaction_count <> actual_transaction_count) | ||
OR (expected_operation_count <> actual_operation_count)) | ||
) | ||
SELECT batch_id, | ||
SUM(expected_transaction_count) as exp_txn_count, | ||
SUM(actual_transaction_count ) as actual_txn_count, | ||
SUM(expected_operation_count ) as exp_op_count, | ||
SUM(actual_operation_count ) as actual_op_count | ||
FROM raw_values | ||
--@TODO: figure out a more precise delay for ledgers. Since tables are loaded on a 15-30 min delay, | ||
-- we do not want a premature alert to row count mismatches when it could be loading latency | ||
WHERE closed_at <= TIMESTAMP_ADD('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL -180 MINUTE ) | ||
GROUP BY batch_id | ||
ORDER BY batch_id |
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,17 @@ | ||
{{ config( | ||
severity="warn" | ||
, tags=["singular_test"] | ||
) | ||
}} | ||
|
||
with surge_pricing_check as ( | ||
select inclusion_fee_charged, | ||
ledger_sequence, | ||
closed_at | ||
from {{ ref('enriched_history_operations_soroban') }} | ||
where closed_at >= TIMESTAMP_SUB('{{ dbt_airflow_macros.ts(timezone=none) }}', INTERVAL 1 HOUR ) | ||
-- inclusion fees over 100 stroops indicate surge pricing on the network | ||
and inclusion_fee_charged > 100 | ||
) | ||
|
||
select * from surge_pricing_check |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this tag runs every 30 mins in airflow. This is a higher frequency compared to what the cloud function and scheduled query tests used to run at. Which is good.
Just mentioning this in case we get noisy alerts where we might want to adjust the query and/or the frequency the tests are run (possibly with a separate dbt tag).