Skip to content

Commit

Permalink
Merge pull request #147 from ImMin5/master
Browse files Browse the repository at this point in the history
Inject domain_id when use system token
  • Loading branch information
ImMin5 authored Jan 16, 2024
2 parents af5add0 + c327dfd commit b8eda53
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
43 changes: 22 additions & 21 deletions src/spaceone/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,50 +93,50 @@ def get_cost(
def filter_costs(self, **conditions):
return self.cost_model.filter(**conditions)

def list_costs(self, query):
query = self._change_filter_project_group_id(query)
def list_costs(self, query: dict, domain_id: str):
query = self._change_filter_project_group_id(query, domain_id)
return self.cost_model.query(**query)

def stat_costs(self, query):
query = self._change_filter_project_group_id(query)
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)

def filter_monthly_costs(self, **conditions):
return self.monthly_cost_model.filter(**conditions)

def list_monthly_costs(self, query):
query = self._change_filter_project_group_id(query)
def list_monthly_costs(self, query: dict, domain_id: str):
query = self._change_filter_project_group_id(query, domain_id)
return self.monthly_cost_model.query(**query)

def stat_monthly_costs(self, query):
query = self._change_filter_project_group_id(query)
def stat_monthly_costs(self, query: dict, domain_id: str):
query = self._change_filter_project_group_id(query, domain_id)
return self.monthly_cost_model.stat(**query)

def analyze_costs(self, query, target="SECONDARY_PREFERRED"):
def analyze_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
query["target"] = target
query["date_field"] = "billed_date"
query["date_field_format"] = "%Y-%m-%d"
_LOGGER.debug(f"[analyze_costs] query: {query}")

query = self._change_filter_project_group_id(query)
query = self._change_filter_project_group_id(query, domain_id)
return self.cost_model.analyze(**query)

def analyze_monthly_costs(self, query, target="SECONDARY_PREFERRED"):
def analyze_monthly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
query["target"] = target
query["date_field"] = "billed_month"
query["date_field_format"] = "%Y-%m"
_LOGGER.debug(f"[analyze_monthly_costs] query: {query}")

query = self._change_filter_project_group_id(query)
query = self._change_filter_project_group_id(query, domain_id)
return self.monthly_cost_model.analyze(**query)

def analyze_yearly_costs(self, query, target="SECONDARY_PREFERRED"):
def analyze_yearly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
query["target"] = target
query["date_field"] = "billed_year"
query["date_field_format"] = "%Y"
_LOGGER.debug(f"[analyze_yearly_costs] query: {query}")

query = self._change_filter_project_group_id(query)
query = self._change_filter_project_group_id(query, domain_id)
return self.monthly_cost_model.analyze(**query)

