From c781912454e24636e0cb306fd05311d0d3ad2a48 Mon Sep 17 00:00:00 2001 From: Jongmin Kim Date: Tue, 15 Oct 2024 10:45:34 +0900 Subject: [PATCH] refactor: optimize query for performance Signed-off-by: Jongmin Kim --- src/spaceone/cost_analysis/manager/cost_manager.py | 2 +- src/spaceone/cost_analysis/service/job_service.py | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/spaceone/cost_analysis/manager/cost_manager.py b/src/spaceone/cost_analysis/manager/cost_manager.py index 7acf04fa..05bc1b04 100644 --- a/src/spaceone/cost_analysis/manager/cost_manager.py +++ b/src/spaceone/cost_analysis/manager/cost_manager.py @@ -123,7 +123,7 @@ def list_costs(self, query: dict, domain_id: str, data_source_id: str): query = self._change_filter_project_group_id(query, domain_id) return self.cost_model.query(**query) - def stat_costs(self, query: dict, domain_id: str, data_source_id: str = None): + def stat_costs(self, query: dict, domain_id: str): query = self._change_filter_project_group_id(query, domain_id) return self.cost_model.stat(**query) diff --git a/src/spaceone/cost_analysis/service/job_service.py b/src/spaceone/cost_analysis/service/job_service.py index 97de3f3a..5cecaa19 100644 --- a/src/spaceone/cost_analysis/service/job_service.py +++ b/src/spaceone/cost_analysis/service/job_service.py @@ -782,16 +782,14 @@ def _delete_old_cost_data(self, data_source_id: str, domain_id: str): ) monthly_cost_vos.delete() - def _distinct_job_id( - self, query_filter: list, domain_id: str, data_source_id: str - ) -> list: + def _distinct_job_id(self, query_filter: list, domain_id: str) -> list: query = { "distinct": "job_id", "filter": query_filter, "target": "PRIMARY", # Execute a query to primary DB } _LOGGER.debug(f"[_distinct_job_id] query: {query}") - response = self.cost_mgr.stat_costs(query, domain_id, data_source_id) + response = self.cost_mgr.stat_monthly_costs(query, domain_id) values = response.get("results", []) _LOGGER.debug(f"[_distinct_job_id] job_ids: {values}") @@ -814,9 +812,7 @@ def _delete_changed_cost_data( if end: query["filter"].append({"k": "billed_month", "v": end, "o": "lte"}) - job_ids = self._distinct_job_id( - copy.deepcopy(query["filter"]), domain_id, job_vo.data_source_id - ) + job_ids = self._distinct_job_id(copy.deepcopy(query["filter"]), domain_id) for key, value in change_filter.items(): query["filter"].append({"k": key, "v": value, "o": "eq"}) @@ -903,7 +899,7 @@ def _distinct_billed_month( "target": "PRIMARY", # Execute a query to primary DB } _LOGGER.debug(f"[_distinct_cost_data] query: {query}") - response = self.cost_mgr.stat_costs(query, domain_id, data_source_id) + response = self.cost_mgr.stat_costs(query, domain_id) values = response.get("results", []) _LOGGER.debug(f"[_distinct_cost_data] billed_month: {values}")