Skip to content

Commit

Permalink
Lint codes
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru committed Nov 24, 2013
1 parent ce586a7 commit 27fc1cd
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 457 deletions.
34 changes: 17 additions & 17 deletions cli-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ program.parse(process.argv);

var interval = 1000;
if ('interval' in program) {
interval = Number(program.interval*1000);
interval = Number(program.interval*1000);
}

function retrieve() {
var next = Date.now() + interval;
nstat.get(
'stat',
'net',
'load',
'mem',
'disk',
function(err, data) {
if (err) {
console.error('ERROR',err.message);
} else {
console.log(JSON.stringify(data));
}
var wait = Math.max(0, next - Date.now());
setTimeout(retrieve, wait);
});
var next = Date.now() + interval;
nstat.get(
'stat',
'net',
'load',
'mem',
'disk',
function(err, data) {
if (err) {
console.error('ERROR',err.message);
} else {
console.log(JSON.stringify(data));
}
var wait = Math.max(0, next - Date.now());
setTimeout(retrieve, wait);
});
}

retrieve();
183 changes: 90 additions & 93 deletions node-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,123 +19,120 @@ nstat.prototype.plugins = {};

// read multiple files into single content
nstat.prototype.read = function() {
var files = Array.prototype.slice.apply(arguments);
var callback = files.pop();
var funcs = [];
files.forEach(function(file) {
funcs.push(function(callback) {
fs.readFile(file, 'utf8', callback);
});
});
async.series(funcs, function(err, contents) {
if (err) {
console.error('ERROR',err.message);
} else {
callback(contents.join(''));
}
});
var files = Array.prototype.slice.apply(arguments);
var callback = files.pop();
var funcs = [];
files.forEach(function(file) {
funcs.push(function(callback) {
fs.readFile(file, 'utf8', callback);
});
});
async.series(funcs, function(err, contents) {
if (err) {
console.error('ERROR',err.message);
} else {
callback(contents.join(''));
}
});
};

// read lines
nstat.prototype.lines = function() {
var files = Array.prototype.slice.apply(arguments);
var endHandler = files.pop();
var lineHandler = files.pop();
var funcs = [];
var self = this;
files.forEach(function(file) {
funcs.push(function(callback) {
fs.readFile(file, 'utf8', function(err, content) {
if (err) {
callback(err);
} else {
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
lineHandler.call(self, lines[i]);
}
callback(null,lines);
}
});
});
});
async.series(funcs, function(err, lineSet) {
endHandler(err, lineSet);
});
var files = Array.prototype.slice.apply(arguments);
var endHandler = files.pop();
var lineHandler = files.pop();
var funcs = [];
var self = this;
files.forEach(function(file) {
funcs.push(function(callback) {
fs.readFile(file, 'utf8', function(err, content) {
if (err) {
callback(err);
} else {
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
lineHandler.call(self, lines[i]);
}
callback(null,lines);
}
});
});
});
async.series(funcs, function(err, lineSet) {
endHandler(err, lineSet);
});
};

// get data from specific plugin
nstat.prototype.get = function get() {
var args = Array.prototype.slice.apply(arguments);
var callback = args.pop();
var self = this;
var funcs = {};
for (var i = 0; i < args.length; i++) {
(function() {
var name = args[i];
funcs[name] = function(callback) {
var plugin = self.plugins[name];
if (plugin) {
plugin.get.call(plugin, self, callback);
} else {
callback(new Error('plugin ' + name + ' does not found'));
}
};
})();
}
async.series(funcs, callback);
var args = Array.prototype.slice.apply(arguments);
var callback = args.pop();
var self = this;
var funcs = {};
args.forEach(function(name) {
funcs[name] = function(callback) {
var plugin = self.plugins[name];
if (plugin) {
plugin.get.call(plugin, self, callback);
} else {
callback(new Error('plugin ' + name + ' does not found'));
}
};
});
async.series(funcs, callback);
};

