<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="{% static 'favicon.ico' %}" type="image/x-icon">
    <title>Info {{ service }} {{ ctype }}</title>
    <link href="{% static 'css/navigation.css' %}" rel="stylesheet" />
    <script src="https://yandex.st/jquery/2.0.3/jquery.min.js" type="text/javascript"></script>
    <script type='text/javascript' src="{% static 'js/data_load.js' %}"></script>
</head>
<style>

    .btn-main-link {
        background-color: brown;
    }
    .btn-main-link:after {
        border-left-color: brown;
    }
    .page-title {
        color: #625f44;
        cursor: pointer;
    }
    .nav-service-link {
        color: #625f44;
    }
    .nav-service-div {
        border-right: 2px solid #f9f7d4;
    }
    .sla-input {
        border: 0;
        border-bottom: 2px solid #f9f7d4;
    }
    .mscomps th {
        border-bottom: 2px solid #f9f7d4;
        padding: 3px;
        text-align: center;
    }

</style>
<script type="text/javascript">
    const ctype = '{{ ctype }}';
    const service = '{{ service }}';
    const is_root = {% if is_root %}true{% else %}false{% endif %};
    const is_rty = {% if is_rty %}true{% else %}false{% endif %};

    const sla_fields = ['ticket', 'abc_user_service', 'abc_quota_service', 'maxdocs', 'service_weight',
        'search_rps', 'search_rps_planned', 'search_q_99_ms', 'search_q_999_ms',
        'index_rps', 'total_index_size_bytes', 'unanswers_5min_perc_crit', 'unanswers_5min_perc_warn',
        'disaster_alerts'
    ];

    function switch_element_hidden(id) {
        try {
            let el = document.getElementById(id);
            if (el.hasAttribute('hidden')) {
                el.removeAttribute('hidden');
            } else {
                el.setAttribute('hidden', 1);
            }
        } catch(err)  {console.log(err); }
    }

    function do_sla_link(el_id, href) {
        try {
            let el = document.getElementById(el_id);
            el.href = href;
            if (el.hasAttribute('hidden')) {
                el.removeAttribute('hidden');
            }
        } catch(err) { console.log(err); }
    }

    function do_fill_sla_info(res) {
        if (!is_root) {
            let inps = document.getElementsByClassName('sla-input');
            for (let i=0; i<inps.length; ++i) {
                inps[i].setAttribute('readonly', 'true');
            }
        }
        if (!res['success']) {return;}
        let ans = res['ans'];
        document.getElementById('sla_owners').value = (res['ans'].hasOwnProperty('owners') && res['ans']['owners'])
            ? res['ans']['owners'].join(', ') : '';
        document.getElementById('sla_responsibles').value = (res['ans'].hasOwnProperty('responsibles') && res['ans']['responsibles'])
            ? res['ans']['responsibles'].join(', ') : '';
        try {
            document.getElementById('sla_ferrymans').value = res['ans']['ferrymans'].join(', ');
        } catch(err) { }
        for (let i=0; i<sla_fields.length; ++i) {
            if (ans.hasOwnProperty(sla_fields[i])) {
                document.getElementById('sla_' + sla_fields[i]).value = ans[sla_fields[i]];
            }
        }
        try {
            if (ans.hasOwnProperty('ticket') && ans['ticket'] !== '') {
                do_sla_link('link_sla_ticket', 'https://st.yandex-team.ru/' + ans['ticket']);
            }
            if (ans.hasOwnProperty('abc_user_service') && !!ans['abc_user_service']) {
                do_sla_link('link_sla_abc_user', 'https://abc.yandex-team.ru/services/' + ans['abc_user_service']);
            }
            if (ans.hasOwnProperty('abc_quota_service') && !!ans['abc_quota_service']) {
                do_sla_link('link_sla_abc_quota', 'https://abc.yandex-team.ru/services/' + ans['abc_quota_service']);
            }
            if (ans.hasOwnProperty('ferrymans') && ans['ferrymans'].length > 0) {
                do_sla_link('link_sla_ferryman', 'https://nanny.yandex-team.ru/ui/#/services/catalog/' + ans['ferrymans'][0]);
                let sol_cluster = (ctype === 'stable') ? 'saas_stable' : (ctype === 'stable_middle_kv' ? 'saas_middle_kv' : ctype);
                do_sla_link('link_sla_ferryman_mon', 'https://solomon.yandex-team.ru/?project=saas_ferryman&cluster=' + sol_cluster + '&service=' + service + '&l.sensor=ProcessTimeYT_*&graph=auto&b=31d');
            }
        } catch(err) { console.log(err); }
    }

    function fill_sla_info() {
        let url = '{% url "sla_get" %}?ctype=' + ctype + '&service=' + service;
        let load_cb = {
            url: url, do_once_any_case: true,
            answer_id: 'sla_answer', on_load: do_fill_sla_info};
        do_data_load(load_cb);
    }

    function send_sla_info() {
        let data = {};
        function convert_logins(logins) {
            let log_arr = logins.split(',');
            let res = [];
            for (let i=0; i<log_arr.length; ++i) {
                if (log_arr[i].trim() !== '') {
                    res.push(log_arr[i].trim());
                }
            }
            return res;
        }
        data['owners'] = convert_logins(document.getElementById('sla_owners').value);
        data['responsibles'] = convert_logins(document.getElementById('sla_responsibles').value);
        data['ferrymans'] = convert_logins(document.getElementById('sla_ferrymans').value);
        for (let i=0; i<sla_fields.length; ++i) {
            data[sla_fields[i]] = document.getElementById('sla_' + sla_fields[i]).value.trim();
        }
        let url = '{%  url "sla_set" %}?service=' + service + '&ctype=' + ctype;
        let load_cb = {
            url: url, do_once_any_case: true,
            post_data: JSON.stringify(data),
            answer_id: 'sla_answer', on_load: function(){}};
        do_data_load(load_cb);
    }

    function do_fill_secret_keys(res) {
        let ans = res['ans'];
        let p_el = document.getElementById('skeys_info');

        let table_el = document.createElement('table');
        for (let ct in ans) {
            if (ans.hasOwnProperty(ct) && ans[ct].hasOwnProperty(service)) {
                let rh_el = document.createElement('tr');
                let d_el = document.createElement('td');
                d_el.setAttribute('colspan', '2');
                d_el.innerHTML = ct;
                rh_el.appendChild(d_el);
                table_el.appendChild(rh_el);
                for (let adapter in ans[ct][service]) {
                    if (ans[ct][service].hasOwnProperty(adapter)) {
                        let r_el = document.createElement('tr');

                        let td_ad_el = document.createElement('td');
                        let td_ha_el = document.createElement('td');
                        td_ad_el.innerHTML = adapter;
                        td_ha_el.innerHTML = ans[ct][service][adapter];

                        r_el.appendChild(td_ad_el);
                        r_el.appendChild(td_ha_el);
                        table_el.appendChild(r_el);
                    }
                }
            }
        }
        p_el.innerHTML = '';
        p_el.appendChild(table_el);
    }

    function fill_secret_keys() {
        let url = '{% url "secret_keys" %}?service=' + service;
        let load_cb = {
            url: url, do_once_any_case: true,
            answer_id: 'skeys_answer', on_load: do_fill_secret_keys};
        do_data_load(load_cb);
    }

    function do_fill_cluster_settings(res) {
        if (!res['success']) { return; }
        if (document.getElementById('smap_settings_info').hasAttribute('hidden')) {
            document.getElementById('smap_settings_info').removeAttribute('hidden');
        }
        let ans = res['ans'];
        let si = ans['cluster'][service];
        console.log(si);
        let misc_props = ['shards', 'dispatch_target', 'distributors', 'stream', 'require_auth', 'locations_weights'];
        let extra_props = {};
        for (let prop in si) {
            if (si.hasOwnProperty(prop) && misc_props.indexOf(prop) !== -1)
                extra_props[prop] = si[prop];
        }
        if (extra_props.length > 0) {
            document.getElementById('smap_dbg').innerHTML = '!!Unused props found!! ' + JSON.stringify(extra_props);
        }
        let per_dc_search = si.hasOwnProperty('per_dc_search') && si['per_dc_search'];
        let el = document.getElementById('per_dc_search');
        if (per_dc_search) {
            el.setAttribute('checked', '1');
        } else if (el.hasAttribute('checked')) {
            el.removeAttribute('checked');
        }
        let shard_by = si.hasOwnProperty('shard_by') && si['shard_by'];
        if (shard_by) {
            document.getElementById('shard_by').shard_by_option.value = shard_by;
        }
    }
    function do_fill_cluster_settings_metaservice(res) {
        if (!res['success']) { return; }
        if (document.getElementById('smap_settings_info_metaservice').hasAttribute('hidden')) {
            document.getElementById('smap_settings_info_metaservice').removeAttribute('hidden');
        }
        let ans = res['ans'];
        let si = ans['cluster'][service];
        console.log(si);
        let el = document.getElementById('metaservice_components');

        if (!si.hasOwnProperty('Components')) {
            document.getElementById('smap_dbg_meta').innerHTML = 'No components in answer';
            return;
        }
        for (let i=0; i<si["Components"].length; ++i) {
            let compEl = el.insertRow(-1);
            compEl.align = 'center';
            compEl.insertCell(-1).innerHTML = si["Components"][i]['ServiceName'];
            compEl.insertCell(-1).innerHTML = si["Components"][i]['SearchDisabled'];
            compEl.insertCell(-1).innerHTML = si["Components"][i]['IndexDisabled'];
            compEl.insertCell(-1).innerHTML = si["Components"][i]['SearchPriority'];
        }

      //  document.getElementById('smap_dbg_meta').innerHTML = 'not supported for metaservices yet';
    }

    function fill_cluster_settings(is_meta) {
        let url = '{% url "service_settings" %}?service=' + service + '&ctype=' + ctype
            + '&service_type=' + (is_meta ? 'metaservice' : 'rtyserver');
        let load_cb = {
            url: url, do_once_any_case: true,
            answer_id: 'smap_settings_answer',
            on_load: is_meta ? do_fill_cluster_settings_metaservice : do_fill_cluster_settings};
        do_data_load(load_cb);
    }
    function send_cluster_settings() {
        let shard_by = document.getElementById('shard_by').shard_by_option.value;
        let per_dc_search = document.getElementById('per_dc_search').checked;
        let service_info = JSON.stringify({shard_by: shard_by, per_dc_search: per_dc_search});
        let url = '{%  url "edit_service_settings" %}?service=' + service + '&ctype=' + ctype
            + '&service_type=rtyserver&service_info=' + service_info;
        let load_cb = {
            url: url, do_once_any_case: true,
            answer_id: 'smap_settings_answer', on_load: function(){}};
        do_data_load(load_cb);
    }

    $(document).ready(function() {
        if (is_rty) {
            fill_sla_info();
            fill_secret_keys();
            window.addEventListener('ismetaservice', function(evt){ fill_cluster_settings(evt.detail.is_meta);});
        }
    });
