Skip to content

Commit

Permalink
Merge pull request #221 from ImMin5/feature-cost-router
Browse files Browse the repository at this point in the history
Add convert function virtual workspace id and workspace id
  • Loading branch information
ImMin5 authored May 9, 2024
2 parents 6be76dd + a5139e2 commit a30c81d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/spaceone/cost_analysis/manager/budget_usage_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ def create_budget_usages(self, budget_vo: Budget) -> None:

dts = [dt for dt in rrule(MONTHLY, dtstart=start_dt, until=end_dt)]
limit_per_month = round(budget_vo.limit / len(dts), 3)
budget_limit = budget_vo.limit

for dt in dts:
if budget_limit - limit_per_month < 0:
limit_per_month = round(budget_limit, 3)
budget_usage_data = {
"budget_id": budget_vo.budget_id,
"name": budget_vo.name,
Expand All @@ -53,6 +56,7 @@ def create_budget_usages(self, budget_vo: Budget) -> None:
}

budget_usage_vo = self.budget_usage_model.create(budget_usage_data)
budget_limit -= limit_per_month

else:
for planned_limit in budget_vo.planned_limits:
Expand Down
6 changes: 1 addition & 5 deletions src/spaceone/cost_analysis/service/budget_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from spaceone.cost_analysis.manager.budget_usage_manager import BudgetUsageManager
from spaceone.cost_analysis.manager.identity_manager import IdentityManager
from spaceone.cost_analysis.model.budget_model import Budget
from spaceone.cost_analysis.model.data_source_model import DataSource
from spaceone.cost_analysis.model.budget_usage_model import BudgetUsage

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,9 +93,7 @@ def create(self, params):
else:
params["provider_filter"] = {"state": "DISABLED", "providers": []}

data_source_mgr: DataSourceManager = self.locator.get_manager(
"DataSourceManager"
)
data_source_mgr = DataSourceManager()