// get data from child processes
nstat.prototype.exec = function exec(path, args, callback) {
var worker = spawn(path, args);
var text = '';
worker.stdin.end();
worker.stdout.setEncoding('utf8');
worker.stdout.on('data', function(data) {
text += data;
});
worker.on('error', function(err) {
callback(err);
});
worker.on('close', function(code) {
if (code === 0) {
callback(null, text);
} else {
callback(new Error(path + ' exist abnormally. code:' + code));
}
});
var worker = spawn(path, args);
var text = '';
worker.stdin.end();
worker.stdout.setEncoding('utf8');
worker.stdout.on('data', function(data) {
text += data;
});
worker.on('error', function(err) {
callback(err);
});
worker.on('close', function(code) {
if (code === 0) {
callback(null, text);
} else {
callback(new Error(path + ' exist abnormally. code:' + code));
}
});
};

nstat.prototype.trim = function(string) {
if (!string) return string;
return string.replace(/^[\s\t]+/,'').replace(/[\s\t]+$/,'');
if (!string) return string;
return string.replace(/^[\s\t]+/,'').replace(/[\s\t]+$/,'');
};

nstat.prototype.split = function(string) {
if (!string) return [];
return string.split(/[\s\t]+/);
}
if (!string) return [];
return string.split(/[\s\t]+/);
};

// add plugin to nstat prototype
function addPlugins(obj) {
for (name in obj) {
nstat.prototype.plugins[name] = obj[name];
}
for (var name in obj) {
nstat.prototype.plugins[name] = obj[name];
}
}

(function() {
var plugindir = path.resolve(__dirname, 'plugins');
var plugins = {};
fs.readdirSync(plugindir)
.filter(function(item) {
return /.+\.js$/.test(item);
})
.forEach(function(item) {
var name = item.substring(0,item.lastIndexOf('.'));
plugins[name] = require('./plugins/'+item);
});
addPlugins(plugins);
var plugindir = path.resolve(__dirname, 'plugins');
var plugins = {};
fs.readdirSync(plugindir)
.filter(function(item) {
return /.+\.js$/.test(item);
})
.forEach(function(item) {
var name = item.substring(0,item.lastIndexOf('.'));
plugins[name] = require('./plugins/'+item);
});
addPlugins(plugins);
})();

exports = module.exports = new nstat;
exports = module.exports = new nstat();
exports.plugin = addPlugins;
46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "node-stat",
"version": "0.0.5",
"description": "the statistics tool like dstat which outputs JSON. This module works only on Linux >= 2.6",
"keywords": ["stat", "dstat", "statistics"],
"main": "index.js",
"bin": {
"nodestat": "cli-wrapper.js"
},
"repository": {
"type": "git",
"url": "git://github.com/suguru/node-stat.git"
},
"bugs": "https://github.com/suguru/node-stat/issues",
"homepage": "https://github.com/suguru/node-stat",
"dependencies": {
"commander": "*",
"async": "*"
},
"author": "Suguru Namura <snamura@gmail.com>",
"contributors": [
"Suguru Namura <snamura@gmail.com>"
],
"license": "MIT"
"name": "node-stat",
"version": "0.0.5",
"description": "the statistics tool like dstat which outputs JSON. This module works only on Linux >= 2.6",
"keywords": ["stat", "dstat", "statistics"],
"main": "index.js",
"bin": {
"nodestat": "cli-wrapper.js"
},
"repository": {
"type": "git",
"url": "git://github.com/suguru/node-stat.git"
},
"bugs": "https://github.com/suguru/node-stat/issues",
"homepage": "https://github.com/suguru/node-stat",
"dependencies": {
"commander": "*",
"async": "*"
},
"author": "Suguru Namura <snamura@gmail.com>",
"contributors": [
"Suguru Namura <snamura@gmail.com>"
],
"license": "MIT"
}
Loading

0 comments on commit 27fc1cd

Please sign in to comment.