From 286e7edeef963e484123eea100928fcb25507af6 Mon Sep 17 00:00:00 2001 From: marko-kraemer Date: Sun, 3 Aug 2025 01:15:56 +0200 Subject: [PATCH] wip --- .../use-knowledge-base-queries.ts | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/react-query/knowledge-base/use-knowledge-base-queries.ts b/frontend/src/hooks/react-query/knowledge-base/use-knowledge-base-queries.ts index 260e3119..4d3ce1e5 100644 --- a/frontend/src/hooks/react-query/knowledge-base/use-knowledge-base-queries.ts +++ b/frontend/src/hooks/react-query/knowledge-base/use-knowledge-base-queries.ts @@ -277,6 +277,8 @@ export function useAgentProcessingJobs(agentId: string) { return useQuery({ queryKey: knowledgeBaseKeys.processingJobs(agentId), queryFn: async (): Promise => { + console.log('📊 Fetching processing jobs for agent:', agentId); + const headers = await getHeaders(); const response = await fetch(`${API_URL}/knowledge-base/agents/${agentId}/processing-jobs`, { headers }); @@ -285,9 +287,43 @@ export function useAgentProcessingJobs(agentId: string) { throw new Error(error || 'Failed to fetch processing jobs'); } - return await response.json(); + const data = await response.json(); + console.log('📊 Processing jobs response:', { + agentId, + jobCount: data.jobs?.length || 0, + activeJobs: data.jobs?.filter(job => job.status === 'processing' || job.status === 'pending').length || 0 + }); + + return data; }, enabled: !!agentId, - refetchInterval: 5000, + // Smart polling: only poll when there are active processing jobs + refetchInterval: (query) => { + const data = query.state.data as ProcessingJobsResponse | undefined; + + // If no data yet, check once after 2 seconds + if (!data) { + console.log('⏱️ No data yet, polling in 2 seconds'); + return 2000; + } + + // Check if there are any active processing jobs (pending or processing status) + const hasActiveJobs = data.jobs?.some(job => + job.status === 'processing' || job.status === 'pending' + ); + + const nextInterval = hasActiveJobs ? 3000 : 30000; + console.log('⏱️ Polling decision:', { + hasActiveJobs, + nextInterval: `${nextInterval/1000}s`, + jobStatuses: data.jobs?.map(job => job.status) || [] + }); + + // If there are active jobs, poll every 3 seconds + // If no active jobs, poll every 30 seconds (much less frequent) + return nextInterval; + }, + // Stop polling when window is not focused to save resources + refetchIntervalInBackground: false, }); }