Skip to content

Commit 460f435

Browse files
committed
feat: modify delete old unified cost logic
Signed-off-by: ImMin5 <[email protected]>
1 parent ae0a746 commit 460f435

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/spaceone/cost_analysis/service/unified_cost_service.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def run_unified_cost_by_scheduler(self, params: dict) -> None:
7878

7979
except Exception as e:
8080
_LOGGER.error(
81-
f"[create_unified_cost_jobs_by_domain_config] error: {e}",
81+
f"[create_unified_cost_jobs_by_domain_config] domain_id :{domain_id}, error: {e}",
8282
exc_info=True,
8383
)
8484

@@ -134,7 +134,9 @@ def run_unified_cost(self, params: dict):
134134
workspace_ids = self._get_workspace_ids(domain_id)
135135

136136
try:
137+
137138
for workspace_id in workspace_ids:
139+
unified_cost_created_at = datetime.utcnow()
138140
self.create_unified_cost_with_workspace(
139141
exchange_source,
140142
domain_id,
@@ -144,6 +146,13 @@ def run_unified_cost(self, params: dict):
144146
aggregation_date,
145147
is_confirmed,
146148
)
149+
self._delete_old_unified_costs(
150+
domain_id,
151+
workspace_id,
152+
aggregation_month,
153+
is_confirmed,
154+
unified_cost_created_at,
155+
)
147156

148157
self.unified_cost_job_mgr.update_is_confirmed_unified_cost_job(
149158
unified_cost_job_vo, is_confirmed
@@ -382,7 +391,6 @@ def create_unified_cost_with_workspace(
382391

383392
exchange_date_str = exchange_date.strftime("%Y-%m-%d")
384393
aggregation_date_str = aggregation_date.strftime("%Y-%m-%d")
385-
unified_cost_created_at = datetime.utcnow()
386394

387395
row_count = 0
388396
for row in cursor:
@@ -439,13 +447,6 @@ def create_unified_cost_with_workspace(
439447
_LOGGER.debug(
440448
f"[create_unified_cost_with_workspace] create count: {row_count} (workspace_id: {workspace_id})"
441449
)
442-
self._delete_old_unified_costs(
443-
domain_id,
444-
workspace_id,
445-
unified_cost_billed_month,
446-
is_confirmed,
447-
unified_cost_created_at,
448-
)
449450

450451
def _get_data_source_currency_map(
451452
self, domain_id: str, workspace_id: str
@@ -505,14 +506,10 @@ def _delete_old_unified_costs(
505506
{"key": "billed_month", "value": unified_cost_month, "operator": "eq"},
506507
{"key": "is_confirmed", "value": is_confirmed, "operator": "eq"},
507508
{"key": "domain_id", "value": domain_id, "operator": "eq"},
509+
{"key": "created_at", "value": created_at, "operator": "lt"},
508510
],
509511
}
510512

511-
if not is_confirmed:
512-
query_filter["filter"].append(
513-
{"key": "created_at", "value": created_at, "operator": "lt"}
514-
)
515-
516513
_LOGGER.debug(
517514
f"[delete_old_unified_costs] delete query filter conditions: {query_filter}"
518515
)
@@ -522,7 +519,7 @@ def _delete_old_unified_costs(
522519
)
523520

524521
_LOGGER.debug(
525-
f"[delete_old_unified_costs] delete count: {total_count} ({unified_cost_month})({workspace_id})"
522+
f"[delete_old_unified_costs] delete count: {total_count} ({unified_cost_month})({workspace_id}, is_confirmed: {is_confirmed})"
526523
)
527524
unified_cost_vos.delete()
528525

@@ -544,16 +541,18 @@ def _get_unified_cost_job(
544541
self, domain_id: str, aggregation_month: str
545542
) -> UnifiedCostJob:
546543

547-
unified_cost_job_vo = self.unified_cost_job_mgr.filter_unified_cost_jobs(
544+
unified_cost_job_vos = self.unified_cost_job_mgr.filter_unified_cost_jobs(
548545
domain_id=domain_id, billed_month=aggregation_month
549546
)
550-
if not unified_cost_job_vo:
547+
if not unified_cost_job_vos:
551548
unified_cost_job_vo = self.unified_cost_job_mgr.create_unified_cost(
552549
{
553550
"domain_id": domain_id,
554551
"billed_month": aggregation_month,
555552
}
556553
)
554+
else:
555+
unified_cost_job_vo = unified_cost_job_vos[0]
557556
return unified_cost_job_vo
558557

559558
def _get_aggregation_date(

0 commit comments

Comments
 (0)