All files / frontend/src/store analyticsStore.ts

85.86% Statements 79/92
57.14% Branches 12/21
77.77% Functions 7/9
85.86% Lines 79/92

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1391x 1x                                                                       1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x     1x 6x 6x 6x 5x 5x 5x 5x 6x 1x 1x 1x 1x 6x   1x 6x 6x 6x 5x 5x 5x 5x 6x 1x 1x 1x 1x 6x   1x 6x 6x 6x 6x 6x 6x 6x 6x         6x   1x 6x 6x 6x 6x 6x 6x 6x 6x         6x   1x 6x 6x   6x 6x 6x 6x 6x 6x 6x   6x 6x       6x 6x 6x 6x   1x     1x
import { create } from 'zustand';
import { analyticsService } from '@/services/api';
import type {
    PredictionMetrics,
    WebSDRPerformance,
    SystemPerformance
} from '@/services/api/analytics';
 
interface AnalyticsState {
    // Data
    predictionMetrics: PredictionMetrics | null;
    websdrPerformance: WebSDRPerformance[];
    systemPerformance: SystemPerformance | null;
    accuracyDistribution: {
        accuracy_ranges: string[];
        counts: number[];
    } | null;
 
    // UI State
    isLoading: boolean;
    error: string | null;
    timeRange: string;
 
    // Actions
    setLoading: (loading: boolean) => void;
    setError: (error: string | null) => void;
    setTimeRange: (timeRange: string) => void;
 
    // API Actions
    fetchPredictionMetrics: (timeRange?: string) => Promise<void>;
    fetchWebSDRPerformance: (timeRange?: string) => Promise<void>;
    fetchSystemPerformance: (timeRange?: string) => Promise<void>;
    fetchAccuracyDistribution: (timeRange?: string) => Promise<void>;
    fetchAllAnalytics: (timeRange?: string) => Promise<void>;
    refreshData: () => Promise<void>;
}
 
export const useAnalyticsStore = create<AnalyticsState>((set, get) => ({
    // Initial state
    predictionMetrics: null,
    websdrPerformance: [],
    systemPerformance: null,
    accuracyDistribution: null,
    isLoading: false,
    error: null,
    timeRange: '7d',
 
    // Basic setters
    setLoading: (loading) => set({ isLoading: loading }),
    setError: (error) => set({ error }),
    setTimeRange: (timeRange) => set({ timeRange }),
 
    // API Actions
    fetchPredictionMetrics: async (timeRange) => {
        const range = timeRange || get().timeRange;
        try {
            const metrics = await analyticsService.getPredictionMetrics(range);
            set({
                predictionMetrics: metrics,
                error: null,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch prediction metrics';
            set({ error: errorMessage });
            console.error('Failed to fetch prediction metrics:', error);
        }
    },
 
    fetchWebSDRPerformance: async (timeRange) => {
        const range = timeRange || get().timeRange;
        try {
            const performance = await analyticsService.getWebSDRPerformance(range);
            set({
                websdrPerformance: performance,
                error: null,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch WebSDR performance';
            set({ error: errorMessage });
            console.error('Failed to fetch WebSDR performance:', error);
        }
    },
 
    fetchSystemPerformance: async (timeRange) => {
        const range = timeRange || get().timeRange;
        try {
            const performance = await analyticsService.getSystemPerformance(range);
            set({
                systemPerformance: performance,
                error: null,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch system performance';
            set({ error: errorMessage });
            console.error('Failed to fetch system performance:', error);
        }
    },
 
    fetchAccuracyDistribution: async (timeRange) => {
        const range = timeRange || get().timeRange;
        try {
            const distribution = await analyticsService.getAccuracyDistribution(range);
            set({
                accuracyDistribution: distribution,
                error: null,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch accuracy distribution';
            set({ error: errorMessage });
            console.error('Failed to fetch accuracy distribution:', error);
        }
    },
 
    fetchAllAnalytics: async (timeRange) => {
        const range = timeRange || get().timeRange;
        set({ isLoading: true, error: null });
 
        try {
            await Promise.allSettled([
                get().fetchPredictionMetrics(range),
                get().fetchWebSDRPerformance(range),
                get().fetchSystemPerformance(range),
                get().fetchAccuracyDistribution(range),
            ]);
 
            set({ timeRange: range });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch analytics data';
            set({ error: errorMessage });
            console.error('Failed to fetch all analytics:', error);
        } finally {
            set({ isLoading: false });
        }
    },
 
    refreshData: async () => {
        await get().fetchAllAnalytics();
    },
}));