|
| 1 | +// dependencies |
| 2 | + |
| 3 | +var os = require('os'); |
| 4 | +var _ = require('underscore'); |
| 5 | +var AWS = require('aws-sdk'); |
| 6 | +var CloudWatch; |
| 7 | + |
| 8 | +var namespace = 'empty'; |
| 9 | + |
| 10 | +/////////////// |
| 11 | +// Prototype // |
| 12 | +/////////////// |
| 13 | + |
| 14 | +function Timer(name, pulse) { |
| 15 | + |
| 16 | + // validate |
| 17 | + if (!(this instanceof Timer)) return new Timer('unnamed', 0); |
| 18 | + if (!_.isString(name)) return console.error('Name must be a string'); |
| 19 | + if (_.isUndefined(pulse)) pulse = 0; |
| 20 | + if (!_.isNumber(pulse) || _.isNaN(pulse) || pulse < 0) |
| 21 | + return console.error('Pulse must be positive integer'); |
| 22 | + |
| 23 | + // set |
| 24 | + this.name = name; |
| 25 | + this.pulse = pulse; |
| 26 | + |
| 27 | + // init |
| 28 | + this.startDate = null; |
| 29 | + this.timerAverageCounter = 0; |
| 30 | + this.timerAverageSum = 0; |
| 31 | + |
| 32 | + return this; |
| 33 | +} |
| 34 | + |
| 35 | +//////////////// |
| 36 | +// CloudWatch // |
| 37 | +//////////////// |
| 38 | + |
| 39 | +/** |
| 40 | + * pretty-prints the result of the timer |
| 41 | + * @param {String} name name of the timer |
| 42 | + * @param {Number} cycletime Cycletime (or average cycletime) in seconds |
| 43 | + * @param {Boolean} isAverageValue defines whether this is an average value |
| 44 | + */ |
| 45 | +function print(name, cycletime, isAverageValue) { |
| 46 | + var log = '⏱ ' + name + ' on ' + os.hostname() + ': '; |
| 47 | + log += isAverageValue ? 'Taking' : 'Took'; |
| 48 | + log += ' ' + cycletime + ' s'; |
| 49 | + log += isAverageValue ? ' on average' : ''; |
| 50 | + |
| 51 | + console.log(log); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * uploads a measurement to AWS CloudWatch |
| 56 | + * @param {String} metric name of the metric |
| 57 | + * @param {Number} value value of the metric |
| 58 | + */ |
| 59 | +function uploadMetricToCloudWatch(metric, value) { |
| 60 | + |
| 61 | + var params = { |
| 62 | + MetricData: [ |
| 63 | + { |
| 64 | + MetricName: metric, |
| 65 | + Timestamp: new Date(), |
| 66 | + Unit: 'Seconds', |
| 67 | + Value: value, |
| 68 | + }, |
| 69 | + ], |
| 70 | + Namespace: 'Cycletime/' + namespace, |
| 71 | + }; |
| 72 | + |
| 73 | + CloudWatch.putMetricData(params, function (err) { |
| 74 | + if (err) console.error('Could not upload to CloudWatch: ', JSON.stringify(err)); |
| 75 | + }); |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +///////////////////// |
| 80 | +// Pulse Functions // |
| 81 | +///////////////////// |
| 82 | + |
| 83 | +/** |
| 84 | + * update temp variables to calculate pulse |
| 85 | + * @param {Number} cycletime the cycletime of a single loop |
| 86 | + */ |
| 87 | +Timer.prototype.updatePulse = function (cycletime) { |
| 88 | + |
| 89 | + this.timerAverageCounter++; |
| 90 | + this.timerAverageSum += cycletime; |
| 91 | + |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * regular functions which calculates overage over pulse interval |
| 96 | + * calls itself with defined pulse timeout |
| 97 | + */ |
| 98 | +Timer.prototype.pushPulseToCloudWatch = function () { |
| 99 | + var _this = this; |
| 100 | + setTimeout(function () { |
| 101 | + |
| 102 | + // calc |
| 103 | + var averageCycletime = _this.timerAverageSum / _this.timerAverageCounter; |
| 104 | + |
| 105 | + // inform |
| 106 | + if (!_.isNaN(averageCycletime)) { |
| 107 | + print(_this.name, averageCycletime, true); |
| 108 | + uploadMetricToCloudWatch(_this.name, averageCycletime); |
| 109 | + } |
| 110 | + |
| 111 | + // reset |
| 112 | + _this.timerAverageCounter = 0; |
| 113 | + _this.timerAverageSum = 0; |
| 114 | + |
| 115 | + // do it again |
| 116 | + _this.pushPulseToCloudWatch(); |
| 117 | + }, this.pulse * 1000); |
| 118 | +}; |
| 119 | + |
| 120 | +////////////////////// |
| 121 | +// Public Functions // |
| 122 | +////////////////////// |
| 123 | + |
| 124 | +/** |
| 125 | + * returns a timer object |
| 126 | + * @param {String} name the name of the timer |
| 127 | + * @param {Number} pulse the pulse in seconds, is optional |
| 128 | + * @return {[type]} [description] |
| 129 | + */ |
| 130 | +function getTimer(name, pulse) { |
| 131 | + |
| 132 | + if (_.isUndefined(pulse)) pulse = 0; |
| 133 | + |
| 134 | + var timer = new Timer(name, pulse); |
| 135 | + if (timer.pulse > 0) timer.pushPulseToCloudWatch(); |
| 136 | + return timer; |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * starts timer and return timer object, is the only function exposed on require |
| 142 | + */ |
| 143 | +Timer.prototype.start = function () { |
| 144 | + |
| 145 | + this.startDate = Date.now(); |
| 146 | + |
| 147 | +}; |
| 148 | + |
| 149 | +/** |
| 150 | + * ends the timer |
| 151 | + * @return {Number} cycletime of timer in seconds |
| 152 | + */ |
| 153 | +Timer.prototype.end = function () { |
| 154 | + if (_.isNull(this.startDate)) return; |
| 155 | + |
| 156 | + // calc |
| 157 | + var cycletime = (Date.now() - this.startDate) / 1000; |
| 158 | + this.startDate = null; |
| 159 | + |
| 160 | + // propagate cycletime via pulse |
| 161 | + if (!_.isNaN(cycletime)) { |
| 162 | + if (this.pulse > 0) this.updatePulse(cycletime); |
| 163 | + if (this.pulse === 0) uploadMetricToCloudWatch(this.name, cycletime); |
| 164 | + } |
| 165 | + |
| 166 | + // inform |
| 167 | + print(this.name, cycletime, false); |
| 168 | + return cycletime; |
| 169 | +}; |
| 170 | + |
| 171 | +//////////// |
| 172 | +// Export // |
| 173 | +//////////// |
| 174 | + |
| 175 | +/** |
| 176 | + * on require AWS credentials are stored and the constructor method is returned |
| 177 | + * @param {String} region AWS Credentials: Region of CloudWatch, defaults to 'eu-central-1' |
| 178 | + * @param {String} key AWS Credentials: Key |
| 179 | + * @param {String} secret AWS Credentials: Token |
| 180 | + * @param {String} ns Namespace for all Metrics |
| 181 | + * @return {Object} object with the constructor method startTimer() |
| 182 | + */ |
| 183 | +module.exports = function (region, key, secret, ns) { |
| 184 | + |
| 185 | + if (_.isNull(region)) region = 'eu-central-1'; |
| 186 | + |
| 187 | + AWS.config.region = region; |
| 188 | + AWS.config.accessKeyId = key; |
| 189 | + AWS.config.secretAccessKey = secret; |
| 190 | + |
| 191 | + CloudWatch = new AWS.CloudWatch(); |
| 192 | + |
| 193 | + namespace = ns; |
| 194 | + |
| 195 | + return { |
| 196 | + getTimer: getTimer, |
| 197 | + }; |
| 198 | + |
| 199 | +}; |
0 commit comments