<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
    <link rel="icon" href="{% static 'favicon.ico' %}" type="image/x-icon">
    <title>{{ service }} alerts</title>
    <link href="https://yandex.st/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
</head>
    <script src="https://yandex.st/jquery/2.0.3/jquery.min.js"></script>
    <script src="https://yandex.st/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<script type='text/javascript' src="{% static 'js/xmlhttp.js' %}"></script>

<style>
    body {
        margin: 8px;
    }
    .a-collapse {
        cursor: pointer;
        text-decoration: underline;
        text-decoration-style: dotted;
    }
    .input-digit {
        width: 50px;
    }
    .smth-off {
        color: #d07200;
    }
    .smth-on {
        color: #39b7e8;
    }
    input {
        border: 0;
        border-bottom: 2px solid #fecb2f;
        padding-left: 6px;
    }
    button {
        background-color: white;
    }
</style>

<script type='text/javascript'>
    let service = '{{ service }}';
    let absent_skeys = {};
    {% for sk, lims in suggest_limits.items %}
        absent_skeys['{{ sk }}'] = {'crit': '{{ lims.crit }}', 'warn': '{{ lims.warn }}', 'avgsec': '{{ lims.avgsec }}'}; {% endfor %}

    function send_to_backend(req_url, reload_after) {
        let xmlhttp = getXmlHttp();
        xmlhttp.open('GET', '{%  url "set_alerts" %}' + req_url, true);
        document.getElementById("answer").innerHTML = 'opening...';
        xmlhttp.onreadystatechange = function() {
            try {
            if (xmlhttp.readyState === 4) {
                //console.log('answered');
                document.getElementById("answer").innerHTML = xmlhttp.status + ' ' + xmlhttp.responseText;
                try {
                    let ansJs = JSON.parse(xmlhttp.responseText);
                    if (ansJs.id) {
                        document.getElementById("answer").innerHTML +=
                            "<br><a href='http://saas-dm.yandex.net/deploy_info?id=" + ansJs.id + "' target='_blank'>DM task info</a>";
                    }
                } catch (err) { console.log(err); }
                if ( reload_after && xmlhttp.status === 200) { window.location.reload(); }
            }
            } catch(err) { console.log(err); }
        };
        xmlhttp.send();
    }

    function send_alert(alert_id, ctype, is_skey, reload_after) {
        try {
        let warn = document.getElementById(ctype + '-' + alert_id + '-warn').value;
        let crit = document.getElementById(ctype + '-' + alert_id + '-crit').value;
        let avgsec = document.getElementById(ctype + '-' + alert_id + '-avgsec').value;
        let cgi_id = is_skey ? "skey=" : "alert_id=";
        let req_url = '?' + cgi_id + alert_id + '&service=' + service
            + '&warn=' + warn + '&crit=' + crit + '&avgsec=' + avgsec;
       // console.log(req_url);

        send_to_backend(req_url, reload_after);
        } catch(err) {console.log(err);}
    }

    function send_flaps(sigkey, ctype, reload_after) {
        try {
            let crit = document.getElementById(ctype + '-' + sigkey + '-flaps-crit').value;
            let stab = document.getElementById(ctype + '-' + sigkey + '-flaps-stable').value;
            let boost = document.getElementById(ctype + '-' + sigkey + '-flaps-boost').value;
            let req_url = '?skey=' + sigkey + '&service=' + service +
                '&critical_time=' + crit + '&stable_time=' + stab + '&boost_time=' + boost;
            send_to_backend(req_url, reload_after);
        } catch(err) {console.log(err);}
    }

    function send_times(sigkey, ctype, reload_after) {
        try {
            let pht = document.getElementById(ctype + '-' + sigkey + '-times').value;
            let req_url = '?skey=' + sigkey + '&service=' + service
                + '&phone_times=' + pht;
            send_to_backend(req_url, reload_after);
        } catch(err) { console.log(err);}
    }

    function send_user_group(sigkey, ctype, reload_after) {
        try {
            let ugr = document.getElementById(ctype + '-' + sigkey + '-usergroup').value;
            let req_url = '?skey=' + sigkey + '&service=' + service
                + '&user_group=' + ugr;
            send_to_backend(req_url, reload_after);
        } catch(err) { console.log(err);}
    }

    function send_disabled_notifs(sigkey, value) {
        if (sigkey === ''){
            alert('not implemented');
            return;
        }
        let cfm = confirm('Are you sure?');
        if (! cfm) {return; }
        let req_url = '?skey=' + sigkey + '&service=' + service
            + '&disable_notify=' + value.toString();
        send_to_backend(req_url, true);
    }

    function select_limits(id, warn, crit, avgsec) {
        try {
            let el = document.getElementById(id + '-warn');
            el.value = warn;
            el = document.getElementById(id + '-crit');
            el.value = crit;
            el = document.getElementById(id + '-avgsec');
            el.value = avgsec;
        } catch(err) { console.log(err); }
    }

    function select_flaps(id, crit, stab, boost) {
        try {
            let el = document.getElementById(id + '-flaps-crit');
            el.value = crit;
            el = document.getElementById(id + '-flaps-stable');
            el.value = stab;
            el = document.getElementById(id + '-flaps-boost');
            el.value = boost;
        } catch(err) { console.log(err); }
    }

    function select_new_limits() {
        let sigkey = document.getElementById('new-alert-sigkey').value;
        if (!absent_skeys.hasOwnProperty(sigkey)) { return; }
        let crit_el = document.getElementById('new-crit');
        let warn_el = document.getElementById('new-warn');
        let avgsec_el = document.getElementById('new-avgsec');
        crit_el.value = absent_skeys[sigkey]['crit'];
        warn_el.value = absent_skeys[sigkey]['warn'];
        avgsec_el.value = absent_skeys[sigkey]['avgsec'];
    }

    function new_alert(service) {
        try {
            let req_url = '?add=1&service=' + service;
            let skey = document.getElementById('new-alert-sigkey').value;
            if (skey === '') { alert('select alert key'); return;}
            let warn = document.getElementById('new-warn').value;
            let crit = document.getElementById('new-crit').value;
            let avgsec = document.getElementById('new-avgsec').value;
            req_url += '&skey=' + skey + '&warn=' + warn + '&crit=' + crit + '&avgsec=' + avgsec;
            let set_all_el = document.getElementById('all-services-set');
            if (set_all_el && set_all_el.checked) {
                req_url += '&add_default=1'
            }
            let is_users = document.getElementById('new-is-users');
            if (is_users && is_users.checked) {
                req_url += '&is_users=1'
            }
            let is_sla_only = document.getElementById('new-is-sla');
            if (is_sla_only && is_sla_only.checked) {
                req_url += '&sla_only=1'
            }
            send_to_backend(req_url, true);
        } catch(err) { console.log(err); }
    }

    function actualize_alerts(service, do_juggler) {
        try {
            let req_url = '?rewrite=1&service=' + service;
            if (do_juggler) { req_url += '&juggler=1'; }
            send_to_backend(req_url, true);
        } catch(err) { console.log(err); }
    }

    function show_sla_info() {
        alert('this is limits from our sla settings, you cannot change warn and crit values here, please contact saas command if you want to change sla');
    }

    function golovan_check_form() {
        console.log('form here');
    }
