fw_log.volt: add filter templates

This commit is contained in:
kulikov-a 2021-04-06 10:42:05 +03:00 committed by GitHub
parent 6c96038ced
commit 7d66c4bf9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
{#
#
# Copyright (c) 2014-2016 Deciso B.V.
# Copyright (c) 2014-2021 Deciso B.V.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -37,7 +37,7 @@
let hostnameMap = {};
/**
* reverse lookup address fields (replace adres part for hostname if found)
* reverse lookup address fields (replace address part for hostname if found)
*/
function reverse_lookup() {
let to_fetch = [];
@ -354,6 +354,95 @@
fetch_log();
});
// templates actions
$("#templates").change(function () {
if ($('#templ_save_start').is(':visible')) {
//apply chosen template
let t_data = $(this).find('option:selected').data('template') ? $(this).find('option:selected').data('template') : {'filters': "0", 'or': "0"};
set_selection(t_data.filters.split(','), t_data.or);
} else {
//choose template to modify or create new one. Show Name input if New option clicked
if ($('#templates').val() === "00001") {
$('#templates').selectpicker('hide');
$('#templ_name').show().focus();
}
}
});
$('#templ_save_start').click(function () {
if ($(".badge").text() == '') {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
title: "{{ lang._('Save filters template') }}",
message: "{{ lang._('Filters not set') }}",
buttons: [{
label: "{{ lang._('Close') }}",
action: function (dialogRef) {
dialogRef.close();
}
}]
});
} else {
$('.templates').hide();
$('.templ_save').show();
$('#templ_name').focus();
if ($("#templates option").length == 3){
//no stored templates. skip to new template name
$('#templates').val("00001").selectpicker('refresh').change();
}
}
});
$("#templ_save_cancel").click(function () {
$('#templ_name').val("").hide();
$('.templ_save').hide();
$('.templates').show();
$('#templates').val('').selectpicker('refresh').selectpicker('show');
});
$("#templ_name").on('keyup', function (e) {
if (e.key === 'Enter' || e.keyCode === 13) {
$('#templ_save_apply').click();
} else if (e.keyCode === 27) {
$('#templ_name').val("").hide();
$('#templates').val('').selectpicker('refresh').selectpicker('show');
}
});
$("#templ_save_apply").click(function () {
let fltrs = "";
$('.badge').each(function () {
fltrs += $(this).text() + ",";
});
fltrs = fltrs.slice(0, -1);
let or = $('#filter_or_type').prop("checked") ? "1" : "0";
let t_data = {
'template': {
'filters': fltrs,
'or': or
}
};
$('#templates').selectpicker('refresh').selectpicker('show');
if ($("#templ_name").val().length >= 1 && $("#templ_name").is(':visible')) {
//new template
t_data.template.name = $("#templ_name").val();
$('#templ_name').val("").hide();
addTemplate(t_data);
} else if ($("#templ_name").val().length == 0 && $("#templ_name").is(':hidden') && $("#templates").val().length == 36) {
//edit template
let t_id = $("#templates").val();
t_data.template.name = $("#templates option:selected").text();
editTemplate(t_id, t_data);
}
});
$("#template_delete").click(function () {
let t_id = $('#templates').val();
if (t_id.length == 36) {
delTemplate(t_id);
}
});
// fetch interface mappings on load
ajaxGet('/api/diagnostics/interface/getInterfaceNames', {}, function(data, status) {
interface_descriptions = data;
@ -389,18 +478,27 @@
filter_value.show();
}
}).change();
fetchTemplates("00000", true);
}
});
});
// startup poller
poller();
/**
* set new selection
* @param items list of lexical expressions
* @param operator enable or disable global OR operator
*/
function set_selection(items)
function set_selection(items, operator)
{
// remove old selection
$("#filters > span.badge").click();
// operator default value
operator = operator || "0";
// collect valid condition types
let conditions = [];
$("#filter_condition > option").each(function(){
@ -413,15 +511,121 @@
$("#filter_condition").val(parts[1]);
$("#filter_value").val(parts[2]);
$("#add_filter_condition").click();
} else if (value.toLowerCase() == "or=1") {
operator = "1";
}
});
$("#filter_or_type").prop('checked', operator === "1" ? true : false);
$(".selectpicker").selectpicker('refresh');
$("#filter_tag").change();
}
/**
* add new filters template
* @param t_data template's parameters
*/
function addTemplate(t_data) {
ajaxCall('/api/diagnostics/lvtemplates/addItem/', t_data, function(data, status) {
if ((status == "success") && (data.result == "saved")) {
fetchTemplates(data.uuid);
} else {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
title: "{{ lang._('Add filters template') }}",
message: "{{ lang._('Template save failed. Message: ') }}" + data.result,
buttons: [{
label: "{{ lang._('Close') }}",
action: function (dialogRef) {
dialogRef.close();
}
}]
});
fetchTemplates("00000");
}
})
}
/**
* set template new values
* @param t_id template uuid
* @param t_data template's parameters
*/
function editTemplate(t_id, t_data) {
ajaxCall('/api/diagnostics/lvtemplates/setItem/' + t_id, t_data, function(data, status) {
if ((status == "success") && (data.result == "saved")) {
fetchTemplates(t_id);
} else {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
title: "{{ lang._('Filters template edit') }}",
message: "{{ lang._('Template edit failed. Message: ') }}" + data.result,
buttons: [{
label: "{{ lang._('Close') }}",
action: function (dialogRef) {
dialogRef.close();
}
}]
});
fetchTemplates(t_id);
}
})
}
/**
* delete filters template
* @param t_id template uuid
*/
function delTemplate(t_id) {
ajaxCall('/api/diagnostics/lvtemplates/delItem/' + t_id, {}, function(data, status) {
if ((status == "success") && (data.result == "deleted")) {
//don't reset current filters so template can be restored right after delete
$("#templates option[value=" + t_id + "]").remove();
$("#templates").val("").selectpicker('refresh');
} else {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
title: "{{ lang._('Filters template delete') }}",
message: "{{ lang._('Template delete failed. Result: ') }}" + data.result,
buttons: [{
label: "{{ lang._('Close') }}",
action: function (dialogRef) {
dialogRef.close();
}
}]
});
}
})
}
/**
* fetch templates from config
* @param opt select value to make :selected and apply
* @param parse_url flag to parse and apply url params on page load
*/
function fetchTemplates(opt, parse_url) {
opt = opt || "00000";
//apply = apply || true;
$('#templ_name').val("");
$('#templates').empty();
$('#templates').append($('<option/>', {value: "00000", text: "None"}).data('template', {'filters': "0", 'or': "0"}).addClass("disp_none_opt templates"));
$('#templates').append($('<option/>', {value: "00001", text: "New"}).data('template', {'filters': "0", 'or': "0"}).data('icon','glyphicon-file').addClass("add_new_opt templ_save"));
$('#templates').selectpicker('refresh');
$('.templates').show();
$('.templ_save').hide();
ajaxGet('/api/diagnostics/lvtemplates/searchItem/', {}, function(data, status) {
let templates = data.rows;
$.each(templates, function(i, template) {
$('#templates').append(template.uuid == opt ? $('<option/>', {value:template.uuid, text:template.name, selected: "selected" }).data('template', template) : $('<option/>', {value:template.uuid, text:template.name }).data('template', template));
});
$('#templates').selectpicker('refresh');
$('.badge').click();
$("#templates").change();
if (parse_url) {
// get and apply url params. ie11 compat
set_selection(window.location.search.substring(1).split("&"));
}
});
}
// get and apply url params. ie11 compat
set_selection(window.location.search.substring(1).split("&"));
// startup poller
poller();
});
</script>
<style>
@ -473,7 +677,7 @@
</select>
</td>
<td style="width:125px;">
<select id="filter_condition" class="condition" data-width="120px">
<select id="filter_condition" class="selectpicker" data-width="120px">
<option value="~" selected=selected>{{ lang._('contains') }}</option>
<option value="=">{{ lang._('is') }}</option>
<option value="!~">{{ lang._('does not contain') }}</option>
@ -505,14 +709,34 @@
<tfoot>
<tr>
<td colspan="4">
<input id="filter_or_type" type="checkbox">
{{ lang._('Select any of given criteria (or)') }}
<label>
<input id="filter_or_type" type="checkbox">
{{ lang._('Select any of given criteria (or)') }}
</label>
</td>
</tr>
</tfoot>
</table>
</div>
<div class="col-lg-6 col-sm-12">
<div class="col-lg-4 col-sm-12">
<div class="pull-right">
<button type="button" class="btn btn-default templates"
title="Save the current set of filters" id="templ_save_start"><span
class="fa fa-angle-double-right"></span></button>
<button type="button" class="btn btn-default templ_save" title="Cancel" id="templ_save_cancel"><span
class="fa fa-times"></span></button>
<div style="display: inline-block;vertical-align: top;"><select id="templates" class="selectpicker" title="Choose template" data-width="200"></select>
<input type="text" id="templ_name" placeholder="Template name" style="width:200px;vertical-align:middle;display:none;">
</div>
<button type="button" class="btn btn-default templ_save" title="Save template" id="templ_save_apply"><span class="fa fa-save"></span></button>
<span class="templates">
<button id="template_delete" type="button" class="btn btn-default" title="Deleted selected template" if="templ_del">
<span class="fa fa-trash"></span>
</button>
</span>
</div>
</div>
<div class="col-lg-2 col-sm-12">
<div class="pull-right">
<div class="checkbox-inline">
<label>