|
|
|
|
|
|
|
|
|
|
|
import type { HistoryEntry, HistoryData } from './historyLoader'; |
|
|
|
|
|
|
|
|
const API_BASE_URL = 'http://localhost:8503/api'; |
|
|
|
|
|
|
|
|
export const loadRealHistoryData = async (userId: string): Promise<HistoryData> => { |
|
|
try { |
|
|
const response = await fetch(`${API_BASE_URL}/history/${userId}`); |
|
|
|
|
|
if (!response.ok) { |
|
|
throw new Error(`API request failed: ${response.status}`); |
|
|
} |
|
|
|
|
|
const data = await response.json(); |
|
|
|
|
|
|
|
|
const processedData = { |
|
|
pregnancyRisk: data.pregnancyRisk.map((entry: any) => ({ |
|
|
...entry, |
|
|
|
|
|
id: entry.id || `preg_${Date.now()}`, |
|
|
timestamp: entry.timestamp || new Date().toISOString(), |
|
|
type: 'pregnancy_risk' as const, |
|
|
user_id: userId, |
|
|
confidence: entry.confidence || 0, |
|
|
prediction: entry.prediction || 'Unknown', |
|
|
input_data: entry.input_data || {}, |
|
|
probabilities: entry.probabilities || { high_risk: 0, low_risk: 0 } |
|
|
})), |
|
|
fetalClassification: data.fetalClassification.map((entry: any) => ({ |
|
|
...entry, |
|
|
|
|
|
id: entry.id || `fetal_${Date.now()}`, |
|
|
timestamp: entry.timestamp || new Date().toISOString(), |
|
|
type: 'fetal_classification' as const, |
|
|
user_id: userId, |
|
|
confidence: entry.confidence || 0, |
|
|
predicted_label: entry.predicted_label || 'Unknown', |
|
|
image_filename: entry.image_filename || 'unknown.png', |
|
|
|
|
|
image_path: entry.image_path ? `http://localhost:8503/${entry.image_path}` : null, |
|
|
top_predictions: entry.top_predictions || [] |
|
|
})), |
|
|
total: data.total || 0 |
|
|
}; |
|
|
|
|
|
return processedData; |
|
|
} catch (error) { |
|
|
console.error('Error loading real history data:', error); |
|
|
|
|
|
|
|
|
return { |
|
|
pregnancyRisk: [], |
|
|
fetalClassification: [], |
|
|
total: 0 |
|
|
}; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
export const hasHistoryData = async (userId: string): Promise<boolean> => { |
|
|
try { |
|
|
const response = await fetch(`${API_BASE_URL}/history/${userId}`); |
|
|
if (!response.ok) return false; |
|
|
|
|
|
const data = await response.json(); |
|
|
return data.total > 0; |
|
|
} catch { |
|
|
return false; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
export const getUsersWithHistory = async (): Promise<string[]> => { |
|
|
try { |
|
|
const response = await fetch(`${API_BASE_URL}/users`); |
|
|
if (!response.ok) return []; |
|
|
|
|
|
return await response.json(); |
|
|
} catch { |
|
|
return []; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
export const cleanupUserData = async (userId: string): Promise<{success: boolean, message: string}> => { |
|
|
try { |
|
|
const response = await fetch(`${API_BASE_URL}/cleanup/${userId}`); |
|
|
if (!response.ok) { |
|
|
return { success: false, message: 'API request failed' }; |
|
|
} |
|
|
|
|
|
const data = await response.json(); |
|
|
return { success: true, message: data.message }; |
|
|
} catch (error) { |
|
|
return { success: false, message: `Error: ${error}` }; |
|
|
} |
|
|
}; |