@cache.cacheable(
Expand All @@ -146,7 +146,7 @@ def analyze_yearly_costs(self, query, target="SECONDARY_PREFERRED"):
def stat_monthly_costs_with_cache(
self, query, query_hash, domain_id, data_source_id
):
return self.stat_monthly_costs(query)
return self.stat_monthly_costs(query, domain_id)

@cache.cacheable(
key="cost-analysis:analyze-costs:daily:{domain_id}:{data_source_id}:{query_hash}",
Expand All @@ -155,7 +155,7 @@ def stat_monthly_costs_with_cache(
def analyze_costs_with_cache(
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
):
return self.analyze_costs(query, target)
return self.analyze_costs(query, domain_id, target)

@cache.cacheable(
key="cost-analysis:analyze-costs:monthly:{domain_id}:{data_source_id}:{query_hash}",
Expand All @@ -164,7 +164,7 @@ def analyze_costs_with_cache(
def analyze_monthly_costs_with_cache(
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
):
return self.analyze_monthly_costs(query, target)
return self.analyze_monthly_costs(query, domain_id, target)

@cache.cacheable(
key="cost-analysis:analyze-costs:yearly:{domain_id}:{data_source_id}:{query_hash}",
Expand All @@ -173,7 +173,7 @@ def analyze_monthly_costs_with_cache(
def analyze_yearly_costs_with_cache(
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
):
return self.analyze_yearly_costs(query, target)
return self.analyze_yearly_costs(query, domain_id, target)

def analyze_costs_by_granularity(
self, query: dict, domain_id: dict, data_source_id: dict
Expand Down Expand Up @@ -354,7 +354,7 @@ def _get_billed_at_from_billed_date(billed_date):
except Exception as e:
raise ERROR_INVALID_PARAMETER_TYPE(key="billed_date", type="YYYY-MM-DD")

def _change_filter_project_group_id(self, query: dict) -> dict:
def _change_filter_project_group_id(self, query: dict, domain_id: str) -> dict:
change_filter = []
self.identity_mgr = None

Expand All @@ -375,7 +375,8 @@ def _change_filter_project_group_id(self, query: dict) -> dict:
"only": ["project_group_id"],
"filter": [{"k": key, "v": value, "o": operator}],
}
}
},
domain_id,
)

project_group_ids = [
Expand All @@ -387,7 +388,7 @@ def _change_filter_project_group_id(self, query: dict) -> dict:

for project_group_id in project_group_ids:
projects_info = self.identity_mgr.get_projects_in_project_group(
project_group_id
project_group_id, domain_id
)
project_ids.extend(
[
Expand Down
21 changes: 14 additions & 7 deletions src/spaceone/cost_analysis/manager/identity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ def list_projects(self, params: dict, domain_id):
else:
return self.identity_conn.dispatch("Project.list", params)

def list_project_groups(
self,
params: dict,
) -> dict:
return self.identity_conn.dispatch("ProjectGroup.list", params)
def list_project_groups(self, params: dict, domain_id: str) -> dict:
if self.token_type == "SYSTEM_TOKEN":
return self.identity_conn.dispatch(
"ProjectGroup.list", params, x_domain_id=domain_id
)
else:
return self.identity_conn.dispatch("ProjectGroup.list", params)

@cache.cacheable(key="cost-analysis:projects-in-pg:{project_group_id}", expire=300)
def get_projects_in_project_group(self, project_group_id: str):
def get_projects_in_project_group(self, project_group_id: str, domain_id: str):
params = {
"query": {
"only": ["project_id"],
Expand All @@ -96,4 +98,9 @@ def get_projects_in_project_group(self, project_group_id: str):
"include_children": True,
}

return self.identity_conn.dispatch("Project.list", params)
if self.token_type == "SYSTEM_TOKEN":
return self.identity_conn.dispatch(
"ProjectGroup.list_projects", params, x_domain_id=domain_id
)
else:
return self.identity_conn.dispatch("Project.list", params)
3 changes: 2 additions & 1 deletion src/spaceone/cost_analysis/service/cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def list(self, params):
"""

query = params.get("query", {})
return self.cost_mgr.list_costs(query)
domain_id = params["domain_id"]
return self.cost_mgr.list_costs(query, domain_id)

@transaction(
permission="cost-analysis:Cost.read",
Expand Down
24 changes: 16 additions & 8 deletions src/spaceone/cost_analysis/service/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,11 @@ def _close_job(

for changed_vo in job_vo.changed:
self._delete_changed_cost_data(
job_vo, changed_vo.start, changed_vo.end, changed_vo.filter
job_vo,
changed_vo.start,
changed_vo.end,
changed_vo.filter,
domain_id,
)

except Exception as e:
Expand Down Expand Up @@ -698,7 +702,7 @@ def _delete_old_cost_data(self, data_source_id, domain_id):
]
}

cost_vos, total_count = self.cost_mgr.list_costs(cost_delete_query)
cost_vos, total_count = self.cost_mgr.list_costs(cost_delete_query, domain_id)
_LOGGER.debug(f"[_delete_old_cost_data] delete costs (count = {total_count})")
cost_vos.delete()

Expand All @@ -711,14 +715,16 @@ def _delete_old_cost_data(self, data_source_id, domain_id):
}

monthly_cost_vos, total_count = self.cost_mgr.list_monthly_costs(
monthly_cost_delete_query
monthly_cost_delete_query, domain_id
)
_LOGGER.debug(
f"[_delete_old_cost_data] delete monthly costs (count = {total_count})"
)
monthly_cost_vos.delete()

def _delete_changed_cost_data(self, job_vo: Job, start, end, change_filter):
def _delete_changed_cost_data(
self, job_vo: Job, start, end, change_filter, domain_id
):
query = {
"filter": [
{"k": "billed_month", "v": start, "o": "gte"},
Expand All @@ -736,14 +742,16 @@ def _delete_changed_cost_data(self, job_vo: Job, start, end, change_filter):

_LOGGER.debug(f"[_delete_changed_cost_data] query: {query}")

cost_vos, total_count = self.cost_mgr.list_costs(copy.deepcopy(query))
cost_vos, total_count = self.cost_mgr.list_costs(
copy.deepcopy(query), domain_id
)
cost_vos.delete()
_LOGGER.debug(
f"[_delete_changed_cost_data] delete costs (count = {total_count})"
)

monthly_cost_vos, total_count = self.cost_mgr.list_monthly_costs(
copy.deepcopy(query)
copy.deepcopy(query), domain_id
)
monthly_cost_vos.delete()
_LOGGER.debug(
Expand Down Expand Up @@ -784,7 +792,7 @@ def _distinct_billed_month(self, data_source_id, domain_id, job_id, job_task_id)
"target": "PRIMARY", # Execute a query to primary DB
}
_LOGGER.debug(f"[_distinct_cost_data] query: {query}")
response = self.cost_mgr.stat_costs(query)
response = self.cost_mgr.stat_costs(query, domain_id)
values = response.get("results", [])

_LOGGER.debug(f"[_distinct_cost_data] billed_month: {values}")
Expand Down Expand Up @@ -833,7 +841,7 @@ def _aggregate_monthly_cost_data(
if workspace_id:
query["filter"].append({"k": "workspace_id", "v": workspace_id, "o": "eq"})

response = self.cost_mgr.analyze_costs(query, target="PRIMARY")
response = self.cost_mgr.analyze_costs(query, domain_id, target="PRIMARY")
results = response.get("results", [])
for aggregated_cost_data in results:
aggregated_cost_data["data_source_id"] = data_source_id
Expand Down

0 comments on commit b8eda53

Please sign in to comment.