Skip to content

Commit

Permalink
Merge pull request #301 from whdalsrnt/master
Browse files Browse the repository at this point in the history
refactor: optimize query for performance
  • Loading branch information
whdalsrnt authored Oct 15, 2024
2 parents 3bafd59 + c781912 commit d283e26
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/spaceone/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 4 additions & 8 deletions src/spaceone/cost_analysis/service/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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"})
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit d283e26

Please sign in to comment.