Skip to content

Commit

Permalink
feat: add cost_report_data analyze api
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMin5 committed Jan 25, 2024
1 parent eecbd35 commit bea918e
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 100 deletions.
6 changes: 6 additions & 0 deletions src/spaceone/cost_analysis/interface/grpc/cost_report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ def analyze(self, request, context):
cost_report_data_svc = CostReportDataService(metadata)
response: dict = cost_report_data_svc.analyze(params)
return self.dict_to_message(response)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
cost_report_data_svc = CostReportDataService(metadata)
response: dict = cost_report_data_svc.stat(params)
return self.dict_to_message(response)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def _init_config(self):

def create_task(self) -> list:
tasks = []
tasks.extend(self._send_cost_report_task())
tasks.extend(self._create_cost_report_run_task())
tasks.extend(self._create_data_source_sync_task())
return tasks
Expand Down Expand Up @@ -99,3 +100,32 @@ def _create_cost_report_run_task(self):
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] data_source_sync_time: {self._data_source_sync_hour} hour (UTC)"
)
return []

def _send_cost_report_task(self):
if datetime.utcnow().hour == self._data_source_sync_hour:
stp = {
"name": "cost_report_run_schedule",
"version": "v1",
"executionEngine": "BaseWorker",
"stages": [
{
"locator": "SERVICE",
"name": "CostReportService",
"metadata": {"token": self._token},
"method": "send_cost_report_by_cost_report_config",
"params": {"params": {}},
}
],
}
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] send_cost_report_by_cost_report_config => START"
)
return [stp]
else:
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] send_cost_report_by_cost_report_config => SKIP"
)
print(
f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] data_source_sync_time: {self._data_source_sync_hour} hour (UTC)"
)
return []
14 changes: 4 additions & 10 deletions src/spaceone/cost_analysis/manager/cost_report_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,11 @@ def run_cost_report_config(self, cost_report_config_vo: CostReportConfig) -> Non
pass

def get_cost_report_config(
self, cost_report_config_id: str, domain_id: str, workspace_id: str = None
self, cost_report_config_id: str, domain_id: str
) -> CostReportConfig:
conditions = {
"cost_report_config_id": cost_report_config_id,
"domain_id": domain_id,
}

if workspace_id:
conditions["workspace_id"] = workspace_id

return self.cost_report_config_model.get(**conditions)
return self.cost_report_config_model.get(
domain_id=domain_id, cost_report_config_id=cost_report_config_id
)

def list_cost_report_config(self, query: dict, domain_id) -> Tuple[QuerySet, int]:
self._create_default_cost_report_config(domain_id)
Expand Down
4 changes: 2 additions & 2 deletions src/spaceone/cost_analysis/manager/email_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def send_cost_report_email(
workspace_name=cost_report_vo.workspace_name,
report_date=cost_report_vo.issue_date,
report_period=self.get_date_range_of_month(cost_report_vo.report_month),
reset_password_link=cost_report_link,
download_link=cost_report_link,
)
subject = f'[{service_name}] #{cost_report_vo.report_number} {language_map_info["cost_report"]}'

Expand All @@ -61,5 +61,5 @@ def _get_service_name():
@staticmethod
def get_date_range_of_month(report_month: str):
year, month = report_month.split("-")
first_day, last_day = calendar.monthrange(year, month)
first_day, last_day = calendar.monthrange(int(year), int(month))
return f"{year}-{month}-{first_day} ~ {year}-{month}-{last_day}"
10 changes: 9 additions & 1 deletion src/spaceone/cost_analysis/manager/identity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ def __init__(self, *args, **kwargs):
SpaceConnector, service="identity"
)

def check_workspace(self, workspace_id: str, domain_id: str) -> None:
def get_domain_name(self, domain_id: str) -> str:
system_token = config.get_global("TOKEN")

self.identity_conn.dispatch(
"Domain.get", {"domain_id": domain_id}, token=system_token
)

def check_workspace(self, workspace_id: str, domain_id: str) -> None:
system_token = config.get_global("TOKEN")

domain_info = self.identity_conn.dispatch(
"Workspace.check",
{"workspace_id": workspace_id, "domain_id": domain_id},
token=system_token,
)
return domain_info["name"]

