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

Improve handling of overage spend #154

Merged
merged 3 commits into from
May 14, 2024
Merged
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
81 changes: 44 additions & 37 deletions models/hourly_spend.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ hours as (
from hour_spine
),

usage_in_currency_daily as (
select
* exclude(usage_type),
replace(usage_type, 'overage-', '') as usage_type
from {{ ref('stg_usage_in_currency_daily') }}
),
ian-whitestone marked this conversation as resolved.
Show resolved Hide resolved

storage_terabytes_daily as (
select
date,
Expand Down Expand Up @@ -163,14 +170,14 @@ data_transfer_spend_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
spend as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} as stg_usage_in_currency_daily on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = 'data transfer'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = 'data transfer'
and hours.hour::date = usage_in_currency_daily.usage_date
),

ai_services_spend_hourly as (
Expand All @@ -184,14 +191,14 @@ ai_services_spend_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
spend as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} as stg_usage_in_currency_daily on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = 'ai services'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = 'ai services'
and hours.hour::date = usage_in_currency_daily.usage_date
),

logging_spend_hourly as (
Expand All @@ -204,14 +211,14 @@ logging_spend_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
spend as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} as stg_usage_in_currency_daily on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = 'logging'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = 'logging'
and hours.hour::date = usage_in_currency_daily.usage_date
),

-- For now we just use the daily reported usage and evenly distribute it across the day
Expand All @@ -228,14 +235,14 @@ logging_spend_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
spend as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} as stg_usage_in_currency_daily on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = '{{ reader_usage_type }}'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = '{{ reader_usage_type }}'
and hours.hour::date = usage_in_currency_daily.usage_date
),
{% endfor %}

Expand All @@ -246,14 +253,14 @@ reader_adj_for_incl_cloud_services_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
0 as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} as stg_usage_in_currency_daily on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = 'reader adj for incl cloud services'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = 'reader adj for incl cloud services'
and hours.hour::date = usage_in_currency_daily.usage_date
),

reader_cloud_services_hourly as (
Expand All @@ -263,14 +270,14 @@ reader_cloud_services_hourly as (
null as storage_type,
null as warehouse_name,
null as database_name,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(stg_usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) + reader_adj_for_incl_cloud_services_hourly.spend as spend_net_cloud_services,
stg_usage_in_currency_daily.currency as currency
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) as spend,
coalesce(usage_in_currency_daily.usage_in_currency / hours.hours_thus_far, 0) + reader_adj_for_incl_cloud_services_hourly.spend as spend_net_cloud_services,
usage_in_currency_daily.currency as currency
from hours
left join {{ ref('stg_usage_in_currency_daily') }} on
stg_usage_in_currency_daily.account_locator = {{ account_locator() }}
and stg_usage_in_currency_daily.usage_type = 'reader cloud services'
and hours.hour::date = stg_usage_in_currency_daily.usage_date
left join usage_in_currency_daily on
usage_in_currency_daily.account_locator = {{ account_locator() }}
and usage_in_currency_daily.usage_type = 'reader cloud services'
and hours.hour::date = usage_in_currency_daily.usage_date
left join reader_adj_for_incl_cloud_services_hourly on
hours.hour = reader_adj_for_incl_cloud_services_hourly.hour
),
Expand Down
Loading