1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/client-metrics/index.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
2016-11-04 16:16:55 +01:00
const Projection = require('./projection.js');
const TTLList = require('./ttl-list.js');
module.exports = class UnleashClientMetrics {
constructor () {
this.globalCount = 0;
2016-11-04 16:16:55 +01:00
this.apps = {};
this.clients = {};
2016-11-04 16:16:55 +01:00
2016-11-07 09:13:19 +01:00
this.lastHourProjection = new Projection();
this.lastMinuteProjection = new Projection();
this.lastHourList = new TTLList({
interval: 10000,
});
this.lastMinuteList = new TTLList({
interval: 10000,
expireType: 'minutes',
expireAmount: 1,
});
this.lastHourList.on('expire', (toggles) => {
2016-11-04 16:16:55 +01:00
Object.keys(toggles).forEach(toggleName => {
2016-11-07 09:13:19 +01:00
this.lastHourProjection.substract(toggleName, toggles[toggleName]);
});
});
this.lastMinuteList.on('expire', (toggles) => {
Object.keys(toggles).forEach(toggleName => {
this.lastMinuteProjection.substract(toggleName, toggles[toggleName]);
2016-11-04 16:16:55 +01:00
});
});
}
toJSON () {
2016-11-04 16:16:55 +01:00
return JSON.stringify(this.getMetricsOverview(), null, 4);
}
2016-11-04 16:16:55 +01:00
getMetricsOverview () {
return {
globalCount: this.globalCount,
apps: this.apps,
clients: this.clients,
};
}
2016-11-04 16:16:55 +01:00
getTogglesMetrics () {
2016-11-07 09:13:19 +01:00
return {
lastHour: this.lastHourProjection.getProjection(),
lastMinute: this.lastMinuteProjection.getProjection(),
};
2016-11-04 16:16:55 +01:00
}
2016-11-02 12:49:25 +01:00
addPayload (data) {
this.addClient(data.appName, data.instanceId);
2016-10-27 15:16:27 +02:00
this.addBucket(data.appName, data.instanceId, data.bucket);
}
2016-10-27 15:16:27 +02:00
addBucket (appName, instanceId, bucket) {
let count = 0;
2016-11-04 16:16:55 +01:00
// TODO stop should be createdAt
const { stop, toggles } = bucket;
2016-10-27 15:16:27 +02:00
2016-11-04 16:16:55 +01:00
Object.keys(toggles).forEach((n) => {
const entry = toggles[n];
2016-11-07 09:13:19 +01:00
this.lastHourProjection.add(n, entry);
this.lastMinuteProjection.add(n, entry);
2016-11-04 16:16:55 +01:00
count += (entry.yes + entry.no);
});
2016-11-04 16:16:55 +01:00
2016-11-07 09:13:19 +01:00
this.lastHourList.add(toggles, stop);
this.lastMinuteList.add(toggles, stop);
2016-11-04 16:16:55 +01:00
this.addClientCount(appName, instanceId, count);
}
2016-11-04 16:16:55 +01:00
addClientCount (appName, instanceId, count) {
if (typeof count === 'number' && count > 0) {
this.globalCount += count;
if (this.clients[instanceId]) {
this.clients[instanceId].count += count;
}
2016-11-13 21:25:37 +01:00
if (this.apps[appName]) {
this.apps[appName].count += count;
}
}
}
2016-11-02 12:49:25 +01:00
addClient (appName, instanceId, started = new Date()) {
2016-11-04 16:16:55 +01:00
this.addApp(appName, instanceId);
if (instanceId) {
if (this.clients[instanceId]) {
this.clients[instanceId].ping = new Date();
} else {
this.clients[instanceId] = {
appName,
count: 0,
2016-11-02 12:49:25 +01:00
started,
init: new Date(),
ping: new Date(),
};
}
}
}
2016-11-04 16:16:55 +01:00
addApp (appName, instanceId) {
if (appName && !this.apps[appName]) {
this.apps[appName] = {
count: 0,
clients: [],
};
}
if (instanceId && !this.apps[appName].clients.includes(instanceId)) {
this.apps[appName].clients.push(instanceId);
}
}
2016-11-13 18:14:29 +01:00
destroy () {
this.lastHourList.destroy();
this.lastMinuteList.destroy();
}
};