diff --git a/index.js b/index.js index cac80fb..cfcd7dc 100644 --- a/index.js +++ b/index.js @@ -8,53 +8,57 @@ var joi = require('joi'); module.exports = { - /* - * opts: { - * log: [dirname, rotation, maxsize] - * stat: [dirname, rotation, maxsize, interval, key] - * alarm: [dirname, rotation, maxsize] - * } - */ - init: function(opts) { - var logstatalarm = undefined; - validate.joi(opts, this.getSchema(), function(err, opts) { - if(err) { - throw new Error(err); - } - }); - return { log: new log(opts.dirname, opts.log), - stat: new stat(opts.dirname, opts.stat, opts.alarm) - }; - }, + /* + * opts: { + * log: [dirname, rotation, maxsize] + * stat: [dirname, rotation, maxsize, interval, key] + * alarm: [dirname, rotation, maxsize] + * } + */ + init: function (opts) { + var logstatalarm = undefined; + validate.joi(opts, this.getSchema(), function (err, opts) { + if (err) { + throw new Error(err); + } + }); + return { + log: new log(opts.dirname, opts.log), + stat: new stat(opts.dirname, opts.stat, opts.alarm) + }; + }, - /* - * Pre-define schema - */ - getSchema: function() { - return joi.object().keys({ - dirname: joi.string().required(), - log: joi.object().keys({ - rotation: joi.number().min(0), - maxsize: joi.number().min(0) - }).required(), - stat: joi.object().keys({ - rotation: joi.number().min(0), - maxsize: joi.number().min(0), - interval: joi.number().min(0), - data: joi.array().items(joi.object().keys({ - key: joi.string().required(), - threshold: joi.number().min(0), - threshold_inv: joi.number().min(0) - })) - }).required(), - alarm: joi.object().keys({ - rotation: joi.number().min(0), - maxsize: joi.number().min(0), - external: joi.array().items(joi.object().keys({ - fn: joi.func().required(), - args: joi.array().items(joi.any()) - })) - }).required() - }); - } -}; + /* + * Pre-define schema + */ + getSchema: function () { + return joi.object().keys({ + dirname: joi.string().required(), + log: joi.object().keys({ + rotation: joi.number().min(0), + maxsize: joi.number().min(0), + filename: joi.string() + }).required(), + stat: joi.object().keys({ + rotation: joi.number().min(0), + maxsize: joi.number().min(0), + filename: joi.string(), + interval: joi.number().min(0), + data: joi.array().items(joi.object().keys({ + key: joi.string().required(), + threshold: joi.number().min(0), + threshold_inv: joi.number().min(0) + })) + }).required(), + alarm: joi.object().keys({ + rotation: joi.number().min(0), + maxsize: joi.number().min(0), + filename: joi.string(), + external: joi.array().items(joi.object().keys({ + fn: joi.func().required(), + args: joi.array().items(joi.any()) + })) + }).required() + }); + } +}; \ No newline at end of file diff --git a/model/alarm.js b/model/alarm.js index 6cb16ce..89e5b38 100644 --- a/model/alarm.js +++ b/model/alarm.js @@ -10,93 +10,94 @@ var _ = require('lodash'); * maxsize: interger (byte) */ function alarm(dirname, opts) { - opts = opts || {}; - this.dirname = dirname; - this.rotation = opts.rotation || 15 * 60 * 1000; - this.maxsize = opts.maxsize || 20000; - this.external = opts.external || []; - this.currentsize = 0; - this.timestamp = 0; - this.foldername = 'alarm/'; - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); + opts = opts || {}; + this.dirname = dirname; + this.rotation = opts.rotation || 15 * 60 * 1000; + this.maxsize = opts.maxsize || 20000; + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; + this.external = opts.external || []; + this.currentsize = 0; + this.timestamp = 0; + this.foldername = 'alarm/'; + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); }; /* * parameters: * data: any */ -alarm.prototype.appendAlarm = function(data) { - data = this.formatData(data); - this.currentsize = this.currentsize + helper.getLengthOfContent(data); - this.request(data); - fs.appendFile(this.getDir(), data, function(err) {}); +alarm.prototype.appendAlarm = function (data) { + data = this.formatData(data); + this.currentsize = this.currentsize + helper.getLengthOfContent(data); + this.request(data); + fs.appendFile(this.getDir(), data, function (err) {}); }; /* * parameters: * none */ -alarm.prototype.getDir = function() { - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); - this.resetCurrentSize(time.unix()); - time = time.format('YYYY-MM-DDTHH-mm-ss'); - var count = this.getCount(); - return `${this.dirname}${this.foldername}${time}_${count}.txt`; +alarm.prototype.getDir = function () { + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); + this.resetCurrentSize(time.unix()); + var count = this.getCount(); + time = time.format(this.filename.replace("${count}", count)); + return `${this.dirname}${this.foldername}${time}`; }; /* * parameters: * time_unix: string */ -alarm.prototype.resetCurrentSize = function(time_unix) { - if(time_unix > this.timestamp) { - this.currentsize = 0 - this.timestamp = time_unix; - } +alarm.prototype.resetCurrentSize = function (time_unix) { + if (time_unix > this.timestamp) { + this.currentsize = 0 + this.timestamp = time_unix; + } }; /* * parameters: * none */ -alarm.prototype.getCount = function() { - var count = Math.floor((this.currentsize / this.maxsize) + 1); - return ((count * 1e-5).toFixed(5)).split('.')[1]; +alarm.prototype.getCount = function () { + var count = Math.floor((this.currentsize / this.maxsize) + 1); + return ((count * 1e-5).toFixed(5)).split('.')[1]; }; /* * parameters: * data: any */ -alarm.prototype.formatData = function(data) { - var date = moment().toISOString().trim(); - var timestamp = moment().unix(); - data = this._formatObject(data).trim(); - return `${date} ${timestamp} ${data}\r\n`; +alarm.prototype.formatData = function (data) { + var date = moment().toISOString().trim(); + var timestamp = moment().unix(); + data = this._formatObject(data).trim(); + return `${date} ${timestamp} ${data}\r\n`; }; /* * parameters: * data: any */ -alarm.prototype._formatObject = function(data) { - if(_.isObject(data)) { - return JSON.stringify(data); - } - if(_.isNumber(data)) { - return toString(data); - } - return data; +alarm.prototype._formatObject = function (data) { + if (_.isObject(data)) { + return JSON.stringify(data); + } + if (_.isNumber(data)) { + return toString(data); + } + return data; }; /* * parameters: * data: any */ -alarm.prototype.request = function(data) { - _.forEach(this.external, function(external) { - external.fn.apply(this, [data].concat(external.args)); - }); +alarm.prototype.request = function (data) { + _.forEach(this.external, function (external) { + external.fn.apply(this, [data].concat(external.args)); + }); }; -module.exports = alarm; +module.exports = alarm; \ No newline at end of file diff --git a/model/log.js b/model/log.js index df9b946..ce2164c 100644 --- a/model/log.js +++ b/model/log.js @@ -9,81 +9,82 @@ var _ = require('lodash'); * rotation: interger (ms) */ function log(dirname, opts) { - opts = opts || {}; - this.dirname = dirname; - this.rotation = opts.rotation || 15 * 60 * 1000; - this.maxsize = opts.maxsize || 20000; - this.currentsize = 0; - this.timestamp = 0; - this.foldername = 'log/'; - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); + opts = opts || {}; + this.dirname = dirname; + this.rotation = opts.rotation || 15 * 60 * 1000; + this.maxsize = opts.maxsize || 20000; + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; + this.currentsize = 0; + this.timestamp = 0; + this.foldername = 'log/'; + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); }; /* * parameters: * data: any */ -log.prototype.append = function(data) { - data = this.formatData(data); - this.currentsize = this.currentsize + helper.getLengthOfContent(data); - fs.appendFile(this.getDir(), data, function(err) {}); +log.prototype.append = function (data) { + data = this.formatData(data); + this.currentsize = this.currentsize + helper.getLengthOfContent(data); + fs.appendFile(this.getDir(), data, function (err) {}); }; /* * parameters: * none */ -log.prototype.getDir = function() { - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); - this.resetCurrentSize(time.unix()); - time = time.format('YYYY-MM-DDTHH-mm-ss'); - var count = this.getCount(); - return `${this.dirname}${this.foldername}${time}_${count}.txt`; +log.prototype.getDir = function () { + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); + this.resetCurrentSize(time.unix()); + var count = this.getCount(); + time = time.format(this.filename.replace("${count}", count)); + return `${this.dirname}${this.foldername}${time}`; }; /* * parameters: * time_unix: string */ -log.prototype.resetCurrentSize = function(time_unix) { - if(time_unix > this.timestamp) { - this.currentsize = 0 - this.timestamp = time_unix; - } +log.prototype.resetCurrentSize = function (time_unix) { + if (time_unix > this.timestamp) { + this.currentsize = 0 + this.timestamp = time_unix; + } }; /* * parameters: * none */ -log.prototype.getCount = function() { - var count = Math.floor((this.currentsize / this.maxsize) + 1); - return ((count * 1e-5).toFixed(5)).split('.')[1]; +log.prototype.getCount = function () { + var count = Math.floor((this.currentsize / this.maxsize) + 1); + return ((count * 1e-5).toFixed(5)).split('.')[1]; }; /* * parameters: * data: any */ -log.prototype.formatData = function(data) { - var date = moment().toISOString().trim(); - var timestamp = moment().unix(); - data = this._formatObject(data).trim(); - return `${date} ${timestamp} ${data}\r\n`; +log.prototype.formatData = function (data) { + var date = moment().toISOString().trim(); + var timestamp = moment().unix(); + data = this._formatObject(data).trim(); + return `${date} ${timestamp} ${data}\r\n`; }; /* * parameters: * data: any */ -log.prototype._formatObject = function(data) { - if(_.isObject(data)) { - return JSON.stringify(data); - } - if(_.isNumber(data)) { - return toString(data); - } - return data; +log.prototype._formatObject = function (data) { + if (_.isObject(data)) { + return JSON.stringify(data); + } + if (_.isNumber(data)) { + return toString(data); + } + return data; }; -module.exports = log; +module.exports = log; \ No newline at end of file diff --git a/model/stat.js b/model/stat.js index 0d34854..0428ced 100644 --- a/model/stat.js +++ b/model/stat.js @@ -14,31 +14,32 @@ var alarm = require('./alarm'); * alarm: object (alarm object) */ function stat(dirname, opts, alarmData) { - opts = opts || {}; - alarmData = alarmData || {}; - this.dirname = dirname; - this.rotation = opts.rotation || 15 * 60 * 1000; - this.maxsize = opts.maxsize || 20000; - this.currentsize = 0; - this.timestamp = 0; - this.foldername = 'stat/'; - this.intervalId = undefined; - this.interval = opts.interval || 60 * 1000; - this.data = transformKeys(opts.data); - this.rules = opts.data; - this.alarm = new alarm(dirname, alarmData); - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); + opts = opts || {}; + alarmData = alarmData || {}; + this.dirname = dirname; + this.rotation = opts.rotation || 15 * 60 * 1000; + this.maxsize = opts.maxsize || 20000; + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; + this.currentsize = 0; + this.timestamp = 0; + this.foldername = 'stat/'; + this.intervalId = undefined; + this.interval = opts.interval || 60 * 1000; + this.data = transformKeys(opts.data); + this.rules = opts.data; + this.alarm = new alarm(dirname, alarmData); + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); }; function transformKeys(data) { - keys = _.map(data, function(obj) { - var fakey = {}; - fakey[obj.key] = 0; - return fakey; - }); - return _.reduce(keys, function(new_obj, obj) { - return _.merge(new_obj, obj); - }, {}); + keys = _.map(data, function (obj) { + var fakey = {}; + fakey[obj.key] = 0; + return fakey; + }); + return _.reduce(keys, function (new_obj, obj) { + return _.merge(new_obj, obj); + }, {}); }; @@ -48,132 +49,134 @@ function transformKeys(data) { * rules: [obj] */ function alarmDataOutOfThreshold(data, rules) { - _alarm = []; - _.forEach(data, function(v, k) { - rule = _.find(rules, ['key', k]); - var alarmObj = {}; - if(v < rule.threshold_inv) { - _alarm.push({key: k, - count: v, - threshold_inv: rule.threshold_inv, - message: `${k} count is below inverted threshold`}); - } - if(v > rule.threshold) { - _alarm.push({key: k, - count: v, - threshold: - rule.threshold, - message: `${k} count is above threshold`}); - } - }); - return _alarm + _alarm = []; + _.forEach(data, function (v, k) { + rule = _.find(rules, ['key', k]); + var alarmObj = {}; + if (v < rule.threshold_inv) { + _alarm.push({ + key: k, + count: v, + threshold_inv: rule.threshold_inv, + message: `${k} count is below inverted threshold` + }); + } + if (v > rule.threshold) { + _alarm.push({ + key: k, + count: v, + threshold: rule.threshold, + message: `${k} count is above threshold` + }); + } + }); + return _alarm }; /* * parameters: * data: any */ -stat.prototype.appendStat = function(data) { - data = this.formatData(data); - this.currentsize = this.currentsize + helper.getLengthOfContent(data); - fs.appendFile(this.getDir(), data, function(err) {}); +stat.prototype.appendStat = function (data) { + data = this.formatData(data); + this.currentsize = this.currentsize + helper.getLengthOfContent(data); + fs.appendFile(this.getDir(), data, function (err) {}); }; /* * parameters: * none */ -stat.prototype.start = function() { - var self = this; - this.intervalId = setInterval(function(){ - var alarmData = alarmDataOutOfThreshold(self.data, self.rules); - if(!_.isEmpty(alarmData)) { - self.alarm.appendAlarm(alarmData); - } - self.appendStat(self.data); - self.reset(); - }, self.interval); +stat.prototype.start = function () { + var self = this; + this.intervalId = setInterval(function () { + var alarmData = alarmDataOutOfThreshold(self.data, self.rules); + if (!_.isEmpty(alarmData)) { + self.alarm.appendAlarm(alarmData); + } + self.appendStat(self.data); + self.reset(); + }, self.interval); }; /* * parameters: * none */ -stat.prototype.stop = function() { - clearInterval(this.intervalId); +stat.prototype.stop = function () { + clearInterval(this.intervalId); }; /* * parameters: * data: string */ -stat.prototype.increment = function(data) { - if(_.has(this.data, data)) { - this.data[data]++; - } - else this.data[data] = 1; +stat.prototype.increment = function (data) { + if (_.has(this.data, data)) { + this.data[data]++; + } else this.data[data] = 1; }; /* * parameters: * none */ -stat.prototype.reset = function() { - var self = this; - _.forEach(this.data, function(v, k) { - self.data[k] = 0; - }); +stat.prototype.reset = function () { + var self = this; + _.forEach(this.data, function (v, k) { + self.data[k] = 0; + }); }; /* * parameters: * none */ -stat.prototype.getDir = function() { - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); - this.resetCurrentSize(time.unix()); - time = time.format('YYYY-MM-DDTHH-mm-ss'); - var count = this.getCount(); - return `${this.dirname}${this.foldername}${time}_${count}.txt`; +stat.prototype.getDir = function () { + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); + this.resetCurrentSize(time.unix()); + var count = this.getCount(); + time = time.format(this.filename.replace("${count}", count)); + return `${this.dirname}${this.foldername}${time}`; }; /* * parameters: * time_unix: string */ -stat.prototype.resetCurrentSize = function(time_unix) { - if(time_unix > this.timestamp) { - this.currentsize = 0 - this.timestamp = time_unix; - } +stat.prototype.resetCurrentSize = function (time_unix) { + if (time_unix > this.timestamp) { + this.currentsize = 0 + this.timestamp = time_unix; + } }; /* * parameters: * none */ -stat.prototype.getCount = function() { - var count = Math.floor((this.currentsize / this.maxsize) + 1); - return ((count * 1e-5).toFixed(5)).split('.')[1]; +stat.prototype.getCount = function () { + var count = Math.floor((this.currentsize / this.maxsize) + 1); + return ((count * 1e-5).toFixed(5)).split('.')[1]; }; /* * parameters: * data: any */ -stat.prototype.formatData = function(data) { - var date = moment().toISOString().trim(); - var timestamp = moment().unix(); - data = this._formatObject(data).trim(); - return `${date} ${timestamp} ${data}\r\n`; +stat.prototype.formatData = function (data) { + var date = moment().toISOString().trim(); + var timestamp = moment().unix(); + data = this._formatObject(data).trim(); + return `${date} ${timestamp} ${data}\r\n`; }; /* * parameters: * data: any */ -stat.prototype._formatObject = function(data) { - return JSON.stringify(data); +stat.prototype._formatObject = function (data) { + return JSON.stringify(data); }; -module.exports = stat; +module.exports = stat; \ No newline at end of file -- libgit2 0.21.2