Skip to content

Commit

Permalink
Merge pull request #88 from whdalsrnt/master
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
whdalsrnt authored Sep 11, 2023
2 parents f6e2aa1 + 21e65c8 commit b70119c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/spaceone/cost_analysis/manager/budget_usage_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def _update_monthly_budget_usage(self, budget_vo: Budget, cost_mgr: CostManager)
budget_usage_vos = self.budget_usage_model.filter(budget_id=budget_vo.budget_id)
for budget_usage_vo in budget_usage_vos:
if budget_usage_vo.date in update_data:
budget_usage_vo.update({'usd_cost': update_data[budget_usage_vo.date]})
budget_usage_vo.update({'cost': update_data[budget_usage_vo.date]})
else:
budget_usage_vo.update({'usd_cost': 0})
budget_usage_vo.update({'cost': 0})

def _make_cost_analyze_query(self, budget_vo: Budget):
query = {
Expand All @@ -125,6 +125,7 @@ def _make_cost_analyze_query(self, budget_vo: Budget):
},
'filter': [
{'k': 'domain_id', 'v': budget_vo.domain_id, 'o': 'eq'},
{'k': 'data_source_id', 'v': budget_vo.data_source_id, 'o': 'eq'},
]
}

Expand All @@ -142,7 +143,7 @@ def _make_cost_analyze_query(self, budget_vo: Budget):

query['filter'].append({'k': 'project_id', 'v': project_ids, 'o': 'in'})

if budget_vo.provider_filter.state == 'ENABLED':
if budget_vo.provider_filter and budget_vo.provider_filter.state == 'ENABLED':
query['filter'].append({'k': 'provider', 'v': budget_vo.provider_filter.providers, 'o': 'in'})

return query
6 changes: 3 additions & 3 deletions src/spaceone/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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)

# @cache.cacheable(key='analyze-costs:daily:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
@cache.cacheable(key='analyze-costs:daily:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
def analyze_costs_with_cache(self, query, query_hash, domain_id, data_source_id, target='SECONDARY_PREFERRED'):
return self.analyze_costs(query, target)

# @cache.cacheable(key='analyze-costs:monthly:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
@cache.cacheable(key='analyze-costs:monthly:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
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)

# @cache.cacheable(key='analyze-costs:yearly:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
@cache.cacheable(key='analyze-costs:yearly:{domain_id}:{data_source_id}:{query_hash}', expire=3600 * 24)
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)

Expand Down
7 changes: 5 additions & 2 deletions src/spaceone/cost_analysis/service/budget_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ def create(self, params):
if len(provider_filter.get('providers', [])) == 0:
raise ERROR_PROVIDER_FILTER_IS_EMPTY()
else:
params['provider_filter']['providers'] = []
params['provider_filter'] = {
'state': 'DISABLED',
'providers': []
}

data_source_mgr: DataSourceManager = self.locator.get_manager('DataSourceManager')
data_source_vo: DataSource = data_source_mgr.get_data_source(data_source_id, domain_id)
data_source_metadata = data_source_vo.plugin_info.get('metadata', {})
data_source_metadata = data_source_vo.plugin_info.metadata
params['currency'] = data_source_metadata['currency']

if time_unit == 'TOTAL':
Expand Down
4 changes: 2 additions & 2 deletions src/spaceone/cost_analysis/service/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,13 @@ def _close_job(self, job_id, domain_id, data_source_id):
self.job_mgr.change_error_status(job_vo, e)
raise e

self.budget_usage_mgr.update_budget_usage(domain_id, data_source_id)
self.cost_mgr.remove_stat_cache(domain_id, data_source_id)

if not no_preload_cache:
self.job_mgr.preload_cost_stat_queries(domain_id, data_source_id)

self.budget_usage_mgr.update_budget_usage(domain_id, data_source_id)

self._update_last_sync_time(job_vo)
self.job_mgr.change_success_status(job_vo)

Expand Down Expand Up @@ -575,7 +576,6 @@ def _aggregate_monthly_cost_data(self, data_source_id, domain_id, job_id, job_ta
'allow_disk_use': True # Allow disk use for large data
}

_LOGGER.debug(f'[_aggregate_monthly_cost_data] query: {query}')
response = self.cost_mgr.analyze_costs(query)
results = response.get('results', [])
for aggregated_cost_data in results:
Expand Down

0 comments on commit b70119c

Please sign in to comment.