Files
cameleer-server/ui/src/api/queries/executions.ts

88 lines
2.4 KiB
TypeScript
Raw Normal View History

import { useQuery } from '@tanstack/react-query';
import { api } from '../client';
import type { SearchRequest } from '../schema';
export function useExecutionStats() {
return useQuery({
queryKey: ['executions', 'stats'],
queryFn: async () => {
const { data, error } = await api.GET('/search/stats');
if (error) throw new Error('Failed to load stats');
return data!;
},
refetchInterval: 10_000,
});
}
export function useSearchExecutions(filters: SearchRequest, live = false) {
return useQuery({
queryKey: ['executions', 'search', filters],
queryFn: async () => {
const { data, error } = await api.POST('/search/executions', {
body: filters,
});
if (error) throw new Error('Search failed');
return data!;
},
placeholderData: (prev) => prev,
refetchInterval: live ? 5_000 : false,
});
}
export function useStatsTimeseries(timeFrom: string | undefined, timeTo: string | undefined) {
return useQuery({
queryKey: ['executions', 'timeseries', timeFrom, timeTo],
queryFn: async () => {
const { data, error } = await api.GET('/search/stats/timeseries', {
params: {
query: {
from: timeFrom!,
to: timeTo || undefined,
buckets: 24,
},
},
});
if (error) throw new Error('Failed to load timeseries');
return data!;
},
enabled: !!timeFrom,
refetchInterval: 30_000,
});
}
export function useExecutionDetail(executionId: string | null) {
return useQuery({
queryKey: ['executions', 'detail', executionId],
queryFn: async () => {
const { data, error } = await api.GET('/executions/{executionId}', {
params: { path: { executionId: executionId! } },
});
if (error) throw new Error('Failed to load execution detail');
return data!;
},
enabled: !!executionId,
});
}
export function useProcessorSnapshot(
executionId: string | null,
index: number | null,
) {
return useQuery({
queryKey: ['executions', 'snapshot', executionId, index],
queryFn: async () => {
const { data, error } = await api.GET(
'/executions/{executionId}/processors/{index}/snapshot',
{
params: {
path: { executionId: executionId!, index: index! },
},
},
);
if (error) throw new Error('Failed to load snapshot');
return data!;
},
enabled: !!executionId && index !== null,
});
}