]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[WebUI] Rework neighbours query function
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 10 Jan 2017 17:33:11 +0000 (17:33 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 10 Jan 2017 17:33:11 +0000 (17:33 +0000)
interface/index.html
interface/js/jquery.cookie.min.js [deleted file]
interface/js/rspamd.js

index e5f6340c1a091b25f484896dfa3a3479aae152a3..78311a4c2b89d1222d1e758fee136d339cab8db5 100644 (file)
 <div id="backDrop" class="modal-backdrop fade in" style="display:none"></div>
 <script src="./js/jquery-3.1.1.min.js"></script>
 <script src="./js/bootstrap.min.js"></script>
-<script src="./js/jquery.cookie.min.js"></script>
 <script src="./js/visibility.min.js"></script>
 <script src="./js/d3.min.js"></script>
 <script src="./js/d3pie.min.js"></script>
diff --git a/interface/js/jquery.cookie.min.js b/interface/js/jquery.cookie.min.js
deleted file mode 100644 (file)
index 1781722..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-/*!
- * jQuery Cookie Plugin v1.4.1
- * https://github.com/carhartl/jquery-cookie
- *
- * Copyright 2006, 2014 Klaus Hartl
- * Released under the MIT license
- */
-!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?a(require("jquery")):a(jQuery)}(function(a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}});
\ No newline at end of file
index 9f3703667cdd1b849cd3550c160750942390fb64..2421e7cbe395f9c76f8fc5e904a7243d44056400 100644 (file)
@@ -2,7 +2,7 @@
  The MIT License (MIT)
 
  Copyright (C) 2012-2013 Anton Simonov <untone@gmail.com>
