@@ -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' ;
3236import { useNavigate } from 'react-router-dom' ;
3337import 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 ,
0 commit comments