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