- Copyright (C) 2014-2015 Vsevolod Stakhov <vsevolod@highsecure.ru>
+ Copyright (C) 2014-2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,6 @@
 (function () {
     $(document).ready(function () {
         // begin
-        //$.cookie.json = true;
         var pie;
         var rrd_pie;
         var history;
         }
         // @update credentials
         function saveActions(data) {
-            if (!supportsSessionStorage()) {
-                $.cookie('rspamdactions', data);
-            } else {
-                sessionStorage.setItem('Actions', JSON.stringify(data));
-            }
+            sessionStorage.setItem('Actions', JSON.stringify(data));
         }
         // @update credentials
         function saveMaps(data) {
-            if (!supportsSessionStorage()) {
-                $.cookie('rspamdmaps', data, {
-                    expires: 1
-                }, {
-                    path: '/'
-                });
-            } else {
-                sessionStorage.setItem('Maps', JSON.stringify(data));
-            }
+            sessionStorage.setItem('Maps', JSON.stringify(data));
         }
         // @clean credentials
         function cleanCredentials() {
         }
 
         function isLogged() {
-            if (sessionStorage.getItem('Password') != null) {
+            if (sessionStorage.getItem('Credentials') != null) {
                 return true;
             }
             return false;
         }
         // @get map by id
         function getMapById(mode) {
-            var data;
-            if (!supportsSessionStorage()) {
-                data = $.cookie('rspamdmaps', data, {
-                    expires: 1
-                }, {
-                    path: '/'
-                });
-            } else {
-                data = JSON.parse(sessionStorage.getItem('Maps'));
-            }
-            if (mode === 'update') {
-                $('#modalBody').empty();
-                getMaps();
-            }
+            var data = JSON.parse(sessionStorage.getItem('Maps'));
+            $('#modalBody').empty();
 
             $.each(data, function (i, item) {
                 $.ajax({
           $(widgets).empty().hide();
           var servers = JSON.parse(sessionStorage.getItem('Credentials'));
 
-          var data = servers[checked_server].data;
+          var data = {}
+          if (servers && servers[checked_server]) {
+              data = servers[checked_server].data;
+          }
           var stat_w = [];
 
           $.each(data, function (i, item) {
           });
           $(widgets).show();
         }
-        // @show widgets
-        function statWidgets() {
+
+        // Query neighbours and call the specified function at the end,
+        // Data received will be pushed inside object:
+        // {server1: data, server2: data} and passed to a callback
+        function queryNeighbours(req_url, on_success, on_error) {
             $.ajax({
                 dataType: "json",
                 type: "GET",
                 },
                 success: function (data) {
                     if (jQuery.isEmptyObject(data)) {
-                        neighbours = { local : { host : "localhost", url : "./" }};
+                        neighbours = {
+                                local:  {
+                                    host: window.location.host,
+                                    url: window.location.path
+                                }
+                        };
                     }   else {
                         neighbours = data;
                     }
                     $.each(neighbours_status, function (ind) {
                         "use strict";
                         $.ajax({
-                          jsonp: false,
-                          beforeSend: function (xhr) {
-                              xhr.setRequestHeader("Password", getPassword());
-                          },
-                          url: neighbours_status[ind].url + "/auth",
-                          success: function (data) {
-                              neighbours_status[ind].checked = true;
-                              if (jQuery.isEmptyObject(data)) {
-                                  neighbours_status[ind].status = false; //serv does not work
-                              } else {
-                                  neighbours_status[ind].status = true; //serv does not work
-                                  neighbours_status[ind].data = data;
-                                  if (!('config_id' in neighbours_status[ind].data)) {
-                                    neighbours_status[ind].data.config_id = "";
-                                  }
-                              }
-                              if (neighbours_status.every(function (elt) {return elt.checked;})) {
-                                  var neighbours_sum = {
-                                      version: neighbours_status[0].data.version,
-                                      auth: "ok",
-                                      uptime: 0,
-                                      clean: 0,
-                                      probable: 0,
-                                      greylist: 0,
-                                      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) {
-                                          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.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();
-                              }
-                          }
-                          //error display
+                            jsonp: false,
+                            beforeSend: function (xhr) {
+                                xhr.setRequestHeader("Password", getPassword());
+                            },
+                            url: neighbours_status[ind].url + req_url,
+                            success: function (data) {
+                                neighbours_status[ind].checked = true;
+                                if (jQuery.isEmptyObject(data)) {
+                                    neighbours_status[ind].status = false; //serv does not work
+                                } else {
+                                    neighbours_status[ind].status = true; //serv does not work
+                                    neighbours_status[ind].data = data;
+                                    if (!('config_id' in neighbours_status[ind].data)) {
+                                        neighbours_status[ind].data.config_id = "";
+                                    }
+                                }
+                                if (neighbours_status.every(function (elt) {return elt.checked;})) {
+                                    on_success(neighbours_status);
+                                }
+                            },
+                            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);
+                                }
+                            }
+                            //error display
                         });
                     });
+                },
+                error: function () {
+                    alertMessage('alert-error', 'Cannot receive neighbours data');
+                },
+            });
+        }
+
+        // @show widgets
+        function statWidgets() {
+            queryNeighbours("/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,
+                        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) {
+                        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.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();
+            },
+            function (serv, jqXHR, textStatus, errorThrown) {
+                alertMessage('alert-error', 'Cannot receive stats data from: ' +
+                        serv.name + ', error: ' + errorThrown);
             });
         }
 
 //        }
 
         function getChart() {
-               var data = JSON.parse(sessionStorage.getItem('Credentials'))[checked_server].data;
-               var new_data = [{"color":"#66cc00","label":"Clean","data":data.clean,"value":data.clean},
-                           {"color":"#cc9966","label":"Temporary rejected","data":data.learned,"value":data.learned},
-                           {"color":"#FFD700","label":"Probable spam","data":data.probable,"value":data.probable},
-                           {"color":"#436EEE","label":"Greylisted","data":data.greylist,"value":data.greylist},
-                           {"color":"#FF0000","label":"Rejected","data":data.rejected,"value":data.rejected}];
-            pie = drawPie(pie, "chart", new_data);
+            var creds = JSON.parse(sessionStorage.getItem('Credentials'));
+            if (creds && creds[checked_server]) {
+                var data = creds[checked_server].data;
+                var new_data = [ {
+                    "color" : "#66cc00",
+                    "label" : "Clean",
+                    "data" : data.clean,
+                    "value" : data.clean
+                }, {
+                    "color" : "#cc9966",
+                    "label" : "Temporary rejected",
+                    "data" : data.learned,
+                    "value" : data.learned
+                }, {
+                    "color" : "#FFD700",
+                    "label" : "Probable spam",
+                    "data" : data.probable,
+                    "value" : data.probable
+                }, {
+                    "color" : "#436EEE",
+                    "label" : "Greylisted",
+                    "data" : data.greylist,
+                    "value" : data.greylist
+                }, {
+                    "color" : "#FF0000",
+                    "label" : "Rejected",
+                    "data" : data.rejected,
+                    "value" : data.rejected
+                } ];
+                pie = drawPie(pie, "chart", new_data);
+            }
         }
-        
+
         function drawPie(obj, id, data, conf) {
             if (obj) {
                 obj.updateProp("data.content",
             if (isLogged()) {
                 var data = JSON.parse(sessionStorage.getItem('Credentials'));
 
-                if (data[checked_server].read_only) {
+                if (data && data[checked_server].read_only) {
                     read_only = true;
                     $('#learning_nav').hide();
                     $('#resetHistory').attr('disabled', true);
             $(disconnect).show();
         }
 
+        $.ajaxSetup({
+            timeout: 1000,
+            jsonp: false
+        })
         connectRSPAMD();
 
         $(document).ajaxStart(function () {