-
Notifications
You must be signed in to change notification settings - Fork 35
/
aggregator.js
62 lines (57 loc) · 1.84 KB
/
aggregator.js
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
/*
* @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
*
* @licence SPM for Docker is free-to-use, proprietary software.
* THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
* This source code may not be copied, reverse engineered, or altered for any purpose.
* This source code is to be used exclusively by users and customers of Sematext.
* Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
*/
'use strict'
var Measured = require('measured-core')
var Aggregator = function (metricCallback, id) {
this.metrics = {}
this.id = id
this.lastValues = {}
this.metricCallback = metricCallback
this.lastUpdate = Date.now()
}
Aggregator.prototype.update = function (timestamp, name, value, calcDiff) {
this.lastUpdate = Date.now()
if (isNaN(value)) {
return
}
if (this.metrics[name] === undefined) {
this.metrics[name] = new Measured.Histogram()
}
if (!this.lastValues[name]) {
this.lastValues[name] = {value: value, ts: timestamp}
}
if (calcDiff === true) {
var diff = value - this.lastValues[name].value
if (diff < 0) {
// container stop might have caused a reset of metric value.
this.lastValues[name].value = 0
} else {
this.metrics[name].update(diff, timestamp)
this.lastValues[name] = {value: value, ts: timestamp}
}
} else {
this.metrics[name].update(value, timestamp)
}
}
Aggregator.prototype.get = function (name) {
if (this.metrics[name] !== undefined) {
var rv = this.metrics[name].toJSON()
rv.name = name
this.metrics[name].reset()
return rv
} else {
return {count: 0, sum: 0, mean: 0, err: 'no metrics object for ' + name}
}
}
Aggregator.prototype.reset = function () {
this.metrics = null
this.metrics = {}
}
module.exports = Aggregator