Update
This commit is contained in:
@@ -1,46 +1,46 @@
|
||||
class CircularBuffer {
|
||||
constructor(size) {
|
||||
this.buffer = new Array(size);
|
||||
this.size = size;
|
||||
this.head = 0;
|
||||
this.tail = 0;
|
||||
}
|
||||
|
||||
push(item) {
|
||||
this.buffer[this.head] = item;
|
||||
this.head = (this.head + 1) % this.size;
|
||||
if (this.head === this.tail) {
|
||||
this.tail = (this.tail + 1) % this.size;
|
||||
}
|
||||
}
|
||||
|
||||
toArray() {
|
||||
const result = [];
|
||||
let current = this.tail;
|
||||
for (let i = 0; i < this.size; i++) {
|
||||
if (this.buffer[current] !== undefined) {
|
||||
result.push(this.buffer[current]);
|
||||
}
|
||||
current = (current + 1) % this.size;
|
||||
}
|
||||
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 };
|
||||
class CircularBuffer {
|
||||
constructor(size) {
|
||||
this.buffer = new Array(size);
|
||||
this.size = size;
|
||||
this.head = 0;
|
||||
this.tail = 0;
|
||||
}
|
||||
|
||||
push(item) {
|
||||
this.buffer[this.head] = item;
|
||||
this.head = (this.head + 1) % this.size;
|
||||
if (this.head === this.tail) {
|
||||
this.tail = (this.tail + 1) % this.size;
|
||||
}
|
||||
}
|
||||
|
||||
toArray() {
|
||||
const result = [];
|
||||
let current = this.tail;
|
||||
for (let i = 0; i < this.size; i++) {
|
||||
if (this.buffer[current] !== undefined) {
|
||||
result.push(this.buffer[current]);
|
||||
}
|
||||
current = (current + 1) % this.size;
|
||||
}
|
||||
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 };
|
||||
|
@@ -1,30 +1,30 @@
|
||||
const cliProgress = require("cli-progress");
|
||||
const { Metric } = require("./metrics");
|
||||
|
||||
class MetricManager {
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
if (options.bars) {
|
||||
this.metricBufferSize = 1000;
|
||||
this.multibar = new cliProgress.MultiBar(
|
||||
{
|
||||
clearOnComplete: false,
|
||||
barCompleteChar: "\u2588",
|
||||
barIncompleteChar: "\u2591",
|
||||
format: " {bar} | {name} | {value}/{total}",
|
||||
},
|
||||
cliProgress.Presets.shades_grey
|
||||
);
|
||||
setInterval(() => this.multibar.update(), 200);
|
||||
}
|
||||
}
|
||||
|
||||
AddMetrics(name, refresh = true) {
|
||||
if (this.options.bars) {
|
||||
const metric = new Metric(name, this.multibar, this.metricBufferSize, this.options, refresh);
|
||||
return metric;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { MetricManager };
|
||||
const cliProgress = require("cli-progress");
|
||||
const { Metric } = require("./metrics");
|
||||
|
||||
class MetricManager {
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
if (options.bars) {
|
||||
this.metricBufferSize = 1000;
|
||||
this.multibar = new cliProgress.MultiBar(
|
||||
{
|
||||
clearOnComplete: false,
|
||||
barCompleteChar: "\u2588",
|
||||
barIncompleteChar: "\u2591",
|
||||
format: " {bar} | {name} | {value}/{total}",
|
||||
},
|
||||
cliProgress.Presets.shades_grey
|
||||
);
|
||||
setInterval(() => this.multibar.update(), 200);
|
||||
}
|
||||
}
|
||||
|
||||
AddMetrics(name, refresh = true) {
|
||||
if (this.options.bars) {
|
||||
const metric = new Metric(name, this.multibar, this.metricBufferSize, this.options, refresh);
|
||||
return metric;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { MetricManager };
|
||||
|
@@ -1,39 +1,39 @@
|
||||
const { CircularBuffer } = require("./circularBuffer");
|
||||
|
||||
class Metric {
|
||||
constructor(barName, multibar, bufferSize, options, refresh = true) {
|
||||
this.options = options;
|
||||
this.multibar = multibar;
|
||||
this.bar = multibar.create(0, 0);
|
||||
this.bar.update(0, { name: barName });
|
||||
this.maxRate = this.options.defaultmaxrate;
|
||||
this.bar.total = this.maxRate;
|
||||
this.buffer = new CircularBuffer(bufferSize);
|
||||
if (refresh) {
|
||||
setInterval(this.UpdateBar.bind(this), 100);
|
||||
}
|
||||
}
|
||||
|
||||
AddEvent() {
|
||||
const timestamp = Date.now();
|
||||
this.buffer.push({ timestamp, count: 1 });
|
||||
}
|
||||
|
||||
GetRate() {
|
||||
const entries = this.buffer.toArrayRecent(this.options.metricsinterval);
|
||||
|
||||
const totalRX = entries.reduce((sum, entry) => sum + entry.count, 0);
|
||||
return Math.round((totalRX / this.options.metricsinterval) * 100) / 100;
|
||||
}
|
||||
|
||||
UpdateBar() {
|
||||
const eps = this.GetRate();
|
||||
if (eps > this.maxRate) {
|
||||
this.bar.total = eps;
|
||||
this.maxRate = eps;
|
||||
}
|
||||
this.bar.update(eps);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Metric };
|
||||
const { CircularBuffer } = require("./circularBuffer");
|
||||
|
||||
class Metric {
|
||||
constructor(barName, multibar, bufferSize, options, refresh = true) {
|
||||
this.options = options;
|
||||
this.multibar = multibar;
|
||||
this.bar = multibar.create(0, 0);
|
||||
this.bar.update(0, { name: barName });
|
||||
this.maxRate = this.options.defaultmaxrate;
|
||||
this.bar.total = this.maxRate;
|
||||
this.buffer = new CircularBuffer(bufferSize);
|
||||
if (refresh) {
|
||||
setInterval(this.UpdateBar.bind(this), 100);
|
||||
}
|
||||
}
|
||||
|
||||
AddEvent() {
|
||||
const timestamp = Date.now();
|
||||
this.buffer.push({ timestamp, count: 1 });
|
||||
}
|
||||
|
||||
GetRate() {
|
||||
const entries = this.buffer.toArrayRecent(this.options.metricsinterval);
|
||||
|
||||
const totalRX = entries.reduce((sum, entry) => sum + entry.count, 0);
|
||||
return Math.round((totalRX / this.options.metricsinterval) * 100) / 100;
|
||||
}
|
||||
|
||||
UpdateBar() {
|
||||
const eps = this.GetRate();
|
||||
if (eps > this.maxRate) {
|
||||
this.bar.total = eps;
|
||||
this.maxRate = eps;
|
||||
}
|
||||
this.bar.update(eps);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Metric };
|
||||
|
Reference in New Issue
Block a user