<% set itype = 'drlogger_nel' %>



<# SUGGESTS #>

<< suggest.clear() >>

<< suggest.add_var('prj') >>
<< suggest.set_choice_list('prj', list_prj(itype=itype)) >>

<< suggest.add_var('ctype') >>
<< suggest.set_choice_list('ctype', list_ctype(itype=itype)) >>

<< suggest.add_var('geo') >>
<< suggest.set_choice_list('geo', list_geo(itype=itype)) >>

<< suggest.add_var('neldrlog_host') >>
<< suggest.set_choice_list('neldrlog_host', list_signals(
    itype=itype,
    pattern='unistat-https?_(?P<neldrlog_host>[-_.A-Za-z0-9]*)_ok_summ'
).distinct('neldrlog_host')) >>

<< suggest.add_var('protocol') >>
<< suggest.set_choice_list('protocol', ['http', 'https']) >>

<% for boolean_var in [
        'show_header', 
        'show_errors', 
        'detailed_errors', 
        'show_geos', 
        'show_asn', 
        'detailed_asn',
        'show_container',
] %>
    << suggest.add_var(boolean_var) >>
    << suggest.set_choice_list(boolean_var, ['true', 'false']) >>
<% endfor %>



<# PARAMETERS #>

<% set ctype = ctype | default(','.join(list_ctype(itype=itype, prj='drlognel'))) %>
<% set prj   = prj   | default(','.join(list_prj  (itype=itype, ctype=ctype))) %>
<% set geo   = geo   | default(','.join(list_geo  (itype=itype, prj=prj, ctype=ctype))) %>

<% set geos = None %>
<% if ',' in geo %>
    <% set geos = geo.split(',') %>
<% endif %>

<% set neldrlog_host = neldrlog_host | default(list_signals(
    itype=itype,
    ctype=ctype,
    prj=prj,
    pattern='unistat-https?_(?P<neldrlog_host>[_A-Za-z0-9]*)_ok_summ'
).distinct('neldrlog_host')) %>

<% set neldrlog_host_str = ','.join(neldrlog_host) %>
<% set neldrlog_host = '<' + '|'.join(neldrlog_host) + '>' %>

<% set protocol = protocol | default(['http', 'https']) %>
<% set protocol_str = ','.join(protocol) %>
<% set protocol = '<' + '|'.join(protocol) + '>' %>

<% if show_header is defined and (show_header == 'False' or show_header == 'false') %>
    <% set show_header = False %>
<% else %>
    <% set show_header = True %>
<% endif %>

<% if show_errors is defined and (show_errors == 'False' or show_errors == 'false') %>
    <% set show_errors = False %>
<% else %>
    <% set show_errors = True %>
<% endif %>

<% if detailed_errors is defined and (detailed_errors == 'True' or detailed_errors == 'true') %>
    <% set detailed_errors = True %>
<% else %>
    <% set detailed_errors = False %>
<% endif %>

<% if show_geos is defined and (show_geos == 'False' or show_geos == 'false') %>
    <% set show_geos = False %>
<% else %>
    <% set show_geos = True %>
<% endif %>

<% if show_asn is defined and (show_asn == 'False' or show_asn == 'false') %>
    <% set show_asn = False %>
<% else %>
    <% set show_asn = True %>
<% endif %>

<% if detailed_asn is defined and (detailed_asn == 'True' or detailed_asn == 'true') %>
    <% set detailed_asn = True %>
<% else %>
    <% set detailed_asn = False %>
<% endif %>

<% if show_asn is defined and (show_container == 'True' or show_container == 'true') %>
    <% set show_container = True %>
<% else %>
    <% set show_container = False %>
<% endif %>



<# HEADER INFO #>

