UNPKG

@ahmedhegazee/nestjs-telescope

Version:

Advanced observability and monitoring solution for NestJS applications with ML-powered analytics, enterprise features, and production-ready scaling

109 lines 4.31 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var EntryManagerService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntryManagerService = void 0; const common_1 = require("@nestjs/common"); const storage_service_1 = require("../../storage/storage.service"); const uuid_1 = require("uuid"); let EntryManagerService = EntryManagerService_1 = class EntryManagerService { constructor(storageService, config) { this.storageService = storageService; this.config = config; this.logger = new common_1.Logger(EntryManagerService_1.name); this.batchQueue = []; this.batchTimer = null; this.sequenceCounter = 0; this.startBatchProcessor(); } async process(entry) { this.ensureEntryFields(entry); if (this.config.storage.batch.enabled) { await this.addToBatch(entry); } else { await this.storageService.store(entry); } } async processBatch(entries) { entries.forEach(entry => this.ensureEntryFields(entry)); await this.storageService.storeBatch(entries); } async addToBatch(entry) { this.batchQueue.push(entry); if (this.batchQueue.length >= this.config.storage.batch.size) { await this.flushBatch(); } } ensureEntryFields(entry) { if (!entry.id) { entry.id = `tel_${(0, uuid_1.v4)()}`; } if (!entry.timestamp) { entry.timestamp = new Date(); } if (!entry.sequence) { entry.sequence = ++this.sequenceCounter; } if (!entry.familyHash) { entry.familyHash = this.generateFamilyHash(entry); } if (!entry.tags) { entry.tags = []; } } generateFamilyHash(entry) { const hashInput = `${entry.type}:${JSON.stringify(entry.content).substring(0, 100)}`; let hash = 0; for (let i = 0; i < hashInput.length; i++) { const char = hashInput.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return Math.abs(hash).toString(16); } startBatchProcessor() { if (!this.config.storage.batch.enabled) return; setInterval(async () => { if (this.batchQueue.length > 0) { await this.flushBatch(); } }, this.config.storage.batch.flushInterval); } async flushBatch() { if (this.batchQueue.length === 0) return; const entries = this.batchQueue.splice(0); try { await this.storageService.storeBatch(entries); this.logger.debug(`Flushed batch: ${entries.length} entries`); } catch (error) { this.logger.error(`Failed to flush batch: ${error.message}`); this.batchQueue.unshift(...entries); } if (this.batchTimer) { clearTimeout(this.batchTimer); this.batchTimer = null; } } }; exports.EntryManagerService = EntryManagerService; exports.EntryManagerService = EntryManagerService = EntryManagerService_1 = __decorate([ (0, common_1.Injectable)(), __param(1, (0, common_1.Inject)('TELESCOPE_CONFIG')), __metadata("design:paramtypes", [storage_service_1.StorageService, Object]) ], EntryManagerService); //# sourceMappingURL=entry-manager.service.js.map