{% macro status_counts(caller, status, node_name, events, current_env, unreported_time=False, report_hash=False) -%}
  {{ status|upper }}
  {% if status == 'unreported' %}
     {{ unreported_time|upper }} 
  {% else %}
    {% if events['failures'] %}{{events['failures']}}{% else %}0{% endif%}
    {% if events['successes'] %}{{events['successes']}}{% else %}0{% endif%}
    {% if events['skips'] %}{{events['skips']}}{% else %}0{% endif%}
    {% if events['noops'] %}{{events['noops']}}{% else %}0{% endif%}
  {% endif %}
{%- endmacro %}
{% macro event_status_counts(caller, status, count) -%}
  {% if count == 0 %}
    0
  {% elif status == 'failure' %}
    {{count}}
  {% elif status == 'success' %}
    {{count}}
  {% elif status == 'skipped' %}
    {{count}}
  {% elif status == 'noop' %}
    {{count}}
  {% endif %}
{%- endmacro %}
{% macro report_status(caller, status, node_name, metrics, current_env, unreported_time=False, report_hash=False) -%}
  {{ status|upper }}
  {% if status == 'unreported' %}
     {{ unreported_time|upper }} 
  {% else %}
    {% for metric in config.DISPLAYED_METRICS %}
      {% set path = metric.split('.') %}
      {% set title = ' '.join(path) %}
      {% if metrics[path[0]] and metrics[path[0]][path[1]] %}
      {% set value = metrics[path[0]][path[1]] %}
      {% if value != 0 and value|int != value %}
        {% set format_str = '%.2f' %}
      {% else %}
        {% set format_str = '%s' %}
      {% endif %}
        {{ format_str|format(value) }}
      {% else %}
        0
      {% endif%}
    {% endfor %}
  {% endif %}
{%- endmacro %}
{% macro datatable_init(table_html_id, ajax_url, data, default_length, length_selector, extra_options=None, natural_time_delta_sort=False) -%}
  // Init datatable
  $.fn.dataTable.ext.errMode = 'throw';
  {% if data %}
  var data = {{ data }};
  {% endif %}
  var table = $('#{{ table_html_id }}').DataTable({
    {% if table_html_id == 'query_table' -%}
    "buttons": [
      {
        extend: 'csv',
        filename: 'Puppetboard_query',
        text: 'Download as CSV',
      },
      {
        extend: 'excel',
        filename: 'Puppetboard_query',
        text: 'Download as XLSX',
      },
    ],
    {% endif %}
    {% if data -%}
    // Data provided directly
    "data": data,
    "columns": [
    ],
    {% else %}
    // Data loading URL
    "ajax": "{{ ajax_url }}",
    // Permit flow auto-readjust (responsive)
    "autoWidth": false,
    // Activate "processing" message
    "processing": true,
    // Activate Ajax mode
    "serverSide": true,
    // Responsive
    "responsive": true,
    // Defer rendering out of screen lines (JIT)
    "deferRender": true,
    // Paging options
    "lengthMenu": {{ length_selector }},
    "pageLength": {{ default_length }},
    // Search as regex (does not apply if serverSide)
    "search": {"regex": true},
    {% if table_html_id == 'facts_table' -%}
    "order": [[ 0, "asc" ]],
    {% else -%}
    // Default sort
    "order": [[ 0, "desc" ]],
    {% endif -%}
    // Rendering - add rendering options for columns
    "columnDefs": [
    {% if table_html_id == 'facts_table' -%}
    { "targets": "fact_value",
      "width": "65%",
      "data:": null,
      "render": function (data, type, full, meta) {
        data = JSON.parse(data)
        url = data[0]
        to_show = pretty_print(data[1])
        return `${to_show}`
      },
      {% if natural_time_delta_sort %}
      "type": "natural-time-delta",
      {% endif %}
    },
    {% else -%}
    {
      "targets": -1,
      "data:": null,
      "render": function (data, type, full, meta) {
        shorta = data.toString().replace(/[{},]/g, "
");
        shortb = shorta.replace(/u'/g, " ");
        shortc = shortb.replace(/'/g, " ");
        return shortc;
      }
    },
    {% endif %}
    ],
    {% endif %}
    // Custom options
    {% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
  });
  {% if table_html_id == 'query_table' -%}
  table.buttons().container().appendTo( $('div.dataTables_length', table.table().container()) );
  {% endif %}
  table.on('error', function ( e, settings, json ) {
    table.clear().draw();
    $('#facts_table_processing').hide(); })
  table.on('draw.dt', function(){
    $('#{{ table_html_id }} [data-localise]').transformDatetime();
  });
  // Override Datatables search box events to delay Ajax call while writing
  var searchWait = 0;
  var searchWaitInterval;
  $('.dataTables_filter input')
  .unbind()
  .bind('input', function(e){
    var item = $(this);
    searchWait = 0;
    if(!searchWaitInterval) searchWaitInterval = setInterval(function(){
      if(searchWait>=3){
        clearInterval(searchWaitInterval);
        searchWaitInterval = '';
        searchTerm = $(item).val();
        table.search(searchTerm).draw();
        searchWait = 0;
      }
      searchWait++;
    },80);
  });
{%- endmacro %}
{% macro checkbox_toggle(name, value_current, value_checked, value_unchecked, label) -%}