File size: 3,414 Bytes
0bd6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Real history data loader for medical AI dashboard
// This reads actual JSON files from the API server

import type { HistoryEntry, HistoryData } from './historyLoader';

// API base URL - adjust based on your setup
const API_BASE_URL = 'http://localhost:8503/api';

// Load real history data from actual medical_history.json file via 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();
    
    // Ensure all required fields are present and fix any path issues
    const processedData = {
      pregnancyRisk: data.pregnancyRisk.map((entry: any) => ({
        ...entry,
        // Ensure all required fields are present
        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,
        // Ensure all required fields are present
        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',
        // Fix image path to use API server
        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);
    
    // Fallback to empty data instead of mock data
    return {
      pregnancyRisk: [],
      fetalClassification: [],
      total: 0
    };
  }
};

// Check if user has any history files via API
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;
  }
};

// Get list of all users with history data
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 [];
  }
};

// Clean up duplicate files for a user
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}` };
  }
};