Skip to content

Commit

Permalink
Fix raw_currency conversion for distributed cost.
Browse files Browse the repository at this point in the history
  • Loading branch information
myersCody committed Jul 2, 2024
1 parent 8df33cd commit 6bbc3b0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
109 changes: 72 additions & 37 deletions koku/api/report/ocp/provider_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,6 @@ def __cost_model_volume_cost(self, cost_model_rate_type=None):
* Coalesce("exchange_rate", Value(1, output_field=DecimalField())),
)

def __cost_model_distributed_cost(self, cost_model_rate_type=None):
"""Return ORM term for cost model distributed cost."""

if cost_model_rate_type:
return Sum(
Case(
When(
cost_model_rate_type=cost_model_rate_type,
then=Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())),
),
default=Value(0, output_field=DecimalField()),
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField())),
)
else:
return Sum(
(Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField())),
)

def __init__(self, provider, report_type, schema_name):
"""Constructor."""
self._schema_name = schema_name
Expand Down Expand Up @@ -247,9 +227,13 @@ def __init__(self, provider, report_type, schema_name):
"cost_total_distributed": self.cloud_infrastructure_cost_by_project
+ self.markup_cost_by_project
+ self.cost_model_cost
+ self.cost_model_distributed_cost_by_project,
"cost_platform_distributed": self.platform_distributed_cost_by_project,
"cost_worker_unallocated_distributed": self.worker_unallocated_distributed_cost_by_project,
+ self.distributed_platform_cost
+ self.distributed_worker_cost
+ self.distributed_unattributed_network_cost
+ self.distributed_unattributed_storage_cost,
"cost_platform_distributed": self.distributed_platform_cost,
"cost_worker_unallocated_distributed": self.distributed_worker_cost,
"cost_storage_unattributed_distributed": self.distributed_unattributed_storage_cost,
},
"default_ordering": {"cost_total": "desc"},
"annotations": {
Expand All @@ -272,9 +256,13 @@ def __init__(self, provider, report_type, schema_name):
"cost_total_distributed": self.cloud_infrastructure_cost_by_project
+ self.markup_cost_by_project
+ self.cost_model_cost
+ self.cost_model_distributed_cost_by_project,
"cost_platform_distributed": self.platform_distributed_cost_by_project,
"cost_worker_unallocated_distributed": self.worker_unallocated_distributed_cost_by_project,
+ self.distributed_platform_cost
+ self.distributed_worker_cost
+ self.distributed_unattributed_storage_cost
+ self.distributed_unattributed_network_cost,
"cost_platform_distributed": self.distributed_platform_cost,
"cost_worker_unallocated_distributed": self.distributed_worker_cost,
"cost_storage_unattributed_distributed": self.distributed_unattributed_storage_cost,
# the `currency_annotation` is inserted by the `annotations` property of the query-handler
"cost_units": Coalesce("currency_annotation", Value("USD", output_field=CharField())),
"clusters": ArrayAgg(Coalesce("cluster_alias", "cluster_id"), distinct=True),
Expand All @@ -291,7 +279,10 @@ def __init__(self, provider, report_type, schema_name):
"cost_total_distributed": self.cloud_infrastructure_cost_by_project
+ self.markup_cost_by_project
+ self.cost_model_cost
+ self.cost_model_distributed_cost_by_project,
+ self.distributed_platform_cost
+ self.distributed_worker_cost
+ self.distributed_unattributed_storage_cost
+ self.distributed_unattributed_network_cost,
},
"filter": [{}],
"cost_units_key": "raw_currency",
Expand Down Expand Up @@ -367,7 +358,10 @@ def __init__(self, provider, report_type, schema_name):
"cost_total_distributed": self.cloud_infrastructure_cost_by_project
+ self.markup_cost_by_project
+ self.cost_model_cost
+ self.cost_model_distributed_cost_by_project,
+ self.distributed_platform_cost
+ self.distributed_worker_cost
+ self.distributed_unattributed_storage_cost
+ self.distributed_unattributed_network_cost,
},
"filter": [{"field": "data_source", "operation": "exact", "parameter": "Pod"}],
"conditionals": {
Expand Down Expand Up @@ -918,16 +912,57 @@ def markup_cost_by_project(self):
)