</script>
{% include "navigation.html" %}
{% include "nav_ctype.html" %}
<body>
<div style="border-bottom: 1px solid #e3f2d3">
    <button class="btn-main-link"><a href="{% url 'main' %}" class="main-link">SAAS</a></button>
    <span class="dropdown"><span class="page-title" data-toggle="dropdown">Service conf<span class="caret"></span></span>
        <ul id="nav-screens-menu" class="dropdown-menu">
            <li><a href="{% url 'deploy_page' %}?service={{ service }}&ctype={{ ctype }}">Cluster & deploy</a></li>
            <li><a href="{% url 'cluster_total' %}?service={{ service }}&ctype={{ ctype }}">Cluster hardware</a></li>
            <li><a href="{% url 'service_info' %}?service={{ service }}&ctype={{ ctype }}">Service conf</a></li>
            <li><a href="{% url 'monitor_hosts' %}?service={{ service }}&ctype={{ ctype }}">Monitor hosts</a></li>
            <li><a href="{% url 'monitor_gui' %}?service={{ service }}&ctype={{ ctype }}">Monitor</a></li>
        </ul></span>
    <span id="ctype_selector" style="font-weight:bold;color:#7d7b6b">{{ ctype }}</span><span class="caret"></span>
    <span class="nav-service-link" style="margin-left:10px;font-weight:bold;color:green">{{ service }}</span>
