|
| 1 | +import { useSettings } from "@/contexts/SettingsContext"; |
1 | 2 | import { Job, JobsQueryParams, JobStatus } from "@/pages/Jobs/types"; |
2 | 3 | import { api } from "@/utils/api"; |
3 | 4 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
4 | | -import { useSettings } from "@/contexts/SettingsContext"; |
5 | 5 |
|
6 | 6 | const JOBS_QUERY_KEY = "jobs"; |
7 | 7 | const JOB_FUNCTIONS_QUERY_KEY = "job-functions"; |
8 | 8 | const JOB_COUNTS_QUERY_KEY = "job-counts"; |
9 | 9 |
|
10 | 10 | // Transform frontend params to API params |
11 | 11 | const transformQueryParams = (params: JobsQueryParams, queue?: string) => { |
12 | | - const apiParams: any = { |
| 12 | + const apiParams: Record<string, unknown> = { |
13 | 13 | limit: params.limit, |
14 | 14 | offset: params.offset, |
15 | 15 | status: params.status || undefined, |
16 | 16 | worker: params.worker || undefined, |
17 | 17 | function: params.function || undefined, |
18 | 18 | search: params.search || undefined, |
| 19 | + sort_by: params.sort_by || "created_at", |
| 20 | + sort_order: params.sort_order || "desc", |
19 | 21 | }; |
20 | | - |
| 22 | + |
21 | 23 | // Only include queue if it's defined |
22 | 24 | if (queue) { |
23 | 25 | apiParams.queue = queue; |
24 | 26 | } |
25 | | - |
| 27 | + |
26 | 28 | if (params.created_after || params.created_before) { |
27 | 29 | apiParams.created_after = params.created_after; |
28 | 30 | apiParams.created_before = params.created_before; |
29 | 31 | } |
30 | | - |
| 32 | + |
31 | 33 | if (params.sort_by || params.sort_order) { |
32 | 34 | apiParams.sort_by = params.sort_by; |
33 | 35 | apiParams.sort_order = params.sort_order; |
34 | 36 | } |
35 | | - |
| 37 | + |
36 | 38 | return apiParams; |
37 | 39 | }; |
38 | 40 |
|
39 | 41 | export const useJobs = (params: JobsQueryParams) => { |
40 | 42 | return useQuery({ |
41 | 43 | queryKey: [JOBS_QUERY_KEY, params], |
42 | 44 | queryFn: async () => { |
43 | | - const queues = Array.isArray(params.queue) ? params.queue.filter(Boolean) : params.queue ? [params.queue] : []; |
44 | | - |
45 | | - if (queues.length === 0) { |
46 | | - // No queue filter - get all jobs |
47 | | - const apiParams = transformQueryParams(params, undefined); |
48 | | - const response = await api.get("/jobs", { |
49 | | - params: apiParams, |
50 | | - }); |
51 | | - const data = response.data; |
52 | | - return { |
53 | | - data: data.data as Job[], |
54 | | - total: data.total, |
55 | | - offset: data.offset, |
56 | | - limit: data.limit, |
57 | | - has_more: data.has_more, |
58 | | - }; |
59 | | - } else if (queues.length === 1) { |
60 | | - // Single queue filter |
61 | | - const apiParams = transformQueryParams(params, queues[0]); |
62 | | - const response = await api.get("/jobs", { |
63 | | - params: apiParams, |
64 | | - }); |
65 | | - const data = response.data; |
66 | | - return { |
67 | | - data: data.data as Job[], |
68 | | - total: data.total, |
69 | | - offset: data.offset, |
70 | | - limit: data.limit, |
71 | | - has_more: data.has_more, |
72 | | - }; |
73 | | - } else { |
74 | | - // Multiple queues - fetch from each and combine |
75 | | - const responses = await Promise.all( |
76 | | - queues.map(queue => |
77 | | - api.get("/jobs", { |
78 | | - params: transformQueryParams({ ...params, limit: Math.ceil(params.limit / queues.length) }, queue) |
79 | | - }) |
80 | | - ) |
81 | | - ); |
82 | | - |
83 | | - const allJobs = responses.flatMap(response => response.data.data as Job[]); |
84 | | - const totalJobs = responses.reduce((sum, response) => sum + response.data.total, 0); |
85 | | - |
86 | | - // Sort and paginate combined results |
87 | | - const sortedJobs = allJobs.sort((a, b) => |
88 | | - new Date(b.created_at).getTime() - new Date(a.created_at).getTime() |
| 45 | + const queues = Array.isArray(params.queue) |
| 46 | + ? params.queue.filter(Boolean) |
| 47 | + : params.queue |
| 48 | + ? [params.queue] |
| 49 | + : []; |
| 50 | + |
| 51 | + // Build API params with all filters including sorting and pagination |
| 52 | + const apiParams = transformQueryParams(params, undefined); |
| 53 | + |
| 54 | + const response = await api.get("/jobs", { |
| 55 | + params: apiParams, |
| 56 | + }); |
| 57 | + const data = response.data; |
| 58 | + |
| 59 | + let filteredJobs = data.data as Job[]; |
| 60 | + let totalJobs = data.total; |
| 61 | + |
| 62 | + // Apply queue filtering on client side if needed |
| 63 | + if (queues.length > 0) { |
| 64 | + filteredJobs = filteredJobs.filter( |
| 65 | + (job) => job.queue && queues.includes(job.queue) |
89 | 66 | ); |
90 | | - |
91 | | - const startIndex = params.offset; |
92 | | - const endIndex = startIndex + params.limit; |
93 | | - const paginatedJobs = sortedJobs.slice(startIndex, endIndex); |
94 | | - |
95 | | - return { |
96 | | - data: paginatedJobs, |
97 | | - total: totalJobs, |
98 | | - offset: params.offset, |
99 | | - limit: params.limit, |
100 | | - has_more: endIndex < sortedJobs.length, |
101 | | - }; |
| 67 | + // For accurate total count when filtering, we need to estimate |
| 68 | + // This is an approximation since the API doesn't support multi-queue filtering |
| 69 | + totalJobs = filteredJobs.length; |
102 | 70 | } |
| 71 | + |
| 72 | + // The backend now handles sorting and pagination, so we just return the data as is |
| 73 | + return { |
| 74 | + data: filteredJobs, |
| 75 | + total: totalJobs, |
| 76 | + offset: params.offset, |
| 77 | + limit: params.limit, |
| 78 | + has_more: filteredJobs.length === params.limit, |
| 79 | + }; |
103 | 80 | }, |
104 | 81 | staleTime: 5000, |
105 | 82 | }); |
@@ -136,7 +113,9 @@ export const useJobCounts = () => { |
136 | 113 | const response = await api.get("/jobs/counts"); |
137 | 114 | return response.data as Record<JobStatus, number>; |
138 | 115 | }, |
139 | | - refetchInterval: settings.autoRefresh ? settings.jobsRefreshInterval : false, |
| 116 | + refetchInterval: settings.autoRefresh |
| 117 | + ? settings.jobsRefreshInterval |
| 118 | + : false, |
140 | 119 | }); |
141 | 120 | }; |
142 | 121 |
|
|
0 commit comments