@cached_property
def platform_distributed_cost_by_project(self):
"""Return platform distributed cost model costs."""
return self.__cost_model_distributed_cost(cost_model_rate_type="platform_distributed")
def distributed_unattributed_storage_cost(self):
"""The unattributed storage cost needs to have the infra exchange rate applied to it."""
return Sum(
Case(
When(
cost_model_rate_type="unattributed_storage",
then=Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())),
),
default=Value(0, output_field=DecimalField()),
)
* Coalesce("infra_exchange_rate", Value(1, output_field=DecimalField()))
)

@cached_property
def distributed_unattributed_network_cost(self):
"""The unattributed network cost needs to have the infra exchange rate applied to it."""
return Sum(
Case(
When(
cost_model_rate_type="unattributed_network",
then=Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())),
),
default=Value(0, output_field=DecimalField()),
)
* Coalesce("infra_exchange_rate", Value(1, output_field=DecimalField()))
)

@cached_property
def worker_unallocated_distributed_cost_by_project(self):
"""Return worker unallocated distributed cost model costs."""
return self.__cost_model_distributed_cost(cost_model_rate_type="worker_distributed")
def distributed_platform_cost(self):
"""Platform distributed cost"""
return Sum(
Case(
When(
cost_model_rate_type="platform_distributed",
then=Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())),
),
default=Value(0, output_field=DecimalField()),
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField())),
)

@cached_property
def cost_model_distributed_cost_by_project(self):
"""Return cost model distributed cost."""
return self.__cost_model_distributed_cost()
def distributed_worker_cost(self):
"""Worker unallocated distributed cost"""
return Sum(
Case(
When(
cost_model_rate_type="worker_distributed",
then=Coalesce(F("distributed_cost"), Value(0, output_field=DecimalField())),
),
default=Value(0, output_field=DecimalField()),
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField())),
)
1 change: 1 addition & 0 deletions koku/api/report/ocp/query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(self, parameters):
"cost_usage": {"key": "usage", "group": "cost"},
"cost_platform_distributed": {"key": "platform_distributed", "group": "cost"},
"cost_worker_unallocated_distributed": {"key": "worker_unallocated_distributed", "group": "cost"},
"cost_storage_unattributed_distributed": {"key": "storage_unattributed_distributed", "group": "cost"},
"cost_total_distributed": {"key": "distributed", "group": "cost"},
"cost_total": {"key": "total", "group": "cost"},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ cte_line_items as (
COALESCE(cost_model_volume_cost, 0)
)
END AS distributed_cost,
max(cost_category_id) as cost_category_id
max(cost_category_id) as cost_category_id,
max(raw_currency) as raw_currency
FROM {{schema | sqlsafe}}.reporting_ocpusagelineitem_daily_summary AS lids
LEFT JOIN unattributed_storage_cost as usc
ON usc.usage_start = lids.usage_start
Expand Down Expand Up @@ -130,7 +131,8 @@ INSERT INTO {{schema | sqlsafe}}.reporting_ocpusagelineitem_daily_summary (
source_uuid,
cost_model_rate_type,
distributed_cost,
cost_category_id
cost_category_id,
raw_currency
)
SELECT
uuid_generate_v4(),
Expand Down Expand Up @@ -169,7 +171,8 @@ SELECT
UUID '{{source_uuid | sqlsafe}}' as source_uuid,
'unattributed_storage' as cost_model_rate_type,
ctl.distributed_cost,
ctl.cost_category_id
ctl.cost_category_id,
ctl.raw_currency
FROM cte_line_items as ctl
WHERE ctl.distributed_cost != 0;
{% endif %}

0 comments on commit 6bbc3b0

Please sign in to comment.