Repository navigation
Expand file tree
/
Copy pathuseApi.ts
More file actions
175 lines (163 loc) · 4.87 KB
/
Copy pathuseApi.ts
File metadata and controls
175 lines (163 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use client';
import { useQuery, useQueries, UseQueryOptions } from '@tanstack/react-query';
import { api } from '@/lib/api';
// Query keys for consistent caching
export const queryKeys = {
binaries: ['binaries'] as const,
environments: ['environments'] as const,
pythonVersions: ['pythonVersions'] as const,
environmentsForBinary: (binaryId: string) =>
['environmentsForBinary', binaryId] as const,
commitsForBinaryEnvironment: (binaryId: string, environmentId: string) =>
['commitsForBinaryEnvironment', binaryId, environmentId] as const,
benchmarkNames: (binaryId: string, environmentId: string) =>
['benchmarkNames', binaryId, environmentId] as const,
benchmarkTrends: (params: {
benchmark_name: string;
binary_id: string;
environment_id: string;
limit: number;
}) => ['benchmarkTrends', params] as const,
batchBenchmarkTrends: (
queries: Array<{
benchmark_name: string;
binary_id: string;
environment_id: string;
limit: number;
}>
) => ['batchBenchmarkTrends', queries] as const,
};
// Basic data fetching hooks
export function useBinaries() {
return useQuery({
queryKey: queryKeys.binaries,
queryFn: api.getBinaries,
staleTime: 10 * 60 * 1000, // 10 minutes - binaries rarely change
});
}
export function useEnvironments() {
return useQuery({
queryKey: queryKeys.environments,
queryFn: api.getEnvironments,
staleTime: 10 * 60 * 1000, // 10 minutes - environments rarely change
});
}
export function usePythonVersions() {
return useQuery({
queryKey: queryKeys.pythonVersions,
queryFn: api.getPythonVersions,
staleTime: 5 * 60 * 1000, // 5 minutes
});
}
// Dependent data fetching hooks
export function useEnvironmentsForBinary(binaryId: string | null) {
return useQuery({
queryKey: queryKeys.environmentsForBinary(binaryId || ''),
queryFn: () => api.getEnvironmentsForBinary(binaryId!),
enabled: !!binaryId,
staleTime: 5 * 60 * 1000,
});
}
export function useCommitsForBinaryEnvironment(
binaryId: string | null,
environmentId: string | null
) {
return useQuery({
queryKey: queryKeys.commitsForBinaryEnvironment(
binaryId || '',
environmentId || ''
),
queryFn: () =>
api.getCommitsForBinaryAndEnvironment(binaryId!, environmentId!),
enabled: !!binaryId && !!environmentId,
staleTime: 2 * 60 * 1000, // 2 minutes - commits change more frequently
});
}
export function useBenchmarkNames(
binaryId: string | null,
environmentId: string | null
) {
return useQuery({
queryKey: queryKeys.benchmarkNames(binaryId || '', environmentId || ''),
queryFn: () => api.getBenchmarkNames(binaryId!, environmentId!),
enabled: !!binaryId && !!environmentId,
staleTime: 5 * 60 * 1000,
});
}
// Single benchmark trend hook
export function useBenchmarkTrends(
params: {
benchmark_name: string;
binary_id: string;
environment_id: string;
limit: number;
} | null
) {
return useQuery({
queryKey: params
? queryKeys.benchmarkTrends(params)
: ['benchmarkTrends', 'disabled'],
queryFn: () => api.getBenchmarkTrends(params!),
enabled: !!params,
staleTime: 1 * 60 * 1000, // 1 minute - benchmark data changes frequently
});
}
// Batch benchmark trends hook - optimized for multiple queries
export function useBatchBenchmarkTrends(
queries: Array<{
benchmark_name: string;
binary_id: string;
environment_id: string;
limit: number;
}>
) {
return useQueries({
queries: queries.map((query) => ({
queryKey: queryKeys.benchmarkTrends(query),
queryFn: () => api.getBenchmarkTrends(query),
staleTime: 1 * 60 * 1000,
enabled:
!!query.benchmark_name && !!query.binary_id && !!query.environment_id,
})),
});
}
// Optimized batch trends for build comparison
export function useBatchBenchmarkTrendsOptimized(
queries: Array<{
benchmark_name: string;
binary_id: string;
environment_id: string;
limit: number;
}>,
enabled: boolean = true
) {
return useQuery({
queryKey: queryKeys.batchBenchmarkTrends(queries),
queryFn: () => api.getBatchBenchmarkTrends(queries),
enabled: enabled && queries.length > 0,
staleTime: 1 * 60 * 1000,
// Split large requests into smaller chunks to avoid overwhelming the server
select: (data) => {
// Group results by binary_id for easier processing
const grouped = data.reduce((acc, result) => {
const key = `${result.binary_id}_${result.environment_id}`;
if (!acc[key]) acc[key] = [];
acc[key].push(result);
return acc;
}, {} as Record<string, typeof data>);
return grouped;
},
});
}
// Generic hook for any API call with custom options
export function useApiQuery<T>(
queryKey: string[],
queryFn: () => Promise<T>,
options?: Partial<UseQueryOptions<T>>
) {
return useQuery({
queryKey,
queryFn,
...options,
});
}