2026-03-23 18:28:52 +01:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
import { config } from '../../config';
|
|
|
|
|
import { useAuthStore } from '../../auth/auth-store';
|
2026-03-24 18:10:32 +01:00
|
|
|
import { useRefreshInterval } from './use-refresh-interval';
|
2026-03-23 18:28:52 +01:00
|
|
|
|
2026-04-12 21:59:38 +02:00
|
|
|
export function useAgentMetrics(
|
|
|
|
|
agentId: string | null,
|
|
|
|
|
names: string[],
|
|
|
|
|
buckets = 60,
|
|
|
|
|
from?: string,
|
|
|
|
|
to?: string,
|
2026-04-13 10:56:06 +02:00
|
|
|
mode: 'gauge' | 'delta' = 'gauge',
|
2026-04-12 21:59:38 +02:00
|
|
|
) {
|
2026-03-24 18:10:32 +01:00
|
|
|
const refetchInterval = useRefreshInterval(30_000);
|
2026-03-23 18:28:52 +01:00
|
|
|
return useQuery({
|
2026-04-13 10:56:06 +02:00
|
|
|
queryKey: ['agent-metrics', agentId, names.join(','), buckets, from, to, mode],
|
2026-03-23 18:28:52 +01:00
|
|
|
queryFn: async () => {
|
|
|
|
|
const token = useAuthStore.getState().accessToken;
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
names: names.join(','),
|
|
|
|
|
buckets: String(buckets),
|
2026-04-13 10:56:06 +02:00
|
|
|
mode,
|
2026-03-23 18:28:52 +01:00
|
|
|
});
|
2026-04-12 21:59:38 +02:00
|
|
|
if (from) params.set('from', from);
|
|
|
|
|
if (to) params.set('to', to);
|
2026-03-23 18:28:52 +01:00
|
|
|
const res = await fetch(`${config.apiBaseUrl}/agents/${agentId}/metrics?${params}`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
'X-Cameleer-Protocol-Version': '1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) throw new Error(`${res.status}`);
|
|
|
|
|
return res.json() as Promise<{ metrics: Record<string, Array<{ time: string; value: number }>> }>;
|
|
|
|
|
},
|
|
|
|
|
enabled: !!agentId && names.length > 0,
|
2026-03-24 18:10:32 +01:00
|
|
|
refetchInterval,
|
2026-03-23 18:28:52 +01:00
|
|
|
});
|
|
|
|
|
}
|