Implement metric interval control

This commit is contained in:
2023-11-30 13:21:10 +01:00
parent fc66f568ea
commit 60af808ea0
5 changed files with 44 additions and 15 deletions

View File

@@ -25,6 +25,22 @@ class CircularBuffer {
}
return result;
}
toArrayRecent(n = 10) {
const result = [];
const threshold = Date.now() - n * 1000;
let current = (this.head - 1 + this.size) % this.size;
while (current !== this.tail) {
if (this.buffer[current] !== undefined && this.buffer[current].timestamp > threshold) {
result.push(this.buffer[current]);
} else {
break;
}
current = (current - 1 + this.size) % this.size;
}
return result;
}
}
module.exports = { CircularBuffer };