From e7071e683aecbef2c80638286fb9825de10e3860 Mon Sep 17 00:00:00 2001 From: David N Date: Wed, 3 Jul 2024 14:31:55 -0400 Subject: [PATCH 1/5] [COST-5148] update insert sql filter out empty resource ids offset savings from SavingsPlanCoveredUsage --- ...stentrylineitem_summary_by_ec2_compute.sql | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql index d543c0307a..4ed46fc918 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql @@ -91,12 +91,25 @@ FROM ( resourcetags as tags, costcategory, nullif(pricing_unit, '') as unit, - sum(lineitem_usageamount) as usage_amount, + -- according to AWS docs on savings plans, + -- https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + -- SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items that offset that cost and usage. + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_usageamount + ) 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, - sum(lineitem_unblendedcost) as unblended_cost, + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_unblendedcost + ) as unblended_cost, max(lineitem_blendedrate) as blended_rate, sum(lineitem_blendedcost) as blended_cost, sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, @@ -116,7 +129,8 @@ FROM ( AND year = '{{year | sqlsafe}}' AND month = '{{month | sqlsafe}}' AND lineitem_productcode = 'AmazonEC2' - AND product_productfamily LIKE '%Compute%' + AND product_productfamily LIKE '%Compute Instance%' + AND lineitem_resourceid != '' GROUP BY lineitem_resourceid, lineitem_usageaccountid, product_instancetype, From 122a4fd38a8d5625536343e7cbf8325606cde376 Mon Sep 17 00:00:00 2001 From: Lucas Bacciotti Date: Fri, 5 Jul 2024 11:42:03 +0100 Subject: [PATCH 2/5] closing CASE statement --- .../reporting_awscostentrylineitem_summary_by_ec2_compute.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql index 4ed46fc918..9dfef5c97f 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql @@ -99,6 +99,7 @@ FROM ( WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' THEN 0.0 ELSE lineitem_usageamount + END ) as usage_amount, max(lineitem_normalizationfactor) as normalization_factor, sum(lineitem_normalizedusageamount) as normalized_usage_amount, @@ -109,6 +110,7 @@ FROM ( WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' THEN 0.0 ELSE lineitem_unblendedcost + END ) as unblended_cost, max(lineitem_blendedrate) as blended_rate, sum(lineitem_blendedcost) as blended_cost, From 4b5645fd98f14ac33b4b4dc8b180b8273765d6e7 Mon Sep 17 00:00:00 2001 From: David N Date: Mon, 8 Jul 2024 09:01:23 -0400 Subject: [PATCH 3/5] clean up comment --- ...rting_awscostentrylineitem_summary_by_ec2_compute.sql | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql index 9dfef5c97f..c61460797e 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql @@ -91,9 +91,12 @@ FROM ( resourcetags as tags, costcategory, nullif(pricing_unit, '') as unit, - -- according to AWS docs on savings plans, - -- https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html - -- SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items that offset that cost and usage. + /* According to AWS docs on Savings Plans: + SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items + that offset that cost and usage. + For more information, refer to: + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + */ sum( CASE WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' From b930c1641ec6a5350fd9345d58444ed4a05c773e Mon Sep 17 00:00:00 2001 From: David N Date: Mon, 8 Jul 2024 17:06:44 -0400 Subject: [PATCH 4/5] remove case stmts in favor of filtering out SavingsPlanCoveredUsage --- ...stentrylineitem_summary_by_ec2_compute.sql | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql index c61460797e..39b1088560 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql @@ -91,30 +91,12 @@ FROM ( resourcetags as tags, costcategory, nullif(pricing_unit, '') as unit, - /* According to AWS docs on Savings Plans: - SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items - that offset that cost and usage. - For more information, refer to: - https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html - */ - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_usageamount - END - ) as usage_amount, + sum(lineitem_usageamount) 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, - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_unblendedcost - END - ) as unblended_cost, + sum(lineitem_unblendedcost) as unblended_cost, max(lineitem_blendedrate) as blended_rate, sum(lineitem_blendedcost) as blended_cost, sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, @@ -135,6 +117,11 @@ FROM ( AND month = '{{month | sqlsafe}}' AND lineitem_productcode = 'AmazonEC2' AND product_productfamily LIKE '%Compute Instance%' + /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items + that offset that cost and usage. + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + */ + AND lineitem_lineitemtype != 'SavingsPlanCoveredUsage' AND lineitem_resourceid != '' GROUP BY lineitem_resourceid, lineitem_usageaccountid, From 721420ba4a26324c4e40ad622e8faac700ca1382 Mon Sep 17 00:00:00 2001 From: David N Date: Mon, 8 Jul 2024 17:07:45 -0400 Subject: [PATCH 5/5] clean up --- .../reporting_awscostentrylineitem_summary_by_ec2_compute.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql index 39b1088560..b148591924 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute.sql @@ -117,12 +117,12 @@ FROM ( AND month = '{{month | sqlsafe}}' AND lineitem_productcode = 'AmazonEC2' AND product_productfamily LIKE '%Compute Instance%' + AND lineitem_resourceid != '' /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items that offset that cost and usage. https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html */ AND lineitem_lineitemtype != 'SavingsPlanCoveredUsage' - AND lineitem_resourceid != '' GROUP BY lineitem_resourceid, lineitem_usageaccountid, product_instancetype,