<% set header_kv = [
    ('ctype',           ctype),
    ('prj',             prj),
    ('geo',             geo),
    ('neldrlog_host',   neldrlog_host_str),
    ('protocol',        protocol_str),
    ('show_header',     show_header),
    ('show_errors',     show_errors),
    ('detailed_errors', detailed_errors),
    ('show_geos',       show_geos),
    ('show_asn',        show_asn),
    ('detailed_asn',    detailed_asn),
    ('show_container',  show_container),
] %>



<# ERROR TYPES #>

<% set green     = '#379634' %>
<% set red       = '#E03616' %>
<% set orange    = '#FB8B24' %>
<% set yellow    = '#FFCF00' %>
<% set brown     = '#B26E63' %>
<% set blue      = '#5DA9E9' %>
<% set purple    = '#6D326D' %>

<% set neldrlog_error_types = [
    ('ok',         green),
    ('http_error', red),
    ('tcp_error',  orange),
    ('dns_error',  yellow),
    ('tls_error',  brown),
    ('abandoned',  blue),
    ('unknown',    purple),
] %>

<% if detailed_errors %>
    <% set neldrlog_error_type_groups = [
        ('http',                                            ['http_error', 'http_failed', 'http_protocol_error', 'http_response_invalid', 'http_response_redirect_loop']),
        ('tcp',                                             ['tcp_error', 'tcp_aborted', 'tcp_address_invalid', 'tcp_address_unreachable', 'tcp_closed', 'tcp_failed', 'tcp_refused', 'tcp_reset', 'tcp_timed_out']),
        ('tcp_error & tcp_reset',                           ['tcp_error', 'tcp_reset']),
        ('tcp_timed_out & tcp_closed',                      ['tcp_timed_out', 'tcp_closed']),
        ('tcp_aborted, tcp_refused & tcp_addr_unreachable', ['tcp_aborted', 'tcp_refused', 'tcp_address_unreachable']),
        ('tcp_addr_invalid & tcp_failed',                   ['tcp_address_invalid', 'tcp_failed']),
        ('dns',                                             ['dns_error', 'dns_address_changed', 'dns_failed', 'dns_name_not_resolved', 'dns_unreachable']),
        ('dns_failed & dns_unreachable',                    ['dns_failed', 'dns_unreachable']),
        ('tls',                                             ['tls_error', 'tls_bad_client_auth_cert', 'tls_cert_authority_invalid', 'tls_cert_date_invalid', 'tls_cert_invalid', 'tls_cert_name_invalid', 'tls_cert_pinned_key_not_in_cert_chain', 'tls_cert_protocol_error', 'tls_cert_revoked', 'tls_failed', 'tls_protocol_error', 'tls_version_or_cipher_mismatch']),
        ('tls_cert_auth, _date, _name & tls_protocol',      ['tls_cert_authority_invalid', 'tls_cert_date_invalid', 'tls_cert_name_invalid', 'tls_protocol_error']),
        ('tls_version_or_cipher_mismatch',                  ['tls_version_or_cipher_mismatch']),
        ('rare tls',                                        ['tls_bad_client_auth_cert', 'tls_cert_invalid', 'tls_cert_pinned_key_not_in_cert_chain', 'tls_cert_protocol_error', 'tls_cert_revoked', 'tls_failed']),
    ] %>
<% else %>
    <% set neldrlog_error_type_groups = [] %>
    <% for group_name in ['http', 'tcp', 'dns', 'tls'] %>
        <% set error_types = list_signals(
            itype=itype,
            pattern='unistat-https?_.*_(?P<neldrlog_error_type>' + group_name + '_.*)_summ',
        ).distinct('neldrlog_error_type') %>
        <% do error_types.insert(0, error_types.pop(error_types.index(group_name + '_error'))) %>
        <% do neldrlog_error_type_groups.append((group_name, error_types)) %>
    <% endfor %>
<% endif %>



<# ASNS #>

<% set asns = list_signals(
    itype=itype,
    prj=prj,
    ctype=ctype,
    geo=geo,
    pattern='unistat-(?P<asn>AS[a-zA-Z0-9_.]+)_[a-zA-Z0-9_.]+_ok_summ',
).distinct('asn') %>



