From 5b677f5b3605a7da3ed3860fad7800e23bd91128 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Thu, 2 May 2024 23:47:14 +0900 Subject: [PATCH] feat: modify pay_as_you_go condition Signed-off-by: ImMin5 --- .../cost_analysis/manager/cost_manager.py | 11 ++-- .../cost_analysis/manager/job_manager.py | 62 +++++++++++-------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/cloudforet/cost_analysis/manager/cost_manager.py b/src/cloudforet/cost_analysis/manager/cost_manager.py index 37d8804..d661580 100644 --- a/src/cloudforet/cost_analysis/manager/cost_manager.py +++ b/src/cloudforet/cost_analysis/manager/cost_manager.py @@ -97,7 +97,7 @@ def get_data( def _make_cost_data( self, results: list, end: datetime, options: dict, tenant_id: str = None - ): + ) -> list: """Source Data Model""" costs_data = [] @@ -249,9 +249,12 @@ def _get_cost_from_result_with_options(self, result: dict, options: dict) -> flo return cost def get_pay_as_you_go_cost(self, result: dict, cost: float = 0.0) -> float: - if pay_g_billing_price := result.get("paygcostinbillingcurrency"): - cost_pay_as_you_go = pay_g_billing_price - elif pay_g_price := result.get("paygprice", 0.0): + if "paygcostinbillingcurrency" in result: + cost_pay_as_you_go = result.get("paygcostinbillingcurrency", 0.0) + elif "paygprice" in result: + pay_g_price = self._convert_str_to_float_format( + result.get("paygprice", 0.0) + ) usage_quantity = self._convert_str_to_float_format( result.get("quantity", 0.0) ) diff --git a/src/cloudforet/cost_analysis/manager/job_manager.py b/src/cloudforet/cost_analysis/manager/job_manager.py index 513c275..d3981b9 100644 --- a/src/cloudforet/cost_analysis/manager/job_manager.py +++ b/src/cloudforet/cost_analysis/manager/job_manager.py @@ -73,11 +73,10 @@ def get_tasks( } } ) - changed.append({"start": start_month}) - synced_accounts.extend( - {"account_id": tenant_id} - for tenant_id in divided_customer_tenant_info + synced_accounts = self._extend_synced_accounts( + synced_accounts, divided_customer_tenant_info ) + changed.append({"start": start_month}) if first_sync_tenants: first_sync_start_month = self._get_start_month(start=None) tasks.append( @@ -91,9 +90,15 @@ def get_tasks( } } ) - changed.append({"start": first_sync_start_month}) - synced_accounts.extend( - {"account_id": tenant_id} for tenant_id in first_sync_tenants + for tenant_id in first_sync_tenants: + changed.append( + { + "start": first_sync_start_month, + "filter": {"additional_info.Tenant Id": tenant_id}, + } + ) + synced_accounts = self._extend_synced_accounts( + synced_accounts, first_sync_tenants ) else: tasks = [ @@ -167,27 +172,34 @@ def _get_customer_tenants( if len(customer_tenants) == 0: raise ERROR_EMPTY_CUSTOMER_TENANTS(customer_tenants=customer_tenants) - if linked_accounts: - linked_accounts_map = { - linked_account["account_id"]: linked_account - for linked_account in linked_accounts - } - - for customer_tenant_id in customer_tenants: - if linked_account_info := linked_accounts_map.get(customer_tenant_id): - if not linked_account_info.get("is_sync"): - first_sync_customer_tenants.append( - linked_account_info.get("account_id") - ) - customer_tenants.remove(customer_tenant_id) - else: - _LOGGER.debug( - f"[_get_customer_tenants] Customer tenant is not linked: {linked_account_info}" - ) - customer_tenants.remove(customer_tenant_id) + # if linked_accounts: + # linked_accounts_map = { + # linked_account["account_id"]: linked_account + # for linked_account in linked_accounts + # } + # + # for customer_tenant_id in customer_tenants: + # if linked_account_info := linked_accounts_map.get(customer_tenant_id): + # if not linked_account_info.get("is_sync"): + # first_sync_customer_tenants.append( + # linked_account_info.get("account_id") + # ) + # customer_tenants.remove(customer_tenant_id) + # else: + # _LOGGER.debug( + # f"[_get_customer_tenants] Customer tenant is not linked: {linked_account_info}" + # ) + # customer_tenants.remove(customer_tenant_id) return customer_tenants, first_sync_customer_tenants + @staticmethod + def _extend_synced_accounts(synced_accounts: list, customer_tenants: list) -> list: + synced_accounts.extend( + {"account_id": tenant_id} for tenant_id in customer_tenants + ) + return synced_accounts + @staticmethod def _get_divided_customer_tenants(customer_tenants_info: list) -> list: tenant_size = math.ceil(len(customer_tenants_info) / _TASK_LIST_SIZE)