]> arthur.barton.de Git - netdata.git/blobdiff - node.d/node_modules/netdata.js
changes suggested by codeclimate
[netdata.git] / node.d / node_modules / netdata.js
index 572e639beee9a69b88fc6f721c86675a62150408..11202061e487945abdc39c2ba88d9c54ea873795 100644 (file)
@@ -48,7 +48,7 @@ var netdata = {
     options: {
         filename: __filename,
         DEBUG: false,
-        update_every: 1,
+        update_every: 1
     },
 
     chartAlgorithms: {
@@ -74,10 +74,13 @@ var netdata = {
             name: 'http',
 
             process: function(service, callback) {
-                if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': making ' + this.name + ' request: ' + netdata.stringify(service.request));
+                var __DEBUG = netdata.options.DEBUG;
+
+                if(__DEBUG === true)
+                    netdata.debug(service.module.name + ': ' + service.name + ': making ' + this.name + ' request: ' + netdata.stringify(service.request));
 
                 var req = http.request(service.request, function(response) {
-                    if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': got server response...');
+                    if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': got server response...');
 
                     var end = false;
                     var data = '';
@@ -87,7 +90,7 @@ var netdata = {
                         if(end === false) {
                             service.error('Got HTTP code ' + response.statusCode + ', failed to get data.');
                             end = true;
-                            callback(null);
+                            return callback(null);
                         }
                     }
 
@@ -99,28 +102,28 @@ var netdata = {
                         if(end === false) {
                             service.error(': Read error, failed to get data.');
                             end = true;
-                            callback(null);
+                            return callback(null);
                         }
                     });
 
                     response.on('end', function() {
                         if(end === false) {
-                            if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': read completed.');
+                            if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': read completed.');
                             end = true;
-                            callback(data);
+                            return callback(data);
                         }
                     });
                 });
 
                 req.on('error', function(e) {
-                    if(netdata.options.DEBUG === true) netdata.debug('Failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
+                    if(__DEBUG === true) netdata.debug('Failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
                     service.error('Failed to make request, message: ' + e.message);
-                    callback(null);
+                    return callback(null);
                 });
 
                 // write data to request body
                 if(typeof service.postData !== 'undefined' && service.request.method === 'POST') {
-                    if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': posting data: ' + service.postData);
+                    if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': posting data: ' + service.postData);
                     req.write(service.postData);
                 }
 
@@ -139,7 +142,7 @@ var netdata = {
 
     logdate: function(d) {
         if(typeof d === 'undefined') d = new Date();
-        return this.zeropad2(d.getFullYear()) + '-' + this.zeropad2(d.getMonth()) + '-' + this.zeropad2(d.getDay())
+        return d.getFullYear().toString() + '-' + this.zeropad2(d.getMonth() + 1) + '-' + this.zeropad2(d.getDate())
             + ' ' + this.zeropad2(d.getHours()) + ':' + this.zeropad2(d.getMinutes()) + ':' + this.zeropad2(d.getSeconds());
     },
 
@@ -215,18 +218,18 @@ var netdata = {
             }
         };
 