</div>
<table>
    <tr>
        <td id="nav-services-list" style="vertical-align: top"></td>
        <td style="vertical-align: top">
    <div>
    <a onclick="switch_element_hidden('service_configs')" style="cursor:pointer;color:#625f5c;text-decoration: underline dotted">Configs</a>
    <div id="service_configs">
    {% include "storage.html" %}
    </div>
    </div>
    <div style="margin-top:7px">
        {% if is_rty %}
            <a href="{% url 'polynom_converter' %}?service={{ service }}" style="color:#625f5c" target="_blank">
                Polynom<i class="fa fa-external-link"></i></a><br>
    <a onclick="switch_element_hidden('sla_div')" style="cursor:pointer;color:#625f5c;text-decoration: underline dotted">SLA<span class="caret"></span></a>
    <div id="sla_div" hidden>
        <div id="sla_info">
        <table>
            <tr>
                <td colspan="2">
                Owners: <input type="text" class="sla-input" id="sla_owners" style="width:80%"><br>
                Responsibles: <input type="text" class="sla-input" id="sla_responsibles" style="width:80%"><br>
                User ABC Id: <input type="text" class="sla-input" id="sla_abc_user_service" style="width:20%"><a id="link_sla_abc_user" target="_blank" hidden><i class="fa fa-external-link"></i></a>
                Quota ABC Id: <input type="text" class="sla-input" id="sla_abc_quota_service" style="width:20%"><a id="link_sla_abc_quota" target="_blank" hidden><i class="fa fa-external-link"></i></a><br>
                Ferryman: <input type="text" class="sla-input" id="sla_ferrymans" style="margin-top: 10px; width:80%">
                    <a id="link_sla_ferryman" target="_blank" hidden><i class="fa fa-external-link"></i></a>
                    <a id="link_sla_ferryman_mon" target="_blank" hidden><i class="fa fa-area-chart"></i></a><br>
                </td>
            </tr>
            <tr>
            <td style="position:relative">
                Ticket: <input type="text" class="sla-input" id="sla_ticket"><a id="link_sla_ticket" target="_blank" hidden><i class="fa fa-external-link"></i></a><br>
                Soft MaxDocs: <input type="text" class="sla-input" id="sla_maxdocs"><br>
                Weight: <input type="text" class="sla-input" id="sla_service_weight"><br>
                Disaster alerts: <input type="text" class="sla-input" id="sla_disaster_alerts"><br>
            <div style="position:absolute;bottom:2px">
                <button onclick="fill_sla_info()">Reload</button>
                {% if is_root %}
                    <button onclick="send_sla_info()">Send</button>
                {% endif %}
            </div>
            </td>
            <td>
                Search RPS: <input type="text" class="sla-input" id="sla_search_rps"><br>
                Search RPS Planned: <input type="text" class="sla-input" id="sla_search_rps_planned"><br>
                Search Q99 ms: <input type="text" class="sla-input" id="sla_search_q_99_ms"><br>
                Search Q999 ms: <input type="text" class="sla-input" id="sla_search_q_999_ms"><br>
                Index RPS: <input type="text" class="sla-input" id="sla_index_rps"><br>
                Index size bytes: <input type="text" class="sla-input" id="sla_total_index_size_bytes"><br>
                Unanswer percent warn (5min): <input type="text" class="sla-input" id="sla_unanswers_5min_perc_warn"><br>
                Unanswer percent crit (5min): <input type="text" class="sla-input" id="sla_unanswers_5min_perc_crit">
            </td>
        </tr></table>
        </div>
        <div id="sla_answer"></div>
    </div>
            <br>
    <a onclick="switch_element_hidden('smap_settings_div')" style="cursor:pointer;color:#625f5c;text-decoration: underline dotted">Searchmap settings<span class="caret"></span></a>
    <div id="smap_settings_div" hidden>
        <div id="smap_settings_info" hidden>
            <label for="per_dc_search">per_dc_search</label><input type="checkbox" id="per_dc_search">
            <form id="shard_by">
                Shard by:
            <label><input value="keyprefix" type="radio" name="shard_by_option">keyprefix</label>
            <label><input value="url_hash" type="radio" name="shard_by_option">url_hash</label>
            <label><input value="time" type="radio" name="shard_by_option">time</label>
            <label><input value="external" type="radio" name="shard_by_option">external</label>
            <label><input value="geo" type="radio" name="shard_by_option">geo</label>
            <label><input value="geo_restrict" type="radio" name="shard_by_option">geo_restrict</label>
            <label><input value="url_hash_erasure" type="radio" name="shard_by_option">url_hash_erasure</label>
            <label><input value="querysearch" type="radio" name="shard_by_option">querysearch</label>
            <label><input value="url_to_last_octothorp" type="radio" name="shard_by_option">url_to_last_octothorp</label>
            <label><input value="url_to_last_underscore" type="radio" name="shard_by_option">url_to_last_underscore</label>
            </form>
            {% if is_root %}
                    <button onclick="send_cluster_settings()">Send</button>
            {% endif %}
            <span id="smap_dbg" style="color:red"></span>
        </div>
        <div id="smap_settings_info_metaservice" hidden>
            <table id="metaservice_components" class="mscomps">
                <tr><th colspan='4'>Components</th></tr>
                <tr><th>Service</th><th>SearchDisabled</th><th>IndexDisabled</th><th>Priority</th></tr>
            </table>
            <span id="smap_dbg_meta" style="color:red"></span>
        </div>
        <div id="smap_settings_answer"></div>
    </div>
            <br>
    <a onclick="switch_element_hidden('skeys_div')" style="cursor:pointer;color:#625f5c;text-decoration: underline dotted">Secret keys<span class="caret"></span></a>
    <div id="skeys_div" hidden>
        <div id="skeys_info">
        <table>
            <tr><td>keys will be here</td></tr>
        </table>
        </div>
        <div id="skeys_answer"></div>
    </div>
        {% endif %}
    </div>
    <div id="raw_data" hidden></div>
        </td>
    </tr>
</table>
</body>
</html>
