Added the start, end, and step parameters to solve the update issue on the charts.
parent
553b9141d4
commit
23d2fd7eff
|
|
@ -35,30 +35,36 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
|
|||
}
|
||||
|
||||
@SubscribeMessage('get-metrics')
|
||||
async handleGetMetrics(client: Socket, payload: any) {
|
||||
const { metric, start, end, step } = payload;
|
||||
try {
|
||||
const data = start && end && step
|
||||
? await this.prometheusService.fetchMetricsRange(metric, start, end, step)
|
||||
: await this.prometheusService.fetchMetrics(metric);
|
||||
async handleGetMetrics(client: Socket, payload: any) {
|
||||
const { metric, start, end, step, _t } = payload;
|
||||
this.logger.log(`Received metrics request: ${metric}, start: ${start}, end: ${end}, step: ${step}`);
|
||||
|
||||
client.emit('metrics-data', { metric, data });
|
||||
try {
|
||||
// Для запросов с диапазоном - просто возвращаем данные без подписки
|
||||
if (start && end) {
|
||||
const data = await this.prometheusService.fetchMetricsRange(metric, start, end, step);
|
||||
client.emit('metrics-data', { metric, data });
|
||||
return;
|
||||
}
|
||||
|
||||
// Запускаем автоматические обновления с интервалом по умолчанию
|
||||
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', {
|
||||
metric,
|
||||
error: error.message
|
||||
});
|
||||
// Для запросов без диапазона (realtime) - запускаем подписку
|
||||
const stopUpdates = await this.sendPeriodicUpdates(
|
||||
metric,
|
||||
step || 5000, // Используем переданный шаг или дефолтный
|
||||
client
|
||||
);
|
||||
|
||||
client.on('disconnect', () => stopUpdates());
|
||||
client.on('unsubscribe-metric', () => stopUpdates());
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error(`Error fetching metrics: ${error.message}`);
|
||||
client.emit('metrics-error', {
|
||||
metric,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeMessage('get-metric-types')
|
||||
async handleGetMetricTypes(client: Socket, payload: { metric: string }) {
|
||||
|
|
@ -95,17 +101,17 @@ async handleGetMetrics(client: Socket, payload: any) {
|
|||
|
||||
|
||||
@SubscribeMessage('subscribe-metric')
|
||||
async handleSubscribeMetric(client: Socket, payload: { metric: string, interval?: number }) {
|
||||
const stopUpdates = await this.sendPeriodicUpdates(
|
||||
payload.metric,
|
||||
payload.interval || 5000, // Добавляем значение по умолчанию
|
||||
client // Передаем клиента
|
||||
);
|
||||
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());
|
||||
}
|
||||
// Сохраняем функцию остановки для этого клиента
|
||||
client.on('disconnect', () => stopUpdates());
|
||||
client.on('unsubscribe-metric', () => stopUpdates());
|
||||
}
|
||||
|
||||
// Метод для периодической отправки обновлений
|
||||
async sendPeriodicUpdates(metric: string, interval: number, client: Socket) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue