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