-        service.execute = function(callback) {
-            if(service.enabled === false) {
-                callback(null);
-                return;
-            }
+        service.execute = function(responseProcessor) {
+            var __DEBUG = netdata.options.DEBUG;
+
+            if(service.enabled === false)
+                return responseProcessor(null);
 
             this.module.active++;
             this.running = true;
             this.started = Date.now();
             this.updates++;
 
-            if(netdata.options.DEBUG === true)
+            if(__DEBUG === true)
                 netdata.debug(this.module.name + ': ' + this.name + ': making ' + this.processor.name + ' request: ' + netdata.stringify(this));
 
             this.processor.process(this, function(response) {
@@ -239,35 +242,40 @@ var netdata = {
                 if(response !== null)
                     service.errorClear();
 
-                if(netdata.options.DEBUG === true)
+                if(__DEBUG === true)
                     netdata.debug(service.module.name + ': ' + service.name + ': processing ' + service.processor.name + ' response (received in ' + (service.ended - service.started).toString() + ' ms)');
 
-                callback(service, response);
+                responseProcessor(service, response);
 
                 service.running = false;
                 service.module.active--;
                 if(service.module.active < 0) {
                     service.module.active = 0;
-                    if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': active module counter below zero.');
+                    if(__DEBUG === true)
+                        netdata.debug(service.module.name + ': active module counter below zero.');
                 }
 
                 if(service.module.active === 0) {
                     // check if we run under configure
                     if(service.module.configure_callback !== null) {
-                        if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': configuration finish callback called from processResponse().');
-                        var ccallback = service.module.configure_callback;
+                        if(__DEBUG === true)
+                            netdata.debug(service.module.name + ': configuration finish callback called from processResponse().');
+
+                        var configure_callback = service.module.configure_callback;
                         service.module.configure_callback = null;
-                        ccallback();
+                        configure_callback();
                     }
                 }
             });
         };
 
         service.update = function() {
-            if(netdata.options.DEBUG === true) netdata.debug(this.module.name + ': ' + this.name + ': starting data collection...');
+            if(netdata.options.DEBUG === true)
+                netdata.debug(this.module.name + ': ' + this.name + ': starting data collection...');
 
             this.module.update(this, function() {
-                if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': data collection ended in ' + service.duration.toString() + ' ms.');
+                if(netdata.options.DEBUG === true)
+                    netdata.debug(service.module.name + ': ' + service.name + ': data collection ended in ' + service.duration.toString() + ' ms.');
             });
         };
 
@@ -291,13 +299,17 @@ var netdata = {
         service._send_chart_to_netdata = function(chart) {
             // internal function to send a chart to netdata
             this.queue('CHART "' + chart.id + '" "' + chart.name + '" "' + chart.title + '" "' + chart.units + '" "' + chart.family + '" "' + chart.context + '" "' + chart.type + '" ' + chart.priority.toString() + ' ' + chart.update_every.toString());
-            
-            for(var dim in chart.dimensions) {
-                var d = chart.dimensions[dim];
 
-                this.queue('DIMENSION "' + d.id + '" "' + d.name + '" "' + d.algorithm + '" ' + d.multiplier.toString() + ' ' + d.divisor.toString() + ' ' + ((d.hidden === true)?'hidden':'').toString());
-                d._created = true;
-                d._updated = false;
+            if(typeof(chart.dimensions) !== 'undefined') {
+                var dims = Object.keys(chart.dimensions);
+                var len = dims.length;
+                while(len--) {
+                    var d = chart.dimensions[dims[len]];
+
+                    this.queue('DIMENSION "' + d.id + '" "' + d.name + '" "' + d.algorithm + '" ' + d.multiplier.toString() + ' ' + d.divisor.toString() + ' ' + ((d.hidden === true) ? 'hidden' : '').toString());
+                    d._created = true;
+                    d._updated = false;
+                }
             }
 
             chart._created = true;
@@ -311,7 +323,7 @@ var netdata = {
                 this.end();
             }
 
-            if(typeof(chart.id) === 'undefined' || netdata.charts[chart.id] != chart) {
+            if(typeof(chart.id) === 'undefined' || netdata.charts[chart.id] !== chart) {
                 this.error('Called begin() for chart ' + chart.id + ' that is not mine. Where did you find it? Ignoring it.');
                 return false;
             }
@@ -396,6 +408,8 @@ var netdata = {
 
         // create a netdata chart
         service.chart = function(id, chart) {
+            var __DEBUG = netdata.options.DEBUG;
+
             if(typeof(netdata.charts[id]) === 'undefined') {
                 netdata.charts[id] = {
                     _created: false,
@@ -420,55 +434,59 @@ var netdata = {
             var c = netdata.charts[id];
 
             if(typeof(chart.name) !== 'undefined' && chart.name !== c.name) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its name');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its name');
                 c.name = chart.name;
                 c._updated = true;
             }
 
             if(typeof(chart.title) !== 'undefined' && chart.title !== c.title) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its title');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its title');
                 c.title = chart.title;
                 c._updated = true;
             }
 
             if(typeof(chart.units) !== 'undefined' && chart.units !== c.units) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its units');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its units');
                 c.units = chart.units;
                 c._updated = true;
             }
 
             if(typeof(chart.family) !== 'undefined' && chart.family !== c.family) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its family');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its family');
                 c.family = chart.family;
                 c._updated = true;
             }
 
             if(typeof(chart.context) !== 'undefined' && chart.context !== c.context) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its context');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its context');
                 c.context = chart.context;
                 c._updated = true;
             }
 
             if(typeof(chart.type) !== 'undefined' && chart.type !== c.type) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its type');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its type');
                 c.type = chart.type;
                 c._updated = true;
             }
 
             if(typeof(chart.priority) !== 'undefined' && chart.priority !== c.priority) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its priority');
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its priority');
                 c.priority = chart.priority;
                 c._updated = true;
             }
 
             if(typeof(chart.update_every) !== 'undefined' && chart.update_every !== c.update_every) {
-                if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its update_every from ' + c.update_every + ' to ' + chart.update_every);
+                if(__DEBUG === true) netdata.debug('chart ' + id + ' updated its update_every from ' + c.update_every + ' to ' + chart.update_every);
                 c.update_every = chart.update_every;
                 c._updated = true;
             }
 
             if(typeof(chart.dimensions) !== 'undefined') {
-                for(var x in chart.dimensions) {
+                var dims = Object.keys(chart.dimensions);
+                var len = dims.length;
+                while(len--) {
+                    var x = dims[len];
+
                     if(typeof(c.dimensions[x]) === 'undefined') {
                         c._dimensions_count++;
 
@@ -480,10 +498,10 @@ var netdata = {
                             algorithm: netdata.chartAlgorithms.absolute,    // the id of the netdata algorithm
                             multiplier: 1,          // the multiplier
                             divisor: 1,             // the divisor
-                            hidden: false,          // is hidden (boolean)
+                            hidden: false           // is hidden (boolean)
                         };
 
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' created dimension ' + x);
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ' created dimension ' + x);
                         c._updated = true;
                     }
 
@@ -491,31 +509,31 @@ var netdata = {
                     var d = c.dimensions[x];
 
                     if(typeof(dim.name) !== 'undefined' && d.name !== dim.name) {
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its name');
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its name');
                         d.name = dim.name;
                         d._updated = true;
                     }
 
                     if(typeof(dim.algorithm) !== 'undefined' && d.algorithm !== dim.algorithm) {
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its algorithm from ' + d.algorithm + ' to ' + dim.algorithm);
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its algorithm from ' + d.algorithm + ' to ' + dim.algorithm);
                         d.algorithm = dim.algorithm;
                         d._updated = true;
                     }
 
                     if(typeof(dim.multiplier) !== 'undefined' && d.multiplier !== dim.multiplier) {
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its multiplier');
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its multiplier');
                         d.multiplier = dim.multiplier;
                         d._updated = true;
                     }
 
                     if(typeof(dim.divisor) !== 'undefined' && d.divisor !== dim.divisor) {
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its divisor');
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its divisor');
                         d.divisor = dim.divisor;
                         d._updated = true;
                     }
 
                     if(typeof(dim.hidden) !== 'undefined' && d.hidden !== dim.hidden) {
-                        if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its hidden status');
+                        if(__DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its hidden status');
                         d.hidden = dim.hidden;
                         d._updated = true;
                     }
@@ -557,7 +575,10 @@ var netdata = {
 
         if(this.services.length === 0) {
             this.disableNodePlugin();
-            process.exit(1);
+
+            // eslint suggested way to exit
+            var exit = process.exit;
+            exit(1);
         }
         else this.runAllServices();
     },
@@ -565,7 +586,10 @@ var netdata = {
     // disable the whole node.js plugin
     disableNodePlugin: function() {
         this.send('DISABLE');
-        process.exit(1);
+
+        // eslint suggested way to exit
+        var exit = process.exit;
+        exit(1);
     },
 
     requestFromParams: function(protocol, hostname, port, path, method) {
@@ -616,13 +640,14 @@ var netdata = {
 
         if(module.configure_callback !== null && added === 0) {
             if(netdata.options.DEBUG === true) this.debug(module.name + ': configuration finish callback called from configure().');
+            var configure_callback = module.configure_callback;
             module.configure_callback = null;
-            callback();
+            configure_callback();
         }
 
         return added;
     }
 };
 
-if(netdata.options.DEBUG === true) netdata.debug('loaded netdata from: ' + __filename);
+if(netdata.options.DEBUG === true) netdata.debug('loaded netdata from:', __filename);
 module.exports = netdata;