Add new frontend - fix 9
This commit is contained in:
221
frontend/src/api/services/trainingService.ts
Normal file
221
frontend/src/api/services/trainingService.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
// src/api/services/TrainingService.ts
|
||||
import { apiClient } from '../base/apiClient';
|
||||
import {
|
||||
ApiResponse,
|
||||
TrainingRequest,
|
||||
TrainingJobStatus,
|
||||
TrainedModel,
|
||||
} from '../types/api';
|
||||
|
||||
export interface TrainingJobProgress {
|
||||
id: string;
|
||||
status: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
|
||||
progress: number;
|
||||
current_step?: string;
|
||||
total_steps?: number;
|
||||
step_details?: string;
|
||||
estimated_completion?: string;
|
||||
logs?: string[];
|
||||
}
|
||||
|
||||
export interface ModelMetrics {
|
||||
mape: number;
|
||||
rmse: number;
|
||||
mae: number;
|
||||
r2_score: number;
|
||||
training_samples: number;
|
||||
validation_samples: number;
|
||||
features_used: string[];
|
||||
}
|
||||
|
||||
export interface TrainingConfiguration {
|
||||
include_weather: boolean;
|
||||
include_traffic: boolean;
|
||||
min_data_points: number;
|
||||
forecast_horizon_days: number;
|
||||
cross_validation_folds: number;
|
||||
hyperparameter_tuning: boolean;
|
||||
products?: string[];
|
||||
}
|
||||
|
||||
export class TrainingService {
|
||||
/**
|
||||
* Start new training job
|
||||
*/
|
||||
async startTraining(config: TrainingConfiguration): Promise<TrainingJobStatus> {
|
||||
const response = await apiClient.post<ApiResponse<TrainingJobStatus>>(
|
||||
'/training/jobs',
|
||||
config
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get training job status
|
||||
*/
|
||||
async getTrainingStatus(jobId: string): Promise<TrainingJobProgress> {
|
||||
const response = await apiClient.get<ApiResponse<TrainingJobProgress>>(
|
||||
`/training/jobs/${jobId}`
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all training jobs
|
||||
*/
|
||||
async getTrainingHistory(params?: {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
status?: string;
|
||||
}): Promise<{
|
||||
jobs: TrainingJobStatus[];
|
||||
total: number;
|
||||
page: number;
|
||||
pages: number;
|
||||
}> {
|
||||
const response = await apiClient.get<ApiResponse<any>>('/training/jobs', { params });
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel training job
|
||||
*/
|
||||
async cancelTraining(jobId: string): Promise<void> {
|
||||
await apiClient.post(`/training/jobs/${jobId}/cancel`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trained models
|
||||
*/
|
||||
async getModels(params?: {
|
||||
productName?: string;
|
||||
active?: boolean;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
}): Promise<{
|
||||
models: TrainedModel[];
|
||||
total: number;
|
||||
page: number;
|
||||
pages: number;
|
||||
}> {
|
||||
const response = await apiClient.get<ApiResponse<any>>('/training/models', { params });
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get specific model details
|
||||
*/
|
||||
async getModel(modelId: string): Promise<TrainedModel> {
|
||||
const response = await apiClient.get<ApiResponse<TrainedModel>>(
|
||||
`/training/models/${modelId}`
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get model metrics
|
||||
*/
|
||||
async getModelMetrics(modelId: string): Promise<ModelMetrics> {
|
||||
const response = await apiClient.get<ApiResponse<ModelMetrics>>(
|
||||
`/training/models/${modelId}/metrics`
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate/deactivate model
|
||||
*/
|
||||
async toggleModelStatus(modelId: string, active: boolean): Promise<TrainedModel> {
|
||||
const response = await apiClient.patch<ApiResponse<TrainedModel>>(
|
||||
`/training/models/${modelId}`,
|
||||
{ is_active: active }
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete model
|
||||
*/
|
||||
async deleteModel(modelId: string): Promise<void> {
|
||||
await apiClient.delete(`/training/models/${modelId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Train specific product
|
||||
*/
|
||||
async trainProduct(productName: string, config?: Partial<TrainingConfiguration>): Promise<TrainingJobStatus> {
|
||||
const response = await apiClient.post<ApiResponse<TrainingJobStatus>>(
|
||||
'/training/products/train',
|
||||
{
|
||||
product_name: productName,
|
||||
...config,
|
||||
}
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get training statistics
|
||||
*/
|
||||
async getTrainingStats(): Promise<{
|
||||
total_models: number;
|
||||
active_models: number;
|
||||
avg_accuracy: number;
|
||||
last_training_date: string | null;
|
||||
products_trained: number;
|
||||
training_time_avg_minutes: number;
|
||||
}> {
|
||||
const response = await apiClient.get<ApiResponse<any>>('/training/stats');
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate training data
|
||||
*/
|
||||
async validateTrainingData(products?: string[]): Promise<{
|
||||
valid: boolean;
|
||||
errors: string[];
|
||||
warnings: string[];
|
||||
product_data_points: Record<string, number>;
|
||||
recommendation: string;
|
||||
}> {
|
||||
const response = await apiClient.post<ApiResponse<any>>('/training/validate', {
|
||||
products,
|
||||
});
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get training recommendations
|
||||
*/
|
||||
async getTrainingRecommendations(): Promise<{
|
||||
should_retrain: boolean;
|
||||
reasons: string[];
|
||||
recommended_products: string[];
|
||||
optimal_config: TrainingConfiguration;
|
||||
}> {
|
||||
const response = await apiClient.get<ApiResponse<any>>('/training/recommendations');
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get training logs
|
||||
*/
|
||||
async getTrainingLogs(jobId: string): Promise<string[]> {
|
||||
const response = await apiClient.get<ApiResponse<string[]>>(`/training/jobs/${jobId}/logs`);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export model
|
||||
*/
|
||||
async exportModel(modelId: string, format: 'pickle' | 'onnx' = 'pickle'): Promise<Blob> {
|
||||
const response = await apiClient.get(`/training/models/${modelId}/export`, {
|
||||
params: { format },
|
||||
responseType: 'blob',
|
||||
});
|
||||
return response as unknown as Blob;
|
||||
}
|
||||
}
|
||||
|
||||
export const trainingService = new TrainingService();
|
||||
Reference in New Issue
Block a user