From 519ae50058e21681c22848c4c7ce5267a1d4c860 Mon Sep 17 00:00:00 2001 From: Alexander Moisseev Date: Sat, 28 Jul 2018 21:39:47 +0300 Subject: [PATCH] [Minor] Refactor query function to use named parameters --- interface/js/app/config.js | 36 +++++---- interface/js/app/graph.js | 10 +-- interface/js/app/history.js | 143 ++++++++++++++++++------------------ interface/js/app/rspamd.js | 64 +++++++++++----- interface/js/app/stats.js | 115 +++++++++++++++-------------- interface/js/app/symbols.js | 34 +++++---- 6 files changed, 222 insertions(+), 180 deletions(-) diff --git a/interface/js/app/config.js b/interface/js/app/config.js index b0ba4fcc8..8fbaf4fe0 100644 --- a/interface/js/app/config.js +++ b/interface/js/app/config.js @@ -136,8 +136,8 @@ define(["jquery"], } ui.getActions = function getActions(rspamd, checked_server) { - rspamd.query("actions", - function (data) { + rspamd.query("actions", { + success: function (data) { $("#actionsBody").empty(); $("#actionsForm").empty(); var items = []; @@ -206,10 +206,14 @@ define(["jquery"], (eltsArray[1] === null || eltsArray[2] < eltsArray[1]) && (eltsArray[0] === null || eltsArray[1] < eltsArray[0]) ) { - rspamd.query("saveactions", null, null, "POST", {}, { - data: elts, - dataType: "json" - }, {}, server); + rspamd.query("saveactions", { + method: "POST", + params: { + data: elts, + dataType: "json" + }, + server: server + }); } else { rspamd.alertMessage("alert-modal alert-error", "Incorrect order of metric actions threshold"); } @@ -222,8 +226,8 @@ define(["jquery"], saveActions("All SERVERS"); }); }, - null, "GET", {}, {}, {}, (checked_server === "All SERVERS") ? "local" : checked_server - ); + server: (checked_server === "All SERVERS") ? "local" : checked_server + }); }; // @upload edited actions @@ -262,11 +266,17 @@ define(["jquery"], var action = $(form).attr("action"); var id = $(form).attr("id"); var data = $("#" + id).find("textarea").val(); - rspamd.query(action, save_map_success, save_map_error, "POST", { - Map: id, - }, { - data: data, - dataType: "text", + rspamd.query(action, { + success: save_map_success, + error: save_map_error, + method: "POST", + headers: { + Map: id, + }, + params:{ + data: data, + dataType: "text", + } }); }); }; diff --git a/interface/js/app/graph.js b/interface/js/app/graph.js index 26d138689..8cb2d0379 100644 --- a/interface/js/app/graph.js +++ b/interface/js/app/graph.js @@ -212,8 +212,8 @@ define(["jquery", "d3evolution", "footable"], graphs.graph = initGraph(); } - rspamd.query("graph", - function (req_data) { + rspamd.query("graph", { + success: function (req_data) { var neighbours_data = req_data .filter(function (d) { return d.status; }) // filter out unavailable neighbours .map(function (d) { return d.data; }); @@ -245,7 +245,7 @@ define(["jquery", "d3evolution", "footable"], updateWidgets(neighbours_data[0]); } }, - function (serv, jqXHR, textStatus, errorThrown) { + error: function (serv, jqXHR, textStatus, errorThrown) { var serv_name = (typeof serv === "string") ? serv : serv.name; var alert_status = "alerted_graph_" + serv_name; @@ -255,8 +255,8 @@ define(["jquery", "d3evolution", "footable"], serv_name + ", error: " + errorThrown); } }, - "GET", {}, {}, {type: type} - ); + data: {type: type} + }); }; ui.setup = function () { diff --git a/interface/js/app/history.js b/interface/js/app/history.js index 4e5a69870..19bd30623 100644 --- a/interface/js/app/history.js +++ b/interface/js/app/history.js @@ -570,64 +570,66 @@ define(["jquery", "footable", "humanize"], }; if (checked_server === "All SERVERS") { - rspamd.query("history", function (req_data) { - function differentVersions(neighbours_data) { - var dv = neighbours_data.some(function (e) { - return e.version !== neighbours_data[0].version; - }); - if (dv) { - rspamd.alertMessage("alert-error", - "Neighbours history backend versions do not match. Cannot display history."); - return true; - } - } - - 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 && !differentVersions(neighbours_data)) { - var data = {}; - if (neighbours_data[0].version) { - data.rows = [].concat.apply([], neighbours_data - .map(function (e) { - return e.rows; - })); - data.version = neighbours_data[0].version; - } else { - // Legacy version - data = [].concat.apply([], neighbours_data); + rspamd.query("history", { + success: function (req_data) { + function differentVersions(neighbours_data) { + var dv = neighbours_data.some(function (e) { + return e.version !== neighbours_data[0].version; + }); + if (dv) { + rspamd.alertMessage("alert-error", + "Neighbours history backend versions do not match. Cannot display history."); + return true; + } } - var items = process_history_data(data); - ft.history = FooTable.init("#historyTable", { - columns: get_history_columns(data), - rows: items, - paging: { - enabled: true, - limit: 5, - size: 25 - }, - filtering: { - enabled: true, - position: "left", - connectors: false - }, - sorting: { - enabled: true - }, - components: { - filtering: FooTable.actionFilter - }, - on: { - "ready.ft.table": drawTooltips, - "after.ft.sorting": drawTooltips, - "after.ft.paging": drawTooltips, - "after.ft.filtering": drawTooltips + 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 && !differentVersions(neighbours_data)) { + var data = {}; + if (neighbours_data[0].version) { + data.rows = [].concat.apply([], neighbours_data + .map(function (e) { + return e.rows; + })); + data.version = neighbours_data[0].version; + } else { + // Legacy version + data = [].concat.apply([], neighbours_data); } - }); - } else if (ft.history) { - ft.history.destroy(); - delete ft.history; + + var items = process_history_data(data); + ft.history = FooTable.init("#historyTable", { + columns: get_history_columns(data), + rows: items, + paging: { + enabled: true, + limit: 5, + size: 25 + }, + filtering: { + enabled: true, + position: "left", + connectors: false + }, + sorting: { + enabled: true + }, + components: { + filtering: FooTable.actionFilter + }, + on: { + "ready.ft.table": drawTooltips, + "after.ft.sorting": drawTooltips, + "after.ft.paging": drawTooltips, + "after.ft.filtering": drawTooltips + } + }); + } else if (ft.history) { + ft.history.destroy(); + delete ft.history; + } } }); } else { @@ -697,18 +699,17 @@ define(["jquery", "footable", "humanize"], delete ft.errors; } - rspamd.query("historyreset", - function () { + rspamd.query("historyreset", { + success: function () { ui.getHistory(rspamd, tables, neighbours, checked_server); ui.getErrors(rspamd, tables, neighbours, checked_server); }, - function (serv, jqXHR, textStatus, errorThrown) { + error: 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", {}, {} - ); + } + }); }); }; @@ -764,15 +765,17 @@ define(["jquery", "footable", "humanize"], } }); } else { - rspamd.query("errors", function (req_data) { - var neighbours_data = req_data - .filter(function (d) { - return d.status; - }) // filter out unavailable neighbours - .map(function (d) { - return d.data; - }); - drawErrorsTable([].concat.apply([], neighbours_data)); + rspamd.query("errors", { + success: function (req_data) { + var neighbours_data = req_data + .filter(function (d) { + return d.status; + }) // filter out unavailable neighbours + .map(function (d) { + return d.data; + }); + drawErrorsTable([].concat.apply([], neighbours_data)); + } }); } $("#updateErrors").off("click"); diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index b092d537c..0a27c14bd 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -180,16 +180,15 @@ 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) { + function queryServer(neighbours_status, ind, req_url, o) { var req_params = { - type: typeof method !== "undefined" ? method : "GET", jsonp: false, - data: req_data, + data: o.data, beforeSend: function (xhr) { xhr.setRequestHeader("Password", getPassword()); - if (headers) { - $.each(headers, function (hname, hvalue) { + if (o.headers) { + $.each(o.headers, function (hname, hvalue) { xhr.setRequestHeader(hname, hvalue); }); } @@ -208,8 +207,8 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, error: function (jqXHR, textStatus, errorThrown) { neighbours_status[ind].status = false; neighbours_status[ind].checked = true; - if (on_error) { - on_error(neighbours_status[ind], + if (o.error) { + o.error(neighbours_status[ind], jqXHR, textStatus, errorThrown); } else { alertMessage("alert-error", "Cannot receive data from " + @@ -219,8 +218,8 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, 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); + if (o.success) { + o.success(neighbours_status); } else { alertMessage("alert-success", "Request completed"); } @@ -230,8 +229,11 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, } } }; - if (params) { - $.each(params, function (k, v) { + if (o.method) { + req_params.method = o.method; + } + if (o.params) { + $.each(o.params, function (k, v) { req_params[k] = v; }); } @@ -451,9 +453,32 @@ 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, server) { - var srv = (server) ? server : checked_server; - if (srv === "All SERVERS") { + /** + * @param {string} url - A string containing the URL to which the request is sent + * @param {Object} [options] - A set of key/value pairs that configure the Ajax request. All settings are optional. + * + * @param {Object|string|Array} [options.data] - Data to be sent to the server. + * @param {Function} [options.error] - A function to be called if the request fails. + * @param {Object} [options.headers] - An object of additional header key/value pairs to send along with requests + * using the XMLHttpRequest transport. + * @param {string} [options.method] - The HTTP method to use for the request. + * @param {Object} [options.params] - An object of additional jQuery.ajax() settings key/value pairs. + * @param {string} [options.server] - A server to which send the request. + * @param {Function} [options.success] - A function to be called if the request succeeds. + * + * @returns {undefined} + */ + ui.query = function (url, options) { + // Force options to be an object + var o = options || {}; + Object.keys(o).forEach(function (option) { + if (["data", "error", "headers", "method", "params", "server", "success"].indexOf(option) < 0) { + throw new Error("Unknown option: " + option); + } + }); + + o.server = o.server || checked_server; + if (o.server === "All SERVERS") { $.ajax({ dataType: "json", type: "GET", @@ -485,7 +510,7 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, }); }); $.each(neighbours_status, function (ind) { - queryServer(neighbours_status, ind, req_url, on_success, on_error, method, headers, params, req_data); + queryServer(neighbours_status, ind, url, o); }); }, error: function () { @@ -495,15 +520,14 @@ function ($, d3pie, visibility, tab_stat, tab_graph, tab_config, } else { var neighbours_status = []; neighbours_status[0] = { - name: srv, - url: (srv === "local") ? "" : neighbours[srv].url, - host: (srv === "local") ? "local" : neighbours[srv].host, + name: o.server, + url: (o.server === "local") ? "" : neighbours[o.server].url, + host: (o.server === "local") ? "local" : neighbours[o.server].host, checked: false, data: {}, status: false, }; - - queryServer(neighbours_status, 0, req_url, on_success, on_error, method, headers, params, req_data); + queryServer(neighbours_status, 0, url, o); } }; diff --git a/interface/js/app/stats.js b/interface/js/app/stats.js index 9eb36669a..3bac82606 100644 --- a/interface/js/app/stats.js +++ b/interface/js/app/stats.js @@ -171,63 +171,66 @@ define(["jquery", "d3pie", "humanize"], // Public API var ui = { statWidgets: function (rspamd, graphs, checked_server) { - rspamd.query("/auth", function (neighbours_status) { - var neighbours_sum = { - version: neighbours_status[0].data.version, - auth: "ok", - uptime: 0, - clean: 0, - probable: 0, - greylist: 0, - reject: 0, - soft_reject: 0, - scanned: 0, - learned: 0, - read_only: neighbours_status[0].data.read_only, - config_id: "" - }; - var status_count = 0; - for (var e in neighbours_status) { - if (neighbours_status[e].status === true) { - // Remove alert status - localStorage.removeItem(e + "_alerted"); - neighbours_sum.clean += neighbours_status[e].data.clean; - neighbours_sum.probable += neighbours_status[e].data.probable; - neighbours_sum.greylist += neighbours_status[e].data.greylist; - neighbours_sum.reject += neighbours_status[e].data.reject; - neighbours_sum.soft_reject += neighbours_status[e].data.soft_reject; - neighbours_sum.scanned += neighbours_status[e].data.scanned; - neighbours_sum.learned += neighbours_status[e].data.learned; - neighbours_sum.uptime += neighbours_status[e].data.uptime; - status_count++; + rspamd.query("/auth", { + success: function (neighbours_status) { + var neighbours_sum = { + version: neighbours_status[0].data.version, + auth: "ok", + uptime: 0, + clean: 0, + probable: 0, + greylist: 0, + reject: 0, + soft_reject: 0, + scanned: 0, + learned: 0, + read_only: neighbours_status[0].data.read_only, + config_id: "" + }; + var status_count = 0; + for (var e in neighbours_status) { + if (neighbours_status[e].status === true) { + // Remove alert status + localStorage.removeItem(e + "_alerted"); + neighbours_sum.clean += neighbours_status[e].data.clean; + neighbours_sum.probable += neighbours_status[e].data.probable; + neighbours_sum.greylist += neighbours_status[e].data.greylist; + neighbours_sum.reject += neighbours_status[e].data.reject; + neighbours_sum.soft_reject += neighbours_status[e].data.soft_reject; + neighbours_sum.scanned += neighbours_status[e].data.scanned; + neighbours_sum.learned += neighbours_status[e].data.learned; + neighbours_sum.uptime += neighbours_status[e].data.uptime; + status_count++; + } } - } - neighbours_sum.uptime = Math.floor(neighbours_sum.uptime / status_count); - var to_Credentials = {}; - to_Credentials["All SERVERS"] = { - name: "All SERVERS", - url: "", - host: "", - checked: true, - data: neighbours_sum, - status: true - }; - neighbours_status.forEach(function (elmt) { - to_Credentials[elmt.name] = elmt; - }); - sessionStorage.setItem("Credentials", JSON.stringify(to_Credentials)); - displayStatWidgets(checked_server); - graphs.chart = getChart(rspamd, graphs.chart, checked_server); - }, - function (serv, jqXHR, textStatus, errorThrown) { - var alert_status = "alerted_stats_" + serv.name; - - if (!(alert_status in sessionStorage)) { - sessionStorage.setItem(alert_status, true); - rspamd.alertMessage("alert-error", "Cannot receive stats data from: " + - serv.name + ", error: " + errorThrown); - } - }, "GET", {}, {}, {}, "All SERVERS"); + neighbours_sum.uptime = Math.floor(neighbours_sum.uptime / status_count); + var to_Credentials = {}; + to_Credentials["All SERVERS"] = { + name: "All SERVERS", + url: "", + host: "", + checked: true, + data: neighbours_sum, + status: true + }; + neighbours_status.forEach(function (elmt) { + to_Credentials[elmt.name] = elmt; + }); + sessionStorage.setItem("Credentials", JSON.stringify(to_Credentials)); + displayStatWidgets(checked_server); + graphs.chart = getChart(rspamd, graphs.chart, checked_server); + }, + error: function (serv, jqXHR, textStatus, errorThrown) { + var alert_status = "alerted_stats_" + serv.name; + + if (!(alert_status in sessionStorage)) { + sessionStorage.setItem(alert_status, true); + rspamd.alertMessage("alert-error", "Cannot receive stats data from: " + + serv.name + ", error: " + errorThrown); + } + }, + server: "All SERVERS" + }); }, }; diff --git a/interface/js/app/symbols.js b/interface/js/app/symbols.js index fb69ee1b4..1c5d0408f 100644 --- a/interface/js/app/symbols.js +++ b/interface/js/app/symbols.js @@ -46,20 +46,22 @@ define(["jquery", "footable"], }); }); - rspamd.query(url, - function () { + rspamd.query(url, { + success: function () { rspamd.alertMessage("alert-modal alert-success", "Symbols successfully saved"); }, - function (serv, jqXHR, textStatus, errorThrown) { + error: 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", {}, { + method: "POST", + params: { data: JSON.stringify(values), dataType: "json", - }, {}, server - ); + }, + server: server + }); } function decimalStep(number) { var digits = ((Number(number)).toFixed(20)).replace(/^-?\d*\.?|0+$/g, "").length; @@ -147,8 +149,8 @@ define(["jquery", "footable"], } // @get symbols into modal form ui.getSymbols = function (rspamd, checked_server) { - rspamd.query("symbols", - function (json) { + rspamd.query("symbols", { + success: function (json) { var data = json[0].data; var items = process_symbols_data(data); FooTable.groupFilter = FooTable.Filtering.extend({ @@ -235,11 +237,11 @@ define(["jquery", "footable"], } }); }, - function (data) { + error: function (data) { rspamd.alertMessage("alert-modal alert-error", data.statusText); }, - "GET", {}, {}, {}, (checked_server === "All SERVERS") ? "local" : checked_server - ); + server: (checked_server === "All SERVERS") ? "local" : checked_server + }); $("#symbolsTable") .off("click", ":button") .on("click", ":button", function () { @@ -253,16 +255,16 @@ define(["jquery", "footable"], $("#updateSymbols").on("click", function (e) { e.preventDefault(); var checked_server = getSelector("selSrv"); - rspamd.query("symbols", - function (data) { + rspamd.query("symbols", { + success: function (data) { var items = process_symbols_data(data[0].data)[0]; ft.symbols.rows.load(items); }, - function (data) { + error: function (data) { rspamd.alertMessage("alert-modal alert-error", data.statusText); }, - "GET", {}, {}, {}, (checked_server === "All SERVERS") ? "local" : checked_server - ); + server: (checked_server === "All SERVERS") ? "local" : checked_server + }); }); }; -- 2.39.5