Added the start, end, and step parameters to solve the update issue on the charts.

pull/12/head
DmitriyA 2025-04-08 01:58:15 -04:00
parent 553b9141d4
commit 23d2fd7eff
1 changed files with 38 additions and 32 deletions

View File

@ -36,21 +36,27 @@ export class MetricsGateway implements OnGatewayInit, OnGatewayConnection, OnGat
@SubscribeMessage('get-metrics')
async handleGetMetrics(client: Socket, payload: any) {
const { metric, start, end, step } = payload;
const { metric, start, end, step, _t } = payload;
this.logger.log(`Received metrics request: ${metric}, start: ${start}, end: ${end}, step: ${step}`);
try {
const data = start && end && step
? await this.prometheusService.fetchMetricsRange(metric, start, end, step)
: await this.prometheusService.fetchMetrics(metric);
// Для запросов с диапазоном - просто возвращаем данные без подписки
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(
metric,
5000, // Интервал по умолчанию
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', {