userface
Version:
Universal Data-Driven UI Engine with live data, validation, and multi-platform support
231 lines (230 loc) • 8.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemMonitor = void 0;
const logger_1 = require("./logger");
// Мониторинг системы
class SystemMonitor {
constructor() {
// Общая статистика
Object.defineProperty(this, "stats", {
enumerable: true,
configurable: true,
writable: true,
value: {
totalComponents: 0,
totalSchemas: 0,
totalAdapters: 0,
errors: 0,
warnings: 0,
startTime: new Date(),
lastActivity: new Date()
}
});
// Метрики производительности
Object.defineProperty(this, "performance", {
enumerable: true,
configurable: true,
writable: true,
value: {
registrationTime: 0,
analysisTime: 0,
renderTime: 0,
cacheHits: 0,
cacheMisses: 0
}
});
// События
Object.defineProperty(this, "events", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
}
// === ОСНОВНЫЕ МЕТРИКИ ===
trackRegistration(componentName, duration) {
this.stats.totalComponents++;
this.performance.registrationTime += duration;
this.stats.lastActivity = new Date();
this.addEvent('registration', `Component registered: ${componentName}`, {
name: componentName,
duration
});
logger_1.logger.debug(`Tracked registration: ${componentName}`, 'SystemMonitor', { duration });
}
trackAnalysis(componentName, duration) {
this.performance.analysisTime += duration;
this.stats.lastActivity = new Date();
this.addEvent('analysis', `Component analyzed: ${componentName}`, {
name: componentName,
duration
});
logger_1.logger.debug(`Tracked analysis: ${componentName}`, 'SystemMonitor', { duration });
}
trackRender(adapterId, duration) {
this.performance.renderTime += duration;
this.stats.lastActivity = new Date();
this.addEvent('render', `Rendered with adapter: ${adapterId}`, {
adapterId,
duration
});
logger_1.logger.debug(`Tracked render: ${adapterId}`, 'SystemMonitor', { duration });
}
trackCacheHit(schemaName) {
this.performance.cacheHits++;
this.stats.lastActivity = new Date();
logger_1.logger.debug(`Tracked cache hit: ${schemaName}`, 'SystemMonitor');
}
trackCacheMiss(schemaName) {
this.performance.cacheMisses++;
this.stats.lastActivity = new Date();
logger_1.logger.debug(`Tracked cache miss: ${schemaName}`, 'SystemMonitor');
}
trackError(error, context) {
this.stats.errors++;
this.stats.lastActivity = new Date();
this.addEvent('error', error.message, {
error: error.stack,
context
});
logger_1.logger.error('Tracked error', 'SystemMonitor', error, context);
}
trackWarning(message, context) {
this.stats.warnings++;
this.stats.lastActivity = new Date();
this.addEvent('warning', message, { context });
logger_1.logger.warn('Tracked warning', 'SystemMonitor', { message, context });
}
// === СТАТИСТИКА ===
getStats() {
const uptime = Date.now() - this.stats.startTime.getTime();
const cacheHitRate = this.performance.cacheHits / (this.performance.cacheHits + this.performance.cacheMisses) || 0;
return {
...this.stats,
uptime,
performance: {
...this.performance,
cacheHitRate,
avgRegistrationTime: this.stats.totalComponents > 0 ? this.performance.registrationTime / this.stats.totalComponents : 0,
avgAnalysisTime: this.stats.totalComponents > 0 ? this.performance.analysisTime / this.stats.totalComponents : 0,
avgRenderTime: this.stats.totalAdapters > 0 ? this.performance.renderTime / this.stats.totalAdapters : 0
},
eventsCount: this.events.length
};
}
getPerformanceMetrics() {
return {
...this.performance,
cacheHitRate: this.performance.cacheHits / (this.performance.cacheHits + this.performance.cacheMisses) || 0,
avgRegistrationTime: this.stats.totalComponents > 0 ? this.performance.registrationTime / this.stats.totalComponents : 0,
avgAnalysisTime: this.stats.totalComponents > 0 ? this.performance.analysisTime / this.stats.totalComponents : 0,
avgRenderTime: this.stats.totalAdapters > 0 ? this.performance.renderTime / this.stats.totalAdapters : 0
};
}
getRecentEvents(limit = 10) {
return this.events
.slice(-limit)
.reverse()
.map(event => ({
type: event.type,
message: event.message,
timestamp: event.timestamp,
data: event.data
}));
}
getEventsByType(type) {
return this.events
.filter(event => event.type === type)
.map(event => ({
type: event.type,
message: event.message,
timestamp: event.timestamp,
data: event.data
}));
}
// === ЗДОРОВЬЕ СИСТЕМЫ ===
getSystemHealth() {
const issues = [];
let score = 100;
// Проверяем количество ошибок
if (this.stats.errors > 10) {
issues.push(`High error count: ${this.stats.errors}`);
score -= 20;
}
// Проверяем производительность
const avgRegistrationTime = this.stats.totalComponents > 0 ? this.performance.registrationTime / this.stats.totalComponents : 0;
if (avgRegistrationTime > 1000) {
issues.push(`Slow registration: ${avgRegistrationTime.toFixed(2)}ms average`);
score -= 15;
}
const avgAnalysisTime = this.stats.totalComponents > 0 ? this.performance.analysisTime / this.stats.totalComponents : 0;
if (avgAnalysisTime > 500) {
issues.push(`Slow analysis: ${avgAnalysisTime.toFixed(2)}ms average`);
score -= 15;
}
// Проверяем кеш
const cacheHitRate = this.performance.cacheHits / (this.performance.cacheHits + this.performance.cacheMisses) || 0;
if (cacheHitRate < 0.5) {
issues.push(`Low cache hit rate: ${(cacheHitRate * 100).toFixed(1)}%`);
score -= 10;
}
// Определяем статус
let status;
if (score >= 80) {
status = 'healthy';
}
else if (score >= 50) {
status = 'warning';
}
else {
status = 'critical';
}
logger_1.logger.info('System health check', 'SystemMonitor', {
status,
score,
issuesCount: issues.length
});
return { status, issues, score };
}
// === ОЧИСТКА ===
clearEvents() {
const eventCount = this.events.length;
this.events = [];
logger_1.logger.info(`Cleared events: ${eventCount} removed`, 'SystemMonitor', {
removedCount: eventCount
});
}
resetStats() {
this.stats = {
totalComponents: 0,
totalSchemas: 0,
totalAdapters: 0,
errors: 0,
warnings: 0,
startTime: new Date(),
lastActivity: new Date()
};
this.performance = {
registrationTime: 0,
analysisTime: 0,
renderTime: 0,
cacheHits: 0,
cacheMisses: 0
};
logger_1.logger.info('Reset system statistics', 'SystemMonitor');
}
// === ПРИВАТНЫЕ МЕТОДЫ ===
addEvent(type, message, data) {
this.events.push({
type,
message,
timestamp: new Date(),
data
});
// Ограничиваем количество событий
if (this.events.length > 1000) {
this.events = this.events.slice(-500);
}
}
}
exports.SystemMonitor = SystemMonitor;