import datetime import logging from ui_automation_framework.utils import logger as cl class DataFormatter: log = cl.Logger(logging.DEBUG) @classmethod def round_streams(cls, num): magnitude = 0 if 1 <= num <= 9: if num * 1000 % 50 == 0: value = str(round(num + 0.01, 1)).replace(".0", "") else: value = round(num, 1) elif 9 < num <= 99: if num * 1000 % 50 == 0: value = str(round(num + 0.01)) else: value = round(num) elif 99 < num < 949: value = str(round(int(num), -2) / 1000) elif 949 < num < 1001: value = "1k" else: while abs(num) >= 1000: cls.log.info(num) magnitude += 1 if num % 50 == 0: num = num + 10 num = round(num, -2) cls.log.info(num) num /= 1000.0 value = "%.1f%s" % (num, ["", "k", "M", "G", "T", "P"][magnitude]) if value[-3:] == ".0k": value = value.replace(".0k", "k") if value[-3:] == ".0M": value = value.replace(".0M", "M") if value[-2:] == ".0": value = value.replace(".0", "") return value @classmethod def round_listeners(cls, num): # TODO: simplify magnitude = 0 value = "" if 1 <= num <= 9: if num * 1000 % 50 == 0: value = str(round(num + 0.01, 1)) else: value = str(round(num, 1)) elif 10 <= num <= 99: if num * 1000 % 50 == 0: value = str(round(num + 0.01)) else: value = str(round(num)) elif 99 < num < 949: value = "0.{}k".format(round(int(num), -2)).replace("00", "") elif 949 < num < 1001: value = "1k" else: while abs(num) >= 1000: cls.log.info(num) magnitude += 1 if num % 50 == 0: num = num + 10 num = round(num, -2) cls.log.info(num) num /= 1000.0 value = "%.1f%s" % (num, ["", "k", "M", "G", "T", "P"][magnitude]) if value[-3:] == ".0k": value = value.replace(".0k", "k") if value[-3:] == ".0M": value = value.replace(".0M", "M") if value[-2:] == ".0": value = value.replace(".0", "") return value @classmethod def round_streams_trend(cls, num): magnitude = 0 value = "" if 1 <= abs(num) <= 999: if num * 100 % 50 == 0: value = str(round(abs(num + 0.1))) else: value = str(round(abs(num))) else: while abs(num) >= 999: magnitude += 1 num = round(num, -2) num /= 1000.0 value = "%.1f%s" % (num, ["", "k", "M", "G", "T", "P"][magnitude]) if value[-3:] == ".0k": value = value.replace(".0k", "k") if value[-3:] == ".0M": value = value.replace(".0M", "M") if value[-2:] == ".0": value = value.replace(".0", "") return value @classmethod def round_streams_demographics(cls, num): magnitude = 0 value = "" if 99 < num < 950: value = "0.{}k".format(round(num, -2)).replace("00", "") elif 950 < num < 1000: value = "1K" else: while abs(num) >= 1000: cls.log.info(num) magnitude += 1 if num % 50 == 0: num = num + 10 num = round(num, -2) cls.log.info(num) num /= 1000.0 value = "%.1f%s" % (num, ["", "k", "M", "G", "T", "P"][magnitude]) if value[-3:] == ".0k": value = value.replace(".0k", "k") if value[-3:] == ".0M": value = value.replace(".0M", "M") if value[-2:] == ".0": value = value.replace(".0", "") return value @staticmethod def format_api_to_ui_dates(date): return str(datetime.datetime.strptime(date, "%Y-%m-%d").strftime("%m/%d/%y")) @staticmethod def calculate_streams_trend(streams_current, streams_previous): return round(((streams_current - streams_previous) / streams_previous) * 100) @staticmethod def convert_days_to_years(number_of_days): if number_of_days >= 365: number_of_years = int(number_of_days / 365) if number_of_years == 1: date = "1 Year Ago" else: date = f"{number_of_years} Years Ago" elif number_of_days == 0: date = "Today" elif number_of_days == 1: date = "1 Day Ago" else: date = f"{number_of_days} Days Ago" return date