Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify get is_confirmed value for aggregation month #295

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ def _rollback(old_data):
def update_is_confirmed_unified_cost_job(
unified_cost_job_vo: UnifiedCostJob, is_confirmed: bool
):
update_params = {"is_confirmed": is_confirmed}

if is_confirmed:
return unified_cost_job_vo.update(
{"is_confirmed": True, "confirmed_at": datetime.now()}
)
else:
return unified_cost_job_vo.update({"is_confirmed": False})
update_params["confirmed_at"] = datetime.utcnow()
return unified_cost_job_vo.update(update_params)

def filter_unified_cost_jobs(self, **conditions):
return self.unified_cost_job_model.filter(**conditions)
Expand Down
62 changes: 30 additions & 32 deletions src/spaceone/cost_analysis/service/unified_cost_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_unified_cost_by_scheduler(self, params: dict) -> None:
if current_hour == unified_cost_run_hour:
self.run_current_month_unified_costs(domain_id)

if self._check_unified_cost_job_is_confirmed_with_month(
if not self._check_unified_cost_job_is_confirmed_with_month(
domain_id, current_month
):
last_month = (current_date - relativedelta(months=1)).strftime(
Expand All @@ -91,26 +91,23 @@ def run_unified_cost(self, params: dict):
"month": 'str', (optional),
}
"""

domain_id = params["domain_id"]
aggregation_month: Union[str, None] = params.get("month")

config_mgr = ConfigManager()
unified_cost_config = config_mgr.get_unified_cost_config(domain_id)

if not aggregation_month:
aggregation_month = datetime.utcnow().strftime("%Y-%m")
is_confirmed = False
elif self._get_is_confirmed_with_aggregation_month(aggregation_month):
is_confirmed = True
else:
_LOGGER.debug(
f"[run_unified_cost] skip aggregation month: {aggregation_month}"
is_confirmed = self._get_is_confirmed_with_aggregation_month(
aggregation_month, unified_cost_config
)
return None

# todo: create job logic
unified_cost_job_vo = self._get_unified_cost_job(domain_id, aggregation_month)

config_mgr = ConfigManager()
unified_cost_config = config_mgr.get_unified_cost_config(domain_id)

aggregation_date = self._get_aggregation_date(
unified_cost_config, aggregation_month, is_confirmed
)
Expand Down Expand Up @@ -327,8 +324,6 @@ def create_unified_cost_with_workspace(
aggregation_date: datetime,
is_confirmed: bool = False,
) -> None:
if workspace_id:
return

identity_mgr = IdentityManager(token=config.get_global("TOKEN"))
workspace_ids = [workspace_id]
Expand Down Expand Up @@ -537,12 +532,11 @@ def _check_unified_cost_job_is_confirmed_with_month(
domain_id=domain_id, billed_month=current_month
)
if unified_cost_job_vos:
if unified_cost_job_vos[0].is_confirmed:
return False
else:
return True
unified_cost_job_vo = unified_cost_job_vos[0]

return unified_cost_job_vo.is_confirmed
else:
return True
return False

def _get_unified_cost_job(
self, domain_id: str, aggregation_month: str
Expand Down Expand Up @@ -606,17 +600,17 @@ def _get_exchange_date(

@staticmethod
def get_is_last_day(
current_date: datetime, is_last_day: bool, current_day: int = None
current_date: datetime, is_last_day: bool, current_day: int
) -> int:
current_year = current_date.year
current_month = current_date.month

_, last_day = calendar.monthrange(current_year, current_month)

if is_last_day:
return last_day
return int(last_day)
else:
return min(current_day, last_day)
return int(min(current_day, last_day))

@staticmethod
def _get_workspace_ids_with_none(domain_id: str) -> list:
Expand All @@ -635,18 +629,22 @@ def _get_workspace_ids_with_none(domain_id: str) -> list:

return workspace_ids

@staticmethod
def _get_is_confirmed_with_aggregation_month(aggregation_month: str) -> bool:
def _get_is_confirmed_with_aggregation_month(
self, aggregation_month: str, unified_cost_config: dict
) -> bool:
is_confirmed = False
aggregation_date: datetime = datetime.strptime(aggregation_month, "%Y-%m")
current_date = datetime.utcnow()
if current_date > aggregation_date:
if current_date.year == aggregation_date.year:
if current_date.month == aggregation_date.month:
is_confirmed = False
else:
is_confirmed = True
else:
is_confirmed = True
current_date: datetime = datetime.utcnow()
aggregation_date = datetime.strptime(aggregation_month, "%Y-%m")

aggregation_day = unified_cost_config.get("aggregation_day", 15)
is_last_day = unified_cost_config.get("is_last_day", False)

last_day = self.get_is_last_day(current_date, is_last_day, aggregation_day)

aggregation_date = aggregation_date.replace(day=last_day) + relativedelta(
months=1
)
if current_date >= aggregation_date:
is_confirmed = True

return is_confirmed
Loading