fixed bugs

pull/12/head
DmitriyA 2025-04-04 15:32:21 -04:00
parent 23438a0e7f
commit 553b9141d4
1 changed files with 41 additions and 25 deletions

View File

@ -35,7 +35,7 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
} }
@SubscribeMessage('get-metrics') @SubscribeMessage('get-metrics')
async handleGetMetrics(client: Socket, payload: any) { async handleGetMetrics(client: Socket, payload: any) {
const { metric, start, end, step } = payload; const { metric, start, end, step } = payload;
try { try {
const data = start && end && step const data = start && end && step
@ -43,6 +43,14 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
: await this.prometheusService.fetchMetrics(metric); : await this.prometheusService.fetchMetrics(metric);
client.emit('metrics-data', { metric, data }); client.emit('metrics-data', { metric, data });
// Запускаем автоматические обновления с интервалом по умолчанию
const stopUpdates = await this.sendPeriodicUpdates(
metric,
5000, // Интервал по умолчанию
client
);
client.on('disconnect', () => stopUpdates());
} catch (error) { } catch (error) {
this.logger.error(`Error fetching metrics: ${error.message}`); this.logger.error(`Error fetching metrics: ${error.message}`);
client.emit('metrics-error', { client.emit('metrics-error', {
@ -50,7 +58,7 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
error: error.message error: error.message
}); });
} }
} }
@SubscribeMessage('get-metric-types') @SubscribeMessage('get-metric-types')
async handleGetMetricTypes(client: Socket, payload: { metric: string }) { async handleGetMetricTypes(client: Socket, payload: { metric: string }) {
@ -84,27 +92,35 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
} }
} }
@SubscribeMessage('subscribe-metric') @SubscribeMessage('subscribe-metric')
async handleSubscribeMetric(client: Socket, payload: { metric: string, interval?: number }) { async handleSubscribeMetric(client: Socket, payload: { metric: string, interval?: number }) {
const stopUpdates = await this.sendPeriodicUpdates(payload.metric, payload.interval); const stopUpdates = await this.sendPeriodicUpdates(
payload.metric,
payload.interval || 5000, // Добавляем значение по умолчанию
client // Передаем клиента
);
// Сохраняем функцию остановки для этого клиента // Сохраняем функцию остановки для этого клиента
client.on('disconnect', () => stopUpdates()); client.on('disconnect', () => stopUpdates());
client.on('unsubscribe-metric', () => stopUpdates()); client.on('unsubscribe-metric', () => stopUpdates());
} }
// Метод для периодической отправки обновлений // Метод для периодической отправки обновлений
async sendPeriodicUpdates(metric: string, interval: number = 5000) { async sendPeriodicUpdates(metric: string, interval: number, client: Socket) {
const timer = setInterval(async () => { const timer = setInterval(async () => {
try { try {
const data = await this.prometheusService.fetchMetrics(metric); const data = await this.prometheusService.fetchMetrics(metric);
this.server.emit('metrics-update', { metric, data }); client.emit('metrics-data', { metric, data });
} catch (error) { } catch (error) {
this.logger.error(`Error in periodic update for ${metric}: ${error.message}`); this.logger.error(`Error in periodic update for ${metric}: ${error.message}`);
} }
}, interval); }, interval);
// Возвращаем функцию для остановки обновлений return () => {
return () => clearInterval(timer); clearInterval(timer);
this.logger.log(`Stopped updates for ${metric}`);
};
} }
} }