]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Refactor query function to use named parameters
authorAlexander Moisseev <moiseev@mezonplus.ru>
Sat, 28 Jul 2018 18:39:47 +0000 (21:39 +0300)
committerAlexander Moisseev <moiseev@mezonplus.ru>
Sat, 28 Jul 2018 18:39:47 +0000 (21:39 +0300)
interface/js/app/config.js
interface/js/app/graph.js
interface/js/app/history.js
interface/js/app/rspamd.js
interface/js/app/stats.js
interface/js/app/symbols.js

index b0ba4fcc8e7acad6207391e12a55dc995acd2c2b..8fbaf4fe02204bb9798fa1fd654305f4d37824b4 100644 (file)
@@ -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",
+                    }
                 });
             });
         };
index 26d138689b691fd0c3db7cb63252912da0f3f76e..8cb2d03790b90551fa378a6b17a69a926b3fa6f2 100644 (file)
@@ -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 () {
index 4e5a698704d63f58437a0222810ce57dd94cd928..19bd30623d9797b07665a85456c1b047ecf3e725 100644 (file)
@@ -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");
index b092d537c2dcabd69a863b84d3c94eb4d49e51b9..0a27c14bdb42d3e1b95df1abf223b0fc886dbca4 100644 (file)
@@ -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);
         }
     };
 
index 9eb36669a8ebd1a95dc3c9da8baf34382e911b69..3bac8260676a83e998860cdcc11d28c08a591427 100644 (file)
@@ -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"
+                });
             },
         };
 
index fb69ee1b4b5e980f38fa4cd9b00b45d93ee0e837..1c5d0408ff4e1951ba62035a842f90997fc57ada 100644 (file)
@@ -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
+                });
             });
         };