Skip to content

Commit 1e8fcf8

Browse files
authored
Merge pull request #185 from ImMin5/feature-cost-report
Modify calculate cost with currency when create cost_report and data
2 parents 4cf6670 + 57f4ef4 commit 1e8fcf8

File tree

4 files changed

+73
-57
lines changed

4 files changed

+73
-57
lines changed

src/spaceone/cost_analysis/interface/task/v1/data_source_sync_scheduler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _create_data_source_sync_task(self):
5959
return []
6060

6161
def _create_cost_report_run_task(self):
62-
if datetime.utcnow().hour == self._data_source_sync_hour - 1:
62+
if datetime.utcnow().hour == 0:
6363
stp = {
6464
"name": "cost_report_data_sync_schedule",
6565
"version": "v1",

src/spaceone/cost_analysis/manager/cost_report_manager.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ def stat_cost_reports(self, query: dict) -> dict:
4747
return self.cost_report_model.stat(**query)
4848

4949
@staticmethod
50-
def get_exchange_currency(cost_info: dict, currency_map: dict) -> dict:
51-
from_currency = next(iter(cost_info.keys()))
52-
from_cost = cost_info.get(from_currency)
53-
54-
cost = {}
55-
for currency, ratio_cost in currency_map.items():
56-
cost[currency] = from_cost * ratio_cost
50+
def get_exchange_currency(cost: float, currency: str, currency_map: dict) -> dict:
51+
cost_info = {}
52+
for convert_currency in currency_map.keys():
53+
cost_info.update(
54+
{
55+
convert_currency: currency_map[currency][
56+
f"{currency}/{convert_currency}"
57+
]
58+
* cost
59+
}
60+
)
5761

58-
return cost
62+
return cost_info
5963

6064
@staticmethod
6165
def get_currency_date(currency_date: str) -> str:

src/spaceone/cost_analysis/service/cost_report_data_service.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,16 @@ def _aggregate_monthly_cost_report_data(
208208

209209
results = response.get("results", [])
210210
for aggregated_cost_report_data in results:
211+
ag_cost = aggregated_cost_report_data.pop("cost", 0.0)
211212
ag_cost_report_currency = data_source_currency_map.get(
212213
aggregated_cost_report_data.get("data_source_id")
213214
)
214-
aggregated_cost_report_data["cost"] = {
215-
ag_cost_report_currency: aggregated_cost_report_data.pop("cost", 0.0)
216-
}
215+
216+
aggregated_cost_report_data[
217+
"cost"
218+
] = CostReportManager.get_exchange_currency(
219+
ag_cost, ag_cost_report_currency, self.currency_map
220+
)
217221
aggregated_cost_report_data["currency"] = currency
218222
aggregated_cost_report_data["issue_date"] = issue_date
219223
aggregated_cost_report_data["report_month"] = report_month
@@ -235,15 +239,6 @@ def _aggregate_monthly_cost_report_data(
235239
aggregated_cost_report_data["domain_id"] = domain_id
236240
aggregated_cost_report_data["is_confirmed"] = is_confirmed
237241

238-
aggregated_cost_report_data[
239-
"cost"
240-
] = CostReportManager.get_exchange_currency(
241-
aggregated_cost_report_data["cost"], self.currency_map
242-
)
243-
aggregated_cost_report_data[
244-
"currency_date"
245-
] = CostReportManager.get_currency_date(self.currency_date)
246-
247242
self.cost_report_data_mgr.create_cost_report_data(
248243
aggregated_cost_report_data
249244
)

src/spaceone/cost_analysis/service/cost_report_serivce.py

+53-36
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,16 @@ def _aggregate_monthly_cost_report(
272272
response = self.cost_mgr.analyze_monthly_costs(query, domain_id)
273273
results = response.get("results", [])
274274
for aggregated_cost_report in results:
275+
ag_cost = aggregated_cost_report.pop("cost", 0.0)
275276
ag_cost_report_currency = data_source_currency_map.get(
276277
aggregated_cost_report.pop("data_source_id")
277278
)
278-
aggregated_cost_report["cost"] = {
279-
ag_cost_report_currency: aggregated_cost_report.pop("cost")
280-
}
279+
280+
aggregated_cost_report["cost"] = CostReportManager.get_exchange_currency(
281+
ag_cost, ag_cost_report_currency, self.currency_map
282+
)
281283
aggregated_cost_report["status"] = status
282284
aggregated_cost_report["currency"] = currency
283-
aggregated_cost_report["report_number"] = self.generate_report_number(
284-
report_month, issue_day
285-
)
286285
if issue_month:
287286
aggregated_cost_report["issue_date"] = f"{issue_month}-{issue_day}"
288287
aggregated_cost_report["report_month"] = report_month
@@ -302,9 +301,15 @@ def _aggregate_monthly_cost_report(
302301
cost_report_data_svc.currency_map = self.currency_map
303302
cost_report_data_svc.currency_date = self.currency_date
304303

305-
for aggregated_cost_report in aggregated_cost_report_results:
306-
aggregated_cost_report["cost"] = CostReportManager.get_exchange_currency(
307-
aggregated_cost_report["cost"], self.currency_map
304+
start_cost_report_number = self.get_start_cost_report_number(
305+
domain_id, report_month, issue_day
306+
)
307+
308+
for cost_report_idx, aggregated_cost_report in enumerate(
309+
aggregated_cost_report_results, start=start_cost_report_number
310+
):
311+
aggregated_cost_report["report_number"] = self.generate_report_number(
312+
report_month, issue_day, cost_report_idx
308313
)
309314

310315
aggregated_cost_report[
@@ -383,6 +388,41 @@ def send_cost_report(self, cost_report_vo: CostReport) -> None:
383388
f"[send_cost_report] send cost report ({workspace_id}/{cost_report_vo.cost_report_id}) to {len(users_info)} users"
384389
)
385390

391+
def get_email_verified_workspace_owner_users(
392+
self, domain_id: str, workspace_id: str, role_types: list = None
393+
) -> list:
394+
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
395+
396+
if "WORKSPACE_OWNER" not in role_types:
397+
return []
398+
399+
# list users in workspace
400+
users_info = identity_mgr.list_workspace_users(
401+
params={
402+
"workspace_id": workspace_id,
403+
"state": "ENABLED",
404+
"role_type": "WORKSPACE_OWNER",
405+
"query": {
406+
"filter": [
407+
{"k": "email_verified", "v": True, "o": "eq"},
408+
]
409+
},
410+
},
411+
domain_id=domain_id,
412+
).get("results", [])
413+
414+
return users_info
415+
416+
def get_start_cost_report_number(
417+
self, domain_id: str, report_month: str, issue_day: int
418+
) -> int:
419+
return (
420+
self.cost_report_mgr.filter_cost_reports(
421+
domain_id=domain_id, issue_date=f"{report_month}-{issue_day}"
422+
).count()
423+
+ 1
424+
)
425+
386426
def _get_workspace_name_map(self, domain_id: str) -> Tuple[dict, list]:
387427
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
388428
workspace_name_map = {}
@@ -437,31 +477,6 @@ def _get_temporary_sso_access_token(self, domain_id: str, workspace_id: str) ->
437477
}
438478
return identity_mgr.grant_token(params)
439479

440-
def get_email_verified_workspace_owner_users(
441-
self, domain_id: str, workspace_id: str, role_types: list = None
442-
) -> list:
443-
identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager")
444-
445-
if "WORKSPACE_OWNER" not in role_types:
446-
return []
447-
448-
# list users in workspace
449-
users_info = identity_mgr.list_workspace_users(
450-
params={
451-
"workspace_id": workspace_id,
452-
"state": "ENABLED",
453-
"role_type": "WORKSPACE_OWNER",
454-
"query": {
455-
"filter": [
456-
{"k": "email_verified", "v": True, "o": "eq"},
457-
]
458-
},
459-
},
460-
domain_id=domain_id,
461-
).get("results", [])
462-
463-
return users_info
464-
465480
@staticmethod
466481
def _get_current_and_last_month() -> Tuple[str, str]:
467482
current_month = datetime.utcnow().strftime("%Y-%m")
@@ -482,11 +497,13 @@ def _get_issue_day(is_last_day: bool, issue_day: int) -> int:
482497
return min(issue_day, last_day)
483498

484499
@staticmethod
485-
def generate_report_number(report_month: str, issue_day: int) -> str:
500+
def generate_report_number(
501+
report_month: str, issue_day: int, cost_report_idx: int
502+
) -> str:
486503
report_date = f"{report_month}-{issue_day}"
487504
date_object = datetime.strptime(report_date, "%Y-%m-%d")
488505

489-
return f"CostReport_{date_object.strftime('%y%m%d%H%M')}"
506+
return f"CostReport_{date_object.strftime('%y%m%d')}{str(cost_report_idx).zfill(4)}"
490507

491508
@staticmethod
492509
def _get_data_source_currency_map(

0 commit comments

Comments
 (0)