1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

feat: initial counter

This commit is contained in:
Fredrik Oseberg 2022-09-22 14:44:40 +02:00
parent d7b2874afd
commit aa3e49f8c5

View File

@ -0,0 +1,47 @@
datefns;
export default class RequestCounter {
private requestCache: Object;
private longTermCache: Object;
private interval: number;
constructor() {
this.requestCache = {};
this.longTermCache = [];
this.interval = setInterval(() => {
this.requestCache.endTime = new Date();
const longTermCacheObject = {
...this.requestCache,
rps: this.calculateRPS(),
};
this.longTermCache.push(longTermCacheObject);
this.requestCache = this.createBucket();
}, 300000).unref();
}
createBucket = () => {
const bucket = {
count: 0,
startTime: new Date(),
endTime: null,
};
return bucket;
};
recordRequest = (appName: string): Promise<void> => {
if (this.requestCache[appName]) {
this.requestCache[count] += 1;
} else {
this.requestCache[appName] = { count: 1 };
}
};
calculateRPS = () => {
const { count } = this.requestCache;
return count / 300;
};
}