Skip to content

Commit b315f74

Browse files
committed
refactor: implement time series cleanup preview and execution functions
- Introduce `previewTimeSeriesCleanup` and `executeTimeSeriesCleanup` functions in the timeSeries API service for better handling of cleanup operations. - Update the Admin component to utilize these new functions, improving error handling and state management during cleanup previews and executions. - Remove redundant fetch logic, enhancing code readability and maintainability.
1 parent 50f1272 commit b315f74

2 files changed

Lines changed: 66 additions & 30 deletions

File tree

frontend/src/pages/Admin.tsx

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import {
2828
bulkUpdateTestRunsByUUID,
2929
fetchTestRun,
3030
} from '../services/api/testRuns';
31-
import { fetchTimeSeriesHistory } from '../services/api/timeSeries';
31+
import {
32+
fetchTimeSeriesHistory,
33+
previewTimeSeriesCleanup,
34+
executeTimeSeriesCleanup,
35+
} from '../services/api/timeSeries';
3236
import { useNavigate } from 'react-router-dom';
3337
import type { TestRun, UUIDGroup } from '../types';
3438

@@ -465,26 +469,19 @@ const Admin: React.FC = () => {
465469
setDataCleanupState({ ...dataCleanupState, isLoading: true });
466470

467471
try {
468-
const baseUrl = import.meta.env.VITE_API_URL || 'http://localhost:8000';
469-
const params = new URLSearchParams({
470-
cutoff_date: dataCleanupState.cutoffDate,
471-
mode: dataCleanupState.mode || 'delete-old',
472-
});
472+
const result = await previewTimeSeriesCleanup(
473+
dataCleanupState.cutoffDate,
474+
dataCleanupState.mode || 'delete-old',
475+
dataCleanupState.mode === 'compact' ? dataCleanupState.compactFrequency : undefined
476+
);
473477

474-
if (dataCleanupState.mode === 'compact') {
475-
params.append('frequency', dataCleanupState.compactFrequency);
478+
if (result.error) {
479+
throw new Error(result.error);
476480
}
477481

478-
const response = await fetch(`${baseUrl}/api/time-series/history/cleanup-preview?${params}`, {
479-
credentials: 'include',
480-
});
481-
482-
if (!response.ok) throw new Error('Failed to preview cleanup');
483-
484-
const data = await response.json();
485482
setDataCleanupState({
486483
...dataCleanupState,
487-
previewCount: data.affected_count || 0,
484+
previewCount: result.data?.affected_count || 0,
488485
isLoading: false,
489486
});
490487
} catch (err) {
@@ -499,22 +496,17 @@ const Admin: React.FC = () => {
499496
setDataCleanupState({ ...dataCleanupState, isLoading: true });
500497

501498
try {
502-
const baseUrl = import.meta.env.VITE_API_URL || 'http://localhost:8000';
503-
const response = await fetch(`${baseUrl}/api/time-series/history/cleanup`, {
504-
method: 'POST',
505-
headers: { 'Content-Type': 'application/json' },
506-
credentials: 'include',
507-
body: JSON.stringify({
508-
cutoff_date: dataCleanupState.cutoffDate,
509-
mode: dataCleanupState.mode,
510-
frequency: dataCleanupState.mode === 'compact' ? dataCleanupState.compactFrequency : undefined,
511-
}),
512-
});
499+
const result = await executeTimeSeriesCleanup(
500+
dataCleanupState.cutoffDate,
501+
dataCleanupState.mode || 'delete-old',
502+
dataCleanupState.mode === 'compact' ? dataCleanupState.compactFrequency : undefined
503+
);
513504

514-
if (!response.ok) throw new Error('Failed to execute cleanup');
505+
if (result.error) {
506+
throw new Error(result.error);
507+
}
515508

516-
const data = await response.json();
517-
alert(`Successfully ${dataCleanupState.mode === 'delete-old' ? 'deleted' : 'compacted'} ${data.deleted_count} test runs`);
509+
alert(`Successfully ${dataCleanupState.mode === 'delete-old' ? 'deleted' : 'compacted'} ${result.data?.deleted_count || 0} test runs`);
518510

519511
setDataCleanupState({
520512
...dataCleanupState,

frontend/src/services/api/timeSeries.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,48 @@ export const fetchTimeSeriesAll = async (filters?: {
190190
return apiCall<TimeSeriesDataPoint[]>(url, {
191191
signal: abortSignal
192192
});
193+
};
194+
195+
// Preview cleanup operation (how many rows will be affected)
196+
export const previewTimeSeriesCleanup = async (
197+
cutoffDate: string,
198+
mode: 'delete-old' | 'compact',
199+
frequency?: 'daily' | 'weekly' | 'monthly',
200+
abortSignal?: AbortSignal
201+
) => {
202+
const params = new URLSearchParams({
203+
cutoff_date: cutoffDate,
204+
mode,
205+
});
206+
207+
if (frequency) {
208+
params.append('frequency', frequency);
209+
}
210+
211+
return apiCall<{ affected_count: number }>(`/api/time-series/history/cleanup-preview?${params}`, {
212+
signal: abortSignal
213+
});
214+
};
215+
216+
// Execute cleanup operation
217+
export const executeTimeSeriesCleanup = async (
218+
cutoffDate: string,
219+
mode: 'delete-old' | 'compact',
220+
frequency?: 'daily' | 'weekly' | 'monthly',
221+
abortSignal?: AbortSignal
222+
) => {
223+
const body: any = {
224+
cutoff_date: cutoffDate,
225+
mode,
226+
};
227+
228+
if (frequency) {
229+
body.frequency = frequency;
230+
}
231+
232+
return apiCall<{ deleted_count: number; message: string }>('/api/time-series/history/cleanup', {
233+
method: 'POST',
234+
body: JSON.stringify(body),
235+
signal: abortSignal
236+
});
193237
};

0 commit comments

Comments
 (0)