Skip to content

Commit

Permalink
feat: modify pay_as_you_go condition
Browse files Browse the repository at this point in the history
Signed-off-by: ImMin5 <[email protected]>
  • Loading branch information
ImMin5 committed May 2, 2024
1 parent 7a65725 commit 5b677f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
11 changes: 7 additions & 4 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
)
Expand Down
62 changes: 37 additions & 25 deletions src/cloudforet/cost_analysis/manager/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 = [
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5b677f5

Please sign in to comment.