2016-10-27 13:13:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-11-04 16:16:55 +01:00
|
|
|
const Projection = require('./projection.js');
|
|
|
|
const TTLList = require('./ttl-list.js');
|
|
|
|
|
2016-10-27 13:13:51 +02:00
|
|
|
module.exports = class UnleashClientMetrics {
|
2016-11-28 17:11:11 +01:00
|
|
|
constructor (clientMetricsStore) {
|
|
|
|
|
2016-10-27 13:13:51 +02:00
|
|
|
this.globalCount = 0;
|
2016-11-04 16:16:55 +01:00
|
|
|
this.apps = {};
|
|
|
|
|
2016-11-07 09:13:19 +01:00
|
|
|
this.lastHourProjection = new Projection();
|
|
|
|
this.lastMinuteProjection = new Projection();
|
|
|
|
|
|
|
|
this.lastHourList = new TTLList({
|
|
|
|
interval: 10000,
|
|
|
|
});
|
2016-11-28 17:11:11 +01:00
|
|
|
|
2016-11-07 09:13:19 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
2016-11-28 17:11:11 +01:00
|
|
|
clientMetricsStore.on('metrics', (m) => this.addPayload(m));
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
getAppsWitToggles () {
|
|
|
|
const apps = [];
|
|
|
|
Object.keys(this.apps).forEach(appName => {
|
|
|
|
const seenToggles = Object.keys(this.apps[appName].seenToggles);
|
|
|
|
const metricsCount = this.apps[appName].count;
|
|
|
|
apps.push({appName, seenToggles, metricsCount})
|
|
|
|
});
|
|
|
|
return apps;
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
2016-11-28 17:11:11 +01:00
|
|
|
getSeenTogglesByAppName(appName) {
|
|
|
|
return this.apps[appName] ? Object.keys(this.apps[appName].seenToggles) : [];
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-11-28 17:11:11 +01:00
|
|
|
const { appName, bucket } = data;
|
|
|
|
const app = this.getApp(appName)
|
|
|
|
this.addBucket(app, data.bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
getApp(appName) {
|
|
|
|
this.apps[appName] = this.apps[appName] || {seenToggles: {}, count: 0};
|
|
|
|
return this.apps[appName];
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
addBucket (app, bucket) {
|
2016-10-27 13:13:51 +02:00
|
|
|
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-28 17:11:11 +01:00
|
|
|
const toggleNames = Object.keys(toggles);
|
|
|
|
|
|
|
|
toggleNames.forEach((n) => {
|
2016-11-04 16:16:55 +01:00
|
|
|
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-10-27 13:13:51 +02:00
|
|
|
});
|
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
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
this.globalCount += count;
|
|
|
|
app.count += count;
|
|
|
|
this.addSeenToggles(app, toggleNames);
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
addSeenToggles (app, toggleNames) {
|
|
|
|
toggleNames.forEach(t => app.seenToggles[t] = true);
|
2016-10-27 13:13:51 +02:00
|
|
|
}
|
2016-11-13 18:14:29 +01:00
|
|
|
|
|
|
|
destroy () {
|
|
|
|
this.lastHourList.destroy();
|
|
|
|
this.lastMinuteList.destroy();
|
|
|
|
}
|
2016-10-27 13:13:51 +02:00
|
|
|
};
|