<# PORTO METRICS #>
<# signal / title / color / yAxis #>

<% set cpu_signals = [
    ('portoinst-cpu_guarantee_slot_cores_tmmv', 'guarantee cores', green,  0),
    ('portoinst-cpu_limit_slot_cores_tmmv',     'limit cores',     blue,   0),
    ('portoinst-cpu_usage_cores_tmmv',          'usage cores',     purple, 0),
    ('perc(portoinst-cpu_usage_cores_tmmv, portoinst-cpu_guarantee_slot_cores_tmmv)', 'usage %', orange, 1),
    ('const(100)',                              '100%',            green,  1),
    ('const(0)',                                '0%',              green,  1),
] %>

<% set cpu_wait_signals = [
    ('portoinst-cpu_wait_cores_tmmv',           'CPU wait',  red,    0),
] %>

<% set memory_signals = [
    ('portoinst-memory_guarantee_slot_gb_tmmv', 'guarantee GB',  green,  0),
    ('portoinst-memory_limit_slot_gb_tmmv',     'limit GB',      blue,   0),
    ('portoinst-memory_usage_gb_tmmv',          'usage GB',      purple, 0),
    ('perc(portoinst-memory_usage_gb_tmmv, portoinst-memory_guarantee_slot_gb_tmmv)', 'usage %', orange, 1),
    ('const(100)',                              '100%',          green,  1),
    ('const(0)',                                '0%',            green,  1),
] %>

<% set anon_memory_signals = [
    ('portoinst-anon_usage_gb_tmmv',                     'anon usage GB',          red,    0),
    ('quant(portoinst-anon_limit_usage_perc_hgram, 99)', 'anon usage %, quant 99', orange, 1),
    ('quant(portoinst-anon_limit_usage_perc_hgram, 95)', 'anon usage %, quant 95', purple, 1),
    ('quant(portoinst-anon_limit_usage_perc_hgram, 90)', 'anon usage %, quant 90', blue,   1),
    ('const(100)',                                       '100%',                   green,  1),
    ('const(0)',                                         '0%',                     green,  1),
] %>

<% set page_fault_signals = [
    ('portoinst-major_page_faults_summ',        'major',     red,    1),
    ('portoinst-minor_page_faults_summ',        'minor',     yellow, 0),
] %>

<% set network_signals = [
    ('portoinst-net_guarantee_mb_summ',         'guarantee MB',   green,  0),
    ('portoinst-net_rx_mb_summ',                'receive MB',     purple, 0),
    ('portoinst-net_tx_mb_summ',                'transmit MB',    blue,   0),
    ('portoinst-net_rx_drops_summ',             'reveive drops',  red,    1),
    ('portoinst-net_tx_drops_summ',             'transmit drops', yellow, 1),
] %>

<% set exit_signals = [
    ('instancectl-exit_error_mmmm',             'exit error',     red,    0),
    ('instancectl-exit_ok_mmmm',                'exit ok',        green,  0),
    ('instancectl-exit_signal_11_mmmm',         'exit signal 11', brown,  0),
    ('instancectl-exit_signal_9_mmmm',          'exit signal 9',  blue,   0),
    ('instancectl-exit_signal_6_mmmm',          'exit signal 6',  purple, 0),
    ('instancectl-exit_signal_mmmm',            'exit signal',    yellow, 0),
    ('instancectl-exit_total_mmmm',             'exit total',     orange, 0),
] %>

<% set all_container_signals = [
    (cpu_signals,           'CPU'),
    (cpu_wait_signals,      'CPU wait'),
    (memory_signals,        'memory'),
    (anon_memory_signals,   'anon memory'),
    (page_fault_signals,    'page faults'),
    (network_signals,       'network'),
    (exit_signals,          'exits'),
] %>



<# CHARTS MACROS #>

