All files / frontend/src/store systemStore.ts

0% Statements 0/58
100% Branches 1/1
100% Functions 1/1
0% Lines 0/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                     
/**
 * System Store
 * 
 * Manages system-level state and health monitoring
 */
 
import { create } from 'zustand';
import type { ServiceHealth, ModelPerformanceMetrics } from '@/services/api/types';
import { systemService, inferenceService } from '@/services/api';
 
interface SystemStore {
    servicesHealth: Record<string, ServiceHealth>;
    modelPerformance: ModelPerformanceMetrics | null;
    isLoading: boolean;
    error: string | null;
    lastCheck: Date | null;
    
    checkAllServices: () => Promise<void>;
    checkService: (serviceName: string) => Promise<void>;
    fetchModelPerformance: () => Promise<void>;
    
    isServiceHealthy: (serviceName: string) => boolean;
    getServiceStatus: (serviceName: string) => ServiceHealth | null;
    
    refreshAll: () => Promise<void>;
}
 
export const useSystemStore = create<SystemStore>((set, get) => ({
    servicesHealth: {},
    modelPerformance: null,
    isLoading: false,
    error: null,
    lastCheck: null,
 
    checkAllServices: async () => {
        set({ isLoading: true, error: null });
        try {
            const servicesHealth = await systemService.checkAllServicesHealth();
            set({ 
                servicesHealth, 
                lastCheck: new Date(),
                isLoading: false,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to check services';
            set({ error: errorMessage, isLoading: false });
            console.error('Service health check error:', error);
        }
    },
 
    checkService: async (serviceName: string) => {
        try {
            const health = await systemService.checkServiceHealth(serviceName);
            set((state) => ({
                servicesHealth: {
                    ...state.servicesHealth,
                    [serviceName]: health,
                },
            }));
        } catch (error) {
            console.error(`Failed to check ${serviceName} health:`, error);
        }
    },
 
    fetchModelPerformance: async () => {
        try {
            const modelPerformance = await inferenceService.getModelPerformance();
            set({ modelPerformance });
        } catch (error) {
            console.error('Failed to fetch model performance:', error);
            // Model service might not be available, this is not critical
        }
    },
 
    isServiceHealthy: (serviceName: string) => {
        const health = get().servicesHealth[serviceName];
        return health?.status === 'healthy';
    },
 
    getServiceStatus: (serviceName: string) => {
        return get().servicesHealth[serviceName] || null;
    },
 
    refreshAll: async () => {
        await Promise.all([
            get().checkAllServices(),
            get().fetchModelPerformance(),
        ]);
    },
}));