data_source_vos = data_source_mgr.filter_data_sources(
data_source_id=data_source_id,
Expand Down
23 changes: 22 additions & 1 deletion src/spaceone/cost_analysis/service/cost_report_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from spaceone.core.service import *
from spaceone.core.service.utils import *

from spaceone.cost_analysis.manager import DataSourceAccountManager
from spaceone.cost_analysis.manager.cost_manager import CostManager
from spaceone.cost_analysis.manager.cost_report_data_manager import (
CostReportDataManager,
Expand Down Expand Up @@ -32,6 +33,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cost_mgr = CostManager()
self.cost_report_data_mgr = CostReportDataManager()
self.ds_account_mgr = DataSourceAccountManager()
self.currency_map: Union[dict, None] = None
self.currency_date: Union[str, None] = None

Expand Down Expand Up @@ -195,14 +197,21 @@ def _aggregate_monthly_cost_report_data(
"start": report_month,
"end": report_month,
"filter": [
{"k": "workspace_id", "v": workspace_id, "o": "eq"},
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "billed_year", "v": report_year, "o": "eq"},
{"k": "billed_month", "v": report_month, "o": "eq"},
{"k": "data_source_id", "v": data_source_ids, "o": "in"},
],
}

v_workspace_ids = self._get_virtual_workspace_ids(domain_id, workspace_id)
if v_workspace_ids:
query["filter"].append(
{"k": "workspace_id", "v": [workspace_id] + v_workspace_ids, "o": "in"}
)
else:
query["filter"].append({"k": "workspace_id", "v": workspace_id, "o": "eq"})

_LOGGER.debug(f"[aggregate_monthly_cost_report_data] query: {query}")
response = self.cost_mgr.analyze_monthly_costs(query, domain_id)

Expand Down Expand Up @@ -236,6 +245,7 @@ def _aggregate_monthly_cost_report_data(

aggregated_cost_report_data["cost_report_config_id"] = cost_report_config_id
aggregated_cost_report_data["cost_report_id"] = cost_report_id
aggregated_cost_report_data["workspace_id"] = workspace_id
aggregated_cost_report_data["domain_id"] = domain_id
aggregated_cost_report_data["is_confirmed"] = is_confirmed

Expand Down Expand Up @@ -322,3 +332,14 @@ def _get_data_source_currency_map(
data_source_ids.append(data_source_vo.data_source_id)

return data_source_currency_map, data_source_ids

def _get_virtual_workspace_ids(self, domain_id: str, workspace_id: str) -> list:
v_workspace_ids = []
ds_account_vos = self.ds_account_mgr.filter_data_source_accounts(
domain_id=domain_id, workspace_id=workspace_id
)

for ds_account_vo in ds_account_vos:
v_workspace_ids.append(ds_account_vo.v_workspace_id)

return v_workspace_ids
45 changes: 41 additions & 4 deletions src/spaceone/cost_analysis/service/cost_report_serivce.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from spaceone.core import config
from spaceone.core.service import *

from spaceone.cost_analysis.manager import DataSourceAccountManager
from spaceone.cost_analysis.model.cost_report.database import CostReport
from spaceone.cost_analysis.model.cost_report_config.database import CostReportConfig
from spaceone.cost_analysis.model.cost_report.request import *
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(self, *args, **kwargs):
self.cost_report_config_mgr = CostReportConfigManager()
self.cost_report_mgr = CostReportManager()
self.cost_report_data_mgr = CostReportDataManager()
self.ds_account_mgr = DataSourceAccountManager()
self.currency_map: Union[dict, None] = None
self.currency_date: Union[str, None] = None

Expand Down Expand Up @@ -259,7 +261,7 @@ def _aggregate_monthly_cost_report(
domain_id, cost_report_config_id, report_month, status
)

# collect enabled data sources
# collect enabled data_sources cost data
query = {
"group_by": ["workspace_id", "billed_year", "data_source_id"],
"fields": {
Expand All @@ -272,23 +274,35 @@ def _aggregate_monthly_cost_report(
{"k": "billed_year", "v": report_year, "o": "eq"},
{"k": "billed_month", "v": report_month, "o": "eq"},
{"k": "data_source_id", "v": data_source_ids, "o": "in"},
{"k": "workspace_id", "v": workspace_ids, "o": "in"},
],
}

v_workspace_ids, v_workspace_id_map = self._get_virtual_workspace_ids_and_map(
domain_id, workspace_ids
)
if v_workspace_ids:
workspace_ids.extend(v_workspace_ids)

query["filter"].append({"k": "workspace_id", "v": workspace_ids, "o": "in"})

_LOGGER.debug(f"[aggregate_monthly_cost_report] query: {query}")
response = self.cost_mgr.analyze_monthly_costs(query, domain_id)
results = response.get("results", [])
issue_date = f"{issue_month}-{str(issue_day).zfill(2)}" if issue_month else None

for aggregated_cost_report in results:
ag_cost = aggregated_cost_report.pop("cost", 0.0)
aggregated_cost_report["workspace_id"] = v_workspace_id_map.get(
aggregated_cost_report["workspace_id"],
aggregated_cost_report["workspace_id"],
)

_ag_cost = aggregated_cost_report.pop("cost", 0.0)
ag_cost_report_currency = data_source_currency_map.get(
aggregated_cost_report.pop("data_source_id")
)

aggregated_cost_report["cost"] = CostReportManager.get_exchange_currency(
ag_cost, ag_cost_report_currency, self.currency_map
_ag_cost, ag_cost_report_currency, self.currency_map
)
aggregated_cost_report["status"] = status
aggregated_cost_report["currency"] = currency
Expand Down Expand Up @@ -578,3 +592,26 @@ def _aggregate_result_by_currency(results: list) -> list:
workspace_result_map[workspace_id] = result.copy()

return [workspace_result for workspace_result in workspace_result_map.values()]

def _get_virtual_workspace_ids_and_map(
self, domain_id: str, workspace_ids: list
) -> Tuple[list, dict]:
v_workspace_ids = []
v_workspace_id_map = {}

query = {
"filter": [
{"k": "domain_id", "v": domain_id, "o": "eq"},
{"k": "workspace_id", "v": workspace_ids, "o": "in"},
]
}
ds_account_vos, _ = self.ds_account_mgr.list_data_source_accounts(query)

for ds_account_vo in ds_account_vos:
v_workspace_ids.append(ds_account_vo.v_workspace_id)
if not v_workspace_id_map.get(ds_account_vo.v_workspace_id):
v_workspace_id_map[
ds_account_vo.v_workspace_id
] = ds_account_vo.workspace_id

return v_workspace_ids, v_workspace_id_map

0 comments on commit a30c81d

Please sign in to comment.