Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify delete old unified cost logic #290

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/spaceone/cost_analysis/service/unified_cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run_unified_cost_by_scheduler(self, params: dict) -> None:

except Exception as e:
_LOGGER.error(
f"[create_unified_cost_jobs_by_domain_config] error: {e}",
f"[create_unified_cost_jobs_by_domain_config] domain_id :{domain_id}, error: {e}",
exc_info=True,
)

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

try:

for workspace_id in workspace_ids:
unified_cost_created_at = datetime.utcnow()
self.create_unified_cost_with_workspace(
exchange_source,
domain_id,
Expand All @@ -144,6 +146,13 @@ def run_unified_cost(self, params: dict):
aggregation_date,
is_confirmed,
)
self._delete_old_unified_costs(
domain_id,
workspace_id,
aggregation_month,
is_confirmed,
unified_cost_created_at,
)

self.unified_cost_job_mgr.update_is_confirmed_unified_cost_job(
unified_cost_job_vo, is_confirmed
Expand Down Expand Up @@ -382,7 +391,6 @@ def create_unified_cost_with_workspace(

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

row_count = 0
for row in cursor:
Expand Down Expand Up @@ -439,13 +447,6 @@ def create_unified_cost_with_workspace(
_LOGGER.debug(
f"[create_unified_cost_with_workspace] create count: {row_count} (workspace_id: {workspace_id})"
)
self._delete_old_unified_costs(
domain_id,
workspace_id,
unified_cost_billed_month,
is_confirmed,
unified_cost_created_at,
)

def _get_data_source_currency_map(
self, domain_id: str, workspace_id: str
Expand Down Expand Up @@ -505,14 +506,10 @@ def _delete_old_unified_costs(
{"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": "created_at", "value": created_at, "operator": "lt"}
)

_LOGGER.debug(
f"[delete_old_unified_costs] delete query filter conditions: {query_filter}"
)
Expand All @@ -522,7 +519,7 @@ def _delete_old_unified_costs(
)

_LOGGER.debug(
f"[delete_old_unified_costs] delete count: {total_count} ({unified_cost_month})({workspace_id})"
f"[delete_old_unified_costs] delete count: {total_count} ({unified_cost_month})({workspace_id}, is_confirmed: {is_confirmed})"
)
unified_cost_vos.delete()

Expand All @@ -544,16 +541,18 @@ def _get_unified_cost_job(
self, domain_id: str, aggregation_month: str
) -> UnifiedCostJob:

unified_cost_job_vo = self.unified_cost_job_mgr.filter_unified_cost_jobs(
unified_cost_job_vos = self.unified_cost_job_mgr.filter_unified_cost_jobs(
domain_id=domain_id, billed_month=aggregation_month
)
if not unified_cost_job_vo:
if not unified_cost_job_vos:
unified_cost_job_vo = self.unified_cost_job_mgr.create_unified_cost(
{
"domain_id": domain_id,
"billed_month": aggregation_month,
}
)
else:
unified_cost_job_vo = unified_cost_job_vos[0]
return unified_cost_job_vo

def _get_aggregation_date(
Expand Down
Loading