"""
* Copyright 2019 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""

import logging
import re
import os
import json
from urllib.parse import urlparse
import datetime

logger = logging.getLogger("metricsGatherer.utils")


def remove_credentials_from_url(url):
    parsed_url = urlparse(url)
    new_netloc = re.sub("^.+?:.+?@", "", parsed_url.netloc)
    return url.replace(parsed_url.netloc, new_netloc)


def get_credentials_from_url(url):
    parsed_url = urlparse(url)
    new_netloc = re.search("^(.+?):(.+?)@", parsed_url.netloc)
    try:
        username = new_netloc.group(1).strip()
        password = new_netloc.group(2).strip()
        return username, password
    except: # noqa
        return "", ""


def read_json_file(folder, filename, to_json=False):
    """Read fixture from file"""
    with open(os.path.join(folder, filename), "r") as file:
        return file.read() if not to_json else json.loads(file.read())


def is_the_time_for_task_starting(allowed_start_time, allowed_end_time):
    start = datetime.time(int(allowed_start_time.split(":")[0]), int(allowed_start_time.split(":")[1]))
    end = datetime.time(int(allowed_end_time.split(":")[0]), int(allowed_end_time.split(":")[1]))
    now_time = datetime.datetime.now().time()
    if start > end:
        return (now_time >= start and now_time <= datetime.time(23, 59)) or\
            (now_time >= datetime.time(0, 0) and now_time <= end)
    return now_time >= start and now_time <= end


def take_the_date_to_check():
    now_time = datetime.datetime.now().time()
    if (now_time >= datetime.time(12, 0) and now_time <= datetime.time(23, 59)):
        return datetime.datetime.now()
    return datetime.datetime.now() - datetime.timedelta(days=1)


def build_url(main_url, url_params):
    """Build url by concating url and url_params"""
    return main_url + "/" + "/".join(url_params)


def unite_project_name(project_id, prefix):
    return prefix + project_id


def parse_conditions(conditions):
    parsed_conditions = []
    for condition in conditions.split("|"):
        if not condition.strip():
            continue
        chosen_operator = ""
        for operator in [">=", "<=", "==", "=", "<", ">"]:
            if operator in condition:
                chosen_operator = operator
                break
        condition_changed = condition.replace(chosen_operator, " ").split()
        if len(condition_changed) == 2:
            metric_score = None
            try:
                metric_score = int(condition_changed[1].strip())
            except: # noqa
                try:
                    metric_score = float(condition_changed[1].strip())
                except: # noqa
                    pass
            if metric_score is not None:
                parsed_conditions.append(
                    (condition_changed[0].strip(), chosen_operator, metric_score))
    return parsed_conditions


def compare_metrics(cur_metric, metric_threshold, operator):
    if operator == ">=":
        return cur_metric >= metric_threshold
    if operator == ">":
        return cur_metric > metric_threshold
    if operator == "<=":
        return cur_metric <= metric_threshold
    if operator == "<":
        return cur_metric < metric_threshold
    if operator in ["==", "="]:
        return cur_metric == metric_threshold
    return False


def convert_metrics_to_string(cur_metrics):
    return ";".join(["%s:%s" % (metric[0], metric[1]) for metric in cur_metrics])

[((24, 9, 24, 51), 'logging.getLogger', 'logging.getLogger', ({(24, 27, 24, 50): '"""metricsGatherer.utils"""'}, {}), "('metricsGatherer.utils')", False, 'import logging\n'), ((28, 17, 28, 30), 'urllib.parse.urlparse', 'urlparse', ({(28, 26, 28, 29): 'url'}, {}), '(url)', False, 'from urllib.parse import urlparse\n'), ((29, 17, 29, 59), 're.sub', 're.sub', ({(29, 24, 29, 35): '"""^.+?:.+?@"""', (29, 37, 29, 39): '""""""', (29, 41, 29, 58): 'parsed_url.netloc'}, {}), "('^.+?:.+?@', '', parsed_url.netloc)", False, 'import re\n'), ((34, 17, 34, 30), 'urllib.parse.urlparse', 'urlparse', ({(34, 26, 34, 29): 'url'}, {}), '(url)', False, 'from urllib.parse import urlparse\n'), ((35, 17, 35, 62), 're.search', 're.search', ({(35, 27, 35, 42): '"""^(.+?):(.+?)@"""', (35, 44, 35, 61): 'parsed_url.netloc'}, {}), "('^(.+?):(.+?)@', parsed_url.netloc)", False, 'import re\n'), ((63, 15, 63, 38), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((64, 11, 64, 34), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((64, 37, 64, 63), 'datetime.timedelta', 'datetime.timedelta', (), '', False, 'import datetime\n'), ((46, 14, 46, 44), 'os.path.join', 'os.path.join', ({(46, 27, 46, 33): 'folder', (46, 35, 46, 43): 'filename'}, {}), '(folder, filename)', False, 'import os\n'), ((53, 15, 53, 38), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((61, 15, 61, 38), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((62, 20, 62, 40), 'datetime.time', 'datetime.time', ({(62, 34, 62, 36): '(12)', (62, 38, 62, 39): '(0)'}, {}), '(12, 0)', False, 'import datetime\n'), ((62, 57, 62, 78), 'datetime.time', 'datetime.time', ({(62, 71, 62, 73): '(23)', (62, 75, 62, 77): '(59)'}, {}), '(23, 59)', False, 'import datetime\n'), ((55, 50, 55, 71), 'datetime.time', 'datetime.time', ({(55, 64, 55, 66): '(23)', (55, 68, 55, 70): '(59)'}, {}), '(23, 59)', False, 'import datetime\n'), ((56, 25, 56, 44), 'datetime.time', 'datetime.time', ({(56, 39, 56, 40): '(0)', (56, 42, 56, 43): '(0)'}, {}), '(0, 0)', False, 'import datetime\n')]