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

Fixed the datetime decryption #799

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,6 +1,6 @@
import json
import tempfile
from datetime import datetime
from datetime import datetime, UTC

import typeguard

Expand Down Expand Up @@ -215,7 +215,7 @@ def update_cluster_cost(self):
self.__es_operations.update_elasticsearch_index(index=self.es_cro_index, id=ticket_id,
metadata={
'cluster_cost': cluster_cost,
'timestamp': datetime.utcnow()
'timestamp': datetime.now(UTC.utc)
})

def __prepare_athena_query_for_cluster_cost(self, names: list):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

import typeguard

Expand Down Expand Up @@ -45,7 +45,7 @@ def get_account_budget_from_payer_ce_report(self):
"must": [
{"term": {"CloudName.keyword": self.__public_cloud_name}},
{"term": {"AccountId.keyword": self.__account_id}},
{"term": {"Month": str(datetime.utcnow().year)}},
{"term": {"Month": str(datetime.now(UTC.utc).year)}},
]
}
},
Expand All @@ -61,7 +61,7 @@ def get_total_account_usage_cost(self):
This method returns the total account budget till date for this year
:return:
"""
current_date = datetime.utcnow().date()
current_date = datetime.now(UTC.utc).date()
start_date = datetime(current_date.year, 1, 1).date()
cost_explorer_operations = self.__cost_over_usage.get_cost_explorer_operations()
response = cost_explorer_operations.get_cost_and_usage_from_aws(start_date=str(start_date), end_date=str(current_date+timedelta(days=1)), granularity='MONTHLY')
Expand Down Expand Up @@ -92,7 +92,7 @@ def get_user_cost_data(self, group_by_tag_name: str, group_by_tag_value: str, re
return_key = 'Forecast'
else:
response = self.__cost_over_usage.get_monthly_user_es_cost_data(start_date=start_date,
end_date=datetime.utcnow().replace(microsecond=self.ZERO) + timedelta(days=1),
end_date=datetime.now(UTC.utc).replace(microsecond=self.ZERO) + timedelta(days=1),
extra_matches=extra_filter_matches, extra_operation=self.AND, tag_name=group_by_tag_name)
return_key = 'Cost'
if response:
Expand Down Expand Up @@ -173,7 +173,7 @@ def __prepare_update_es_data(self, source: dict, instance_data: list, user_cost:
source['user_cro'] = instance_data[self.ZERO].get('user_cro')
if instance_data[self.ZERO].get('user') and source.get('user') != instance_data[self.ZERO].get('user'):
source['user'] = instance_data[self.ZERO].get('user')
source['timestamp'] = datetime.utcnow()
source['timestamp'] = datetime.now(UTC.utc)
if source.get('ticket_id_state') != 'in-progress':
source['ticket_id_state'] = 'in-progress'
source['approved_manager'] = instance_data[self.ZERO].get('approved_manager')
Expand Down Expand Up @@ -213,7 +213,7 @@ def __upload_cro_report_to_es(self, monitor_data: dict):
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)
user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, requested_date=datetime.now(UTC.utc), 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):
es_data = self.__cost_over_usage.es_operations.get_es_data_by_id(id=ticket_id,index=self.__cost_over_usage.es_index_cro)
Expand Down Expand Up @@ -270,10 +270,10 @@ def update_in_progress_ticket_cost(self):
user_name=user_name)
user_daily_cost.update(ce_user_daily_report)
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(),
group_by_tag_value=ticket_id, requested_date=datetime.now(UTC.utc),
forecast=True, duration=duration)
update_data = {'actual_cost': user_cost, 'forecast': user_forecast, 'timestamp': datetime.utcnow(),
f'TotalCurrentUsage-{datetime.utcnow().year}': total_account_cost,
update_data = {'actual_cost': user_cost, 'forecast': user_forecast, 'timestamp': datetime.now(UTC.utc),
f'TotalCurrentUsage-{datetime.now(UTC.utc).year}': total_account_cost,
'user_daily_cost': str(user_daily_cost)}
if not source_data.get(self.ALLOCATED_BUDGET):
update_data[self.ALLOCATED_BUDGET] = self.get_account_budget_from_payer_ce_report()
Expand Down Expand Up @@ -302,7 +302,7 @@ def __get_user_usage_by_granularity(self, result_back_data: dict, tag_name: str,
:param tag_value:
:return:
"""
end_date = datetime.utcnow().date()
end_date = datetime.now(UTC.utc).date()
start_date = end_date - timedelta(days=days)
cost_explorer_object = self.__cost_over_usage.get_cost_explorer_operations()
ce_daily_usage = cost_explorer_object.get_cost_by_tags(tag=tag_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
from ast import literal_eval
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

import typeguard

Expand Down Expand Up @@ -45,7 +44,7 @@ def __init__(self):
self.__cro_admins = self.__environment_variables_dict.get('CRO_DEFAULT_ADMINS', [])
self.es_index_cro = self.__environment_variables_dict.get('CRO_ES_INDEX', '')
self.__cro_duration_days = self.__environment_variables_dict.get('CRO_DURATION_DAYS')
self.current_end_date = datetime.utcnow()
self.current_end_date = datetime.now(UTC.utc)
self.current_start_date = self.current_end_date - timedelta(days=self.__cro_duration_days)
self.__public_cloud_name = self.__environment_variables_dict.get('PUBLIC_CLOUD_NAME')
self.__ce_operations = CostExplorerOperations()
Expand Down Expand Up @@ -293,7 +292,7 @@ def get_last_mail_alert_status(self, user: str):
last_alert = response[0]
last_send_date = last_alert.get('_source').get('timestamp')
alert_number = last_alert.get('_source').get('Alert', 0)
current_date = datetime.utcnow().date()
current_date = datetime.now(UTC.utc).date()
last_send_date = datetime.strptime(last_send_date, self.TIMESTAMP_DATE_FORMAT).date()
days = (current_date - last_send_date).days
if days % self.SEND_ALERT_DAY == 0 and last_send_date != current_date:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, UTC

import boto3

Expand All @@ -14,7 +14,7 @@

class RunCRO:

PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.utcnow().date()}'
PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.now(UTC.utc).date()}'
PERSISTENT_RUN_INDEX = 'cloud_resource_orchestration_persistence_run'

