<% if not geo %>
    <% set geo = "[sas,man,iva,myt]" %>
<% endif %>

<% if not panels and not uuid %>
  <% set panels = "[requests,prc,codes,errors,cache,yastat,alerts]" %>
<% elif not panels %>
  <% set panels = "[requests,prc,codes,errors]" %>
<% endif %>

<% if not uuid %>
  <%set uuid = "service_total" %>
<% endif %>

<% macro generate_diff_metric_ends(name) %>
    <% if uuid == "service_total" %>
    "div(max(diff(diff(diff(diff(balancer_report-report-service_total-<< name >>,balancer_report-report-requests_to_awacs-balancer-health-check-<< name >>),balancer_report-report-requests_to_slbping-<< name >>),  balancer_report-report-requests_to_upstream_ok-<< name >>), balancer_report-report-everybodybecoolthisis-<< name >>), 0),normal())"
    <% else %>
    "balancer_report-report-<< uuid >>-<< name >>"
    <% endif %>
<% endmacro %>

<% macro generate_diff_metric_codes(name) %>
    <% if uuid == "service_total" %>
    "div(max(diff(diff(diff(diff(balancer_report-report-service_total-outgoing_<< name >>_summ,balancer_report-report-requests_to_awacs-balancer-health-check-outgoing_<< name >>_summ),balancer_report-report-requests_to_slbping-outgoing_<< name >>_summ), balancer_report-report-requests_to_upstream_ok-outgoing_<< name >>_summ), balancer_report-report-everybodybecoolthisis-outgoing_<< name >>_summ), 0),normal())"
    <% else %>
    "balancer_report-report-<< uuid >>-outgoing_<< name >>_summ"
    <% endif %>
<% endmacro %>

