apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, and Koa.
92 lines (91 loc) • 3.73 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/common/requestCounter.ts
var _RequestCounter = class _RequestCounter {
requestCounts;
requestSizeSums;
responseSizeSums;
responseTimes;
requestSizes;
responseSizes;
constructor() {
this.requestCounts = /* @__PURE__ */ new Map();
this.requestSizeSums = /* @__PURE__ */ new Map();
this.responseSizeSums = /* @__PURE__ */ new Map();
this.responseTimes = /* @__PURE__ */ new Map();
this.requestSizes = /* @__PURE__ */ new Map();
this.responseSizes = /* @__PURE__ */ new Map();
}
getKey(requestInfo) {
return [
requestInfo.consumer || "",
requestInfo.method.toUpperCase(),
requestInfo.path,
requestInfo.statusCode
].join("|");
}
addRequest(requestInfo) {
const key = this.getKey(requestInfo);
this.requestCounts.set(key, (this.requestCounts.get(key) || 0) + 1);
if (!this.responseTimes.has(key)) {
this.responseTimes.set(key, /* @__PURE__ */ new Map());
}
const responseTimeMap = this.responseTimes.get(key);
const responseTimeMsBin = Math.floor(requestInfo.responseTime / 10) * 10;
responseTimeMap.set(responseTimeMsBin, (responseTimeMap.get(responseTimeMsBin) || 0) + 1);
if (requestInfo.requestSize !== void 0) {
requestInfo.requestSize = Number(requestInfo.requestSize);
this.requestSizeSums.set(key, (this.requestSizeSums.get(key) || 0) + requestInfo.requestSize);
if (!this.requestSizes.has(key)) {
this.requestSizes.set(key, /* @__PURE__ */ new Map());
}
const requestSizeMap = this.requestSizes.get(key);
const requestSizeKbBin = Math.floor(requestInfo.requestSize / 1e3);
requestSizeMap.set(requestSizeKbBin, (requestSizeMap.get(requestSizeKbBin) || 0) + 1);
}
if (requestInfo.responseSize !== void 0) {
requestInfo.responseSize = Number(requestInfo.responseSize);
this.responseSizeSums.set(key, (this.responseSizeSums.get(key) || 0) + requestInfo.responseSize);
if (!this.responseSizes.has(key)) {
this.responseSizes.set(key, /* @__PURE__ */ new Map());
}
const responseSizeMap = this.responseSizes.get(key);
const responseSizeKbBin = Math.floor(requestInfo.responseSize / 1e3);
responseSizeMap.set(responseSizeKbBin, (responseSizeMap.get(responseSizeKbBin) || 0) + 1);
}
}
getAndResetRequests() {
const data = [];
this.requestCounts.forEach((count, key) => {
const [consumer, method, path, statusCodeStr] = key.split("|");
const responseTimes = this.responseTimes.get(key) || /* @__PURE__ */ new Map();
const requestSizes = this.requestSizes.get(key) || /* @__PURE__ */ new Map();
const responseSizes = this.responseSizes.get(key) || /* @__PURE__ */ new Map();
data.push({
consumer: consumer || null,
method,
path,
status_code: parseInt(statusCodeStr),
request_count: count,
request_size_sum: this.requestSizeSums.get(key) || 0,
response_size_sum: this.responseSizeSums.get(key) || 0,
response_times: Object.fromEntries(responseTimes),
request_sizes: Object.fromEntries(requestSizes),
response_sizes: Object.fromEntries(responseSizes)
});
});
this.requestCounts.clear();
this.requestSizeSums.clear();
this.responseSizeSums.clear();
this.responseTimes.clear();
this.requestSizes.clear();
this.responseSizes.clear();
return data;
}
};
__name(_RequestCounter, "RequestCounter");
var RequestCounter = _RequestCounter;
export {
RequestCounter as default
};
//# sourceMappingURL=requestCounter.js.map