</script>

<body>
    <div id='answer'></div>
{% for ctype_data in data %}
<div {% if not ctype_data.mustbe %}style="background-color:gainsboro"{% endif %}>
    <h4>{{ service }} {{ ctype_data.ctype }}</h4>
    {% if not ctype_data.mustbe %}<span style="color:red">
        This ctype is not supposed to have alerts<br></span>
    {% endif %}
    {% for sigkey, skey_data in ctype_data.sigkeys.items %}
        <div style="padding:5px">
        <div data-target='#{{ ctype_data.ctype }}-{{ sigkey }}'
               data-toggle="collapse" style="cursor:pointer;">
        <a class="a-collapse">
            <b>{{ sigkey }}</b></a>
        {% if is_default %}<br>Enabled by default:
            {% if skey_data.is_on %}<span class="smth-on">yes</span>{% else %}
                <span class="smth-off">no</span>{% endif %},{% endif %}
            {% if skey_data.limits.conf.from_sla %}<span class="smth-on"> sla</span>{% endif %}
            warn <span style="color:#bfa903">{{ skey_data.limits.conf.warn }}</span>,
            crit <span style="color:#b70000">{{ skey_data.limits.conf.crit }}</span>,
        {% if not is_default %}
        golovan {% if skey_data.golovans|length > 0 %}
            <span class="smth-on">on</span>{% else %}
            <span class="smth-off">off</span>{% endif %},{% endif %}
        juggler {% if skey_data.has_juggler or is_default and skey_data.should_have_juggler %}
            <span class="smth-on">on</span>{% else %}
            <span class="smth-off">off</span>{% endif %}
        {% if skey_data.has_diff and not is_default %}
            <span style="color:red;background-color:lightyellow;padding-left:20px">DIFF</span>{% endif %}
        </div>
        <div class='collapse' id='{{ ctype_data.ctype }}-{{ sigkey }}'>
            {% if not skey_data.mustbe %}<span style="color:red">
                This signal is not supposed to be here<br></span>
            {% endif %}
            Limits:
            {% if skey_data.limits.ro %}
                {% for glim in skey_data.limits.golovan %}
                {{ glim.loc }}: {% for gfield, gval in glim.items %}
                    {{ gfield }}: {{ gval }}{% endfor %}
                    <br>
                {% endfor %}
            {% elif skey_data.limits.same_limits %}
                warn from: <input id='{{ ctype_data.ctype }}-{{ sigkey }}-warn' type='text' value='{{ skey_data.limits.conf.warn }}' class="input-digit" {% if skey_data.limits.conf.from_sla %}readonly{% endif %}>
                crit from: <input id='{{ ctype_data.ctype }}-{{ sigkey }}-crit' type='text' value='{{ skey_data.limits.conf.crit }}' class="input-digit" {% if skey_data.limits.conf.from_sla %}readonly{% endif %}>
                avg sec: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-avgsec" type="text" value="{{ skey_data.limits.conf.avgsec }}" class="input-digit" title="use average value for seconds">
                <button onclick='send_alert("{{ sigkey }}", "{{ ctype_data.ctype }}", true, false)'>Send</button>
                {% if skey_data.limits.conf.from_sla %}
                    <button onclick='show_sla_info()'>SLA</button>
                {% endif %}
            {% else %}
                <div style="background-color:lightyellow;padding:10px">
                alerts limits differ, choose the source:<br>
                <button onclick="select_limits('{{ ctype_data.ctype }}-{{ sigkey }}', '{{ skey_data.limits.conf.warn }}', '{{ skey_data.limits.conf.crit }}', '{{ skey_data.limits.conf.avgsec }}')">
                    config: {{ skey_data.limits.conf.warn }} - {{ skey_data.limits.conf.crit }}, {{ skey_data.limits.conf.avgsec }}s
                </button>
                {% for glim in skey_data.limits.golovan %}
                    <button onclick="select_limits('{{ ctype_data.ctype }}-{{ sigkey }}', '{{ glim.warn }}', '{{ glim.crit }}', '{{ glim.avgsec }}')">
                        yasm-{{ glim.loc }}: {{ glim.warn }} - {{ glim.crit }}, {{ glim.avgsec }}s
                    </button>
                {% endfor %}
                <br><div style="padding-top:10px">
                warn from: <input id='{{ ctype_data.ctype }}-{{ sigkey }}-warn' type='text' value='' class="input-digit">
                crit from: <input id='{{ ctype_data.ctype }}-{{ sigkey }}-crit' type='text' value='' class="input-digit">
                avg sec: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-avgsec" type="text" value="" class="input-digit" title="use average value for seconds">
                <button onclick='send_alert("{{ sigkey }}", "{{ ctype_data.ctype }}", true, true)'>Send</button>
                </div>
                </div>
            {% endif %}
        <br>
        {% if not is_default %}
        Golovan alerts: {% for gal in skey_data.golovans %}
            <a href="https://yasm.yandex-team.ru/alert/{{ gal.name }}" target="_blank">
                        {{ gal.name }}</a><br>
        {% endfor %}
        {% if skey_data.has_juggler_inconsist %}
        <div style="background-color:lightyellow">
        Juggler checks differ from conf:<br>
            conf:{{ skey_data.should_have_juggler }}, jugglers:{{ skey_data.has_juggler_list }}
        </div>
        {% else %}
        Has juggler: {{ skey_data.has_juggler_list }}<br>
        {% endif %}
        {% endif %}
        Flaps:
        {% if skey_data.flaps.same_flaps %}
            critical: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-crit" type="text" class="input-digit" value="{{ skey_data.flaps.conf.critical_time }}">
            stable: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-stable" type="text" class="input-digit" value="{{ skey_data.flaps.conf.stable_time }}">
            boost: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-boost" type="text" class="input-digit" value="{{ skey_data.flaps.conf.boost_time }}">
            <button onclick="send_flaps('{{ sigkey }}', '{{ ctype_data.ctype }}', false)">Send</button>
        {% else %}
            <div style="background-color:lightyellow;padding:10px">
            flaps values differ, choose the source:<br>
            <button onclick="select_flaps('{{ ctype_data.ctype }}-{{ sigkey }}',
                        '{{ skey_data.flaps.conf.critical_time }}',
                        '{{ skey_data.flaps.conf.stable_time }}',
                        '{{ skey_data.flaps.conf.boost_time }}')">
                conf: cr:{{ skey_data.flaps.conf.critical_time }}
                st:{{ skey_data.flaps.conf.stable_time }}
                b:{{ skey_data.flaps.conf.boost_time }}
            </button>
            {% for jf in skey_data.flaps.juggler %}
                <button onclick="select_flaps('{{ ctype_data.ctype }}-{{ sigkey }}',
                        '{{ jf.critical_time }}',
                        '{{ jf.stable_time }}',
                        '{{ jf.boost_time }}')">
                    jug: cr:{{ jf.critical_time }}
                    st:{{ jf.stable_time }}
                    b:{{ jf.boost_time }}
                </button>
            {% endfor %}
            <br><div style="padding-top:10px">
            critical: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-crit" type="text" class="input-digit" value="{{ skey_data.flaps.conf.critical_time }}">
            stable: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-stable" type="text" class="input-digit" value="{{ skey_data.flaps.conf.stable_time }}">
            boost: <input id="{{ ctype_data.ctype }}-{{ sigkey }}-flaps-boost" type="text" class="input-digit" value="{{ skey_data.flaps.conf.boost_time }}">
            <button onclick="send_flaps('{{ sigkey }}', '{{ ctype_data.ctype }}', true)">Send</button>
            </div>
            </div>
        {% endif %}<br>
        Notifications:
            <div style="margin-left:20px">
                Enabled: {% if skey_data.should_have_juggler %}
                <span class="smth-on">Yes</span>
                <button onclick="send_disabled_notifs('{{ sigkey }}', 1)" class="smth-off">
                    Disable</button>
                {% else %}
                <span class="smth-off">No</span>
                <button onclick="send_disabled_notifs('{{ sigkey }}', 0)" class="smth-on">
                    Enable</button>{% endif %}<br>
                Responsible: <select id="{{ ctype_data.ctype }}-{{ sigkey }}-usergroup"
                    style="border:none;border-bottom:2px solid #fecb2f">
                {% if skey_data.users.group != 'dev' and skey_data.users.group != 'user' %}<option>unknown ({% for uc in skey_data.users.conf %} {{ uc }} {% endfor %})</option>
                {% endif %}
                <option value="dev" {% if skey_data.users.group == 'dev' %}selected{% endif %}>dev ({% for uc in skey_data.users.conf %} {{ uc }} {% endfor %})</option>o
                <option value="user" {% if skey_data.users.group == 'user' %}selected{% endif %}>users ({% for uc in skey_data.users.conf %} {{ uc }} {% endfor %})</option>o
                </select>
                <button onclick="send_user_group('{{ sigkey }}', '{{ ctype_data.ctype }}', true)">Send</button>
                {% if not skey_data.users.same and not is_default %}
                    <span style="color:sandybrown">current golem differs: {{ skey_data.users.golem }}</span>{% endif %}<br>
                Phone calls time:
                <input type="text" value="{{ skey_data.calls_time.conf_str }}"
                       title='Format: "hh:mm-hh:mm" or "", "always"'
                       id="{{ ctype_data.ctype }}-{{ sigkey }}-times" style="width:100px">
                <button onclick="send_times('{{ sigkey }}', '{{ ctype_data.ctype }}',
                        {% if skey_data.calls_time.same %}0{% else %}1{% endif %})">
                    Send</button>
                {% if not skey_data.calls_time.same %}
                    <span style="color:sandybrown">current juggler differs: {{ skey_data.calls_time.juggler_str }}</span>{% endif %}
                <br>
                Other notifications: ---
            </div>
        Checks info:
        <a data-target='#{{ ctype_data.ctype }}-{{ sigkey }}-juggler'
               data-toggle="collapse" class="a-collapse">
            Juggler</a>
        <div class='collapse' id='{{ ctype_data.ctype }}-{{ sigkey }}-juggler'>
        {% if  skey_data.signals %}
            {% for jid_data in skey_data.signals %}
                {% if jid_data.juggler %}
                {{ jid_data.juggler.juggler_id }}
                <br>golovan alerts:
                {% for sigdat in jid_data.juggler.golovan_signals %}
                        <a href='http://yasm.yandex-team.ru/alert/{{ sigdat.service }}' target='_blank'>
                            {{ sigdat.service }}</a>
                {% endfor %}

                {% for field, fdat in jid_data.juggler.items %}
                    {% if field != 'juggler_id' and field != 'golovan_signals' %}
                <div><a data-target='#{{ jid_data.juggler.juggler_id }}_{{ field }}' data-toggle="collapse">
                    {{ field }}
                </a></div>
                <div class='collapse' id='{{ jid_data.juggler.juggler_id }}_{{ field }}'>
                    {% if field == 'golovan_signals' %}

                    {% else %}
                        {{ fdat }}
                    {% endif %}
                </div>
                    {% endif %}
                    {% endfor %}
                {% endif %}
                {% for gc in jid_data.golovan %}
                <div>
                    {% for field, val in gc.items %}
                    {{ field }} : {{ val }}<br>
                    {% endfor %}
                </div>
                {% endfor %}
            {% endfor %}
        {% else %}
        No juggler checks
        {% endif %}
        </div>
        </div>
        </div>
    {% endfor %}
