#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

#<<<jenkins_jobs>>>
#cmk_140 clang_tidy blue 90 1554371146806 200 SUCCESS 1251806 1554371146806
#cmk_140 cppcheck blue 100 1554371034436 174 SUCCESS 165226 1554371034436
#cmk_140 daily_build blue 100 1562927640962 23672 SUCCESS 1163 1562927640962
#cmk_140 duplicate_code blue 100 1562573916992 1340 SUCCESS 17199 1562573916992
#cmk_140 git_tests blue 100 1562572797049 1732 SUCCESS 50485 1562572797049
#cmk_140 gui_crawl blue 100 1562574249416 1127 SUCCESS 881681 1562574249416

import json

JenkinsJobInfo = collections.namedtuple("job_info", [
    "display_name", "name", "state", "job_score", "last_suc_build", "build_id", "build_result",
    "build_duration", "build_timestamp"
])

MAP_JOB_STATES = {
    "aborted": {
        "state": 0,
        "info": "Aborted"
    },
    "blue": {
        "state": 0,
        "info": "Success"
    },
    "disabled": {
        "state": 0,
        "info": "Disabled"
    },
    "notbuilt": {
        "state": 0,
        "info": "Not built"
    },
    "red": {
        "state": 2,
        "info": "Failed"
    },
    "yellow": {
        "state": 1,
        "info": "Unstable"
    },
}

MAP_BUILD_STATES = {
    "success": 0,  # no errors
    "unstable": 1,  # some errors but not fatal
    "failure": 2,  # fatal error
    "aborted": 0,  # manually aborted
    "null": 1,  # module was not built
    "none": 0,  # running
}


def parse_jenkins_jobs(info):
    parsed = {}

    for line in info:
        jenkins_data = json.loads(line[0])

        parsed.update(handle_job_type(jenkins_data, {}, ""))

    return parsed


@get_parsed_item_data
def check_jenkins_jobs(item, params, item_data):
    display_name = item_data[0].display_name
    if display_name is not None:
        yield 0, "Display name: %s" % display_name

    job_state = item_data[0].state
    if job_state is not None:
        if "grey" in job_state:
            job_state = "aborted"

        job_cleanup = job_state.replace("_anime", "")
        infotext = "State: %s" % MAP_JOB_STATES[job_cleanup]["info"]
        state = MAP_JOB_STATES[job_cleanup]["state"]
        if "_anime" in job_state:
            infotext += " (in progress)"

        yield params.get("job_state", {}).get(job_cleanup, state), infotext

    now = time.time()
    time_since_last_build = None
    if item_data[0].build_timestamp:
        time_since_last_build = now - item_data[0].build_timestamp

    time_since_last_suc = None
    if item_data[0].last_suc_build:
        time_since_last_suc = now - item_data[0].last_suc_build

    for key, value, infotext, hr_func in [
        ("jenkins_job_score", item_data[0].job_score, "Job score", get_percent_human_readable),
        ("jenkins_last_build", time_since_last_build, "Time since last build",
         get_age_human_readable),
        ("jenkins_time_since", time_since_last_suc, "Time since last successful build",
         get_age_human_readable),
        (None, item_data[0].build_id, "Build id", int),
        ("jenkins_build_duration", item_data[0].build_duration, "Build duration",
         get_age_human_readable),
    ]:

        param_checking = (None, None)
        if params.get(key):
            warn, crit = params.get(key)
            if key == "jenkins_job_score":
                param_checking = (None, None, warn, crit)
            else:
                param_checking = (warn, crit)

        if value:
            yield check_levels(value,
                               key,
                               param_checking,
                               human_readable_func=hr_func,
                               infoname=infotext)

    build_result_data = item_data[0].build_result
    if build_result_data:
        build_result = build_result_data.lower()
        yield params.get("build_result", {}).get(
            build_result,
            MAP_BUILD_STATES[build_result]), "Build result: %s" % build_result.title()


def handle_single_job(job):
    # key healthReport can have an empty list value
    try:
        health_rp = job["healthReport"][0]['score']
    except (IndexError, KeyError, TypeError):
        health_rp = None

    # key lastSuccessfulBuild can have None value: {'lastSuccessfulBuild':None}
    try:
        last_sb = float(job['lastSuccessfulBuild']['timestamp']) / 1000.0
    except (
            KeyError,
            TypeError,
            ValueError,
    ):
        last_sb = None

    # key lastBuild can have None value: {'lastBuild':None}
    try:
        last_br = job["lastBuild"]['result']
        last_bn = job["lastBuild"]['number']
        last_bd = float(job["lastBuild"]["duration"]) / 1000.0
        last_bt = int(int(job["lastBuild"]['timestamp']) / 1000)
    except (KeyError, TypeError, ValueError):
        last_br = None
        last_bn = None
        last_bd = None
        last_bt = None

    return JenkinsJobInfo(job["displayNameOrNull"], job["name"], job["color"], health_rp, last_sb,
                          last_bn, last_br, last_bd, last_bt)


def handle_job_type(data, new_dict, folder=""):
    for job_type in data:
        item_name = folder
        if job_type.get("_class") and job_type["_class"] in [
                "com.cloudbees.hudson.plugins.folder.Folder",
                "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject"
        ]:
            item_name += "%s/" % job_type["name"]
            if job_type.get("jobs") is not None:
                handle_job_type(job_type["jobs"], new_dict, item_name)
            continue

        if item_name is None:
            item_name = "%s" % job_type["name"]
        else:
            item_name = "%s%s" % (item_name, job_type["name"])

        job = handle_single_job(job_type)

        new_dict.setdefault("%s" % item_name, []).append(job)

    return new_dict


check_info["jenkins_jobs"] = {
    "parse_function": parse_jenkins_jobs,
    "check_function": check_jenkins_jobs,
    "inventory_function": discover(),
    "service_description": "Jenkins Job %s",
    "has_perfdata": True,
    "group": "jenkins_jobs",
}
