From d8977873d6e6552b0290c0171c6908c6c45bb24f Mon Sep 17 00:00:00 2001 From: Alexander Moisseev Date: Wed, 25 Jul 2018 10:42:02 +0300 Subject: [PATCH] [WebUI] Refactor query functions into one --- interface/js/app/config.js | 12 +- interface/js/app/graph.js | 89 ++++++------- interface/js/app/history.js | 30 ++--- interface/js/app/rspamd.js | 246 +++++++++++++++++------------------- interface/js/app/stats.js | 4 +- interface/js/app/symbols.js | 30 ++--- 6 files changed, 190 insertions(+), 221 deletions(-) diff --git a/interface/js/app/config.js b/interface/js/app/config.js index b3f99b9c0..490974209 100644 --- a/interface/js/app/config.js +++ b/interface/js/app/config.js @@ -197,7 +197,7 @@ define(["jquery"], $("#actionsFormField").attr("disabled", true); } - function saveActions(callback) { + function saveActions(is_cluster) { var elts = loadActionsFromForm(); // String to array for comparison var eltsArray = JSON.parse(loadActionsFromForm()); @@ -214,20 +214,20 @@ define(["jquery"], (eltsArray[1] === null || eltsArray[2] < eltsArray[1]) && (eltsArray[0] === null || eltsArray[1] < eltsArray[0]) ) { - callback("saveactions", null, null, "POST", {}, { + rspamd.query("saveactions", null, null, "POST", {}, { data: elts, dataType: "json" - }); + }, {}, is_cluster); } else { rspamd.alertMessage("alert-modal alert-error", "Incorrect order of metric actions threshold"); } } $("#saveActionsBtn").on("click", function () { - saveActions(rspamd.queryLocal); + saveActions(); }); $("#saveActionsClusterBtn").on("click", function () { - saveActions(rspamd.queryNeighbours); + saveActions(true); }); }, }); @@ -269,7 +269,7 @@ define(["jquery"], var action = $(form).attr("action"); var id = $(form).attr("id"); var data = $("#" + id).find("textarea").val(); - rspamd.queryNeighbours(action, save_map_success, save_map_error, "POST", { + rspamd.query(action, save_map_success, save_map_error, "POST", { Map: id, }, { data: data, diff --git a/interface/js/app/graph.js b/interface/js/app/graph.js index 356b006f7..26d138689 100644 --- a/interface/js/app/graph.js +++ b/interface/js/app/graph.js @@ -212,58 +212,51 @@ define(["jquery", "d3evolution", "footable"], graphs.graph = initGraph(); } - (function (callback) { - callback("graph", - function (req_data) { - if (checked_server !== "All SERVERS") { - updateWidgets(req_data); - return; - } - - var neighbours_data = req_data - .filter(function (d) { return d.status; }) // filter out unavailable neighbours - .map(function (d) { return d.data; }); + rspamd.query("graph", + function (req_data) { + var neighbours_data = req_data + .filter(function (d) { return d.status; }) // filter out unavailable neighbours + .map(function (d) { return d.data; }); - if (neighbours_data.length > 1) { - neighbours_data.reduce(function (res, curr) { - if ((curr[0][0].x !== res[0][0].x) || - (curr[0][curr[0].length - 1].x !== res[0][res[0].length - 1].x)) { - rspamd.alertMessage("alert-error", - "Neighbours time extents do not match. Check if time is synchronized on all servers."); - updateWidgets(); - return; - } + if (neighbours_data.length > 1) { + neighbours_data.reduce(function (res, curr) { + if ((curr[0][0].x !== res[0][0].x) || + (curr[0][curr[0].length - 1].x !== res[0][res[0].length - 1].x)) { + rspamd.alertMessage("alert-error", + "Neighbours time extents do not match. Check if time is synchronized on all servers."); + updateWidgets(); + return; + } - var data = []; - curr.forEach(function (action, j) { - data.push( - action.map(function (d, i) { - return { - x: d.x, - y: ((res[j][i].y === null) ? d.y : res[j][i].y + d.y) - }; - }) - ); - }); - updateWidgets(data); + var data = []; + curr.forEach(function (action, j) { + data.push( + action.map(function (d, i) { + return { + x: d.x, + y: ((res[j][i].y === null) ? d.y : res[j][i].y + d.y) + }; + }) + ); }); - } else { - updateWidgets(neighbours_data[0]); - } - }, - function (serv, jqXHR, textStatus, errorThrown) { - var serv_name = (typeof serv === "string") ? serv : serv.name; - var alert_status = "alerted_graph_" + serv_name; + updateWidgets(data); + }); + } else { + updateWidgets(neighbours_data[0]); + } + }, + function (serv, jqXHR, textStatus, errorThrown) { + var serv_name = (typeof serv === "string") ? serv : serv.name; + var alert_status = "alerted_graph_" + serv_name; - if (!(alert_status in sessionStorage)) { - sessionStorage.setItem(alert_status, true); - rspamd.alertMessage("alert-error", "Cannot receive throughput data from " + - serv_name + ", error: " + errorThrown); - } - }, - "GET", {}, {}, {type: type} - ); - }((checked_server === "All SERVERS") ? rspamd.queryNeighbours : rspamd.queryLocal)); + if (!(alert_status in sessionStorage)) { + sessionStorage.setItem(alert_status, true); + rspamd.alertMessage("alert-error", "Cannot receive throughput data from " + + serv_name + ", error: " + errorThrown); + } + }, + "GET", {}, {}, {type: type} + ); }; ui.setup = function () { diff --git a/interface/js/app/history.js b/interface/js/app/history.js index e91970663..4e5a69870 100644 --- a/interface/js/app/history.js +++ b/interface/js/app/history.js @@ -570,7 +570,7 @@ define(["jquery", "footable", "humanize"], }; if (checked_server === "All SERVERS") { - rspamd.queryNeighbours("history", function (req_data) { + rspamd.query("history", function (req_data) { function differentVersions(neighbours_data) { var dv = neighbours_data.some(function (e) { return e.version !== neighbours_data[0].version; @@ -697,20 +697,18 @@ define(["jquery", "footable", "humanize"], delete ft.errors; } - (function (callback) { - callback("historyreset", - function () { - ui.getHistory(rspamd, tables, neighbours, checked_server); - ui.getErrors(rspamd, tables, neighbours, checked_server); - }, - function (serv, jqXHR, textStatus, errorThrown) { - var serv_name = (typeof serv === "string") ? serv : serv.name; - rspamd.alertMessage("alert-error", - "Cannot reset history log on " + serv_name + ": " + errorThrown); - }, - "GET", {}, {} - ); - }((checked_server === "All SERVERS") ? rspamd.queryNeighbours : rspamd.queryLocal)); + rspamd.query("historyreset", + function () { + ui.getHistory(rspamd, tables, neighbours, checked_server); + ui.getErrors(rspamd, tables, neighbours, checked_server); + }, + function (serv, jqXHR, textStatus, errorThrown) { + var serv_name = (typeof serv === "string") ? serv : serv.name; + rspamd.alertMessage("alert-error", + "Cannot reset history log on " + serv_name + ": " + errorThrown); + }, + "GET", {}, {} + ); }); }; @@ -766,7 +764,7 @@ define(["jquery", "footable", "humanize"], } }); } else { - rspamd.queryNeighbours("errors", function (req_data) { + rspamd.query("errors", function (req_data) { var neighbours_data = req_data .filter(function (d) { return d.status; diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index 3649deda2..c4832abda 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -180,6 +180,64 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, }, 5000); } + function queryServer(neighbours_status, ind, req_url, on_success, on_error, method, headers, params, req_data) { + var req_params = { + type: typeof method !== "undefined" ? method : "GET", + jsonp: false, + data: req_data, + beforeSend: function (xhr) { + xhr.setRequestHeader("Password", getPassword()); + + if (headers) { + $.each(headers, function (hname, hvalue) { + xhr.setRequestHeader(hname, hvalue); + }); + } + }, + url: neighbours_status[ind].url + req_url, + success: function (json) { + neighbours_status[ind].checked = true; + + if (jQuery.isEmptyObject(json)) { + neighbours_status[ind].status = false; // serv does not work + } else { + neighbours_status[ind].status = true; // serv works + neighbours_status[ind].data = json; + } + }, + error: function (jqXHR, textStatus, errorThrown) { + neighbours_status[ind].status = false; + neighbours_status[ind].checked = true; + if (on_error) { + on_error(neighbours_status[ind], + jqXHR, textStatus, errorThrown); + } else { + alertMessage("alert-error", "Cannot receive data from " + + neighbours_status[ind].host + ": " + errorThrown); + } + }, + complete: function () { + if (neighbours_status.every(function (elt) { return elt.checked; })) { + if (neighbours_status.some(function (elt) { return elt.status; })) { + if (on_success) { + on_success(neighbours_status); + } else { + alertMessage("alert-success", "Request completed"); + } + } else { + alertMessage("alert-error", "Request failed"); + } + } + } + }; + if (params) { + $.each(params, function (k, v) { + req_params[k] = v; + }); + } + $.ajax(req_params); + } + // Public functions ui.alertMessage = alertMessage; ui.setup = function () { @@ -313,139 +371,6 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, }); }; - ui.queryLocal = function (req_url, on_success, on_error, method, headers, params, req_data) { - var req_params = { - type: method, - jsonp: false, - data: req_data, - beforeSend: function (xhr) { - xhr.setRequestHeader("Password", getPassword()); - - if (headers) { - $.each(headers, function (hname, hvalue) { - xhr.setRequestHeader(hname, hvalue); - }); - } - }, - url: neighbours[checked_server].url + req_url, - success: function (data) { - if (on_success) { - on_success(data); - } else { - alertMessage("alert-success", "Data saved"); - } - }, - error: function (jqXHR, textStatus, errorThrown) { - if (on_error) { - on_error(checked_server, jqXHR, textStatus, errorThrown); - } else { - alertMessage("alert-error", "Cannot receive data: " + errorThrown); - } - } - }; - if (params) { - $.each(params, function (k, v) { - req_params[k] = v; - }); - } - $.ajax(req_params); - }; - - ui.queryNeighbours = function (req_url, on_success, on_error, method, headers, params, req_data) { - $.ajax({ - dataType: "json", - type: "GET", - url: "neighbours", - jsonp: false, - beforeSend: function (xhr) { - xhr.setRequestHeader("Password", getPassword()); - }, - success: function (data) { - if (jQuery.isEmptyObject(data)) { - neighbours = { - local: { - host: window.location.host, - url: window.location.href - } - }; - } else { - neighbours = data; - } - var neighbours_status = []; - $.each(neighbours, function (ind) { - neighbours_status.push({ - name: ind, - url: neighbours[ind].url, - host: neighbours[ind].host, - checked: false, - data: {}, - status: false, - }); - }); - $.each(neighbours_status, function (ind) { - var req_params = { - type: typeof method !== "undefined" ? method : "GET", - jsonp: false, - data: req_data, - beforeSend: function (xhr) { - xhr.setRequestHeader("Password", getPassword()); - - if (headers) { - $.each(headers, function (hname, hvalue) { - xhr.setRequestHeader(hname, hvalue); - }); - } - }, - url: neighbours_status[ind].url + req_url, - success: function (json) { - neighbours_status[ind].checked = true; - - if (jQuery.isEmptyObject(json)) { - neighbours_status[ind].status = false; // serv does not work - } else { - neighbours_status[ind].status = true; // serv works - neighbours_status[ind].data = json; - } - }, - error: function (jqXHR, textStatus, errorThrown) { - neighbours_status[ind].status = false; - neighbours_status[ind].checked = true; - if (on_error) { - on_error(neighbours_status[ind], - jqXHR, textStatus, errorThrown); - } else { - alertMessage("alert-error", "Cannot receive data from " + - neighbours_status[ind].host + ": " + errorThrown); - } - }, - complete: function () { - if (neighbours_status.every(function (elt) { return elt.checked; })) { - if (neighbours_status.some(function (elt) { return elt.status; })) { - if (on_success) { - on_success(neighbours_status); - } else { - alertMessage("alert-success", "Request completed"); - } - } else { - alertMessage("alert-error", "Request failed"); - } - } - } - }; - if (params) { - $.each(params, function (k, v) { - req_params[k] = v; - }); - } - $.ajax(req_params); - }); - }, - error: function () { - ui.alertMessage("alert-error", "Cannot receive neighbours data"); - }, - }); - }; - ui.drawPie = function (object, id, data, conf) { var obj = object; if (obj) { @@ -539,5 +464,60 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, ui.getPassword = getPassword; + ui.query = function (req_url, on_success, on_error, method, headers, params, req_data, is_cluster) { + if (checked_server === "All SERVERS" || is_cluster) { + $.ajax({ + dataType: "json", + type: "GET", + url: "neighbours", + jsonp: false, + beforeSend: function (xhr) { + xhr.setRequestHeader("Password", getPassword()); + }, + success: function (data) { + if (jQuery.isEmptyObject(data)) { + neighbours = { + local: { + host: window.location.host, + url: window.location.href + } + }; + } else { + neighbours = data; + } + var neighbours_status = []; + $.each(neighbours, function (ind) { + neighbours_status.push({ + name: ind, + url: neighbours[ind].url, + host: neighbours[ind].host, + checked: false, + data: {}, + status: false, + }); + }); + $.each(neighbours_status, function (ind) { + queryServer(neighbours_status, ind, req_url, on_success, on_error, method, headers, params, req_data); + }); + }, + error: function () { + ui.alertMessage("alert-error", "Cannot receive neighbours data"); + }, + }); + } else { + var neighbours_status = []; + neighbours_status[0] = { + name: checked_server, + url: neighbours[checked_server].url, + host: neighbours[checked_server].host, + checked: false, + data: {}, + status: false, + }; + + queryServer(neighbours_status, 0, req_url, on_success, on_error, method, headers, params, req_data); + } + }; + return ui; }); diff --git a/interface/js/app/stats.js b/interface/js/app/stats.js index 8ef7e25d6..79e7cd6b5 100644 --- a/interface/js/app/stats.js +++ b/interface/js/app/stats.js @@ -171,7 +171,7 @@ define(["jquery", "d3pie", "humanize"], // Public API var ui = { statWidgets: function (rspamd, graphs, checked_server) { - rspamd.queryNeighbours("/auth", function (neighbours_status) { + rspamd.query("/auth", function (neighbours_status) { var neighbours_sum = { version: neighbours_status[0].data.version, auth: "ok", @@ -227,7 +227,7 @@ define(["jquery", "d3pie", "humanize"], rspamd.alertMessage("alert-error", "Cannot receive stats data from: " + serv.name + ", error: " + errorThrown); } - }); + }, "GET", {}, {}, {}, true); }, }; diff --git a/interface/js/app/symbols.js b/interface/js/app/symbols.js index 9c00a5de5..a8a0cc769 100644 --- a/interface/js/app/symbols.js +++ b/interface/js/app/symbols.js @@ -41,22 +41,20 @@ define(["jquery", "footable"], }); }); - (function (callback) { - callback(url, - function () { - rspamd.alertMessage("alert-modal alert-success", "Symbols successfully saved"); - }, - function (serv, jqXHR, textStatus, errorThrown) { - var serv_name = (typeof serv === "string") ? serv : serv.name; - rspamd.alertMessage("alert-modal alert-error", - "Save symbols error on " + serv_name + ": " + errorThrown); - }, - "POST", {}, { - data: JSON.stringify(values), - dataType: "json", - } - ); - }((is_cluster) ? rspamd.queryNeighbours : rspamd.queryLocal)); + rspamd.query(url, + function () { + rspamd.alertMessage("alert-modal alert-success", "Symbols successfully saved"); + }, + function (serv, jqXHR, textStatus, errorThrown) { + var serv_name = (typeof serv === "string") ? serv : serv.name; + rspamd.alertMessage("alert-modal alert-error", + "Save symbols error on " + serv_name + ": " + errorThrown); + }, + "POST", {}, { + data: JSON.stringify(values), + dataType: "json", + }, {}, is_cluster + ); } function decimalStep(number) { var digits = ((Number(number)).toFixed(20)).replace(/^-?\d*\.?|0+$/g, "").length; -- 2.39.5