@cache.cacheable(
key="cost-analysis:workspace-name:{domain_id}:{workspace_id}:name", expire=300
Expand Down
3 changes: 3 additions & 0 deletions src/spaceone/cost_analysis/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
from spaceone.cost_analysis.model.cost_query_set_model import CostQuerySet
from spaceone.cost_analysis.model.job_model import Job
from spaceone.cost_analysis.model.job_task_model import JobTask
from spaceone.cost_analysis.model.cost_report_config.database import CostReportConfig
from spaceone.cost_analysis.model.cost_report_data.database import CostReportData
from spaceone.cost_analysis.model.cost_report.database import CostReport
5 changes: 1 addition & 4 deletions src/spaceone/cost_analysis/model/cost_report/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,5 @@ class CostReport(MongoModel):
"domain_id",
],
"ordering": ["created_at"],
"indexes": [
"domain_id",
"cost_report_id",
],
"indexes": ["cost_report_config_id"],
}
5 changes: 2 additions & 3 deletions src/spaceone/cost_analysis/model/cost_report_data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ class CostReportData(MongoModel):
],
"ordering": ["is_confirmed"],
"indexes": [
"domain_id",
"cost_report_config_id",
"cost_report_id",
"-report_year",
"cost_report_data_id",
"data_source_id",
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ def run(self, params: CostReportConfigRunRequest) -> None:
params.cost_report_config_id, params.domain_id
)

cost_report_service = CostReportService()
cost_report_service.create_cost_report(cost_report_config_vo)

cost_report_data_service = CostReportDataService()
cost_report_data_service.create_cost_report_data(cost_report_config_vo)

cost_report_service = CostReportService()
cost_report_service.create_cost_report(cost_report_config_vo)

@transaction(
permission="cost-analysis:CostReportConfig.read",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"],
Expand All @@ -228,7 +228,7 @@ def get(
CostReportConfigResponse:
"""
cost_report_config_vo = self.cost_report_mgr.get_cost_report_config(
params.cost_report_config_id, params.domain_id, params.workspace_id
params.cost_report_config_id, params.domain_id
)

return CostReportConfigResponse(**cost_report_config_vo.to_dict())
Expand Down
53 changes: 28 additions & 25 deletions src/spaceone/cost_analysis/service/cost_report_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,34 @@ def create_cost_report_data_by_cost_report_config(self, params: dict) -> None:
[
"cost_report_config_id",
"cost_report_data_id",
"product",
"provider",
"is_confirmed",
"data_source_id",
"workspace_id",
"domain_id",
]
)
@append_keyword_filter(["product", "cost_report_data_id"])
@convert_model
def stat(self, params: CostReportDataStatQueryRequest) -> dict:
"""Analyze cost report data"""
def list(
self, params: CostReportDataSearchQueryRequest
) -> Union[CostReportsDataResponse, dict]:
"""List cost report data"""

query = params.query or {}
return self.cost_report_data_mgr.stat_cost_reports_data(query)
(
cost_report_data_vos,
total_count,
) = self.cost_report_data_mgr.list_cost_reports_data(query)

cost_reports_data_info = [
cost_report_data_vo.to_dict()
for cost_report_data_vo in cost_report_data_vos
]
return CostReportsDataResponse(
results=cost_reports_data_info, total_count=total_count
)

@transaction(
permission="cost-analysis:CostReportData.read",
Expand All @@ -87,7 +105,10 @@ def analyze(self, params: CostReportDataAnalyzeQueryRequest) -> dict:
"""Analyze cost report data"""

query = params.query or {}
return self.cost_report_data_mgr.analyze_cost_reports_data(query)

return self.cost_report_data_mgr.analyze_cost_reports_data(
query, target="PRIMARY"
)

@transaction(
permission="cost-analysis:CostReportData.read",
Expand All @@ -97,34 +118,16 @@ def analyze(self, params: CostReportDataAnalyzeQueryRequest) -> dict:
[
"cost_report_config_id",
"cost_report_data_id",
"product",
"provider",
"is_confirmed",
"data_source_id",
"workspace_id",
"domain_id",
]
)
@append_keyword_filter(["product", "cost_report_data_id"])
@convert_model
def list(
self, params: CostReportDataSearchQueryRequest
) -> Union[CostReportsDataResponse, dict]:
"""List cost report data"""
def stat(self, params: CostReportDataStatQueryRequest) -> dict:
"""Analyze cost report data"""

query = params.query or {}
(
cost_report_data_vos,
total_count,
) = self.cost_report_data_mgr.list_cost_reports_data(query)

cost_reports_data_info = [
cost_report_data_vo.to_dict()
for cost_report_data_vo in cost_report_data_vos
]
return CostReportsDataResponse(
results=cost_reports_data_info, total_count=total_count
)
return self.cost_report_data_mgr.stat_cost_reports_data(query)

@staticmethod
def _get_all_cost_report_configs() -> QuerySet:
Expand Down
Loading

0 comments on commit bea918e

Please sign in to comment.