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

Implement caching for unified cost analysis methods and enhance query handling #323

Merged
merged 1 commit into from
Feb 25, 2025
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
40 changes: 35 additions & 5 deletions src/spaceone/cost_analysis/manager/unified_cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dateutil.relativedelta import relativedelta
from mongoengine import QuerySet

from spaceone.core import queue, utils, config
from spaceone.core import queue, utils, config, cache
from spaceone.core.error import *
from spaceone.core.manager import BaseManager

Expand Down Expand Up @@ -78,24 +78,54 @@ def analyze_yearly_unified_costs(self, query, target="SECONDARY_PREFERRED"):

return self.unified_cost_model.analyze(**query)

def analyze_unified_costs_by_granularity(self, query: dict) -> dict:
granularity = query["granularity"]
@cache.cacheable(
key="cost-analysis:analyze-unified-costs:yearly:{query_hash}", expire=3600 * 24
)
def analyze_unified_yearly_costs_with_cache(self, query: dict) -> dict:
return self.analyze_unified_costs(query)

@cache.cacheable(
key="cost-analysis:analyze-unified-costs:monthly:{domain_id}:{query_hash}",
expire=3600 * 24,
)
def analyze_unified_monthly_costs_with_cache(
self, query: dict, query_hash: str, domain_id: str
) -> dict:
return self.analyze_unified_costs(query)

def analyze_unified_costs_by_granularity(self, query: dict, domain_id: str) -> dict:

self._check_unified_cost_data_range(query)
granularity = query["granularity"]

# Save query history to speed up data loading
query_hash: str = utils.dict_to_hash(query)

if granularity == "DAILY":
raise ERROR_INVALID_PARAMETER(
key="query.granularity", reason=f"{granularity} is not supported"
)
elif granularity == "MONTHLY":
response = self.analyze_unified_costs(query)
response = self.analyze_unified_monthly_costs_with_cache(
query, query_hash, domain_id
)
else:
response = self.analyze_yearly_unified_costs(query)
response = self.analyze_unified_yearly_costs_with_cache(
query, query_hash, domain_id
)

return response

def stat_unified_costs(self, query) -> dict:
return self.unified_cost_model.stat(**query)

@staticmethod
def remove_stat_cache(domain_id: str):
cache.delete_pattern(f"cost-analysis:analyze-unified-costs:*:{domain_id}:*")
cache.delete_pattern(f"cost-analysis:stat-unified-costs:*:{domain_id}:*")

_LOGGER.debug(f"[remove_stat_cache] domain_id: {domain_id}")

@staticmethod
def push_unified_cost_job_task(params: dict) -> None:
token = config.get_global("TOKEN")
Expand Down
16 changes: 8 additions & 8 deletions src/spaceone/cost_analysis/service/unified_cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def run_unified_cost_by_scheduler(self, params: dict) -> None:
exc_info=True,
)

# @transaction(exclude=["authenticate", "authorization", "mutation"])
def run_unified_cost(self, params: dict):
@transaction(exclude=["authenticate", "authorization", "mutation"])
def run_unified_cost(self, params: dict) -> None:
"""
Args:
params (dict): {
Expand All @@ -94,11 +94,6 @@ def run_unified_cost(self, params: dict):
"exchange_date": 'str' (optional)
}
"""

from spaceone.core import model

model.init_all(False)

domain_id = params["domain_id"]
aggregation_month = params.get("month")
exchange_date = params.get("exchange_date")
Expand Down Expand Up @@ -167,6 +162,8 @@ def run_unified_cost(self, params: dict):
self.unified_cost_job_mgr.update_is_confirmed_unified_cost_job(
unified_cost_job_vo, is_confirmed
)

self.unified_cost_mgr.remove_stat_cache(domain_id)
except Exception as e:
_LOGGER.error(f"[run_unified_cost] error: {e}", exc_info=True)
self.unified_cost_job_mgr.update_is_confirmed_unified_cost_job(
Expand Down Expand Up @@ -332,8 +329,11 @@ def analyze(self, params: UnifiedCostAnalyzeQueryRequest) -> dict:
"""

query = params.query or {}
domain_id = params.domain_id

return self.unified_cost_mgr.analyze_unified_costs_by_granularity(query)
return self.unified_cost_mgr.analyze_unified_costs_by_granularity(
query, domain_id
)

@transaction(
permission="cost-analysis:UnifiedCost.read",
Expand Down
Loading