Skip to content

Commit

Permalink
Issue #1628: pipe shall allow to export the node utilization report…
Browse files Browse the repository at this point in the history
… - fix for dates
  • Loading branch information
ekazachkova authored and mzueva committed Dec 10, 2020
1 parent ce45f19 commit cf91f22
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
18 changes: 11 additions & 7 deletions pipe-cli/src/utilities/cluster_monitoring_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from src.api.pipeline_run import PipelineRun


OUTPUT_FILE_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'
OUTPUT_FILE_DATE_FORMAT = '%Y-%m-%d'


class ClusterMonitoringManager:
Expand All @@ -48,12 +48,16 @@ def generate_report(cls, instance_id, run_id, output, raw_from_date, raw_to_date
sys.exit(1)
if not interval:
interval = '1m'
date_to = date_utilities.now() if not raw_to_date else date_utilities.format_date(raw_to_date)
date_to = date_utilities.now_utc() if not raw_to_date \
else date_utilities.parse_parameter_date_to_utc(raw_to_date)
date_from = date_utilities.minus_day(date_to) if not raw_from_date \
else date_utilities.format_date(raw_from_date)
else date_utilities.parse_parameter_date_to_utc(raw_from_date)
output_path = cls._build_output_path(output, run_id, instance_id, date_from, date_to, interval)
Cluster.download_usage_report(instance_id, date_from, date_to,
cls._convert_to_duration_format(interval), output_path)
Cluster.download_usage_report(instance_id,
date_utilities.format_date(date_from),
date_utilities.format_date(date_to),
cls._convert_to_duration_format(interval),
output_path)
click.echo("Usage report downloaded to '%s'" % output_path)

@staticmethod
Expand All @@ -80,8 +84,8 @@ def _build_output_file_name(run_id, instance_id, date_from, date_to, interval):
instance_indicator = str(run_id or instance_id)
return "cluster_monitor_%s_%s_%s_%s.csv" % (
instance_indicator,
date_utilities.format_date(date_from, OUTPUT_FILE_DATE_FORMAT),
date_utilities.format_date(date_to, OUTPUT_FILE_DATE_FORMAT),
date_utilities.format_date(date_utilities.to_local(date_from), OUTPUT_FILE_DATE_FORMAT),
date_utilities.format_date(date_utilities.to_local(date_to), OUTPUT_FILE_DATE_FORMAT),
interval)

@staticmethod
Expand Down
40 changes: 29 additions & 11 deletions pipe-cli/src/utilities/date_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ def server_date_representation(date_string):

def parse_date_parameter(date_string):
# accepts string with date in local timezone and converts it to another string of UTC date
if not date_string:
return None
date = _parse_date(date_string)
return _to_string_with_timezone(date)
utc_date = to_uts(date)
return _to_string(utc_date)


def to_local(date):
return date.astimezone(Config.instance().timezone())


def to_uts(date):
if not date:
return None
date_with_time_zone = Config.instance().timezone().localize(date, is_dst=None)
return date_with_time_zone.astimezone(pytz.utc)


def format_date(date_string, date_format=DATE_TIME_PARAMETER_FORMAT):
def parse_parameter_date_to_utc(date_string):
date = _parse_date(date_string)
return _to_string_with_timezone(date, date_format)
return to_uts(date)


def _parse_date(date_string):
Expand All @@ -70,15 +84,19 @@ def _parse_date(date_string):
return date


def _to_string_with_timezone(date, date_format=DATE_TIME_SERVER_FORMAT):
date_with_time_zone = Config.instance().timezone().localize(date, is_dst=None)
return date_with_time_zone.astimezone(pytz.utc).strftime(date_format)
def _to_string(date, date_format=DATE_TIME_SERVER_FORMAT):
if not date:
return None
return date.strftime(date_format)


def now_utc():
return to_uts(datetime.datetime.now())


def now():
return _to_string_with_timezone(datetime.datetime.now(), DATE_TIME_PARAMETER_FORMAT)
def minus_day(date):
return date - datetime.timedelta(days=1)


def minus_day(date_string):
date = datetime.datetime.strptime(date_string, DATE_TIME_PARAMETER_FORMAT)
return _to_string_with_timezone(date - datetime.timedelta(days=1), DATE_TIME_PARAMETER_FORMAT)
def format_date(date, date_format=DATE_TIME_PARAMETER_FORMAT):
return _to_string(date, date_format)

0 comments on commit cf91f22

Please sign in to comment.