From 43f385904ada5e16fd6f08451dc75efd25e39dd5 Mon Sep 17 00:00:00 2001 From: Luke Couzens Date: Thu, 16 Jan 2025 14:15:43 +0000 Subject: [PATCH] Fix amortised cost calculations (#5446) * Fix amortised cost calculations missing additional cost types. --- ...ing_awscostentrylineitem_daily_summary.sql | 8 +- ...entrylineitem_summary_by_ec2_compute_p.sql | 100 +++++++++--------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql index f8f99d3e9e..c289080594 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql @@ -112,10 +112,10 @@ FROM ( sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, sum( CASE - WHEN lineitem_lineitemtype='Tax' - OR lineitem_lineitemtype='Usage' - THEN lineitem_unblendedcost - ELSE savingsplan_savingsplaneffectivecost + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + OR lineitem_lineitemtype='SavingsPlanNegation' + THEN savingsplan_savingsplaneffectivecost + ELSE lineitem_unblendedcost END ) as calculated_amortized_cost, sum(pricing_publicondemandcost) as public_on_demand_cost, diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql index 0b014be4a1..2b674ab73a 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql @@ -112,58 +112,58 @@ SELECT uuid() as uuid, aa.id as account_alias_id FROM ( SELECT min(date(lineitem_usagestartdate)) as usage_start, - max(date(lineitem_usagestartdate)) as usage_end, - max(lineitem_usageaccountid) as usage_account_id, - lineitem_resourceid as resource_id, - max(nullif(product_operatingsystem, '')) as operating_system, - max(nullif(product_region, '')) as region, - max(nullif(pricing_unit, '')) as unit, - -- SavingsPlanNegation needs to be negated to prevent duplicate usage COST-5369 - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanNegation' - THEN 0.0 - ELSE lineitem_usageamount - END - ) as usage_amount, - max(lineitem_normalizationfactor) as normalization_factor, - sum(lineitem_normalizedusageamount) as normalized_usage_amount, - max(lineitem_currencycode) as currency_code, - max(lineitem_unblendedrate) as unblended_rate, - /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items - that offset that cost. - https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html - */ - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_unblendedcost - END - ) as unblended_cost, - max(lineitem_blendedrate) as blended_rate, + max(date(lineitem_usagestartdate)) as usage_end, + max(lineitem_usageaccountid) as usage_account_id, + lineitem_resourceid as resource_id, + max(nullif(product_operatingsystem, '')) as operating_system, + max(nullif(product_region, '')) as region, + max(nullif(pricing_unit, '')) as unit, + -- SavingsPlanNegation needs to be negated to prevent duplicate usage COST-5369 + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanNegation' + THEN 0.0 + ELSE lineitem_usageamount + END + ) as usage_amount, + max(lineitem_normalizationfactor) as normalization_factor, + sum(lineitem_normalizedusageamount) as normalized_usage_amount, + max(lineitem_currencycode) as currency_code, + max(lineitem_unblendedrate) as unblended_rate, + /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items + that offset that cost. + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + */ + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_unblendedcost + END + ) as unblended_cost, + max(lineitem_blendedrate) as blended_rate, /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items - that offset that cost. - https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + that offset that cost. + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html */ - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_blendedcost - END - ) as blended_cost, - sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, - sum( - CASE - WHEN lineitem_lineitemtype='Tax' - OR lineitem_lineitemtype='Usage' - THEN lineitem_unblendedcost - ELSE savingsplan_savingsplaneffectivecost - END - ) as calculated_amortized_cost, - sum(pricing_publicondemandcost) as public_on_demand_cost, - max(pricing_publicondemandrate) as public_on_demand_rate + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_blendedcost + END + ) as blended_cost, + sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + OR lineitem_lineitemtype='SavingsPlanNegation' + THEN savingsplan_savingsplaneffectivecost + ELSE lineitem_unblendedcost + END + ) as calculated_amortized_cost, + sum(pricing_publicondemandcost) as public_on_demand_cost, + max(pricing_publicondemandrate) as public_on_demand_rate FROM hive.{{schema | sqlsafe}}.aws_line_items_daily as lid WHERE source = '{{source_uuid | sqlsafe}}' AND year = '{{year | sqlsafe}}'