diff --git a/koku/api/report/ocp/provider_map.py b/koku/api/report/ocp/provider_map.py index 84ad35ecab..dc9bde403b 100644 --- a/koku/api/report/ocp/provider_map.py +++ b/koku/api/report/ocp/provider_map.py @@ -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 @@ -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": { @@ -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), @@ -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", @@ -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": { @@ -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())), + ) diff --git a/koku/api/report/ocp/query_handler.py b/koku/api/report/ocp/query_handler.py index e48a54e3a6..ebef527f7a 100644 --- a/koku/api/report/ocp/query_handler.py +++ b/koku/api/report/ocp/query_handler.py @@ -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"}, } diff --git a/koku/masu/database/sql/openshift/cost_model/distribute_cost/distribute_unattributed_storage_cost.sql b/koku/masu/database/sql/openshift/cost_model/distribute_cost/distribute_unattributed_storage_cost.sql index dbed3b8067..8ca7fa6148 100644 --- a/koku/masu/database/sql/openshift/cost_model/distribute_cost/distribute_unattributed_storage_cost.sql +++ b/koku/masu/database/sql/openshift/cost_model/distribute_cost/distribute_unattributed_storage_cost.sql @@ -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 @@ -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(), @@ -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 %}