]> source.dussan.org Git - rspamd.git/commitdiff
[WebUI] Refactor query functions into one 2368/head
authorAlexander Moisseev <moiseev@mezonplus.ru>
Wed, 25 Jul 2018 07:42:02 +0000 (10:42 +0300)
committerAlexander Moisseev <moiseev@mezonplus.ru>
Wed, 25 Jul 2018 07:42:02 +0000 (10:42 +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 b3f99b9c015ee5f880ba1be2b149971af90582a1..490974209234f1cc64a95d4d6fcc87171425c89e 100644 (file)
@@ -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,
index 356b006f7e06d42dc8ce6dde6d07345cf55361c8..26d138689b691fd0c3db7cb63252912da0f3f76e 100644 (file)
@@ -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 () {
index e919706637721abdd9949fac0edb82dd0a107d6e..4e5a698704d63f58437a0222810ce57dd94cd928 100644 (file)
@@ -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;
index 3649deda2c7ba3f49d99f188443565bd867a80ee..c4832abdaa7291f8c341139ad45a1109e088a7b3 100644 (file)
@@ -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;
 });
index 8ef7e25d603081a6b4a8460343d970f7216f775d..79e7cd6b501745aca75de7b2dda30e74a1aaf152 100644 (file)
@@ -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);
             },
         };
 
index 9c00a5de55639ff2ecba20a31b2d36fc0828cb39..a8a0cc769ed73107dc945d52b3d2e63532fd9467 100644 (file)
@@ -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;