All files / layouts healthStore.ts

62.5% Statements 10/16
25% Branches 1/4
75% Functions 3/4
57.14% Lines 8/14

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    45x           76x 76x 76x 68x 68x         1x   1x                                                  
import { create } from 'zustand'
import { apiClient } from '@/lib/axios'
 
interface ServiceStatus {
  name: string
  status: 'healthy' | 'degraded' | 'unhealthy'
  message?: string | null
}
 
interface HealthState {
  status: 'healthy' | 'degraded' | 'unhealthy'
  responseTime: number | null
  lastChecked: number | null
  services: ServiceStatus[]
  fetchHealth: () => Promise<void>
}

export const useHealthStore = create<HealthState>()((set) => ({
  status: 'healthy',
  responseTime: null,
  lastChecked: null,
  services: [],
 
  fetchHealth: async () => {
    const start = performance.now()
    try {
      const { data } = await apiClient.get('/health')
      const elapsed = Math.round(performance.now() - start)
      set({
        status: data.status,
        responseTime: elapsed,
        lastChecked: Date.now(),
        services: data.services ?? [],
      })
    } catch {
      set({
        status: 'unhealthy',
        responseTime: null,
        lastChecked: Date.now(),
        services: [],
      })
    }
  },
}))