</div>
<br>
{% endfor %}
    <div style="padding:5px">
    <a data-target="#add-check" data-toggle="collapse" class="a-collapse">
        + Add check</a>
    <div id="add-check" class="collapse">
    <select id="new-alert-sigkey" onchange="select_new_limits()">
        <option value=""></option>
        {% for sigkey in sigkeys %}
        <option value="{{ sigkey }}">{{ sigkey }}</option>
        {% endfor %}
    </select>
    {% if is_default %}
        <input type="checkbox" id="all-services-set">
        <label for="all-services-set">set for all services</label>
        <input type="checkbox" id="new-is-sla">
        <label for="new-is-sla">sla-only use</label>
    {% endif %}
    <br>Limits:
    warn from: <input type="text" id="new-warn" class="input-digit">
    crit from: <input type="text" id="new-crit" class="input-digit">
    avg sec: <input type="text" id="new-avgsec" class="input-digit">
    <br>
    <input type="checkbox" id="new-is-users">
        <label for="new-is-users">is users check</label>
    <br>
    <button onclick="new_alert('{{ service }}')">Create</button>
    </div>
    </div>
    <br>
<div style="padding:5px"><span style="color:#428bca">Common info</span>
<br>Enable notifications: {% if notify_enable %} <span class="smth-on">Yes</span>
        {% if not is_default %}<button id="notifications-all" class="smth-off" onclick="send_disabled_notifs('common', 1)">
            Disable</button>{% endif %}{% else %} <span class="smth-off">No</span>
        <button id="notifications-all" class="smth-on" onclick="send_disabled_notifs('common', 0)">
            Enable</button>{% endif %}
<br>Dev: {% for resp in resps %}{{ resp }} {% endfor %}
<br>Users: {% for ru in resps_users %}{{ ru }} {% endfor %}
</div>
    {% if service %}
    {% if not actuality.is_actual %}
        <div>
        <button onclick='actualize_alerts("{{ service }}", 1)'
        style="color:#38d6a1;font-weight:bold">
            Actualize all</button></div>
    {% endif %}
        {% if actuality.warning %}cannot get actuality: {{ actuality.warning }}{% endif %}
    {% endif %}

{% if not is_default %}
<a href="{%  url 'service_alerts' %}/?service=defaults" target="_blank">default settings</a>
{% endif %}

{% if user %}
    <div><br><i>We are watching you, {{ user }}</i></div>{% endif %}
</body>
</html>
