2026-03-25 18:56:13 +01:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
import { config } from '../../config';
|
|
|
|
|
import { useAuthStore } from '../../auth/auth-store';
|
|
|
|
|
import { useRefreshInterval } from './use-refresh-interval';
|
|
|
|
|
import { useGlobalFilters } from '@cameleer/design-system';
|
|
|
|
|
|
|
|
|
|
export interface LogEntryResponse {
|
|
|
|
|
timestamp: string;
|
|
|
|
|
level: string;
|
|
|
|
|
loggerName: string | null;
|
|
|
|
|
message: string;
|
|
|
|
|
threadName: string | null;
|
|
|
|
|
stackTrace: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useApplicationLogs(
|
|
|
|
|
application?: string,
|
|
|
|
|
agentId?: string,
|
2026-03-25 22:41:00 +01:00
|
|
|
options?: { limit?: number; toOverride?: string },
|
2026-03-25 18:56:13 +01:00
|
|
|
) {
|
|
|
|
|
const refetchInterval = useRefreshInterval(15_000);
|
|
|
|
|
const { timeRange } = useGlobalFilters();
|
2026-03-25 22:41:00 +01:00
|
|
|
const to = options?.toOverride ?? timeRange.end.toISOString();
|
2026-03-25 18:56:13 +01:00
|
|
|
|
|
|
|
|
return useQuery({
|
2026-03-25 22:41:00 +01:00
|
|
|
queryKey: ['logs', application, agentId, timeRange.start.toISOString(), to, options?.limit],
|
2026-03-25 18:56:13 +01:00
|
|
|
queryFn: async () => {
|
|
|
|
|
const token = useAuthStore.getState().accessToken;
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.set('application', application!);
|
|
|
|
|
if (agentId) params.set('agentId', agentId);
|
|
|
|
|
params.set('from', timeRange.start.toISOString());
|
2026-03-25 22:41:00 +01:00
|
|
|
params.set('to', to);
|
2026-03-25 18:56:13 +01:00
|
|
|
if (options?.limit) params.set('limit', String(options.limit));
|
|
|
|
|
const res = await fetch(`${config.apiBaseUrl}/logs?${params}`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
'X-Cameleer-Protocol-Version': '1',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) throw new Error('Failed to load application logs');
|
|
|
|
|
return res.json() as Promise<LogEntryResponse[]>;
|
|
|
|
|
},
|
|
|
|
|
enabled: !!application,
|
|
|
|
|
refetchInterval,
|
|
|
|
|
});
|
|
|
|
|
}
|