<commit_before>import collections
import datetime
import re
import time

import requests
import prettytable

from common import ColoredString

JSON_API_URL = "/api/json"

JenkinsJobs = collections.namedtuple("JenkinsJobs", ["url", "job_names"])


def get_job_info(jenkins_url, job_name):
    job_summary = get_job_summary(jenkins_url, job_name)
    job_info = get_last_job_info(job_summary)
    return (
        job_summary["name"],
        "BUILDING" if job_info["result"] is None and job_info["building"] else job_info["result"],
        get_job_eta(job_info)
    )


def get_job_summary(jenkins_url, job_name):
    return requests.get(jenkins_url + job_name + JSON_API_URL).json()


def get_last_job_info(job_summary):
    return requests.get(job_summary["builds"][0]["url"] + JSON_API_URL).json()


def get_job_eta(job):
    start_time, duration = job["timestamp"], job["estimatedDuration"]
    eta = get_formatted_eta(start_time, duration)
    percent = get_formatted_percent(start_time, duration)
    if job["building"]:
        return "{:>8}, ({}%)".format(eta, percent)
    else:
        return "N/A"


def get_formatted_eta(start_time, duration):
    now = datetime.datetime.now()
    eta = datetime.datetime.fromtimestamp((start_time + duration) / 1000) - now
    return "N/A" if eta.days < 0 else format_time_delta(eta)


def format_time_delta(time_delta):
    hours, remainder = divmod(time_delta.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return "{:02}:{:02}:{:02}".format(hours, minutes, seconds)


def get_formatted_percent(start_time, duration):
    percent = (int(time.time()) - start_time / 1000) * 100 / (duration / 1000)
    return 100 if percent > 100 else percent


class AskJenkinsJobStatus(object):

    def __init__(self, jobs):
        self.jobs = jobs
        self.success_pattern = re.compile("(.*)(SUCCESS)(.*)")
        self.failure_pattern = re.compile("(.*)(FAILURE)(.*)")
        self.unstable_pattern = re.compile("(.*)(UNSTABLE)(.*)")
        self.building_pattern = re.compile("(.*)(BUILDING)(.*)")

    def __call__(self):
        COLUMN_1 = "Jenkins Job Name                     "
        COLUMN_2 = " Status "
        COLUMN_3 = "     E.T.A.     "

        table = prettytable.PrettyTable([COLUMN_1, COLUMN_2, COLUMN_3])
        table.align[COLUMN_1] = "l"

        for job_name in self.jobs.job_names:
            table.add_row(get_job_info(self.jobs.url, job_name))

        text = []
        for line in table.get_string().splitlines():
            hit = self.success_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (0, 255, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.failure_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (255, 0, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.unstable_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (255, 255, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.building_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (0, 0, 255)),
                             ColoredString(hit.group(3))])
                continue
            text.append([ColoredString(line)])
        return text<commit_msg>Minor changes<commit_after>import collections
import datetime
import re
import time

import requests
import prettytable

from common import ColoredString

JSON_API_URL = "/api/json"

JenkinsJobs = collections.namedtuple("JenkinsJobs", ["url", "job_names"])


def get_job_info(jenkins_url, job_name):
    job_summary = get_job_summary(jenkins_url, job_name)
    job_info = get_last_job_info(job_summary)
    return (
        job_summary["name"],
        "BUILDING" if job_info["result"] is None and job_info["building"] else job_info["result"],
        get_job_eta(job_info)
    )


def get_job_summary(jenkins_url, job_name):
    return requests.get(jenkins_url + job_name + JSON_API_URL).json()


def get_last_job_info(job_summary):
    return requests.get(job_summary["builds"][0]["url"] + JSON_API_URL).json()


def get_job_eta(job):
    start_time, duration = job["timestamp"], job["estimatedDuration"]
    eta = get_formatted_eta(start_time, duration)
    percent = get_formatted_percent(start_time, duration)
    if job["building"]:
        return "{:>8}, {:3}%".format(eta, percent)
    else:
        return "N/A"


def get_formatted_eta(start_time, duration):
    now = datetime.datetime.now()
    eta = datetime.datetime.fromtimestamp((start_time + duration) / 1000) - now
    return "N/A" if eta.days < 0 else format_time_delta(eta)


def format_time_delta(time_delta):
    hours, remainder = divmod(time_delta.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return "{:02}:{:02}:{:02}".format(hours, minutes, seconds)


def get_formatted_percent(start_time, duration):
    percent = (int(time.time()) - start_time / 1000) * 100 / (duration / 1000)
    return 100 if percent > 100 else percent


class AskJenkinsJobStatus(object):

    def __init__(self, jobs):
        self.jobs = jobs
        self.success_pattern = re.compile("(.*)(SUCCESS)(.*)")
        self.failure_pattern = re.compile("(.*)(FAILURE)(.*)")
        self.unstable_pattern = re.compile("(.*)(UNSTABLE)(.*)")
        self.building_pattern = re.compile("(.*)(BUILDING)(.*)")

    def __call__(self):
        COLUMN_1 = "Jenkins Job Name                     "
        COLUMN_2 = " Status "
        COLUMN_3 = "    E.T.A.    "

        table = prettytable.PrettyTable([COLUMN_1, COLUMN_2, COLUMN_3])
        table.align[COLUMN_1] = "l"

        for job_name in self.jobs.job_names:
            table.add_row(get_job_info(self.jobs.url, job_name))

        text = []
        for line in table.get_string().splitlines():
            hit = self.success_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (0, 255, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.failure_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (255, 0, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.unstable_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (255, 255, 0)),
                             ColoredString(hit.group(3))])
                continue
            hit = self.building_pattern.match(line)
            if hit:
                text.append([ColoredString(hit.group(1)),
                             ColoredString(hit.group(2), (90, 160, 240)),
                             ColoredString(hit.group(3))])
                continue
            text.append([ColoredString(line)])
        return text
{'fraction_non_alphanumeric': 0.1289498453677558, 'fraction_numerical': 0.02124512572273766, 'mean_word_length': 2.3625678119349005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5310157', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2278, 'n_words': 424}