<% macro error_types_charts(layout, geo) %>
    {
        << layout.coords() >>,
        "type": "graphic",
        <% if ',' in geo %>
            "title": "errors",
        <% else %>
            "title": "errors in << geo >>",
        <% endif %>
        "signals": [
            <% for neldrlog_error_type, color in neldrlog_error_types %>
            {
                "title": "<< neldrlog_error_type >>",
                "color": "<< color >>",
                "tag":   "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
                "host":  "ASEARCH",
                "name":  "sum(unistat-<< protocol >>_<< neldrlog_host >>_<< neldrlog_error_type >>_summ)"
            },
            <% endfor %>
        ]
    },
<% endmacro %>

<% macro extended_error_types_charts(layout, geo) %>
    <% for group_name, group_error_types in neldrlog_error_type_groups %>
    {
        << layout.coords() >>,
        "type": "graphic",
        <% if ',' in geo %>
            "title": "<< group_name >> errors",
        <% else %>
            "title": "<< group_name >> errors in << geo >>",
        <% endif %>
        "signals": [
            <% for error_type in group_error_types %>
            {
                "title": "<< error_type >>",
                "tag":   "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
                "host":  "ASEARCH",
                "name":  "sum(unistat-<< protocol >>_<< neldrlog_host >>_<< error_type >>_summ)"
            },
            <% endfor %>
        ]
    },
    <% endfor %>
<% endmacro %>

<% macro container_charts(layout, geo, column_width) %>
    {
        << layout.coords(width=column_width, height=0.4) >>,
        "type": "text",
        <% if ',' in geo %>
            "text": "# YP instances",
        <% else %>
            "text": "# YP instances in << geo >>",
        <% endif %>
        "signal": {
            "tag": "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
            "host": "ASEARCH",
            "name": "counter-instance_tmmv",
            "title": "YP instances"
        }
    },

    <% for signals, title in all_container_signals %>
    {
        << layout.coords() >>,
        "type": "graphic",
        <% if ',' in geo %>
            "title": "<< title >>",
        <% else %>
            "title": "<< title >> in << geo >>",
        <% endif %>
        "signals": [
            <% for name, title, color, y_axis in signals %>
            {
                "title": "<< title >>",
                "color": "<< color >>",
                "tag":   "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
                "host":  "ASEARCH",
                "name":  "<< name >>",
                "yAxis": << y_axis >>,
                <% if 'guarantee' in title or 'limit' in title %>
                    "skipInScale": true,
                <% endif %>
            },
            <% endfor %>
        ],
    },
    <% endfor %>
<% endmacro%>

<% macro by_asn_per_error_type_charts(layout) %>
    <% for neldrlog_error_type, _ in neldrlog_error_types %>
    {
        <% if neldrlog_error_type == 'ok' %>
            << layout.coords(width=6) >>,
        <% else %>
            << layout.coords() >>,
        <% endif %>
        "type": "graphic",
        "title": "<< neldrlog_error_type >> by asn",
        "signals": [
            <% for asn in asns %>
            {
                "title": "<< asn >>",
                "tag": "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
                "host": "ASEARCH",
                "name": "sum(unistat-<< asn >>_<< neldrlog_host >>_<< neldrlog_error_type >>_summ)"
            },
            <% endfor %>
        ]
    },
    <% endfor %>
<% endmacro %>

<% macro by_error_type_per_asn_charts(layout) %>
    <% for asn in asns %>
    
        {
            << layout.coords() >>,
            "type": "graphic",
            "title": "errors in << asn >>",
            "signals": [
                <% for neldrlog_error_type, color in neldrlog_error_types %>
                {
                    "title": "<< neldrlog_error_type >>",
                    "color": "<< color >>",
                    "tag": "itype=<< itype >>;ctype=<< ctype >>;prj=<< prj >>;geo=<< geo >>",
                    "host": "ASEARCH",
                    "name": "sum(unistat-<< asn >>_<< neldrlog_host >>_<< neldrlog_error_type >>_summ)"
                },
                <% endfor %>
            ]
        },

    <% endfor %>