{
    "abc": "appbalancer_yastatic",
    "title": "yastatic.net",
    "description": "L7 balancer yastatic.net",
    "type": "panel",
    "charts": [
<% set col_shift = 1 %>
<% set row_shift = 1 %>
<% set width = 2 %>
<% set height = 1 %>
<% set geo = geo|from_json %>
<% set panels = panels|from_json %>
<% for location in geo %>
    <% if location == "iva" or location == "myt" %>
        <% set current_geo = "msk;tier=" + location %>
    <% else %>
        <% set current_geo = location %>
    <% endif %>
    <% if not loop.first %>
    ,
    <% endif %>
    <% for panel in panels %>
        <% if not loop.first %>
        ,
        <% endif %>
        <% if panel == "requests" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastatic.net service_total requests",
                "stacked": true,
                "normalize": true,
                "signals": [
                    {
                        "title": "success",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("succ_summ") >>
                    },
                    {
                        "title": "fail",
                        "color": "#169833",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("fail_summ") >>
                    },
                    {
                        "title": "inprogress",
                        "color": "#f6ab31",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("inprog_ammv") >>
                    },
                    {
                        "title": "keepalive",
                        "color": "#c95edd",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("ka_summ") >>
                    },
                    {
                        "title": "reused-keepalive",
                        "color": "#e85b4e",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("reused_summm") >>
                    },
                    {
                        "title": "non-keepalive",
                        "color": "#409fd4",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("nka_summ") >>
                    },
                    {
                        "title": "connection timeout",
                        "color": "#7bbf00",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("conn_timeout_summ") >>
                    },
                    {
                        "title": "backend timeout",
                        "color": "#ff2727",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("backend_timeout_summ") >>
                    },
                    {
                        "title": "connection refused",
                        "color": "#80f320",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_ends("conn_refused_summ") >>
                    }
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "prc" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastatic.net service_total time prc",
                "stacked": true,
                "normalize": true,
                "maxValue": 5,
                "signals": [
                    {
                        "title": "75",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "quant(balancer_report-report-<< uuid >>-processing_time_hgram, 75)"
                    },
                    {
                        "title": "90",
                        "color": "#169833",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "quant(balancer_report-report-<< uuid >>-processing_time_hgram, 90)"
                    },
                    {
                        "title": "95",
                        "color": "#f6ab31",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "quant(balancer_report-report-<< uuid >>-processing_time_hgram, 95)"
                    },
                    {
                        "title": "99",
                        "color": "#c95edd",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "quant(balancer_report-report-<< uuid >>-processing_time_hgram, 99)"
                    }
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "codes" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastatic.net service_total codes answers",
                "stacked": true,
                "normalize": true,
                "signals": [
                    {
                        "title": "1xx",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("1xx") >>
                    },
                    {
                        "title": "2xx",
                        "color": "#169833",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("2xx") >>
                    },
                    {
                        "title": "3xx",
                        "color": "#f6ab31",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("3xx") >>
                    },
                    {
                        "title": "4xx",
                        "color": "#c95edd",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("4xx") >>
                    },
                    {
                        "title": "404",
                        "color": "#e85b4e",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("404") >>
                    },
                    {
                        "title": "5xx",
                        "color": "#409fd4",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": << generate_diff_metric_codes("5xx") >>
                    }
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "errors" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastatic.net client errors/s3 retry/zero length",
                "stacked": true,
                "normalize": true,
                "signals": [
                    {
                        "title": "client errors",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-<< uuid >>-client_error_summ"
                    },
                    <% if uuid == "service_total" %>
                    {
                        "title": "s3 cache attempts",
                        "color": "#f6ab31",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-total_request_with_macache_miss-backend_attempt_summ"
                    },
                    {
                        "title": "s3 quota limit",
                        "color": "#c95edd",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-s3_cached_quota_limit-requests_summ"
                    }
                    <% endif %>
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "cache" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastatic.net cache hit/miss",
                "normalize": true,
                "stacked": true,
                "signals": [
                    {
                        "title": "hit",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "perc(max(diff(balancer_report-report-total_request_with_macache-requests_summ, balancer_report-report-total_request_with_macache_miss-requests_summ), 0), balancer_report-report-s3_cached_report-requests_summ)"
                    },
                    {
                        "title": "miss",
                        "color": "#169833",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "perc(max(balancer_report-report-total_request_with_macache_miss-requests_summ, 0), balancer_report-report-s3_cached_report-requests_summ)"
                    }
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "yastat" %>
            {
                "type": "graphic",
                "width": << width >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "title": "<< location >> yastat requests/errors",
                "normalize": true,
                "stacked": true,
                "signals": [

                    {
                        "title": "requests",
                        "color": "#ff2727",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-requests_summ"
                    },
                    {
                        "title": "1xx",
                        "color": "#37bff2",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_1xx_summ"
                    },
                    {
                        "title": "2xx",
                        "color": "#169833",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_2xx_summ"
                    },
                    {
                        "title": "3xx",
                        "color": "#f6ab31",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_3xx_summ"
                    },
                    {
                        "title": "4xx",
                        "color": "#c95edd",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_4xx_summ"
                    },
                    {
                        "title": "404",
                        "color": "#e85b4e",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_404_summ"
                    },
                    {
                        "title": "5xx",
                        "color": "#409fd4",
                        "tag": "itype=balancer;ctype=prod;prj=l7-balancer-yastatic;geo=<< current_geo >>",
                        "host": "ASEARCH",
                        "name": "balancer_report-report-yastat_net-outgoing_5xx_summ"
                    }
                ],
                "consts": []
            }
            <% set row_shift = row_shift + height  %>
        <% elif panel == "alerts" %>
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift >>,
                "name": "balancer.yastatic.alerts.all.yastatic_2xx_trend",
                "title": "<< location >> yastatic 2xx trend"
            },
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift >>,
                "col": << col_shift + (width / 2) >>,
                "name": "balancer.yastatic.alerts.<< location >>.yastatic_errors_404",
                "title": "<< location >> yastatic error 404"
            },
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift + height >>,
                "col": << col_shift >>,
                "name": "balancer.yastatic.alerts.<< location >>.yastatic_errors_5xx",
                "title": "<< location >> yastatic error 5xx"
            },
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift + height >>,
                "col": << col_shift + (width / 2) >>,
                "name": "balancer.yastatic.alerts.<< location >>.yastatic_cache_miss",
                "title": "<< location >> yastatic cache miss"
            },
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift + height * 2 >>,
                "col": << col_shift >>,
                "name": "balancer.yastatic.alerts.<< location >>.yastatic_p99_time",
                "title": "<< location >> yastatic p99 time"
            },
            {
                "type": "alert",
                "width": << width / 2 >>,
                "height": << height >>,
                "row": << row_shift + height * 2 >>,
                "col": << col_shift + (width / 2) >>,
                "name": "balancer.yastatic.alerts.<< location >>.yastatic_s3_retry",
                "title": "<< location >> yastatic s3 retry"
            }

            <% set row_shift = row_shift + 2 * height  %>
        <% endif %>
    <% endfor %>
    <% set col_shift = col_shift + width %>
<% endfor %>
    ],
}
