Skip to content

Commit

Permalink
feat: add logic for creating workspace_id None value unified cost
Browse files Browse the repository at this point in the history
Signed-off-by: ImMin5 <mino@megazone.com>
  • Loading branch information
ImMin5 committed Oct 2, 2024
1 parent 0505b49 commit 4c97029
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/spaceone/cost_analysis/model/unified_cost/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class UnifiedCost(MongoModel):
"workspace_id",
"billed_year",
"billed_month",
"domain_id",
"created_at",
"is_confirmed",
],
"name": "COMPOUND_INDEX_FOR_DELETE_UNIFIED_COST",
Expand Down
73 changes: 8 additions & 65 deletions src/spaceone/cost_analysis/service/unified_cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def run_unified_cost(self, params: dict):
currency_map = unified_cost_config["custom_exchange_rate"]
exchange_source = unified_cost_config.get("exchange_source", "MANUAL")

workspace_ids = self._get_workspace_ids(domain_id)
workspace_ids = self._get_workspace_ids_with_none(domain_id)

try:

Expand Down Expand Up @@ -322,7 +322,7 @@ def create_unified_cost_with_workspace(
self,
exchange_source: str,
domain_id: str,
workspace_id: str,
workspace_id: Union[str, None],
currency_map: dict,
exchange_date: datetime,
aggregation_date: datetime,
Expand Down Expand Up @@ -504,11 +504,14 @@ def _delete_old_unified_costs(
"operator": "eq",
},
{"key": "billed_month", "value": unified_cost_month, "operator": "eq"},
{"key": "is_confirmed", "value": is_confirmed, "operator": "eq"},
{"key": "domain_id", "value": domain_id, "operator": "eq"},
{"key": "created_at", "value": created_at, "operator": "lt"},
],
}
if not is_confirmed:
query_filter["filter"].append(
{"key": "is_confirmed", "value": is_confirmed, "operator": "eq"}
)

_LOGGER.debug(
f"[delete_old_unified_costs] delete query filter conditions: {query_filter}"
Expand Down Expand Up @@ -612,8 +615,8 @@ def get_is_last_day(
return min(current_day, last_day)

@staticmethod
def _get_workspace_ids(domain_id: str) -> list:
workspace_ids = []
def _get_workspace_ids_with_none(domain_id: str) -> list:
workspace_ids = [None]

identity_mgr = IdentityManager()
system_token = config.get_global("TOKEN")
Expand All @@ -628,66 +631,6 @@ def _get_workspace_ids(domain_id: str) -> list:

return workspace_ids

@staticmethod
def _get_project_name_map(
identity_mgr: IdentityManager, domain_id: str, workspace_id: str
) -> dict:
project_name_map = {}
response = identity_mgr.list_projects(
{
"query": {
"filter": [
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
]
}
},
domain_id,
)
for project in response.get("results", []):
project_name_map[project["project_id"]] = project["name"]
return project_name_map

@staticmethod
def _get_service_account_name_map(
identity_mgr: IdentityManager, workspace_id: str, domain_id: str
) -> dict:
service_account_name_map = {}
service_accounts = identity_mgr.list_service_accounts(
{
"filter": [
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
]
},
domain_id,
)
for service_account in service_accounts.get("results", []):
service_account_name_map[service_account["service_account_id"]] = (
service_account["name"]
)
return service_account_name_map

@staticmethod
def _check_aggregation_date_validity(
aggregation_date: datetime, current_date: datetime
) -> bool:

is_aggregation_date_valid = True
if aggregation_date > current_date:
_LOGGER.debug(
f"skip unified cost aggregation, {aggregation_date} is greater than {current_date}."
)
is_aggregation_date_valid = False

elif aggregation_date.month == current_date.month:
_LOGGER.debug(
f"skip unified cost aggregation, {aggregation_date} should be previous month of {current_date}."
)
is_aggregation_date_valid = False

return is_aggregation_date_valid

@staticmethod
def _get_is_confirmed_with_aggregation_month(aggregation_month: str) -> bool:
is_confirmed = False
Expand Down

0 comments on commit 4c97029

Please sign in to comment.