Skip to content

Commit

Permalink
Updated the cost in the es
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Sep 27, 2023
1 parent 62f3dd7 commit 8a7720c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ def __prepare_update_es_data(self, source: dict, instance_data: list, user_cost:
for instance in instance_data:
instance_meta_data = instance.get('instance_data')
if instance.get('cluster_name') and instance.get('cluster_name') not in es_cluster_names:
source.setdefault('cluster_name', []).append(instance.get('cluster_name'))
source.setdefault('cluster_names', []).append(instance.get('cluster_name'))
if type(instance_meta_data) is not list:
instance_meta_data = [instance.get('instance_data')]
for data in instance_meta_data:
if data not in es_instance_data:
source.setdefault('instance_data', []).append(data)
source['cluster_names'] = list(set(source.get('cluster_names', [])))
source['duration'] = int(instance_data[self.ZERO].get('duration'))
source['estimated_cost'] = round(cost_estimation, self.DEFAULT_ROUND_DIGITS)
source['actual_cost'] = user_cost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ def update_cluster_cost(self):
raise NotImplemented("This method is not implemented")

@logger_time_stamp
def extend_tickets_budget(self, ticket_id: str, region_name: str):
def extend_tickets_budget(self, ticket_id: str, region_name: str, current_budget: int = 0):
"""
This method extends the ticket budget if any
:param ticket_id:
:param region_name:
:param current_budget:
:return:
"""
ticket_extended = False
Expand All @@ -101,6 +102,9 @@ def extend_tickets_budget(self, ticket_id: str, region_name: str):
if string_equal_ignore_case(self.__cloud_name, 'AWS'):
self.update_budget_tag_to_resources(region_name=region_name, ticket_id=ticket_id,
updated_budget=total_budget_to_extend)
update_data = {'estimated_cost': int(current_budget) + int(total_budget_to_extend)}
self.__es_operations.update_elasticsearch_index(index=self.__es_index_cro, metadata=update_data,
id=ticket_id)
for sub_ticket_id in sub_ticket_ids:
self.__jira_operations.move_issue_state(ticket_id=sub_ticket_id, state='closed')
logger.info(f'Updated the budget of the ticket: {ticket_id}')
Expand All @@ -111,7 +115,7 @@ def extend_tickets_budget(self, ticket_id: str, region_name: str):

@typeguard.typechecked
@logger_time_stamp
def extend_ticket_duration(self, ticket_id: str, region_name: str):
def extend_ticket_duration(self, ticket_id: str, region_name: str, current_duration: int = 0):
"""
This method extends the duration of the ticket if any
:param ticket_id:
Expand All @@ -125,6 +129,9 @@ def extend_ticket_duration(self, ticket_id: str, region_name: str):
if string_equal_ignore_case(self.__cloud_name, 'AWS'):
self.update_duration_tag_to_resources(region_name=region_name, ticket_id=ticket_id,
updated_duration=total_duration_to_extend)
update_data = {'duration': int(current_duration) + int(total_duration_to_extend)}
self.__es_operations.update_elasticsearch_index(index=self.__es_index_cro, metadata=update_data,
id=ticket_id)
for sub_ticket_id in sub_ticket_ids:
self.__jira_operations.move_issue_state(ticket_id=sub_ticket_id, state='closed')
logger.info(f'Updated the Duration of the ticket: {ticket_id}')
Expand Down Expand Up @@ -160,7 +167,8 @@ def _monitor_ticket_duration(self, ticket_id: str, region_name: str, duration: i
remaining_duration = duration - completed_duration
subject = body = None
if remaining_duration <= FIRST_CRO_ALERT:
ticket_extended = self.extend_ticket_duration(ticket_id=ticket_id, region_name=region_name)
ticket_extended = self.extend_ticket_duration(ticket_id=ticket_id, region_name=region_name,
current_duration=duration)
if not ticket_extended:
if remaining_duration == FIRST_CRO_ALERT:
subject, body = self.__mail_message.cro_monitor_alert_message(user=user, days=FIRST_CRO_ALERT, ticket_id=ticket_id)
Expand Down Expand Up @@ -192,8 +200,9 @@ def _monitor_ticket_budget(self, ticket_id: str, region_name: str, budget: int,
remaining_budget = budget - used_budget
threshold_budget = budget - (budget * (self.__ticket_over_usage_limit / 100))
subject = body = None
if threshold_budget >= remaining_budget >= 0:
ticket_extended = self.extend_tickets_budget(ticket_id=ticket_id, region_name=region_name)
if threshold_budget >= remaining_budget:
ticket_extended = self.extend_tickets_budget(ticket_id=ticket_id, region_name=region_name,
current_budget=budget)
if not ticket_extended:
subject, body = self.__mail_message.cro_monitor_budget_remain_alert(user=user, budget=budget,
ticket_id=ticket_id,
Expand Down

0 comments on commit 8a7720c

Please sign in to comment.