Skip to content

Commit 3c05436

Browse files
authored
Merge pull request #296 from ImMin5/master
XFix wrong aggregation month value input
2 parents f01c6fb + 5a18fde commit 3c05436

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/spaceone/cost_analysis/manager/identity_manager.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ def check_workspace(self, workspace_id: str, domain_id: str) -> None:
7777
@cache.cacheable(
7878
key="cost-analysis:workspace-name:{domain_id}:{workspace_id}:name", expire=300
7979
)
80-
def get_workspace(self, workspace_id: Union[str, None], domain_id: str) -> str:
81-
if not workspace_id:
82-
return workspace_id
80+
def get_workspace(self, workspace_id: str, domain_id: str) -> str:
8381
try:
8482
workspace_info = self.identity_conn.dispatch(
8583
"Workspace.get",

src/spaceone/cost_analysis/service/unified_cost_service.py

+26-14
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def run_unified_cost(self, params: dict):
108108

109109
unified_cost_job_vo = self._get_unified_cost_job(domain_id, aggregation_month)
110110

111-
aggregation_date = self._get_aggregation_date(
111+
aggregation_execution_date = self._get_aggregation_date(
112112
unified_cost_config, aggregation_month, is_confirmed
113113
)
114114
exchange_date = self._get_exchange_date(
@@ -139,7 +139,8 @@ def run_unified_cost(self, params: dict):
139139
workspace_id,
140140
currency_map,
141141
exchange_date,
142-
aggregation_date,
142+
aggregation_execution_date,
143+
aggregation_month,
143144
is_confirmed,
144145
)
145146
self._delete_old_unified_costs(
@@ -321,14 +322,18 @@ def create_unified_cost_with_workspace(
321322
workspace_id: Union[str, None],
322323
currency_map: dict,
323324
exchange_date: datetime,
324-
aggregation_date: datetime,
325+
aggregation_execution_date: datetime,
326+
aggregation_month: str,
325327
is_confirmed: bool = False,
326328
) -> None:
327329

328330
identity_mgr = IdentityManager(token=config.get_global("TOKEN"))
329331
workspace_ids = [workspace_id]
330332

331-
workspace_name = identity_mgr.get_workspace(workspace_id, domain_id)
333+
if workspace_id:
334+
workspace_name = identity_mgr.get_workspace(workspace_id, domain_id)
335+
else:
336+
workspace_name = None
332337

333338
v_workspace_ids = self._get_virtual_workspace_ids_from_ds_account(
334339
domain_id, workspace_id
@@ -346,8 +351,7 @@ def create_unified_cost_with_workspace(
346351
domain_id, workspace_id
347352
)
348353

349-
unified_cost_billed_year = aggregation_date.strftime("%Y")
350-
unified_cost_billed_month = aggregation_date.strftime("%Y-%m")
354+
unified_cost_billed_year = aggregation_month.split("-")[0]
351355

352356
query = {
353357
"group_by": [
@@ -368,12 +372,12 @@ def create_unified_cost_with_workspace(
368372
"fields": {
369373
"cost": {"key": "cost", "operator": "sum"},
370374
},
371-
"start": unified_cost_billed_year,
372-
"end": unified_cost_billed_month,
375+
"start": aggregation_month,
376+
"end": aggregation_month,
373377
"filter": [
374378
{"k": "domain_id", "v": domain_id, "o": "eq"},
375379
{"k": "data_source_id", "v": data_source_ids, "o": "in"},
376-
{"k": "billed_month", "v": unified_cost_billed_month, "o": "eq"},
380+
{"k": "billed_month", "v": aggregation_month, "o": "eq"},
377381
{"k": "workspace_id", "v": workspace_ids, "o": "in"},
378382
{"k": "billed_year", "v": unified_cost_billed_year, "o": "eq"},
379383
],
@@ -388,7 +392,7 @@ def create_unified_cost_with_workspace(
388392
cursor = self.cost_mgr.analyze_monthly_costs(query, domain_id)
389393

390394
exchange_date_str = exchange_date.strftime("%Y-%m-%d")
391-
aggregation_date_str = aggregation_date.strftime("%Y-%m-%d")
395+
aggregation_execution_date_str = aggregation_execution_date.strftime("%Y-%m-%d")
392396

393397
row_count = 0
394398
for row in cursor:
@@ -439,23 +443,31 @@ def create_unified_cost_with_workspace(
439443
aggregated_unified_cost_data["exchange_source"] = exchange_source
440444

441445
aggregated_unified_cost_data["is_confirmed"] = is_confirmed
442-
aggregated_unified_cost_data["aggregation_date"] = aggregation_date_str
446+
aggregated_unified_cost_data["aggregation_date"] = (
447+
aggregation_execution_date_str
448+
)
443449

444450
self.unified_cost_mgr.create_unified_cost(aggregated_unified_cost_data)
451+
row_count += 1
445452

446453
_LOGGER.debug(
447454
f"[create_unified_cost_with_workspace] create count: {row_count} (workspace_id: {workspace_id})"
448455
)
449456

450457
def _get_data_source_currency_map(
451-
self, domain_id: str, workspace_id: str
458+
self, domain_id: str, workspace_id: Union[str, None]
452459
) -> Tuple[dict, dict, list]:
453460
data_source_currency_map = {}
454461
data_source_name_map = {}
462+
workspace_ids = ["*"]
463+
464+
if workspace_id:
465+
workspace_ids.append(workspace_id)
466+
455467
query = {
456468
"filter": [
457469
{"k": "domain_id", "v": domain_id, "o": "eq"},
458-
{"k": "workspace_id", "v": [workspace_id, "*"], "o": "in"},
470+
{"k": "workspace_id", "v": workspace_ids, "o": "in"},
459471
]
460472
}
461473

@@ -473,7 +485,7 @@ def _get_data_source_currency_map(
473485
return data_source_currency_map, data_source_name_map, data_source_ids
474486

475487
def _get_virtual_workspace_ids_from_ds_account(
476-
self, domain_id: str, workspace_id: str
488+
self, domain_id: str, workspace_id: Union[str, None]
477489
) -> list:
478490
v_workspace_ids = []
479491
ds_account_vos = self.ds_account_mgr.filter_data_source_accounts(

0 commit comments

Comments
 (0)