@onurege3467/zerohelper
Version:
ZeroHelper is a versatile high-performance utility library and database framework for Node.js, fully written in TypeScript.
42 lines (41 loc) • 1.42 kB
JavaScript
"use strict";
// database/telemetry.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.telemetry = void 0;
class TelemetrySystem {
constructor() {
this.dbMetrics = [];
this.cacheStats = { hits: 0, misses: 0 };
this.maxLogs = 1000;
}
recordDb(metric) {
this.dbMetrics.push(metric);
if (this.dbMetrics.length > this.maxLogs)
this.dbMetrics.shift();
}
recordCacheHit() { this.cacheStats.hits++; }
recordCacheMiss() { this.cacheStats.misses++; }
getMetrics() {
const totalOps = this.dbMetrics.length;
const avgDuration = totalOps > 0
? this.dbMetrics.reduce((s, m) => s + m.duration, 0) / totalOps
: 0;
return {
database: {
totalOperations: totalOps,
averageDuration: avgDuration.toFixed(2) + 'ms',
slowestOperations: [...this.dbMetrics].sort((a, b) => b.duration - a.duration).slice(0, 5),
recentLogs: this.dbMetrics.slice(-10)
},
cache: {
...this.cacheStats,
ratio: (this.cacheStats.hits / (this.cacheStats.hits + this.cacheStats.misses || 1) * 100).toFixed(2) + '%'
}
};
}
clear() {
this.dbMetrics = [];
this.cacheStats = { hits: 0, misses: 0 };
}
}
exports.telemetry = new TelemetrySystem();