Files
Atomizer/atomizer-dashboard/frontend/src/api/client.ts

199 lines
6.1 KiB
TypeScript
Raw Normal View History

import { StudyListResponse, HistoryResponse, PruningResponse, StudyStatus } from '../types';
const API_BASE = '/api';
export interface OptimizationControlResponse {
success: boolean;
message: string;
pid?: number;
}
export interface ReadmeResponse {
content: string;
path: string;
}
export interface ReportResponse {
content: string;
generated_at?: string;
}
export interface ConfigResponse {
config: Record<string, any>;
objectives: Array<{
name: string;
direction: string;
weight?: number;
target?: number;
units?: string;
}>;
design_variables: Array<{
name: string;
min: number;
max: number;
baseline?: number;
units?: string;
}>;
constraints?: Array<{
name: string;
type: string;
max_value?: number;
min_value?: number;
units?: string;
}>;
}
export interface ProcessStatus {
is_running: boolean;
pid?: number;
start_time?: string;
iteration?: number;
fea_count?: number;
nn_count?: number;
}
class ApiClient {
async getStudies(): Promise<StudyListResponse> {
const response = await fetch(`${API_BASE}/optimization/studies`);
if (!response.ok) throw new Error('Failed to fetch studies');
return response.json();
}
async getStudyStatus(studyId: string): Promise<StudyStatus> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/status`);
if (!response.ok) throw new Error('Failed to fetch study status');
return response.json();
}
async getStudyHistory(studyId: string): Promise<HistoryResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/history`);
if (!response.ok) throw new Error('Failed to fetch study history');
return response.json();
}
async getStudyPruning(studyId: string): Promise<PruningResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/pruning`);
if (!response.ok) throw new Error('Failed to fetch pruning data');
return response.json();
}
async createStudy(config: any): Promise<{ study_id: string }> {
const response = await fetch(`${API_BASE}/optimization/studies`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(config),
});
if (!response.ok) throw new Error('Failed to create study');
return response.json();
}
async getStudyReport(studyId: string): Promise<ReportResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/report`);
if (!response.ok) throw new Error('Failed to fetch report');
return response.json();
}
async getStudyReadme(studyId: string): Promise<ReadmeResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/readme`);
if (!response.ok) throw new Error('Failed to fetch README');
return response.json();
}
async getStudyConfig(studyId: string): Promise<ConfigResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/config`);
if (!response.ok) throw new Error('Failed to fetch config');
return response.json();
}
async getConsoleOutput(studyId: string, lines: number = 200): Promise<{
lines: string[];
total_lines: number;
displayed_lines: number;
log_file: string | null;
timestamp: string;
message?: string;
}> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/console?lines=${lines}`);
if (!response.ok) throw new Error('Failed to fetch console output');
return response.json();
}
async getProcessStatus(studyId: string): Promise<ProcessStatus> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/process`);
if (!response.ok) throw new Error('Failed to fetch process status');
return response.json();
}
// Control operations
async startOptimization(studyId: string, options?: {
freshStart?: boolean;
maxIterations?: number;
feaBatchSize?: number;
tuneTrials?: number;
ensembleSize?: number;
patience?: number;
}): Promise<OptimizationControlResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options || {}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to start optimization');
}
return response.json();
}
async stopOptimization(studyId: string): Promise<OptimizationControlResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/stop`, {
method: 'POST',
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to stop optimization');
}
return response.json();
}
async validateOptimization(studyId: string, options?: {
topN?: number;
}): Promise<OptimizationControlResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/validate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options || {}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to start validation');
}
return response.json();
}
async generateReport(studyId: string): Promise<ReportResponse> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/report/generate`, {
method: 'POST',
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to generate report');
}
return response.json();
}
// Optuna dashboard
async launchOptunaDashboard(studyId: string): Promise<{ url: string; pid: number }> {
const response = await fetch(`${API_BASE}/optimization/studies/${studyId}/optuna-dashboard`, {
method: 'POST',
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to launch Optuna dashboard');
}
return response.json();
}
}
export const apiClient = new ApiClient();