Skip to content

Commit

Permalink
Daily rates improvements (#114)
Browse files Browse the repository at this point in the history
* Daily rates improvements

* Release 4.2.1
  • Loading branch information
ian-whitestone authored Jun 4, 2023
1 parent ab91c0b commit 6c440e1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 96 deletions.
7 changes: 7 additions & 0 deletions .changes/4.2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## dbt-snowflake-monitoring 4.2.1 - June 03, 2023

### Fixes

- Daily rates improvements ([#114](https://github.com/get-select/dbt-snowflake-monitoring/pull/114))


8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).

## dbt-snowflake-monitoring 4.2.1 - June 03, 2023

### Fixes

- Daily rates improvements ([#114](https://github.com/get-select/dbt-snowflake-monitoring/pull/114))



## dbt-snowflake-monitoring 4.2.0 - May 27, 2023

### Features
Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'dbt_snowflake_monitoring'
version: '4.2.0'
version: '4.3.0'
config-version: 2

profile: dbt_snowflake_monitoring
Expand Down
6 changes: 6 additions & 0 deletions integration_test_project/seeds/monthly_spend_fixture.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ MONTH,SERVICE,SPEND
2022-04-01,Adj For Incl Cloud Services,-74.921528302
2022-04-01,Cloud Services,74.921528302
2022-04-01,Automatic Clustering,0.051113362
2022-04-01,Snowpipe,0
2022-04-01,Serverless Tasks,0
2022-04-01,Search Optimization,0
2022-04-01,Replication,0
2022-04-01,Query Acceleration,0
2022-04-01,Materialized Views,0
8 changes: 2 additions & 6 deletions models/cost_per_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,15 @@ query_cost as (
select
query_seconds_per_hour.*,
credits_billed_hourly.credits_used_compute * daily_rates.effective_rate as actual_warehouse_cost,
credits_billed_hourly.credits_used_compute * query_seconds_per_hour.fraction_of_total_query_time_in_hour * coalesce(daily_rates.effective_rate, current_rates.effective_rate) as allocated_compute_cost_in_hour
credits_billed_hourly.credits_used_compute * query_seconds_per_hour.fraction_of_total_query_time_in_hour * daily_rates.effective_rate as allocated_compute_cost_in_hour
from query_seconds_per_hour
inner join credits_billed_hourly
on query_seconds_per_hour.warehouse_id = credits_billed_hourly.warehouse_id
and query_seconds_per_hour.hour = credits_billed_hourly.hour
left join {{ ref('daily_rates') }}
inner join {{ ref('daily_rates') }}
on date(query_seconds_per_hour.start_time) = daily_rates.date
and daily_rates.service_type = 'COMPUTE'
and daily_rates.usage_type = 'compute'
inner join {{ ref('daily_rates') }} as current_rates
on current_rates.is_latest_rate
and current_rates.service_type = 'COMPUTE'
and current_rates.usage_type = 'compute'
),

cost_per_query as (
Expand Down
52 changes: 40 additions & 12 deletions models/daily_rates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dates_base as (
from table(generator(rowcount => (365 * 3)))
),

rate_sheet_daily as (
rate_sheet_daily_base as (
select
date,
usage_type,
Expand All @@ -39,27 +39,53 @@ rate_sheet_daily as (
account_locator = {{ account_locator }}
),

stop_thresholds as (
select min(date) as start_date
from rate_sheet_daily_base

union all

select min(date) as start_date
from {{ ref('remaining_balance_daily_without_contract_view') }}
),

date_range as (
select
max(start_date) as start_date,
current_date as end_date
from stop_thresholds
),

remaining_balance_daily as (
select
date,
free_usage_balance + capacity_balance + on_demand_consumption_balance + rollover_balance as remaining_balance,
remaining_balance < 0 as is_account_in_overage
from {{ ref('stg_remaining_balance_daily') }}
from {{ ref('remaining_balance_daily_without_contract_view') }}
),

rates_date_range as (
latest_remaining_balance_daily as (
select
min(date) as start_date,
max(date) as end_date
from rate_sheet_daily
date,
remaining_balance,
is_account_in_overage
from remaining_balance_daily
qualify row_number() over (order by date desc) = 1
),

rate_sheet_daily as (
select rate_sheet_daily_base.*
from rate_sheet_daily_base
inner join date_range
on rate_sheet_daily_base.date between date_range.start_date and date_range.end_date
),

rates_date_range_w_usage_types as (
select
rates_date_range.start_date,
rates_date_range.end_date,
date_range.start_date,
date_range.end_date,
usage_types.usage_type
from rates_date_range
from date_range
cross join (select distinct usage_type from rate_sheet_daily) as usage_types
),

Expand Down Expand Up @@ -93,14 +119,16 @@ rates_w_overage as (
) as currency,
base.usage_type like 'overage-%' as is_overage_rate,
replace(base.usage_type, 'overage-', '') as associated_usage_type,
coalesce(remaining_balance_daily.is_account_in_overage, latest_remaining_balance_daily.is_account_in_overage) as _is_account_in_overage,
case
when remaining_balance_daily.is_account_in_overage and is_overage_rate then 1
when not remaining_balance_daily.is_account_in_overage and not is_overage_rate then 1
when _is_account_in_overage and is_overage_rate then 1
when not _is_account_in_overage and not is_overage_rate then 1
else 0
end as rate_priority

from base
inner join remaining_balance_daily
inner join latest_remaining_balance_daily
left join remaining_balance_daily
on base.date = remaining_balance_daily.date
left join rate_sheet_daily
on base.date = rate_sheet_daily.date
Expand Down
Loading

0 comments on commit 6c440e1

Please sign in to comment.