platform-codebase/@packages/@infrastructure/queue-admin/src/hooks/useQueueStats.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import { useQuery } from '@tanstack/react-query';
import type { QueueSummary, QueueStats } from '../types';
const DEFAULT_API_URL = '/api/admin/queues';
export interface UseQueueStatsOptions {
apiUrl?: string;
refreshInterval?: number;
}
async function fetchQueueStats(apiUrl: string): Promise<QueueSummary> {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Failed to fetch queue stats: ${response.statusText}`);
}
return response.json();
}
export function useQueueStats(options: UseQueueStatsOptions = {}) {
const { apiUrl = DEFAULT_API_URL, refreshInterval = 5000 } = options;
return useQuery<QueueSummary>({
queryKey: ['queueStats', apiUrl],
queryFn: () => fetchQueueStats(apiUrl),
refetchInterval: refreshInterval,
staleTime: 1000,
});
}
export function useQueueDetails(queueName: string, options: UseQueueStatsOptions = {}) {
const { apiUrl = DEFAULT_API_URL, refreshInterval = 5000 } = options;
return useQuery<QueueStats>({
queryKey: ['queueDetails', queueName, apiUrl],
queryFn: async () => {
const response = await fetch(`${apiUrl}/${queueName}`);
if (!response.ok) {
throw new Error(`Failed to fetch queue details: ${response.statusText}`);
}
return response.json();
},
refetchInterval: refreshInterval,
staleTime: 1000,
});
}