@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
39 lines • 1.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsService = void 0;
const lodash_1 = __importDefault(require("lodash"));
const models_1 = require("../models");
const utils_1 = require("../../utils");
class MetricsService extends models_1.Service {
constructor(server, options) {
super(server);
this.server = server;
this.options = options;
this.limit = this.options?.limit ?? 300;
this.points = {
containers: (0, utils_1.cast)([]),
memory: (0, utils_1.cast)([]),
cache: (0, utils_1.cast)([]),
rate: (0, utils_1.cast)([]),
};
}
register(name, values) {
if (!this.points[name]) {
throw new Error('Invalid metric name');
}
const group = this.points[name];
const point = { timestamp: Date.now(), values };
group.push(point);
group.splice(0, lodash_1.default.clamp(group.length - this.limit, 0, Infinity));
this.server.exchanges.io.publish('metric:registered', { name, point });
return this;
}
static build(server, options) {
return new MetricsService(server, options);
}
}
exports.MetricsService = MetricsService;
//# sourceMappingURL=metrics.service.js.map