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')
|
@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, _t } = payload;
|
||||||
try {
|
this.logger.log(`Received metrics request: ${metric}, start: ${start}, end: ${end}, step: ${step}`);
|
||||||
const data = start && end && step
|
|
||||||
? await this.prometheusService.fetchMetricsRange(metric, start, end, step)
|
|
||||||
: await this.prometheusService.fetchMetrics(metric);
|
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
// Запускаем автоматические обновления с интервалом по умолчанию
|
// Для запросов без диапазона (realtime) - запускаем подписку
|
||||||
const stopUpdates = await this.sendPeriodicUpdates(
|
const stopUpdates = await this.sendPeriodicUpdates(
|
||||||
metric,
|
metric,
|
||||||
5000, // Интервал по умолчанию
|
step || 5000, // Используем переданный шаг или дефолтный
|
||||||
client
|
client
|
||||||
);
|
);
|
||||||
client.on('disconnect', () => stopUpdates());
|
|
||||||
} catch (error) {
|
client.on('disconnect', () => stopUpdates());
|
||||||
this.logger.error(`Error fetching metrics: ${error.message}`);
|
client.on('unsubscribe-metric', () => stopUpdates());
|
||||||
client.emit('metrics-error', {
|
|
||||||
metric,
|
} catch (error) {
|
||||||
error: error.message
|
this.logger.error(`Error fetching metrics: ${error.message}`);
|
||||||
});
|
client.emit('metrics-error', {
|
||||||
|
metric,
|
||||||
|
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 }) {
|
||||||
|
|
@ -95,17 +101,17 @@ async handleGetMetrics(client: Socket, payload: any) {
|
||||||
|
|
||||||
|
|
||||||
@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(
|
const stopUpdates = await this.sendPeriodicUpdates(
|
||||||
payload.metric,
|
payload.metric,
|
||||||
payload.interval || 5000, // Добавляем значение по умолчанию
|
payload.interval || 5000, // Добавляем значение по умолчанию
|
||||||
client // Передаем клиента
|
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, client: Socket) {
|
async sendPeriodicUpdates(metric: string, interval: number, client: Socket) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue