]> arthur.barton.de Git - netdata.git/blobdiff - web/dashboard.js
Merge remote-tracking branch 'lucadev/master'
[netdata.git] / web / dashboard.js
index 6bc35031701f65c5ea6c723518a605a8b7d8edb8..1dc57a1ac58a66dbe7efd63bd52a56011a4f429d 100644 (file)
@@ -16,6 +16,7 @@
 // var netdataRegistryCallback = null;  // Callback function that will be invoked with one param,
 //                                         the URLs from the registry
 // var netdataShowHelp = true;          // enable/disable help
+// var netdataShowAlarms = true;        // enable/disable help
 //
 // You can also set the default netdata server, using the following.
 // When this variable is not set, we assume the page is hosted on your
     if(typeof netdataShowHelp === 'undefined')
         netdataShowHelp = true;
 
+    if(typeof netdataShowAlarms === 'undefined')
+        netdataShowAlarms = true;
+
     NETDATA.colors = NETDATA.themes.current.colors;
 
     // these are the colors Google Charts are using
             pan_and_zoom_factor_multiplier_shift: 3.0,
             pan_and_zoom_factor_multiplier_alt: 4.0,
 
+            abort_ajax_on_scroll: false,
+
             setOptionCallback: function() { ; }
         },
 
         // console.log('onscroll');
 
         NETDATA.options.last_page_scroll = new Date().getTime();
+        NETDATA.options.auto_refresher_stop_until = 0;
+
         if(NETDATA.options.targets === null) return;
 
         // when the user scrolls he sees that we have
         // the charts back to visible quickly
         var targets = NETDATA.options.targets;
         var len = targets.length;
-        while(len--) targets[len].isVisible();
+        if(NETDATA.options.abort_ajax_on_scroll === true) {
+            while (len--) {
+                if (targets[len]._updating === true) {
+                    if (typeof targets[len].xhr !== 'undefined') {
+                        targets[len].xhr.abort();
+                        targets[len].running = false;
+                        targets[len]._updating = false;
+                    }
+                    targets[len].isVisible();
+                }
+            }
+        }
+        else {
+            while (len--)
+                targets[len].isVisible();
+        }
     };
 
     window.onresize = NETDATA.onresize;
         411: { message: "Netdata registry server send invalid response to DELETE ", alert: false },
         412: { message: "Netdata registry DELETE failed", alert: false },
         413: { message: "Netdata registry server send invalid response to SWITCH ", alert: false },
-        414: { message: "Netdata registry SWITCH failed", alert: false }
+        414: { message: "Netdata registry SWITCH failed", alert: false },
+        415: { message: "Netdata alarms download failed", alert: false },
+        416: { message: "Netdata alarms log download failed", alert: false }
     };
     NETDATA.errorLast = {
         code: 0,
                 async: true,
                 xhrFields: { withCredentials: true } // required for the cookie
             })
-            .success(function(data) {
+            .done(function(data) {
+                that.xhr = undefined;
+
                 if(that.debug === true)
                     that.log('data received. updating chart.');
 
                 that.updateChartWithData(data);
             })
-            .fail(function() {
-                error('data download failed for url: ' + that.data_url);
+            .fail(function(msg) {
+                that.xhr = undefined;
+
+                if(msg.statusText !== 'abort')
+                    error('data download failed for url: ' + that.data_url);
             })
             .always(function() {
+                that.xhr = undefined;
+
                 NETDATA.statistics.refreshes_active--;
                 that._updating = false;
                 if(typeof callback === 'function') callback();
     // the first set will be executed in parallel
     // the second will be given to NETDATA.chartRefresher_uninitialized()
     NETDATA.chartRefresher = function() {
+        // console.log('auto-refresher...');
+
         if(NETDATA.options.pause === true) {
             // console.log('auto-refresher is paused');
             setTimeout(NETDATA.chartRefresher,
         }
 
         if(NETDATA.options.current.parallel_refresher === false) {
+            // console.log('auto-refresher is calling chartRefresherNoParallel(0)');
             NETDATA.chartRefresherNoParallel(0);
             return;
         }
         if(NETDATA.options.updated_dom === true) {
             // the dom has been updated
             // get the dom parts again
+            // console.log('auto-refresher is calling parseDom()');
             NETDATA.parseDom(NETDATA.chartRefresher);
             return;
         }
         }
 
         if(parallel.length > 0) {
+            // console.log('auto-refresher executing in parallel for ' + parallel.length.toString() + ' charts');
             // this will execute the jobs in parallel
             $(parallel).each(function() {
                 this.autoRefresh();
             })
         }
+        //else {
+        //    console.log('auto-refresher nothing to do');
+        //}
 
         // run the next refresh iteration
         setTimeout(NETDATA.chartRefresher,
 
         NETDATA.parseDom(NETDATA.chartRefresher);
 
+        // Alarms initialization
+        if(netdataShowAlarms === true)
+            setTimeout(NETDATA.alarms.init, 1000);
+
         // Registry initialization
-        setTimeout(NETDATA.registry.init, 1000);
+        setTimeout(NETDATA.registry.init, 1500);
     };
 
     // ----------------------------------------------------------------------------------------------------------------
     };
 
 
+    // ----------------------------------------------------------------------------------------------------------------
+    // Registry of netdata hosts
+
+    NETDATA.alarms = {
+        server: null,
+        current: null,
+        callback: null,
+
+        get: function(what, callback) {
+            $.ajax({
+                url: NETDATA.alarms.server + '/api/v1/alarms?' + what.toString(),
+                async: true,
+                cache: false,
+                xhrFields: { withCredentials: true } // required for the cookie
+            })
+                .done(function(data) {
+                    if(typeof callback === 'function')
+                        callback(data);
+                })
+                .fail(function() {
+                    NETDATA.error(415, NETDATA.alarms.server);
+
+                    if(typeof callback === 'function')
+                        callback(null);
+                });
+        },
+
+        update_forever: function() {
+            NETDATA.alarms.get('active', function(data) {
+                if(data !== null) {
+                    if('Notification' in window && NETDATA.alarms.current != null) {
+                        if(Object.keys(NETDATA.alarms.current.alarms).length < Object.keys(data.alarms).length) {
+                            if (Notification.permission === 'granted') {
+                                new Notification('Netdata Alarm!', {body: 'Your Server needs attention!',
+                                    icon: 'images/seo-performance-128.png'});
+                            }
+                        }
+                    }
+
+                    NETDATA.alarms.current = data;
+
+                    if (typeof NETDATA.alarms.callback === 'function') {
+                        NETDATA.alarms.callback(data);
+                    }
+                }
+
+                setTimeout(NETDATA.alarms.update_forever, 10000);
+            });
+        },
+
+        get_log: function(callback) {
+            $.ajax({
+                url: NETDATA.alarms.server + '/api/v1/alarm_log',
+                async: true,
+                cache: false,
+                xhrFields: { withCredentials: true } // required for the cookie
+            })
+                .done(function(data) {
+                    if(typeof callback === 'function')
+                        callback(data);
+                })
+                .fail(function() {
+                    NETDATA.error(416, NETDATA.alarms.server);
+
+                    if(typeof callback === 'function')
+                        callback(null);
+                });
+        },
+
+        init: function() {
+            var host = NETDATA.serverDefault;
+            while(host.slice(-1) === '/')
+                host = host.substring(0, host.length - 1);
+            NETDATA.alarms.server = host;
+            
+            NETDATA.alarms.update_forever();
+            if ('Notification' in window && Notification.permission === 'default') {
+                Notification.requestPermission();
+            }
+        }
+    };
+
     // ----------------------------------------------------------------------------------------------------------------
     // Registry of netdata hosts