From: Vsevolod Stakhov Date: Tue, 31 Jan 2017 17:45:00 +0000 (+0000) Subject: [WebUI] Move history tab X-Git-Tag: 1.5.0~206 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=712d1dc173dce57404f5909096bae30187e3f469;p=rspamd.git [WebUI] Move history tab --- diff --git a/interface/js/app/history.js b/interface/js/app/history.js new file mode 100644 index 000000000..957b7f70b --- /dev/null +++ b/interface/js/app/history.js @@ -0,0 +1,195 @@ +/* + The MIT License (MIT) + + Copyright (C) 2017 Vsevolod Stakhov + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +define(['jquery', 'datatables'], +function($) { + var interface = {}; + + interface.getHistory = function (rspamd, tables) { + if (tables.history !== undefined) { + var history_length = document.getElementsByName('historyLog_length')[0]; + if (history_length !== undefined) { + history_length = parseInt(history_length.value); + } else { + history_length = 10; + } + tables.history.destroy(); + tables.history = undefined; + $('#historyLog').children('tbody').remove(); + } + + var items = []; + $.ajax({ + dataType: 'json', + url: 'history', + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + }, + error: function () { + rspamd.alertMessage('alert-error', 'Cannot receive history'); + }, + success: function (data) { + $.each(data, function (i, item) { + var action; + + if (item.action === 'clean' || item.action === 'no action') { + action = 'label-success'; + } else if (item.action === 'rewrite subject' || item.action === 'add header' || item.action === 'probable spam') { + action = 'label-warning'; + } else if (item.action === 'spam' || item.action === 'reject') { + action = 'label-danger'; + } else { + action = 'label-info'; + } + + var score; + if (item.score < item.required_score) { + score = 'label-success'; + } else { + score = 'label-danger'; + } + + items.push( + '' + item.time + '' + + '
' + item.id + '
' + + '
' + item.ip + '
' + + '' + item.action + '' + + '' + item.score.toFixed(2) + ' / ' + item.required_score.toFixed(2) + '' + + '
' + item.symbols + '
' + + '' + item.size + '' + + '' + item.scan_time.toFixed(3) + '' + + '
' + item.user + '
'); + }); + $('', { + html: items.join('') + }).insertAfter('#historyLog thead'); + tables.history = $('#historyLog').DataTable({ + "aLengthMenu": [ + [100, 200, -1], + [100, 200, "All"] + ], + "bStateSave": true, + "order": [ + [0, "desc"] + ], + "pageLength": history_length + }); + } + }); + }; + + interface.getErrors = function(rspamd, tables) { + if (rspamd.read_only) return; + + if (tables.errors) { + tables.errors.destroy(); + $('#errorsLog').children('tbody').remove(); + tables.errors = undefined; + } + + var items = []; + $.ajax({ + dataType: 'json', + url: 'errors', + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + }, + error: function () { + rspamd.alertMessage('alert-error', 'Cannot receive errors'); + }, + success: function (data) { + $.each(data, function (i, item) { + + items.push( + '' + new Date(item.ts * 1000) + '' + + '' + item.type + '' + + '' + item.pid + '' + + '' + item.module + '' + + '' + item.id + '' + + '
' + item.message + '
' + ); + }); + $('', { + html: items.join('') + }).insertAfter('#errorsLog thead'); + tables.errors = $('#errorsLog').DataTable({ + "paging": true, + "orderMulti": false, + "order": [ + [0, "desc"], + ], + "info": false, + "columns": [ + {"width": "15%", "searchable": true, "orderable": true, "type": "num"}, + {"width": "5%", "searchable": true, "orderable": true}, + {"width": "5%", "searchable": true, "orderable": true}, + {"width": "3%", "searchable": true, "orderable": true}, + {"width": "3%", "searchable": true, "orderable": true}, + {"width": "65%", "searchable": true, "orderable": true}, + ], + }); + tables.errors.columns.adjust().draw(); + } + }); + }; + + interface.setup = function(rspamd, tables) { + $('#updateHistory').on('click', function () { + interface.getHistory(rspamd, tables); + }); + $('#updateErrors').on('click', function () { + interface.getErrors(rspamd, tables); + }); + // @reset history log + $('#resetHistory').on('click', function () { + if (!confirm("Are you sure you want to reset history log?")) { + return; + } + if (tables.history) { + tables.history.destroy(); + tables.history = undefined; + $('#historyLog').children('tbody').remove(); + } + $.ajax({ + dataType: 'json', + type: 'GET', + jsonp: false, + url: 'historyreset', + beforeSend: function (xhr) { + xhr.setRequestHeader('Password', rspamd.getPassword()); + }, + success: function () { + interface.getHistory(rspamd, tables); + interface.getErrors(rspamd, tables); + }, + error: function (data) { + rspamd.alertMessage('alert-modal alert-error', data.statusText); + } + }); + }); + }; + return interface; +}); \ No newline at end of file diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index e5d91495c..b12f1d864 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -23,8 +23,9 @@ THE SOFTWARE. */ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config', - 'app/symbols'], - function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, tab_symbols) { + 'app/symbols', 'app/history'], + function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, + tab_symbols, tab_history) { // begin var graphs = {}; var tables = {}; @@ -110,8 +111,8 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config', tab_symbols.getSymbols(interface, tables, checked_server); break; case "#history_nav": - getHistory(); - getErrors(); + tab_history.getHistory(interface, tables, checked_server); + tab_history.getErrors(interface, tables, checked_server); break; case "#disconnect": disconnect(); @@ -198,6 +199,7 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config', }); tab_config.setup(interface); tab_symbols.setup(interface, tables); + tab_history.setup(interface, tables); }; interface.alertMessage = function (alertState, alertText) { @@ -474,194 +476,6 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config', return interface; - // @alert popover - - // @opem modal with target form enabled - - - - // @get history log - // function getChart() { - // //console.log(data) - // $.ajax({ - // dataType: 'json', - // url: './pie', - // beforeSend: function(xhr) { - // xhr.setRequestHeader('Password', getPassword()) - // }, - // error: function() { - // alertMessage('alert-error', 'Cannot receive history'); - // }, - // success: function(data) { - // console.log(data); - // } - // }); - // } - // @get history log - function getHistory() { - - if (history) { - var history_length = document.getElementsByName('historyLog_length')[0]; - if (history_length !== undefined) { - history_length = parseInt(history_length.value); - } else { - history_length = 10; - } - history.destroy(); - $('#historyLog').children('tbody').remove(); - } - - var items = []; - $.ajax({ - dataType: 'json', - url: 'history', - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - error: function () { - alertMessage('alert-error', 'Cannot receive history'); - }, - success: function (data) { - $.each(data, function (i, item) { - var action; - - if (item.action === 'clean' || item.action === 'no action') { - action = 'label-success'; - } else if (item.action === 'rewrite subject' || item.action === 'add header' || item.action === 'probable spam') { - action = 'label-warning'; - } else if (item.action === 'spam' || item.action === 'reject') { - action = 'label-danger'; - } else { - action = 'label-info'; - } - - var score; - if (item.score < item.required_score) { - score = 'label-success'; - } else { - score = 'label-danger'; - } - - items.push( - '' + item.time + '' + - '
' + item.id + '
' + - '
' + item.ip + '
' + - '' + item.action + '' + - '' + item.score.toFixed(2) + ' / ' + item.required_score.toFixed(2) + '' + - '
' + item.symbols + '
' + - '' + item.size + '' + - '' + item.scan_time.toFixed(3) + '' + - '
' + item.user + '
'); - }); - $('', { - html: items.join('') - }).insertAfter('#historyLog thead'); - history = $('#historyLog').DataTable({ - "aLengthMenu": [ - [100, 200, -1], - [100, 200, "All"] - ], - "bStateSave": true, - "order": [ - [0, "desc"] - ], - "pageLength": history_length - }); - } - }); - } - - function getErrors() { - if (interface.read_only) return; - - if (errors) { - errors.destroy(); - $('#errorsLog').children('tbody').remove(); - } - - var items = []; - $.ajax({ - dataType: 'json', - url: 'errors', - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - error: function () { - alertMessage('alert-error', 'Cannot receive errors'); - }, - success: function (data) { - $.each(data, function (i, item) { - - items.push( - '' + new Date(item.ts * 1000) + '' + - '' + item.type + '' + - '' + item.pid + '' + - '' + item.module + '' + - '' + item.id + '' + - '
' + item.message + '
' - ); - }); - $('', { - html: items.join('') - }).insertAfter('#errorsLog thead'); - errors = $('#errorsLog').DataTable({ - "paging": true, - "orderMulti": false, - "order": [ - [0, "desc"], - ], - "info": false, - "columns": [ - {"width": "15%", "searchable": true, "orderable": true, "type": "num"}, - {"width": "5%", "searchable": true, "orderable": true}, - {"width": "5%", "searchable": true, "orderable": true}, - {"width": "3%", "searchable": true, "orderable": true}, - {"width": "3%", "searchable": true, "orderable": true}, - {"width": "65%", "searchable": true, "orderable": true}, - ], - }); - errors.columns.adjust().draw(); - } - }); - } - - // @reset history log - $('#resetHistory').on('click', function () { - if (!confirm("Are you sure you want to reset history log?")) { - return; - } - if (history) { - history.destroy(); - $('#historyLog').children('tbody').remove(); - } - $.ajax({ - dataType: 'json', - type: 'GET', - jsonp: false, - url: 'historyreset', - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - success: function () { - getHistory(); - getErrors(); - }, - error: function (data) { - alertMessage('alert-modal alert-error', data.statusText); - } - }); - }); - - // @update history log - $('#updateHistory').on('click', function () { - getHistory(); - }); - $('#updateErrors').on('click', function () { - getErrors(); - }); - // @upload text function uploadText(data, source, headers) { @@ -845,48 +659,4 @@ define(['jquery', 'd3pie', 'visibility', 'app/stats', 'app/graph', 'app/config', $(this).closest('form').find('button').attr('disabled').addClass('disabled'); } }); - - // @upload symbols from modal - function saveSymbols(action, id, is_cluster) { - var inputs = $('#' + id + ' :input[data-role="numerictextbox"]'); - var url = action; - var values = []; - $(inputs).each(function () { - values.push({ - name: $(this).attr('id').substring(5), - value: parseFloat($(this).val()) - }); - }); - if (is_cluster) { - interface.queryNeighbours(url, function () { - alertMessage('alert-modal alert-success', 'Symbols successfully saved'); - }, function (serv, qXHR, textStatus, errorThrown) { - alertMessage('alert-modal alert-error', - 'Save symbols error on ' + - serv.name + ': ' + errorThrown); - }, "POST", {}, { - data: JSON.stringify(values), - dataType: "json", - }); - } - else { - $.ajax({ - data: JSON.stringify(values), - dataType: 'json', - type: 'POST', - url: url, - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader('Password', getPassword()); - }, - success: function () { - alertMessage('alert-modal alert-success', 'Symbols successfully saved'); - }, - error: function (data) { - alertMessage('alert-modal alert-error', data.statusText); - } - }); - } - } - // @connect to server });