Skip to content

Commit

Permalink
test #10
Browse files Browse the repository at this point in the history
  • Loading branch information
laysabit committed Mar 14, 2024
1 parent bf99883 commit 4ae7a3b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
8 changes: 4 additions & 4 deletions models/marts/enriched_history_operations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ with
where
cast(batch_run_date as date) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 2 day)
and date(closed_at) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% if is_incremental() %}
{% if is_incremental() %}
and cast(batch_run_date as date) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
and date(closed_at) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% endif %}
Expand Down Expand Up @@ -76,7 +76,7 @@ with
where
cast(batch_run_date as date) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 2 day)
and date(closed_at) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% if is_incremental() %}
{% if is_incremental() %}
and cast(batch_run_date as date) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
and date(closed_at) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% endif %}
Expand Down Expand Up @@ -205,8 +205,8 @@ with
where
cast(batch_run_date as date) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 2 day)
and date(closed_at) < date_add(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% if is_incremental() %}
and cast(batch_run_date as date) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% if is_incremental() %}
and cast(batch_run_date as date) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
and date(closed_at) >= date_sub(date('{{ dbt_airflow_macros.ds() }}'), interval 1 day)
{% endif %}

Expand Down
33 changes: 18 additions & 15 deletions tests/anomaly_detection_trade_count.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{{ config(severity="warn") }}

with trade_counts as (
select
date(ledger_closed_at) as close_date
, count(*) as trade_count
from {{ ref('stg_history_trades') }}
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) < current_date()
group by close_date
with
trade_counts as (
select
date(ledger_closed_at) as close_date
, count(*) as trade_count
from {{ ref('stg_history_trades') }}
where
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) < current_date()
group by close_date
)

, bounds as (
select
(avg(trade_count) - (3 * stddev(trade_count))) as lower_bound
, (avg(trade_count) + (3 * stddev(trade_count))) as upper_bound
from trade_counts
, bounds as (
select
(avg(trade_count) - (3 * stddev(trade_count))) as lower_bound
, (avg(trade_count) + (3 * stddev(trade_count))) as upper_bound
from trade_counts
)

select
close_date
, trade_count
from trade_counts, bounds
where close_date = date_sub(current_date(), interval 1 day)
where
close_date = date_sub(current_date(), interval 1 day)
and trade_count >= upper_bound
or trade_count <= lower_bound
or trade_count <= lower_bound
80 changes: 42 additions & 38 deletions tests/anomaly_detection_trade_volume.sql
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
{{ config(severity="warn") }}

with base_trades as (
select
date(ledger_closed_at) as close_date
, sum(buying_amount) as amount
from {{ ref('stg_history_trades') }}
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) <= current_date()
group by close_date
)
with
base_trades as (
select
date(ledger_closed_at) as close_date
, sum(buying_amount) as amount
from {{ ref('stg_history_trades') }}
where
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) <= current_date()
group by close_date
)

, counter_trades as (
select
date(ledger_closed_at) as close_date
, sum(selling_amount) as amount
from {{ ref('stg_history_trades') }}
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) < current_date()
group by close_date
)
, counter_trades as (
select
date(ledger_closed_at) as close_date
, sum(selling_amount) as amount
from {{ ref('stg_history_trades') }}
where
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day)
and date(ledger_closed_at) < current_date()
group by close_date
)

, data_table as (
select
close_date
, sum(amount) as amount
from (
select *
from base_trades
union all
select *
from counter_trades
, data_table as (
select
close_date
, sum(amount) as amount
from (
select *
from base_trades
union all
select *
from counter_trades
)
group by close_date
)
group by close_date
)

, bounds as (
select
(avg(amount) - (3 * stddev(amount))) as lower_bound
, (avg(amount) + (3 * stddev(amount))) as upper_bound
from data_table
)
, bounds as (
select
(avg(amount) - (3 * stddev(amount))) as lower_bound
, (avg(amount) + (3 * stddev(amount))) as upper_bound
from data_table
)

select
close_date
, amount
from data_table, bounds
where close_date = date_sub(current_date(), interval 1 day)
where
close_date = date_sub(current_date(), interval 1 day)
and amount >= upper_bound
or amount <= lower_bound
or amount <= lower_bound

0 comments on commit 4ae7a3b

Please sign in to comment.