Commit 885ab5d18d2ced21e8d97b582e9f6f4e3f21eb9a
1 parent
c1b0c854
Exists in
master
add filename
Showing
4 changed files
with
233 additions
and
224 deletions
Show diff stats
index.js
| @@ -8,53 +8,57 @@ var joi = require('joi'); | @@ -8,53 +8,57 @@ var joi = require('joi'); | ||
| 8 | 8 | ||
| 9 | module.exports = { | 9 | module.exports = { |
| 10 | 10 | ||
| 11 | - /* | ||
| 12 | - * opts: { | ||
| 13 | - * log: [dirname, rotation, maxsize] | ||
| 14 | - * stat: [dirname, rotation, maxsize, interval, key] | ||
| 15 | - * alarm: [dirname, rotation, maxsize] | ||
| 16 | - * } | ||
| 17 | - */ | ||
| 18 | - init: function(opts) { | ||
| 19 | - var logstatalarm = undefined; | ||
| 20 | - validate.joi(opts, this.getSchema(), function(err, opts) { | ||
| 21 | - if(err) { | ||
| 22 | - throw new Error(err); | ||
| 23 | - } | ||
| 24 | - }); | ||
| 25 | - return { log: new log(opts.dirname, opts.log), | ||
| 26 | - stat: new stat(opts.dirname, opts.stat, opts.alarm) | ||
| 27 | - }; | ||
| 28 | - }, | 11 | + /* |
| 12 | + * opts: { | ||
| 13 | + * log: [dirname, rotation, maxsize] | ||
| 14 | + * stat: [dirname, rotation, maxsize, interval, key] | ||
| 15 | + * alarm: [dirname, rotation, maxsize] | ||
| 16 | + * } | ||
| 17 | + */ | ||
| 18 | + init: function (opts) { | ||
| 19 | + var logstatalarm = undefined; | ||
| 20 | + validate.joi(opts, this.getSchema(), function (err, opts) { | ||
| 21 | + if (err) { | ||
| 22 | + throw new Error(err); | ||
| 23 | + } | ||
| 24 | + }); | ||
| 25 | + return { | ||
| 26 | + log: new log(opts.dirname, opts.log), | ||
| 27 | + stat: new stat(opts.dirname, opts.stat, opts.alarm) | ||
| 28 | + }; | ||
| 29 | + }, | ||
| 29 | 30 | ||
| 30 | - /* | ||
| 31 | - * Pre-define schema | ||
| 32 | - */ | ||
| 33 | - getSchema: function() { | ||
| 34 | - return joi.object().keys({ | ||
| 35 | - dirname: joi.string().required(), | ||
| 36 | - log: joi.object().keys({ | ||
| 37 | - rotation: joi.number().min(0), | ||
| 38 | - maxsize: joi.number().min(0) | ||
| 39 | - }).required(), | ||
| 40 | - stat: joi.object().keys({ | ||
| 41 | - rotation: joi.number().min(0), | ||
| 42 | - maxsize: joi.number().min(0), | ||
| 43 | - interval: joi.number().min(0), | ||
| 44 | - data: joi.array().items(joi.object().keys({ | ||
| 45 | - key: joi.string().required(), | ||
| 46 | - threshold: joi.number().min(0), | ||
| 47 | - threshold_inv: joi.number().min(0) | ||
| 48 | - })) | ||
| 49 | - }).required(), | ||
| 50 | - alarm: joi.object().keys({ | ||
| 51 | - rotation: joi.number().min(0), | ||
| 52 | - maxsize: joi.number().min(0), | ||
| 53 | - external: joi.array().items(joi.object().keys({ | ||
| 54 | - fn: joi.func().required(), | ||
| 55 | - args: joi.array().items(joi.any()) | ||
| 56 | - })) | ||
| 57 | - }).required() | ||
| 58 | - }); | ||
| 59 | - } | ||
| 60 | -}; | 31 | + /* |
| 32 | + * Pre-define schema | ||
| 33 | + */ | ||
| 34 | + getSchema: function () { | ||
| 35 | + return joi.object().keys({ | ||
| 36 | + dirname: joi.string().required(), | ||
| 37 | + log: joi.object().keys({ | ||
| 38 | + rotation: joi.number().min(0), | ||
| 39 | + maxsize: joi.number().min(0), | ||
| 40 | + filename: joi.string() | ||
| 41 | + }).required(), | ||
| 42 | + stat: joi.object().keys({ | ||
| 43 | + rotation: joi.number().min(0), | ||
| 44 | + maxsize: joi.number().min(0), | ||
| 45 | + filename: joi.string(), | ||
| 46 | + interval: joi.number().min(0), | ||
| 47 | + data: joi.array().items(joi.object().keys({ | ||
| 48 | + key: joi.string().required(), | ||
| 49 | + threshold: joi.number().min(0), | ||
| 50 | + threshold_inv: joi.number().min(0) | ||
| 51 | + })) | ||
| 52 | + }).required(), | ||
| 53 | + alarm: joi.object().keys({ | ||
| 54 | + rotation: joi.number().min(0), | ||
| 55 | + maxsize: joi.number().min(0), | ||
| 56 | + filename: joi.string(), | ||
| 57 | + external: joi.array().items(joi.object().keys({ | ||
| 58 | + fn: joi.func().required(), | ||
| 59 | + args: joi.array().items(joi.any()) | ||
| 60 | + })) | ||
| 61 | + }).required() | ||
| 62 | + }); | ||
| 63 | + } | ||
| 64 | +}; | ||
| 61 | \ No newline at end of file | 65 | \ No newline at end of file |
model/alarm.js
| @@ -10,93 +10,94 @@ var _ = require('lodash'); | @@ -10,93 +10,94 @@ var _ = require('lodash'); | ||
| 10 | * maxsize: interger (byte) | 10 | * maxsize: interger (byte) |
| 11 | */ | 11 | */ |
| 12 | function alarm(dirname, opts) { | 12 | function alarm(dirname, opts) { |
| 13 | - opts = opts || {}; | ||
| 14 | - this.dirname = dirname; | ||
| 15 | - this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 16 | - this.maxsize = opts.maxsize || 20000; | ||
| 17 | - this.external = opts.external || []; | ||
| 18 | - this.currentsize = 0; | ||
| 19 | - this.timestamp = 0; | ||
| 20 | - this.foldername = 'alarm/'; | ||
| 21 | - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | 13 | + opts = opts || {}; |
| 14 | + this.dirname = dirname; | ||
| 15 | + this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 16 | + this.maxsize = opts.maxsize || 20000; | ||
| 17 | + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; | ||
| 18 | + this.external = opts.external || []; | ||
| 19 | + this.currentsize = 0; | ||
| 20 | + this.timestamp = 0; | ||
| 21 | + this.foldername = 'alarm/'; | ||
| 22 | + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | ||
| 22 | }; | 23 | }; |
| 23 | 24 | ||
| 24 | /* | 25 | /* |
| 25 | * parameters: | 26 | * parameters: |
| 26 | * data: any | 27 | * data: any |
| 27 | */ | 28 | */ |
| 28 | -alarm.prototype.appendAlarm = function(data) { | ||
| 29 | - data = this.formatData(data); | ||
| 30 | - this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 31 | - this.request(data); | ||
| 32 | - fs.appendFile(this.getDir(), data, function(err) {}); | 29 | +alarm.prototype.appendAlarm = function (data) { |
| 30 | + data = this.formatData(data); | ||
| 31 | + this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 32 | + this.request(data); | ||
| 33 | + fs.appendFile(this.getDir(), data, function (err) {}); | ||
| 33 | }; | 34 | }; |
| 34 | 35 | ||
| 35 | /* | 36 | /* |
| 36 | * parameters: | 37 | * parameters: |
| 37 | * none | 38 | * none |
| 38 | */ | 39 | */ |
| 39 | -alarm.prototype.getDir = function() { | ||
| 40 | - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 41 | - this.resetCurrentSize(time.unix()); | ||
| 42 | - time = time.format('YYYY-MM-DDTHH-mm-ss'); | ||
| 43 | - var count = this.getCount(); | ||
| 44 | - return `${this.dirname}${this.foldername}${time}_${count}.txt`; | 40 | +alarm.prototype.getDir = function () { |
| 41 | + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 42 | + this.resetCurrentSize(time.unix()); | ||
| 43 | + var count = this.getCount(); | ||
| 44 | + time = time.format(this.filename.replace("${count}", count)); | ||
| 45 | + return `${this.dirname}${this.foldername}${time}`; | ||
| 45 | }; | 46 | }; |
| 46 | 47 | ||
| 47 | /* | 48 | /* |
| 48 | * parameters: | 49 | * parameters: |
| 49 | * time_unix: string | 50 | * time_unix: string |
| 50 | */ | 51 | */ |
| 51 | -alarm.prototype.resetCurrentSize = function(time_unix) { | ||
| 52 | - if(time_unix > this.timestamp) { | ||
| 53 | - this.currentsize = 0 | ||
| 54 | - this.timestamp = time_unix; | ||
| 55 | - } | 52 | +alarm.prototype.resetCurrentSize = function (time_unix) { |
| 53 | + if (time_unix > this.timestamp) { | ||
| 54 | + this.currentsize = 0 | ||
| 55 | + this.timestamp = time_unix; | ||
| 56 | + } | ||
| 56 | }; | 57 | }; |
| 57 | 58 | ||
| 58 | /* | 59 | /* |
| 59 | * parameters: | 60 | * parameters: |
| 60 | * none | 61 | * none |
| 61 | */ | 62 | */ |
| 62 | -alarm.prototype.getCount = function() { | ||
| 63 | - var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 64 | - return ((count * 1e-5).toFixed(5)).split('.')[1]; | 63 | +alarm.prototype.getCount = function () { |
| 64 | + var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 65 | + return ((count * 1e-5).toFixed(5)).split('.')[1]; | ||
| 65 | }; | 66 | }; |
| 66 | 67 | ||
| 67 | /* | 68 | /* |
| 68 | * parameters: | 69 | * parameters: |
| 69 | * data: any | 70 | * data: any |
| 70 | */ | 71 | */ |
| 71 | -alarm.prototype.formatData = function(data) { | ||
| 72 | - var date = moment().toISOString().trim(); | ||
| 73 | - var timestamp = moment().unix(); | ||
| 74 | - data = this._formatObject(data).trim(); | ||
| 75 | - return `${date} ${timestamp} ${data}\r\n`; | 72 | +alarm.prototype.formatData = function (data) { |
| 73 | + var date = moment().toISOString().trim(); | ||
| 74 | + var timestamp = moment().unix(); | ||
| 75 | + data = this._formatObject(data).trim(); | ||
| 76 | + return `${date} ${timestamp} ${data}\r\n`; | ||
| 76 | }; | 77 | }; |
| 77 | 78 | ||
| 78 | /* | 79 | /* |
| 79 | * parameters: | 80 | * parameters: |
| 80 | * data: any | 81 | * data: any |
| 81 | */ | 82 | */ |
| 82 | -alarm.prototype._formatObject = function(data) { | ||
| 83 | - if(_.isObject(data)) { | ||
| 84 | - return JSON.stringify(data); | ||
| 85 | - } | ||
| 86 | - if(_.isNumber(data)) { | ||
| 87 | - return toString(data); | ||
| 88 | - } | ||
| 89 | - return data; | 83 | +alarm.prototype._formatObject = function (data) { |
| 84 | + if (_.isObject(data)) { | ||
| 85 | + return JSON.stringify(data); | ||
| 86 | + } | ||
| 87 | + if (_.isNumber(data)) { | ||
| 88 | + return toString(data); | ||
| 89 | + } | ||
| 90 | + return data; | ||
| 90 | }; | 91 | }; |
| 91 | 92 | ||
| 92 | /* | 93 | /* |
| 93 | * parameters: | 94 | * parameters: |
| 94 | * data: any | 95 | * data: any |
| 95 | */ | 96 | */ |
| 96 | -alarm.prototype.request = function(data) { | ||
| 97 | - _.forEach(this.external, function(external) { | ||
| 98 | - external.fn.apply(this, [data].concat(external.args)); | ||
| 99 | - }); | 97 | +alarm.prototype.request = function (data) { |
| 98 | + _.forEach(this.external, function (external) { | ||
| 99 | + external.fn.apply(this, [data].concat(external.args)); | ||
| 100 | + }); | ||
| 100 | }; | 101 | }; |
| 101 | 102 | ||
| 102 | -module.exports = alarm; | 103 | -module.exports = alarm; |
| 104 | +module.exports = alarm; | ||
| 103 | \ No newline at end of file | 105 | \ No newline at end of file |
model/log.js
| @@ -9,81 +9,82 @@ var _ = require('lodash'); | @@ -9,81 +9,82 @@ var _ = require('lodash'); | ||
| 9 | * rotation: interger (ms) | 9 | * rotation: interger (ms) |
| 10 | */ | 10 | */ |
| 11 | function log(dirname, opts) { | 11 | function log(dirname, opts) { |
| 12 | - opts = opts || {}; | ||
| 13 | - this.dirname = dirname; | ||
| 14 | - this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 15 | - this.maxsize = opts.maxsize || 20000; | ||
| 16 | - this.currentsize = 0; | ||
| 17 | - this.timestamp = 0; | ||
| 18 | - this.foldername = 'log/'; | ||
| 19 | - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | 12 | + opts = opts || {}; |
| 13 | + this.dirname = dirname; | ||
| 14 | + this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 15 | + this.maxsize = opts.maxsize || 20000; | ||
| 16 | + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; | ||
| 17 | + this.currentsize = 0; | ||
| 18 | + this.timestamp = 0; | ||
| 19 | + this.foldername = 'log/'; | ||
| 20 | + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | ||
| 20 | }; | 21 | }; |
| 21 | 22 | ||
| 22 | /* | 23 | /* |
| 23 | * parameters: | 24 | * parameters: |
| 24 | * data: any | 25 | * data: any |
| 25 | */ | 26 | */ |
| 26 | -log.prototype.append = function(data) { | ||
| 27 | - data = this.formatData(data); | ||
| 28 | - this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 29 | - fs.appendFile(this.getDir(), data, function(err) {}); | 27 | +log.prototype.append = function (data) { |
| 28 | + data = this.formatData(data); | ||
| 29 | + this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 30 | + fs.appendFile(this.getDir(), data, function (err) {}); | ||
| 30 | }; | 31 | }; |
| 31 | 32 | ||
| 32 | /* | 33 | /* |
| 33 | * parameters: | 34 | * parameters: |
| 34 | * none | 35 | * none |
| 35 | */ | 36 | */ |
| 36 | -log.prototype.getDir = function() { | ||
| 37 | - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 38 | - this.resetCurrentSize(time.unix()); | ||
| 39 | - time = time.format('YYYY-MM-DDTHH-mm-ss'); | ||
| 40 | - var count = this.getCount(); | ||
| 41 | - return `${this.dirname}${this.foldername}${time}_${count}.txt`; | 37 | +log.prototype.getDir = function () { |
| 38 | + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 39 | + this.resetCurrentSize(time.unix()); | ||
| 40 | + var count = this.getCount(); | ||
| 41 | + time = time.format(this.filename.replace("${count}", count)); | ||
| 42 | + return `${this.dirname}${this.foldername}${time}`; | ||
| 42 | }; | 43 | }; |
| 43 | 44 | ||
| 44 | /* | 45 | /* |
| 45 | * parameters: | 46 | * parameters: |
| 46 | * time_unix: string | 47 | * time_unix: string |
| 47 | */ | 48 | */ |
| 48 | -log.prototype.resetCurrentSize = function(time_unix) { | ||
| 49 | - if(time_unix > this.timestamp) { | ||
| 50 | - this.currentsize = 0 | ||
| 51 | - this.timestamp = time_unix; | ||
| 52 | - } | 49 | +log.prototype.resetCurrentSize = function (time_unix) { |
| 50 | + if (time_unix > this.timestamp) { | ||
| 51 | + this.currentsize = 0 | ||
| 52 | + this.timestamp = time_unix; | ||
| 53 | + } | ||
| 53 | }; | 54 | }; |
| 54 | 55 | ||
| 55 | /* | 56 | /* |
| 56 | * parameters: | 57 | * parameters: |
| 57 | * none | 58 | * none |
| 58 | */ | 59 | */ |
| 59 | -log.prototype.getCount = function() { | ||
| 60 | - var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 61 | - return ((count * 1e-5).toFixed(5)).split('.')[1]; | 60 | +log.prototype.getCount = function () { |
| 61 | + var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 62 | + return ((count * 1e-5).toFixed(5)).split('.')[1]; | ||
| 62 | }; | 63 | }; |
| 63 | 64 | ||
| 64 | /* | 65 | /* |
| 65 | * parameters: | 66 | * parameters: |
| 66 | * data: any | 67 | * data: any |
| 67 | */ | 68 | */ |
| 68 | -log.prototype.formatData = function(data) { | ||
| 69 | - var date = moment().toISOString().trim(); | ||
| 70 | - var timestamp = moment().unix(); | ||
| 71 | - data = this._formatObject(data).trim(); | ||
| 72 | - return `${date} ${timestamp} ${data}\r\n`; | 69 | +log.prototype.formatData = function (data) { |
| 70 | + var date = moment().toISOString().trim(); | ||
| 71 | + var timestamp = moment().unix(); | ||
| 72 | + data = this._formatObject(data).trim(); | ||
| 73 | + return `${date} ${timestamp} ${data}\r\n`; | ||
| 73 | }; | 74 | }; |
| 74 | 75 | ||
| 75 | /* | 76 | /* |
| 76 | * parameters: | 77 | * parameters: |
| 77 | * data: any | 78 | * data: any |
| 78 | */ | 79 | */ |
| 79 | -log.prototype._formatObject = function(data) { | ||
| 80 | - if(_.isObject(data)) { | ||
| 81 | - return JSON.stringify(data); | ||
| 82 | - } | ||
| 83 | - if(_.isNumber(data)) { | ||
| 84 | - return toString(data); | ||
| 85 | - } | ||
| 86 | - return data; | 80 | +log.prototype._formatObject = function (data) { |
| 81 | + if (_.isObject(data)) { | ||
| 82 | + return JSON.stringify(data); | ||
| 83 | + } | ||
| 84 | + if (_.isNumber(data)) { | ||
| 85 | + return toString(data); | ||
| 86 | + } | ||
| 87 | + return data; | ||
| 87 | }; | 88 | }; |
| 88 | 89 | ||
| 89 | -module.exports = log; | 90 | -module.exports = log; |
| 91 | +module.exports = log; | ||
| 90 | \ No newline at end of file | 92 | \ No newline at end of file |
model/stat.js
| @@ -14,31 +14,32 @@ var alarm = require('./alarm'); | @@ -14,31 +14,32 @@ var alarm = require('./alarm'); | ||
| 14 | * alarm: object (alarm object) | 14 | * alarm: object (alarm object) |
| 15 | */ | 15 | */ |
| 16 | function stat(dirname, opts, alarmData) { | 16 | function stat(dirname, opts, alarmData) { |
| 17 | - opts = opts || {}; | ||
| 18 | - alarmData = alarmData || {}; | ||
| 19 | - this.dirname = dirname; | ||
| 20 | - this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 21 | - this.maxsize = opts.maxsize || 20000; | ||
| 22 | - this.currentsize = 0; | ||
| 23 | - this.timestamp = 0; | ||
| 24 | - this.foldername = 'stat/'; | ||
| 25 | - this.intervalId = undefined; | ||
| 26 | - this.interval = opts.interval || 60 * 1000; | ||
| 27 | - this.data = transformKeys(opts.data); | ||
| 28 | - this.rules = opts.data; | ||
| 29 | - this.alarm = new alarm(dirname, alarmData); | ||
| 30 | - helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | 17 | + opts = opts || {}; |
| 18 | + alarmData = alarmData || {}; | ||
| 19 | + this.dirname = dirname; | ||
| 20 | + this.rotation = opts.rotation || 15 * 60 * 1000; | ||
| 21 | + this.maxsize = opts.maxsize || 20000; | ||
| 22 | + this.filename = opts.filename || '[log]YYYY-MM-DDTHH-mm-ss[_${count}.txt]'; | ||
| 23 | + this.currentsize = 0; | ||
| 24 | + this.timestamp = 0; | ||
| 25 | + this.foldername = 'stat/'; | ||
| 26 | + this.intervalId = undefined; | ||
| 27 | + this.interval = opts.interval || 60 * 1000; | ||
| 28 | + this.data = transformKeys(opts.data); | ||
| 29 | + this.rules = opts.data; | ||
| 30 | + this.alarm = new alarm(dirname, alarmData); | ||
| 31 | + helper.mkdirIfNotExist(`${this.dirname}/${this.foldername}`); | ||
| 31 | }; | 32 | }; |
| 32 | 33 | ||
| 33 | function transformKeys(data) { | 34 | function transformKeys(data) { |
| 34 | - keys = _.map(data, function(obj) { | ||
| 35 | - var fakey = {}; | ||
| 36 | - fakey[obj.key] = 0; | ||
| 37 | - return fakey; | ||
| 38 | - }); | ||
| 39 | - return _.reduce(keys, function(new_obj, obj) { | ||
| 40 | - return _.merge(new_obj, obj); | ||
| 41 | - }, {}); | 35 | + keys = _.map(data, function (obj) { |
| 36 | + var fakey = {}; | ||
| 37 | + fakey[obj.key] = 0; | ||
| 38 | + return fakey; | ||
| 39 | + }); | ||
| 40 | + return _.reduce(keys, function (new_obj, obj) { | ||
| 41 | + return _.merge(new_obj, obj); | ||
| 42 | + }, {}); | ||
| 42 | 43 | ||
| 43 | }; | 44 | }; |
| 44 | 45 | ||
| @@ -48,132 +49,134 @@ function transformKeys(data) { | @@ -48,132 +49,134 @@ function transformKeys(data) { | ||
| 48 | * rules: [obj] | 49 | * rules: [obj] |
| 49 | */ | 50 | */ |
| 50 | function alarmDataOutOfThreshold(data, rules) { | 51 | function alarmDataOutOfThreshold(data, rules) { |
| 51 | - _alarm = []; | ||
| 52 | - _.forEach(data, function(v, k) { | ||
| 53 | - rule = _.find(rules, ['key', k]); | ||
| 54 | - var alarmObj = {}; | ||
| 55 | - if(v < rule.threshold_inv) { | ||
| 56 | - _alarm.push({key: k, | ||
| 57 | - count: v, | ||
| 58 | - threshold_inv: rule.threshold_inv, | ||
| 59 | - message: `${k} count is below inverted threshold`}); | ||
| 60 | - } | ||
| 61 | - if(v > rule.threshold) { | ||
| 62 | - _alarm.push({key: k, | ||
| 63 | - count: v, | ||
| 64 | - threshold: | ||
| 65 | - rule.threshold, | ||
| 66 | - message: `${k} count is above threshold`}); | ||
| 67 | - } | ||
| 68 | - }); | ||
| 69 | - return _alarm | 52 | + _alarm = []; |
| 53 | + _.forEach(data, function (v, k) { | ||
| 54 | + rule = _.find(rules, ['key', k]); | ||
| 55 | + var alarmObj = {}; | ||
| 56 | + if (v < rule.threshold_inv) { | ||
| 57 | + _alarm.push({ | ||
| 58 | + key: k, | ||
| 59 | + count: v, | ||
| 60 | + threshold_inv: rule.threshold_inv, | ||
| 61 | + message: `${k} count is below inverted threshold` | ||
| 62 | + }); | ||
| 63 | + } | ||
| 64 | + if (v > rule.threshold) { | ||
| 65 | + _alarm.push({ | ||
| 66 | + key: k, | ||
| 67 | + count: v, | ||
| 68 | + threshold: rule.threshold, | ||
| 69 | + message: `${k} count is above threshold` | ||
| 70 | + }); | ||
| 71 | + } | ||
| 72 | + }); | ||
| 73 | + return _alarm | ||
| 70 | }; | 74 | }; |
| 71 | 75 | ||
| 72 | /* | 76 | /* |
| 73 | * parameters: | 77 | * parameters: |
| 74 | * data: any | 78 | * data: any |
| 75 | */ | 79 | */ |
| 76 | -stat.prototype.appendStat = function(data) { | ||
| 77 | - data = this.formatData(data); | ||
| 78 | - this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 79 | - fs.appendFile(this.getDir(), data, function(err) {}); | 80 | +stat.prototype.appendStat = function (data) { |
| 81 | + data = this.formatData(data); | ||
| 82 | + this.currentsize = this.currentsize + helper.getLengthOfContent(data); | ||
| 83 | + fs.appendFile(this.getDir(), data, function (err) {}); | ||
| 80 | }; | 84 | }; |
| 81 | 85 | ||
| 82 | /* | 86 | /* |
| 83 | * parameters: | 87 | * parameters: |
| 84 | * none | 88 | * none |
| 85 | */ | 89 | */ |
| 86 | -stat.prototype.start = function() { | ||
| 87 | - var self = this; | ||
| 88 | - this.intervalId = setInterval(function(){ | ||
| 89 | - var alarmData = alarmDataOutOfThreshold(self.data, self.rules); | ||
| 90 | - if(!_.isEmpty(alarmData)) { | ||
| 91 | - self.alarm.appendAlarm(alarmData); | ||
| 92 | - } | ||
| 93 | - self.appendStat(self.data); | ||
| 94 | - self.reset(); | ||
| 95 | - }, self.interval); | 90 | +stat.prototype.start = function () { |
| 91 | + var self = this; | ||
| 92 | + this.intervalId = setInterval(function () { | ||
| 93 | + var alarmData = alarmDataOutOfThreshold(self.data, self.rules); | ||
| 94 | + if (!_.isEmpty(alarmData)) { | ||
| 95 | + self.alarm.appendAlarm(alarmData); | ||
| 96 | + } | ||
| 97 | + self.appendStat(self.data); | ||
| 98 | + self.reset(); | ||
| 99 | + }, self.interval); | ||
| 96 | }; | 100 | }; |
| 97 | 101 | ||
| 98 | /* | 102 | /* |
| 99 | * parameters: | 103 | * parameters: |
| 100 | * none | 104 | * none |
| 101 | */ | 105 | */ |
| 102 | -stat.prototype.stop = function() { | ||
| 103 | - clearInterval(this.intervalId); | 106 | +stat.prototype.stop = function () { |
| 107 | + clearInterval(this.intervalId); | ||
| 104 | }; | 108 | }; |
| 105 | 109 | ||
| 106 | /* | 110 | /* |
| 107 | * parameters: | 111 | * parameters: |
| 108 | * data: string | 112 | * data: string |
| 109 | */ | 113 | */ |
| 110 | -stat.prototype.increment = function(data) { | ||
| 111 | - if(_.has(this.data, data)) { | ||
| 112 | - this.data[data]++; | ||
| 113 | - } | ||
| 114 | - else this.data[data] = 1; | 114 | +stat.prototype.increment = function (data) { |
| 115 | + if (_.has(this.data, data)) { | ||
| 116 | + this.data[data]++; | ||
| 117 | + } else this.data[data] = 1; | ||
| 115 | }; | 118 | }; |
| 116 | 119 | ||
| 117 | /* | 120 | /* |
| 118 | * parameters: | 121 | * parameters: |
| 119 | * none | 122 | * none |
| 120 | */ | 123 | */ |
| 121 | -stat.prototype.reset = function() { | ||
| 122 | - var self = this; | ||
| 123 | - _.forEach(this.data, function(v, k) { | ||
| 124 | - self.data[k] = 0; | ||
| 125 | - }); | 124 | +stat.prototype.reset = function () { |
| 125 | + var self = this; | ||
| 126 | + _.forEach(this.data, function (v, k) { | ||
| 127 | + self.data[k] = 0; | ||
| 128 | + }); | ||
| 126 | }; | 129 | }; |
| 127 | 130 | ||
| 128 | /* | 131 | /* |
| 129 | * parameters: | 132 | * parameters: |
| 130 | * none | 133 | * none |
| 131 | */ | 134 | */ |
| 132 | -stat.prototype.getDir = function() { | ||
| 133 | - var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 134 | - this.resetCurrentSize(time.unix()); | ||
| 135 | - time = time.format('YYYY-MM-DDTHH-mm-ss'); | ||
| 136 | - var count = this.getCount(); | ||
| 137 | - return `${this.dirname}${this.foldername}${time}_${count}.txt`; | 135 | +stat.prototype.getDir = function () { |
| 136 | + var time = moment(Math.floor((+moment()) / this.rotation) * this.rotation); | ||
| 137 | + this.resetCurrentSize(time.unix()); | ||
| 138 | + var count = this.getCount(); | ||
| 139 | + time = time.format(this.filename.replace("${count}", count)); | ||
| 140 | + return `${this.dirname}${this.foldername}${time}`; | ||
| 138 | }; | 141 | }; |
| 139 | 142 | ||
| 140 | /* | 143 | /* |
| 141 | * parameters: | 144 | * parameters: |
| 142 | * time_unix: string | 145 | * time_unix: string |
| 143 | */ | 146 | */ |
| 144 | -stat.prototype.resetCurrentSize = function(time_unix) { | ||
| 145 | - if(time_unix > this.timestamp) { | ||
| 146 | - this.currentsize = 0 | ||
| 147 | - this.timestamp = time_unix; | ||
| 148 | - } | 147 | +stat.prototype.resetCurrentSize = function (time_unix) { |
| 148 | + if (time_unix > this.timestamp) { | ||
| 149 | + this.currentsize = 0 | ||
| 150 | + this.timestamp = time_unix; | ||
| 151 | + } | ||
| 149 | }; | 152 | }; |
| 150 | 153 | ||
| 151 | /* | 154 | /* |
| 152 | * parameters: | 155 | * parameters: |
| 153 | * none | 156 | * none |
| 154 | */ | 157 | */ |
| 155 | -stat.prototype.getCount = function() { | ||
| 156 | - var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 157 | - return ((count * 1e-5).toFixed(5)).split('.')[1]; | 158 | +stat.prototype.getCount = function () { |
| 159 | + var count = Math.floor((this.currentsize / this.maxsize) + 1); | ||
| 160 | + return ((count * 1e-5).toFixed(5)).split('.')[1]; | ||
| 158 | }; | 161 | }; |
| 159 | 162 | ||
| 160 | /* | 163 | /* |
| 161 | * parameters: | 164 | * parameters: |
| 162 | * data: any | 165 | * data: any |
| 163 | */ | 166 | */ |
| 164 | -stat.prototype.formatData = function(data) { | ||
| 165 | - var date = moment().toISOString().trim(); | ||
| 166 | - var timestamp = moment().unix(); | ||
| 167 | - data = this._formatObject(data).trim(); | ||
| 168 | - return `${date} ${timestamp} ${data}\r\n`; | 167 | +stat.prototype.formatData = function (data) { |
| 168 | + var date = moment().toISOString().trim(); | ||
| 169 | + var timestamp = moment().unix(); | ||
| 170 | + data = this._formatObject(data).trim(); | ||
| 171 | + return `${date} ${timestamp} ${data}\r\n`; | ||
| 169 | }; | 172 | }; |
| 170 | 173 | ||
| 171 | /* | 174 | /* |
| 172 | * parameters: | 175 | * parameters: |
| 173 | * data: any | 176 | * data: any |
| 174 | */ | 177 | */ |
| 175 | -stat.prototype._formatObject = function(data) { | ||
| 176 | - return JSON.stringify(data); | 178 | +stat.prototype._formatObject = function (data) { |
| 179 | + return JSON.stringify(data); | ||
| 177 | }; | 180 | }; |
| 178 | 181 | ||
| 179 | -module.exports = stat; | 182 | -module.exports = stat; |
| 183 | +module.exports = stat; | ||
| 180 | \ No newline at end of file | 184 | \ No newline at end of file |