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

refactor: optimize query for performance #301

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
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
Loading