From 55cfd3b1637d9c1857801693553dc5e2ee0c43ee Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti Date: Tue, 3 Oct 2023 23:25:20 +0530 Subject: [PATCH] Moved the check of in_progress to update es method --- .../clouds/aws/ec2/collect_cro_reports.py | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py b/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py index 72abd627..79cbbed3 100644 --- a/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py +++ b/cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py @@ -203,19 +203,19 @@ def __upload_cro_report_to_es(self, monitor_data: dict): """ upload_data = {} for ticket_id, instance_data in monitor_data.items(): - if self.__check_value_in_es(tag_key='ticket_id_state', tag_value='in-progress', ticket_id=ticket_id): - ticket_id = ticket_id.split('-')[-1] - user = instance_data[self.ZERO].get('user') - user_project = instance_data[self.ZERO].get('project') - issue_description = self.jira_operations.get_issue_description(ticket_id=ticket_id, state='ANY') - ticket_opened_date = issue_description.get('TicketOpenedDate') - group_by_tag_name = self.COST_EXPLORER_TAGS[self.TICKET_ID_KEY] - user_cost = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, - requested_date=ticket_opened_date) - duration = int(instance_data[self.ZERO].get('duration', 0)) - user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, requested_date=datetime.utcnow(), extra_filter_key_values={'Project': user_project}, forecast=True, duration=duration) - cost_estimation = float(instance_data[self.ZERO].get('estimated_cost', self.ZERO)) - if self.__cost_over_usage.es_operations.verify_elastic_index_doc_id(index=self.__cost_over_usage.es_index_cro, doc_id=ticket_id): + ticket_id = ticket_id.split('-')[-1] + user = instance_data[self.ZERO].get('user') + user_project = instance_data[self.ZERO].get('project') + issue_description = self.jira_operations.get_issue_description(ticket_id=ticket_id, state='ANY') + ticket_opened_date = issue_description.get('TicketOpenedDate') + group_by_tag_name = self.COST_EXPLORER_TAGS[self.TICKET_ID_KEY] + user_cost = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, + requested_date=ticket_opened_date) + duration = int(instance_data[self.ZERO].get('duration', 0)) + user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, requested_date=datetime.utcnow(), extra_filter_key_values={'Project': user_project}, forecast=True, duration=duration) + cost_estimation = float(instance_data[self.ZERO].get('estimated_cost', self.ZERO)) + if self.__cost_over_usage.es_operations.verify_elastic_index_doc_id(index=self.__cost_over_usage.es_index_cro, doc_id=ticket_id): + if self.__check_value_in_es(tag_key='ticket_id_state', tag_value='in-progress', ticket_id=ticket_id): es_data = self.__cost_over_usage.es_operations.get_es_data_by_id(id=ticket_id, index=self.__cost_over_usage.es_index_cro) es_data['_source']['ticket_opened_date'] = ticket_opened_date.date() es_data['_source']['forecast'] = user_forecast @@ -223,16 +223,16 @@ def __upload_cro_report_to_es(self, monitor_data: dict): source = self.__prepare_update_es_data(source=es_data.get('_source'), instance_data=instance_data, cost_estimation=cost_estimation, user_cost=user_cost) self.__cost_over_usage.es_operations.update_elasticsearch_index(index=self.__es_index_cro, id=ticket_id, metadata=source) upload_data[ticket_id] = source - else: - if ticket_id not in upload_data: - source = self.prepare_instance_data(instance_data=instance_data, ticket_id=ticket_id, cost_estimation=cost_estimation, user=user, user_cost=user_cost, ticket_opened_date=ticket_opened_date) - source['ticket_opened_date'] = ticket_opened_date.date() - source['forecast'] = user_forecast - source['user'] = user - if not source.get(self.ALLOCATED_BUDGET): - source[self.ALLOCATED_BUDGET] = self.get_account_budget_from_payer_ce_report() - self.__cost_over_usage.es_operations.upload_to_elasticsearch(index=self.__es_index_cro, data=source, id=ticket_id) - upload_data[ticket_id] = source + else: + if ticket_id not in upload_data: + source = self.prepare_instance_data(instance_data=instance_data, ticket_id=ticket_id, cost_estimation=cost_estimation, user=user, user_cost=user_cost, ticket_opened_date=ticket_opened_date) + source['ticket_opened_date'] = ticket_opened_date.date() + source['forecast'] = user_forecast + source['user'] = user + if not source.get(self.ALLOCATED_BUDGET): + source[self.ALLOCATED_BUDGET] = self.get_account_budget_from_payer_ce_report() + self.__cost_over_usage.es_operations.upload_to_elasticsearch(index=self.__es_index_cro, data=source, id=ticket_id) + upload_data[ticket_id] = source return upload_data @logger_time_stamp