def __init__(self):
Expand All @@ -39,7 +39,7 @@ def send_cro_alerts(self):
last_run_time = source.get(f'last_run_{self.account.lower()}')
if last_run_time:
last_updated_time = datetime.strptime(last_run_time, "%Y-%m-%dT%H:%M:%S.%f").date()
if last_updated_time == datetime.utcnow().date():
if last_updated_time == datetime.now(UTC.utc).date():
first_run = False
self.__environment_variables_dict.update({'CRO_FIRST_RUN': first_run})
if first_run:
Expand All @@ -57,9 +57,9 @@ def save_current_timestamp(self):
:return:
"""
if not self.cro_cost_over_usage.es_operations.verify_elastic_index_doc_id(index=self.PERSISTENT_RUN_INDEX, doc_id=self.PERSISTENT_RUN_DOC_ID):
self.cro_cost_over_usage.es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={f'last_run_{self.account}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
self.cro_cost_over_usage.es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={f'last_run_{self.account}': datetime.now(UTC.utc)}, id=self.PERSISTENT_RUN_DOC_ID)
else:
self.cro_cost_over_usage.es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX, metadata={f'last_run_{self.account}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
self.cro_cost_over_usage.es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX, metadata={f'last_run_{self.account}': datetime.now(UTC.utc)}, id=self.PERSISTENT_RUN_DOC_ID)

@logger_time_stamp
def run_cloud_resources(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

import typeguard

from cloud_governance.cloud_resource_orchestration.clouds.azure.resource_groups.cost_over_usage import CostOverUsage
from cloud_governance.cloud_resource_orchestration.clouds.common.abstract_collect_cro_reports import \
AbstractCollectCROReports
from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp
from cloud_governance.main.environment_variables import environment_variables


class CollectCROReports(AbstractCollectCROReports):
Expand All @@ -33,7 +31,7 @@ def _get_account_budget_from_payer_ce_report(self):
"must": [
{"term": {"CloudName.keyword": self._public_cloud_name}},
{"term": {"AccountId.keyword": self._account_id}},
{"term": {"Month": str(datetime.utcnow().year)}},
{"term": {"Month": str(datetime.now(UTC.utc).year)}},
]
}
},
Expand Down Expand Up @@ -69,7 +67,7 @@ def get_user_cost_data(self, group_by_tag_name: str, group_by_tag_value: str, re
resource_type = 'Forecast'
pass
else:
end_date = datetime.utcnow().replace(microsecond=self.ZERO) + timedelta(days=1)
end_date = datetime.now(UTC.utc).replace(microsecond=self.ZERO) + timedelta(days=1)
response = self.__cost_over_usage.get_monthly_user_es_cost_data(start_date=start_date, end_date=end_date,
extra_matches=extra_filter_matches,
extra_operation=self.AND,
Expand Down Expand Up @@ -124,7 +122,7 @@ def _get_total_account_usage_cost(self):
This method returns the total account budget till date for this year
:return:
"""
current_date = datetime.utcnow()
current_date = datetime.now(UTC.utc)
start_date = datetime(current_date.year, 1, 1, 0, 0, 0)
end_date = current_date + timedelta(days=1)
cost_explorer_operations = self.__cost_over_usage.get_cost_management_object()
Expand Down Expand Up @@ -163,8 +161,8 @@ def update_in_progress_ticket_cost(self):
group_by_tag_name=group_by_tag_name,
user_name=user_name)
user_daily_cost.update(ce_user_daily_report)
update_data = {'actual_cost': user_cost, 'timestamp': datetime.utcnow(),
f'TotalCurrentUsage-{datetime.utcnow().year}': total_account_cost,
update_data = {'actual_cost': user_cost, 'timestamp': datetime.now(UTC.utc),
f'TotalCurrentUsage-{datetime.now(UTC.utc).year}': total_account_cost,
'user_daily_cost': str(user_daily_cost)}
if not source_data.get(self.ALLOCATED_BUDGET):
update_data[self.ALLOCATED_BUDGET] = self._get_account_budget_from_payer_ce_report()
Expand Down Expand Up @@ -192,7 +190,7 @@ def _get_user_usage_by_granularity(self, result_back_data: dict, tag_name: str,
:param tag_value:
:return:
"""
end_date = datetime.utcnow()
end_date = datetime.now(UTC.utc)
start_date = end_date - timedelta(days=days)
cost_explorer_object = self.__cost_over_usage.get_cost_management_object()
ce_daily_usage = cost_explorer_object.get_usage(scope=self.__scope, grouping=[tag_name], granularity='Daily',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import logging
from abc import ABC
from datetime import datetime, timedelta
from datetime import datetime, UTC

import typeguard

from cloud_governance.cloud_resource_orchestration.clouds.aws.ec2.cost_over_usage import CostOverUsage
from cloud_governance.common.clouds.aws.iam.iam_operations import IAMOperations
from cloud_governance.common.elasticsearch.elasticsearch_operations import ElasticSearchOperations
from cloud_governance.common.jira.jira_operations import JiraOperations
from cloud_governance.common.logger.init_logger import handler
from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp
from cloud_governance.main.environment_variables import environment_variables

Expand Down Expand Up @@ -116,7 +112,7 @@ def _prepare_update_es_data(self, source: dict, instance_data: list, user_cost:
source['user_cro'] = instance_data[self.ZERO].get('user_cro')
if instance_data[self.ZERO].get('user') and source.get('user') != instance_data[self.ZERO].get('user'):
source['user'] = instance_data[self.ZERO].get('user')
source['timestamp'] = datetime.utcnow()
source['timestamp'] = datetime.now(UTC.utc)
if source.get('ticket_id_state') != 'in-progress':
source['ticket_id_state'] = 'in-progress'
source['approved_manager'] = instance_data[self.ZERO].get('approved_manager')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from abc import ABC, abstractmethod
from ast import literal_eval
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC

import typeguard

Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self):
self.es_index_cro = self._environment_variables_dict.get('CRO_ES_INDEX', '')
self._cro_duration_days = self._environment_variables_dict.get('CRO_DURATION_DAYS')
self._over_usage_threshold = OVER_USAGE_THRESHOLD * self._over_usage_amount
self.current_end_date = datetime.utcnow()
self.current_end_date = datetime.now(UTC.utc)
self.current_start_date = self.current_end_date - timedelta(days=self._cro_duration_days)
self.es_operations = ElasticSearchOperations(es_host=self._es_host, es_port=self._es_port)
self._elastic_search_queries = ElasticSearchQueries(cro_duration_days=self._cro_duration_days)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from abc import abstractmethod, ABC
from datetime import datetime
from datetime import datetime, UTC

import typeguard

from cloud_governance.cloud_resource_orchestration.utils.common_operations import string_equal_ignore_case
from cloud_governance.cloud_resource_orchestration.utils.elastic_search_queries import ElasticSearchQueries
from cloud_governance.cloud_resource_orchestration.utils.constant_variables import FIRST_CRO_ALERT, SECOND_CRO_ALERT, \
CLOSE_JIRA_TICKET, JIRA_ISSUE_NEW_STATE, DATE_FORMAT
CLOSE_JIRA_TICKET, DATE_FORMAT
from cloud_governance.common.elasticsearch.elasticsearch_operations import ElasticSearchOperations
from cloud_governance.common.jira.jira_operations import JiraOperations
from cloud_governance.common.logger.init_logger import logger
Expand Down Expand Up @@ -147,7 +147,7 @@ def __close_and_update_ticket_data_in_es(self, ticket_id: str):
This method close the ticket and update in ElasticSearch
:return:
"""
data = {'timestamp': datetime.utcnow(), 'ticket_id_state': 'closed'}
data = {'timestamp': datetime.now(UTC.utc), 'ticket_id_state': 'closed'}
if self.__es_operations.check_elastic_search_connection():
self.__es_operations.update_elasticsearch_index(index=self.__es_index_cro, id=ticket_id, metadata=data)
self.__jira_operations.move_issue_state(ticket_id, state='CLOSED')
Expand Down Expand Up @@ -238,7 +238,7 @@ def _monitor_in_progress_tickets(self):
duration = int(source_data.get('duration', 0))
used_budget = int(source_data.get('actual_cost', 0))
ticket_start_date = datetime.strptime(source_data.get('ticket_opened_date'), DATE_FORMAT).date()
completed_duration = (datetime.utcnow().date() - ticket_start_date).days
completed_duration = (datetime.now(UTC.utc).date() - ticket_start_date).days
self._monitor_ticket_budget(ticket_id=ticket_id, region_name=region_name, budget=budget,
used_budget=used_budget,
user_cro=source_data.get('user_cro'),
Expand Down
10 changes: 5 additions & 5 deletions cloud_governance/cloud_resource_orchestration/common/run_cro.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, UTC

from cloud_governance.cloud_resource_orchestration.common.cro_object import CroObject
from cloud_governance.cloud_resource_orchestration.utils.common_operations import string_equal_ignore_case
Expand All @@ -11,7 +11,7 @@
class RunCRO:
"""This class monitors cro activities"""

PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.utcnow().date()}'
PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.now(UTC.utc).date()}'
PERSISTENT_RUN_INDEX = 'cloud_resource_orchestration_persistence_run'

def __init__(self):
Expand All @@ -34,10 +34,10 @@ def save_current_timestamp(self):
if not self.__es_operations.verify_elastic_index_doc_id(index=self.PERSISTENT_RUN_INDEX,
doc_id=self.PERSISTENT_RUN_DOC_ID):
self.__es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={
f'last_run_{self.__account.lower()}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
f'last_run_{self.__account.lower()}': datetime.now(UTC.utc)}, id=self.PERSISTENT_RUN_DOC_ID)
else:
self.__es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX,
metadata={f'last_run_{self.__account.lower()}': datetime.utcnow()},
metadata={f'last_run_{self.__account.lower()}': datetime.now(UTC.utc)},
id=self.PERSISTENT_RUN_DOC_ID)

@logger_time_stamp
Expand All @@ -54,7 +54,7 @@ def __send_cro_alerts(self):
last_run_time = source.get(f'last_run_{self.__account.lower()}')
if last_run_time:
last_updated_time = datetime.strptime(last_run_time, "%Y-%m-%dT%H:%M:%S.%f").date()
if last_updated_time == datetime.utcnow().date():
if last_updated_time == datetime.now(UTC.utc).date():
first_run = False
self.__environment_variables_dict.update({'CRO_FIRST_RUN': first_run})
if first_run:
Expand Down
Loading
Loading