diff --git a/src/spaceone/cost_analysis/interface/task/v1/data_source_sync_scheduler.py b/src/spaceone/cost_analysis/interface/task/v1/data_source_sync_scheduler.py index 02a0de77..2379cdc1 100644 --- a/src/spaceone/cost_analysis/interface/task/v1/data_source_sync_scheduler.py +++ b/src/spaceone/cost_analysis/interface/task/v1/data_source_sync_scheduler.py @@ -72,30 +72,17 @@ def _create_cost_report_run_task(self): "metadata": {"token": self._token}, "method": "create_cost_report_by_cost_report_config", "params": {"params": {}}, - }, - { - "locator": "SERVICE", - "name": "CostReportDataService", - "metadata": {"token": self._token}, - "method": "create_cost_report_data_by_cost_report_config", - "params": {"params": {}}, - }, + } ], } print( f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] create_cost_report_by_cost_report_config => START" ) - print( - f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] create_cost_report_data_by_cost_report_config => START" - ) return [stp] else: print( f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] create_cost_report_by_cost_report_config => SKIP" ) - print( - f"{utils.datetime_to_iso8601(datetime.utcnow())} [INFO] [create_task] create_cost_report_data_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)" ) diff --git a/src/spaceone/cost_analysis/manager/cost_report_data_manager.py b/src/spaceone/cost_analysis/manager/cost_report_data_manager.py index 303d771a..bb476120 100644 --- a/src/spaceone/cost_analysis/manager/cost_report_data_manager.py +++ b/src/spaceone/cost_analysis/manager/cost_report_data_manager.py @@ -16,7 +16,7 @@ def __init__(self, *args, **kwargs): def create_cost_report_data(self, params: dict): def _rollback(vo: CostReportData): _LOGGER.info( - f"[create_cost_report_data._rollback] Delete cost report data : {vo.cost_report_name}, {vo.cost_report_data_id} " + f"[create_cost_report_data._rollback] Delete cost report data : {vo.cost_report_id}, {vo.cost_report_data_id} " ) vo.delete() diff --git a/src/spaceone/cost_analysis/model/cost_report_data/database.py b/src/spaceone/cost_analysis/model/cost_report_data/database.py index e1e84afa..633e5eb9 100644 --- a/src/spaceone/cost_analysis/model/cost_report_data/database.py +++ b/src/spaceone/cost_analysis/model/cost_report_data/database.py @@ -7,6 +7,7 @@ class CostReportData(MongoModel): cost_report_data_id = StringField(max_length=40, generate_id="cdr", unique=True) cost = DictField(default={}) cost_report_name = StringField(max_length=255) + issue_date = StringField(max_length=20) report_year = StringField(max_length=20) report_month = StringField(max_length=40) is_confirmed = BooleanField(default=False) diff --git a/src/spaceone/cost_analysis/model/cost_report_data/response.py b/src/spaceone/cost_analysis/model/cost_report_data/response.py index a122b825..7175d29d 100644 --- a/src/spaceone/cost_analysis/model/cost_report_data/response.py +++ b/src/spaceone/cost_analysis/model/cost_report_data/response.py @@ -10,6 +10,7 @@ class CostReportDataResponse(BaseModel): cost_report_data_id: Union[str, None] = None cost: Union[dict, None] = None cost_report_name: Union[str, None] = None + issue_date: Union[str, None] = None report_year: Union[str, None] = None report_month: Union[str, None] = None is_confirmed: Union[bool, None] = None diff --git a/src/spaceone/cost_analysis/service/cost_report_config_service.py b/src/spaceone/cost_analysis/service/cost_report_config_service.py index 5a3969d7..b008363f 100644 --- a/src/spaceone/cost_analysis/service/cost_report_config_service.py +++ b/src/spaceone/cost_analysis/service/cost_report_config_service.py @@ -205,9 +205,6 @@ def run(self, params: CostReportConfigRunRequest) -> None: params.cost_report_config_id, params.domain_id ) - 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) diff --git a/src/spaceone/cost_analysis/service/cost_report_data_service.py b/src/spaceone/cost_analysis/service/cost_report_data_service.py index ad1ba824..69ed8016 100644 --- a/src/spaceone/cost_analysis/service/cost_report_data_service.py +++ b/src/spaceone/cost_analysis/service/cost_report_data_service.py @@ -1,9 +1,5 @@ -import calendar import logging -from datetime import datetime -from dateutil.relativedelta import relativedelta from typing import Union, Tuple -from mongoengine import QuerySet from spaceone.core.service import * from spaceone.core.service.utils import * @@ -18,9 +14,9 @@ from spaceone.cost_analysis.manager.currency_manager import CurrencyManager from spaceone.cost_analysis.manager.data_source_manager import DataSourceManager from spaceone.cost_analysis.manager.identity_manager import IdentityManager +from spaceone.cost_analysis.model.cost_report.database import CostReport from spaceone.cost_analysis.model.cost_report_data.request import * from spaceone.cost_analysis.model.cost_report_data.response import * -from spaceone.cost_analysis.model.cost_report_config.database import CostReportConfig _LOGGER = logging.getLogger(__name__) @@ -37,15 +33,6 @@ def __init__(self, *args, **kwargs): self.cost_mgr = CostManager() self.cost_report_data_mgr = CostReportDataManager() - @transaction(exclude=["authentication", "authorization", "mutation"]) - def create_cost_report_data_by_cost_report_config(self, params: dict) -> None: - """Create cost report by cost report config""" - - for cost_report_config_vo in self._get_all_cost_report_configs(): - issue_day = self._get_issue_day(cost_report_config_vo) - if issue_day == datetime.utcnow().day: - self.create_cost_report_data(cost_report_config_vo) - @transaction( permission="cost-analysis:CostReportData.read", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"], @@ -65,7 +52,7 @@ def create_cost_report_data_by_cost_report_config(self, params: dict) -> None: @append_keyword_filter(["product", "cost_report_data_id"]) @convert_model def list( - self, params: CostReportDataSearchQueryRequest + self, params: CostReportDataSearchQueryRequest ) -> Union[CostReportsDataResponse, dict]: """List cost report data""" @@ -132,69 +119,64 @@ def stat(self, params: CostReportDataStatQueryRequest) -> dict: query = params.query or {} return self.cost_report_data_mgr.stat_cost_reports_data(query) - @staticmethod - def _get_all_cost_report_configs() -> QuerySet: + def create_cost_report_data(self, cost_report_vo: CostReport): cost_report_config_mgr = CostReportConfigManager() - return cost_report_config_mgr.filter_cost_report_configs(state="ENABLED") - def create_cost_report_data(self, cost_report_config_vo: CostReportConfig): - domain_id = cost_report_config_vo.domain_id + domain_id = cost_report_vo.domain_id + workspace_id = cost_report_vo.workspace_id + cost_report_config_id = cost_report_vo.cost_report_config_id + cost_report_id = cost_report_vo.cost_report_id + currency = cost_report_vo.currency + report_month = cost_report_vo.report_month + issue_date = cost_report_vo.issue_date + workspace_name = self._get_workspace_name(domain_id, workspace_id) + is_confirmed = True if cost_report_vo.status == "SUCCESS" else False + + cost_report_config_vo = cost_report_config_mgr.get_cost_report_config( + domain_id=domain_id, cost_report_config_id=cost_report_config_id + ) data_source_filter = cost_report_config_vo.data_source_filter or {} - workspace_name_map, workspace_ids = self._get_workspace_name_map(domain_id) data_source_currency_map, data_source_ids = self._get_data_source_currency_map( - data_source_filter, workspace_ids, domain_id + data_source_filter, workspace_id, domain_id ) - project_name_map = self._get_project_name_map(workspace_ids, domain_id) + project_name_map = self._get_project_name_map(workspace_id, domain_id) service_account_name_map = self._get_service_account_name_map( - workspace_ids, domain_id + workspace_id, domain_id ) - current_month, last_month = self._get_current_and_last_month() - issue_day = self._get_issue_day(cost_report_config_vo) - - if issue_day == datetime.utcnow().day: - self._aggregate_monthly_cost_report_data( - cost_report_config_vo=cost_report_config_vo, - workspace_name_map=workspace_name_map, - workspace_ids=workspace_ids, - project_name_map=project_name_map, - service_account_name_map=service_account_name_map, - data_source_currency_map=data_source_currency_map, - data_source_ids=data_source_ids, - report_month=last_month, - issue_day=issue_day, - is_confirmed=True, - ) - self._aggregate_monthly_cost_report_data( - cost_report_config_vo=cost_report_config_vo, - workspace_name_map=workspace_name_map, - workspace_ids=workspace_ids, + domain_id=domain_id, + workspace_id=workspace_id, + cost_report_config_id=cost_report_config_id, + cost_report_id=cost_report_id, + currency=currency, + workspace_name=workspace_name, project_name_map=project_name_map, service_account_name_map=service_account_name_map, data_source_currency_map=data_source_currency_map, data_source_ids=data_source_ids, - report_month=current_month, - issue_day=issue_day, - is_confirmed=False, + report_month=report_month, + issue_date=issue_date, + is_confirmed=is_confirmed, ) def _aggregate_monthly_cost_report_data( - self, - cost_report_config_vo: CostReportConfig, - workspace_name_map: dict, - workspace_ids: list, - project_name_map: dict, - service_account_name_map: dict, - data_source_currency_map: dict, - data_source_ids: list, - report_month: str, - issue_day: int, - is_confirmed: bool = False, + self, + domain_id: str, + workspace_id: str, + cost_report_config_id, + cost_report_id: str, + currency: str, + workspace_name: str, + project_name_map: dict, + service_account_name_map: dict, + data_source_currency_map: dict, + data_source_ids: list, + report_month: str, + issue_date: str, + is_confirmed: bool = False, ): - domain_id = cost_report_config_vo.domain_id - currency = cost_report_config_vo.currency report_year = report_month.split("-")[0] query = { @@ -202,7 +184,6 @@ def _aggregate_monthly_cost_report_data( "billed_year", "workspace_id", "project_id", - "data_source_id", "service_account_id", "data_source_id", "product", @@ -214,97 +195,106 @@ 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"}, - {"k": "workspace_id", "v": workspace_ids, "o": "in"}, ], } - _LOGGER.debug(f"[aggregate_monthly_cost_report] query: {query}") + _LOGGER.debug(f"[aggregate_monthly_cost_report_data] query: {query}") response = self.cost_mgr.analyze_monthly_costs( query, domain_id, target="PRIMARY" ) results = response.get("results", []) currency_mgr = CurrencyManager() - for aggregated_cost_report in results: + for aggregated_cost_report_data in results: ag_cost_report_currency = data_source_currency_map.get( - aggregated_cost_report.pop("data_source_id") + aggregated_cost_report_data.get("data_source_id") ) - aggregated_cost_report["cost"] = { - ag_cost_report_currency: aggregated_cost_report.pop("cost", 0.0) + aggregated_cost_report_data["cost"] = { + ag_cost_report_currency: aggregated_cost_report_data.pop("cost", 0.0) } - aggregated_cost_report["currency"] = currency - aggregated_cost_report["issue_date"] = f"{report_month}-{issue_day}" - aggregated_cost_report["report_month"] = report_month - aggregated_cost_report["report_year"] = aggregated_cost_report.pop( - "billed_year" - ) - aggregated_cost_report["workspace_name"] = workspace_name_map.get( - aggregated_cost_report["workspace_id"], "Unknown" + aggregated_cost_report_data["currency"] = currency + aggregated_cost_report_data["issue_date"] = issue_date + aggregated_cost_report_data["report_month"] = report_month + aggregated_cost_report_data[ + "report_year" + ] = aggregated_cost_report_data.pop("billed_year") + aggregated_cost_report_data["workspace_name"] = workspace_name + aggregated_cost_report_data["project_name"] = project_name_map.get( + aggregated_cost_report_data["project_id"], "Unknown" ) - aggregated_cost_report["project_name"] = project_name_map.get( - aggregated_cost_report["project_id"], "Unknown" - ) - aggregated_cost_report[ + aggregated_cost_report_data[ "service_account_name" ] = service_account_name_map.get( - aggregated_cost_report["service_account_id"], "Unknown" + aggregated_cost_report_data["service_account_id"], "Unknown" ) - aggregated_cost_report[ - "cost_report_config_id" - ] = cost_report_config_vo.cost_report_config_id - aggregated_cost_report["domain_id"] = domain_id - aggregated_cost_report["is_confirmed"] = is_confirmed + 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["domain_id"] = domain_id + aggregated_cost_report_data["is_confirmed"] = is_confirmed - aggregated_cost_report["cost"] = currency_mgr.convert_exchange_rate( - aggregated_cost_report + aggregated_cost_report_data["cost"] = currency_mgr.convert_exchange_rate( + aggregated_cost_report_data ).get("cost") - self.cost_report_data_mgr.create_cost_report_data(aggregated_cost_report) + self.cost_report_data_mgr.create_cost_report_data( + aggregated_cost_report_data + ) _LOGGER.debug( - f"[aggregate_monthly_cost_report] create cost report ({report_month}) (count = {len(results)})" + f"[aggregate_monthly_cost_report] create cost report data({report_month}) (count = {len(results)})" ) - @staticmethod - def _get_current_and_last_month() -> Tuple[str, str]: - current_month = datetime.utcnow().strftime("%Y-%m") - last_month = (datetime.utcnow() - relativedelta(months=1)).strftime("%Y-%m") - return current_month, last_month - - @staticmethod - def _get_issue_day(cost_report_config_vo: CostReportConfig) -> int: - current_date = datetime.utcnow() - current_year = current_date.year - current_month = current_date.month - - _, last_day = calendar.monthrange(current_year, current_month) + def _get_workspace_name(self, domain_id: str, workspace_id: str) -> str: + identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") + return identity_mgr.get_workspace( + domain_id=domain_id, workspace_id=workspace_id + ) - if cost_report_config_vo.is_last_day: - return last_day - else: - return min(cost_report_config_vo.issue_day, last_day) + def _get_project_name_map(self, workspace_id: str, domain_id: str) -> dict: + identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") + project_name_map = {} + projects = identity_mgr.list_projects( + { + "query": { + "filter": [ + {"k": "domain_id", "v": domain_id, "o": "eq"}, + {"k": "workspace_id", "v": workspace_id, "o": "eq"}, + ] + } + }, + domain_id, + ) + for project in projects.get("results", []): + project_name_map[project["project_id"]] = project["name"] + return project_name_map - def _get_workspace_name_map(self, domain_id: str) -> Tuple[dict, list]: + def _get_service_account_name_map(self, workspace_id: str, domain_id: str) -> dict: identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") - workspace_name_map = {} - workspaces = identity_mgr.list_workspaces( - {"query": {"filter": [{"k": "state", "v": "ENABLED", "o": "eq"}]}}, + service_account_name_map = {} + service_accounts = identity_mgr.list_service_accounts( + { + "filter": [ + {"k": "domain_id", "v": domain_id, "o": "eq"}, + {"k": "workspace_id", "v": workspace_id, "o": "eq"}, + ] + }, domain_id, ) - workspace_ids = [] - for workspace in workspaces.get("results", []): - workspace_name_map[workspace["workspace_id"]] = workspace["name"] - workspace_ids.append(workspace["workspace_id"]) - return workspace_name_map, workspace_ids + for service_account in service_accounts.get("results", []): + service_account_name_map[ + service_account["service_account_id"] + ] = service_account["name"] + return service_account_name_map @staticmethod def _get_data_source_currency_map( - data_source_filter: dict, workspace_ids, domain_id: str + data_source_filter: dict, workspace_id: str, domain_id: str ) -> Tuple[dict, list]: data_source_currency_map = {} data_source_mgr = DataSourceManager() @@ -312,7 +302,7 @@ def _get_data_source_currency_map( query = { "filter": [ {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "workspace_id", "v": workspace_ids, "o": "in"}, + {"k": "workspace_id", "v": [workspace_id, "*"], "o": "in"}, ] } @@ -335,39 +325,3 @@ 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_project_name_map(self, workspace_ids, domain_id: str) -> dict: - identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") - project_name_map = {} - projects = identity_mgr.list_projects( - { - "query": { - "filter": [ - {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "workspace_id", "v": workspace_ids, "o": "in"}, - ] - } - }, - domain_id, - ) - for project in projects.get("results", []): - project_name_map[project["project_id"]] = project["name"] - return project_name_map - - def _get_service_account_name_map(self, workspace_ids, domain_id: str) -> dict: - identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") - service_account_name_map = {} - service_accounts = identity_mgr.list_service_accounts( - { - "filter": [ - {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "workspace_id", "v": workspace_ids, "o": "in"}, - ] - }, - domain_id, - ) - for service_account in service_accounts.get("results", []): - service_account_name_map[ - service_account["service_account_id"] - ] = service_account["name"] - return service_account_name_map diff --git a/src/spaceone/cost_analysis/service/cost_report_serivce.py b/src/spaceone/cost_analysis/service/cost_report_serivce.py index ab4f00ee..73eebf4f 100644 --- a/src/spaceone/cost_analysis/service/cost_report_serivce.py +++ b/src/spaceone/cost_analysis/service/cost_report_serivce.py @@ -24,6 +24,9 @@ from spaceone.cost_analysis.manager.data_source_manager import DataSourceManager from spaceone.cost_analysis.manager.email_manager import EmailManager from spaceone.cost_analysis.manager.identity_manager import IdentityManager +from spaceone.cost_analysis.service.cost_report_data_service import ( + CostReportDataService, +) _LOGGER = logging.getLogger(__name__) @@ -50,7 +53,10 @@ def create_cost_report_by_cost_report_config(self, params: dict): def send_cost_report_by_cost_report_config(self, params: dict): """Send cost report by cost report config""" - cost_report_vos = self.cost_report_mgr.filter_cost_reports(status="SUCCESS") + current_date = datetime.now().strftime("%Y-%m-%d") + cost_report_vos = self.cost_report_mgr.filter_cost_reports( + status="SUCCESS", issue_date=current_date + ) for cost_report_vo in cost_report_vos: self.send_cost_report(cost_report_vo) @@ -176,56 +182,69 @@ def stat(self, params: CostReportDataStatQueryRequest) -> dict: return self.cost_report_mgr.stat_cost_reports(query) def create_cost_report(self, cost_report_config_vo: CostReportConfig): + cost_report_config_id = cost_report_config_vo.cost_report_config_id domain_id = cost_report_config_vo.domain_id data_source_filter = cost_report_config_vo.data_source_filter or {} + is_last_day = cost_report_config_vo.is_last_day + issue_day = cost_report_config_vo.issue_day + currency = cost_report_config_vo.currency workspace_name_map, workspace_ids = self._get_workspace_name_map(domain_id) data_source_currency_map, data_source_ids = self._get_data_source_currency_map( data_source_filter, workspace_ids, domain_id ) + issue_day = self._get_issue_day(is_last_day, issue_day) current_month, last_month = self._get_current_and_last_month() - issue_day = self._get_issue_day(cost_report_config_vo) if issue_day == datetime.utcnow().day: self._aggregate_monthly_cost_report( - cost_report_config_vo, - workspace_name_map, - workspace_ids, - data_source_currency_map, - data_source_ids, - last_month, - issue_day, - "SUCCESS", + domain_id=domain_id, + cost_report_config_id=cost_report_config_id, + workspace_name_map=workspace_name_map, + workspace_ids=workspace_ids, + data_source_currency_map=data_source_currency_map, + data_source_ids=data_source_ids, + report_month=last_month, + currency=currency, + issue_day=issue_day, + status="SUCCESS", + issue_month=current_month, ) self._aggregate_monthly_cost_report( - cost_report_config_vo, - workspace_name_map, - workspace_ids, - data_source_currency_map, - data_source_ids, - current_month, - issue_day, - "IN_PROGRESS", + domain_id=domain_id, + cost_report_config_id=cost_report_config_id, + workspace_name_map=workspace_name_map, + workspace_ids=workspace_ids, + data_source_currency_map=data_source_currency_map, + data_source_ids=data_source_ids, + report_month=current_month, + currency=currency, + issue_day=issue_day, + status="IN_PROGRESS", ) def _aggregate_monthly_cost_report( self, - cost_report_config_vo: CostReportConfig, + domain_id: str, + cost_report_config_id: str, workspace_name_map: dict, workspace_ids: list, data_source_currency_map: dict, data_source_ids: list, report_month: str, + currency: str, issue_day: int, - status: str = None, + status: str, + issue_month: str = None, ) -> None: - domain_id = cost_report_config_vo.domain_id - currency = cost_report_config_vo.currency report_year = report_month.split("-")[0] - # collect enabled data sources + # delete old cost_reports + self._delete_old_cost_reports(report_month, domain_id, cost_report_config_id) + + # collect enabled data sources query = { "group_by": ["workspace_id", "billed_year", "data_source_id"], "fields": { @@ -260,7 +279,8 @@ def _aggregate_monthly_cost_report( aggregated_cost_report["report_number"] = self.generate_report_number( report_month, issue_day ) - aggregated_cost_report["issue_date"] = f"{report_month}-{issue_day}" + if issue_month: + aggregated_cost_report["issue_date"] = f"{issue_month}-{issue_day}" aggregated_cost_report["report_month"] = report_month aggregated_cost_report["report_year"] = aggregated_cost_report.pop( "billed_year" @@ -269,10 +289,8 @@ def _aggregate_monthly_cost_report( aggregated_cost_report["workspace_id"], "Unknown" ) aggregated_cost_report["bank_name"] = "Yahoo! Finance" # todo : replace - aggregated_cost_report[ - "cost_report_config_id" - ] = cost_report_config_vo.cost_report_config_id - aggregated_cost_report["domain_id"] = cost_report_config_vo.domain_id + aggregated_cost_report["cost_report_config_id"] = cost_report_config_id + aggregated_cost_report["domain_id"] = domain_id aggregated_cost_report_results = self._aggregate_result_by_currency(results) @@ -281,25 +299,29 @@ def _aggregate_monthly_cost_report( aggregated_cost_report = currency_mgr.convert_exchange_rate( aggregated_cost_report ) - self.cost_report_mgr.create_cost_report(aggregated_cost_report) + cost_report_vo = self.cost_report_mgr.create_cost_report( + aggregated_cost_report + ) + cost_report_data_svc = CostReportDataService() + cost_report_data_svc.create_cost_report_data(cost_report_vo) _LOGGER.debug( f"[aggregate_monthly_cost_report] create cost report ({report_month}) \ (count = {len(aggregated_cost_report_results)})" ) - self._delete_old_cost_reports(report_month, domain_id) def _get_all_cost_report_configs(self) -> QuerySet: return self.cost_report_config_mgr.filter_cost_report_configs(state="ENABLED") - def _delete_old_cost_reports(self, report_month: str, domain_id: str) -> None: - yesterday_datetime = datetime.utcnow() - relativedelta(day=1) # todo : refactor + def _delete_old_cost_reports( + self, report_month: str, domain_id: str, cost_report_config_id: str + ) -> None: cost_report_delete_query = { "filter": [ + {"k": "cost_report_config_id", "v": cost_report_config_id, "o": "eq"}, {"k": "report_month", "v": report_month, "o": "eq"}, {"k": "status", "v": "IN_PROGRESS", "o": "eq"}, {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "created_at", "v": yesterday_datetime, "o": "datetime_lt"}, ] } cost_reports_vos, total_count = self.cost_report_mgr.list_cost_reports( @@ -416,17 +438,17 @@ def _get_current_and_last_month() -> Tuple[str, str]: return current_month, last_month @staticmethod - def _get_issue_day(cost_report_config_vo: CostReportConfig) -> int: + def _get_issue_day(is_last_day: bool, issue_day: int) -> int: current_date = datetime.utcnow() current_year = current_date.year current_month = current_date.month _, last_day = calendar.monthrange(current_year, current_month) - if cost_report_config_vo.is_last_day: + if is_last_day: return last_day else: - return min(cost_report_config_vo.issue_day, last_day) + return min(issue_day, last_day) @staticmethod def generate_report_number(report_month: str, issue_day: int) -> str: @@ -445,7 +467,7 @@ def _get_data_source_currency_map( query = { "filter": [ {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "workspace_id", "v": workspace_ids, "o": "in"}, + {"k": "workspace_id", "v": workspace_ids + ["*"], "o": "in"}, ] } if data_sources := data_source_filter.get("data_sources"):