fixed bugs
parent
23438a0e7f
commit
553b9141d4
|
|
@ -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', {
|
||||||
|
|
@ -84,9 +92,15 @@ 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());
|
||||||
|
|
@ -94,17 +108,19 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
|
||||||
}
|
}
|
||||||
|
|
||||||
// Метод для периодической отправки обновлений
|
// Метод для периодической отправки обновлений
|
||||||
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}`);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue