Skip to content

Instantly share code, notes, and snippets.

@emlun007
Last active February 14, 2018 08:04
Show Gist options
  • Save emlun007/0aa273863ea5bc8ceba6f2d97fd58726 to your computer and use it in GitHub Desktop.
Save emlun007/0aa273863ea5bc8ceba6f2d97fd58726 to your computer and use it in GitHub Desktop.
Structures facts for dataTables
{% macro status_counts(caller, status, node_name, events, current_env, unreported_time=False, report_hash=False) -%}
<a class="ui {{status}} label status" href="{{url_for('report', env=current_env, node_name=node_name, report_id=report_hash)}}">{{ status|upper }}</a>
{% if status == 'unreported' %}
<span class="ui label status"> {{ unreported_time|upper }} </span>
{% else %}
{% if events['failures'] %}<span class="ui small count label failed">{{events['failures']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
{% if events['successes'] %}<span class="ui small count label changed">{{events['successes']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
{% if events['skips'] %}<span class="ui small count label skipped">{{events['skips']}}</span>{% else %}<span class="ui small count label">0</span>{% endif%}
{% endif %}
{%- endmacro %}
{% macro report_status(caller, status, node_name, metrics, current_env, unreported_time=False, report_hash=False) -%}
<a class="ui {{status}} label status" href="{{url_for('report', env=current_env, node_name=node_name, report_id=report_hash)}}">{{ status|upper }}</a>
{% if status == 'unreported' %}
<span class="ui label status"> {{ unreported_time|upper }} </span>
{% 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 %}
<span title="{{ title }}" class="ui small count label {{ title }}">{{ format_str|format(value) }}</span>
{% else %}
<span title="{{ title }}" class="ui small count label">0</span>
{% endif%}
{% endfor %}
{% endif %}
{%- endmacro %}
{% macro datatable_init(table_html_id, ajax_url, default_length, length_selector, extra_options=None) -%}
// Init datatable
$.fn.dataTable.ext.errMode = 'throw';
var table = $('#{{ table_html_id }}').DataTable({
// 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,
// Data loading URL
"ajax": "{{ ajax_url }}",
// Paging options
"lengthMenu": {{ length_selector }},
"pageLength": {{ default_length }},
// Search as regex (does not apply if serverSide)
"search": {"regex": true},
// Default sort
"order": [[ 0, "desc" ]],
// Rendering - add rendering options for columns
"columnDefs": [ {
"targets": -1,
"data:": null,
"render": function (data, type, full, meta) {
shorta = data.replace(/[{},]/g, "<br />");
shortb = shorta.replace(/u'/g, " ");
shortc = shortb.replace(/'/g, " ");
return shortc;
}}],
// Custom options
{% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
});
table.on('error', function ( e, settings, json ) {
table.clear().draw();
$('#facts_table_processing').hide(); })
table.on('draw.dt', function(){
$('#{{ table_html_id }} [rel=utctimestamp]').each(
function(index, timestamp){
$(this).localise_timestamp();
});
});
// Custom options
{% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
});
table.on('error', function ( e, settings, json ) {
table.clear().draw();
$('#facts_table_processing').hide(); })
table.on('draw.dt', function(){
$('#{{ table_html_id }} [rel=utctimestamp]').each(
function(index, timestamp){
$(this).localise_timestamp();
});
});
// 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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment