From 97a9b078cd2c5e81dc9a3ac109611b3378ddd8e0 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Wed, 2 Oct 2024 11:55:14 +0900 Subject: [PATCH 1/2] feat: add global_conf var `UNIFIED_COST_AGGREGATION_DAY` Signed-off-by: ImMin5 --- src/spaceone/cost_analysis/conf/global_conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/spaceone/cost_analysis/conf/global_conf.py b/src/spaceone/cost_analysis/conf/global_conf.py index c1669f03..fb9cd6f2 100644 --- a/src/spaceone/cost_analysis/conf/global_conf.py +++ b/src/spaceone/cost_analysis/conf/global_conf.py @@ -96,6 +96,7 @@ COST_REPORT_RUN_HOUR = 0 # Hour (UTC) COST_REPORT_RETRY_DAYS = 7 # Day UNIFIED_COST_RUN_HOUR = 0 # Hour (UTC) +UNIFIED_COST_AGGREGATION_DAY = 15 # Day DEFAULT_EXCHANGE_RATE = { # 'KRW': 1178.7, From ab8921bf65c85be891b028ba4e8fd4e338c98147 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Wed, 2 Oct 2024 13:32:12 +0900 Subject: [PATCH 2/2] fix: fix create unified cost error when workspace_id is None Signed-off-by: ImMin5 --- .../cost_analysis/manager/identity_manager.py | 5 ++++- .../cost_analysis/manager/unified_cost_manager.py | 2 +- .../cost_analysis/model/unified_cost/database.py | 2 +- .../cost_analysis/service/unified_cost_service.py | 15 ++++++++++----- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/spaceone/cost_analysis/manager/identity_manager.py b/src/spaceone/cost_analysis/manager/identity_manager.py index f60fb966..9f6c0557 100644 --- a/src/spaceone/cost_analysis/manager/identity_manager.py +++ b/src/spaceone/cost_analysis/manager/identity_manager.py @@ -1,4 +1,5 @@ import logging +from typing import Union from spaceone.core import cache from spaceone.core import config @@ -76,7 +77,9 @@ def check_workspace(self, workspace_id: str, domain_id: str) -> None: @cache.cacheable( key="cost-analysis:workspace-name:{domain_id}:{workspace_id}:name", expire=300 ) - def get_workspace(self, workspace_id: str, domain_id: str) -> str: + def get_workspace(self, workspace_id: Union[str, None], domain_id: str) -> str: + if not workspace_id: + return workspace_id try: workspace_info = self.identity_conn.dispatch( "Workspace.get", diff --git a/src/spaceone/cost_analysis/manager/unified_cost_manager.py b/src/spaceone/cost_analysis/manager/unified_cost_manager.py index 00a7254a..d81fe381 100644 --- a/src/spaceone/cost_analysis/manager/unified_cost_manager.py +++ b/src/spaceone/cost_analysis/manager/unified_cost_manager.py @@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs): def create_unified_cost(self, params: dict) -> UnifiedCost: def _rollback(vo: UnifiedCost): _LOGGER.info( - f"[create_unified_cost._rollback] Delete unified_cost : {vo.unified_cost_id}, {vo.unified_cost_id} " + f"[create_unified_cost._rollback] Delete unified_cost : {vo.unified_cost_id}, {vo.to_dict()} " ) vo.delete() diff --git a/src/spaceone/cost_analysis/model/unified_cost/database.py b/src/spaceone/cost_analysis/model/unified_cost/database.py index a36764df..1b80e07c 100644 --- a/src/spaceone/cost_analysis/model/unified_cost/database.py +++ b/src/spaceone/cost_analysis/model/unified_cost/database.py @@ -25,7 +25,7 @@ class UnifiedCost(MongoModel): service_account_name = StringField(max_length=255) data_source_name = StringField(max_length=255) project_name = StringField(max_length=255) - workspace_name = StringField(max_length=255) + workspace_name = StringField(max_length=255, default=None, null=True) service_account_id = StringField(max_length=40) data_source_id = StringField(max_length=40) project_id = StringField(max_length=40, default=None, null=True) diff --git a/src/spaceone/cost_analysis/service/unified_cost_service.py b/src/spaceone/cost_analysis/service/unified_cost_service.py index 248966ef..efd9de54 100644 --- a/src/spaceone/cost_analysis/service/unified_cost_service.py +++ b/src/spaceone/cost_analysis/service/unified_cost_service.py @@ -327,11 +327,15 @@ def create_unified_cost_with_workspace( exchange_date: datetime, aggregation_date: datetime, is_confirmed: bool = False, - ): + ) -> None: + if workspace_id: + return + identity_mgr = IdentityManager(token=config.get_global("TOKEN")) - workspace_name = identity_mgr.get_workspace(workspace_id, domain_id) workspace_ids = [workspace_id] + workspace_name = identity_mgr.get_workspace(workspace_id, domain_id) + v_workspace_ids = self._get_virtual_workspace_ids_from_ds_account( domain_id, workspace_id ) @@ -421,11 +425,12 @@ def create_unified_cost_with_workspace( aggregated_unified_cost_data["domain_id"] = domain_id # set workspace name - aggregated_unified_cost_data["workspace_id"] = workspace_id - aggregated_unified_cost_data["workspace_name"] = workspace_name + if workspace_id: + aggregated_unified_cost_data["workspace_id"] = workspace_id + aggregated_unified_cost_data["workspace_name"] = workspace_name # set project name - project_id = aggregated_unified_cost_data.get("project_id") + project_id = aggregated_unified_cost_data.get("project_id", None) aggregated_unified_cost_data["project_name"] = project_name_map.get( project_id, project_id )