<% endmacro %>



<# SECTION MACROS#>

<% macro header_section(main_layout) %>
    <% if show_header %>
        <% set layout = main_layout.sub('flow', columns=6, default_width=2, default_height=0.25) %>
        <% for key, value in header_kv %>
        {
            << layout.coords() >>,
            "type": "text",
            "text": "<< key >> = << value >>"
        },
        <% endfor %>
        << main_layout.skip(height=0.25) >>
    <% endif %>
<% endmacro %>

<% macro errors_section(main_layout) %>
    <% if show_errors %>
        << error_types_charts(main_layout, geo) >>
        <% set layout = main_layout.sub('flow', columns=6, default_width=3, default_height=1) %>
        << extended_error_types_charts(layout, geo) >>
    <% endif %>
<% endmacro %>

<% macro container_section(main_layout) %>
    <% if show_container %>
        <% set layout = main_layout.sub('flow', columns=6, default_width=3, default_height=1) %>
        {
            << layout.coords(width=6, height=0.25) >>,
            "type": "text",
            "text": "container",
        },
        << container_charts(layout, geo, 6) >>
    <% endif %>
<% endmacro %>

<% macro geos_section(main_layout) %>
    <% if geos and show_geos %>
        <% set geos_layout = main_layout.sub('horizontal') %>
        <% set column_width = 6 / (geos|length) %>
        
        <% for local_geo in geos %>
            <% set geo_layout = geos_layout.sub('vertical', default_width=column_width, default_height=1.5) %>
            
            <# GEO TITLE #>
            <% if show_errors %>
                {
                    << geo_layout.coords(width=column_width, height=0.25) >>,
                    "type": "text",
                    "text": "<< local_geo >>"
                },
            <% endif %>

            <# GEO ERRORS #>
            <% if show_errors %>
                << error_types_charts(geo_layout, local_geo) >>
                <% set layout = geo_layout.sub('flow', columns=column_width, default_width=column_width/2, default_height=0.75) %>
                << extended_error_types_charts(layout, local_geo) >>
            <% endif %>

            <# CONTAINER #>
            <% if show_container %>
                <% set layout = geo_layout.sub('flow', columns=column_width, default_width=column_width/2, default_height=0.75) %>
                {
                    << layout.coords(width=column_width, height=0.25) >>,
                    "type": "text",
                    "text": "<< local_geo >> container",
                },
                << container_charts(layout, local_geo, column_width) >>
            <% endif %>
        
        <% endfor %>
    <% endif %>
<% endmacro %>

<% macro asn_section(main_layout) %>
    <% if show_asn %>
        
        <# BY ASN PER ERROR TYPE #>
        {
            << main_layout.coords(height=0.25) >>,
            "type": "text",
            "text": "by ASN per error type",
        },
        <% set layout = main_layout.sub('flow', columns=6, default_width=3, default_height=2) %>
        << by_asn_per_error_type_charts(layout) >>

        <# BY ERROR TYPE PER ASN #>
        <% if detailed_asn %>
            {
                << main_layout.coords(height=0.25) >>,
                "type": "text",
                "text": "by error type per ASN",
            },
            <% set layout = main_layout.sub('flow', columns=6, default_width=2, default_height=1.5) %>
            << by_error_type_per_asn_charts(layout) >>
        <% endif %>

    <% endif %>
<% endmacro %>



<# PANEL #>

{
    "abc": "drlog",
    "user": "soniachrn",
    "title": "neldrlog",
    "type": "panel",
    
    "charts": [
        <% set main_layout = create_main_layout('vertical', default_height=2, default_width=6) %>        
        
        << header_section   (main_layout) >>
        << errors_section   (main_layout) >>
        << container_section(main_layout) >>
        << geos_section     (main_layout) >>
        << asn_section      (main_layout) >>
    ]
} 

