// [DEF:health_store:Store] // @TIER: STANDARD // @PURPOSE: Manage dashboard health summary state and failing counts for UI badges. // @LAYER: UI // @RELATION: DEPENDS_ON -> api.getHealthSummary import { writable, derived } from 'svelte/store'; import { api } from '../api.js'; /** * @typedef {Object} HealthState * @property {Array} items - List of dashboard health items * @property {number} pass_count - Number of passing dashboards * @property {number} warn_count - Number of warning dashboards * @property {number} fail_count - Number of failing dashboards * @property {number} unknown_count - Number of unknown status dashboards * @property {boolean} loading - Loading state * @property {Date|null} lastUpdated - Last successful fetch timestamp */ function createHealthStore() { const { subscribe, set, update } = writable({ items: [], pass_count: 0, warn_count: 0, fail_count: 0, unknown_count: 0, loading: false, lastUpdated: null }); /** * Refresh health summary from API * @param {string|null} environmentId - Optional environment filter */ async function refresh(environmentId = null) { update(s => ({ ...s, loading: true })); try { console.log(`[HealthStore][Action] Refreshing health summary context={{'environmentId': '${environmentId}'}}`); const summary = await api.getHealthSummary(environmentId); set({ ...summary, loading: false, lastUpdated: new Date() }); } catch (error) { console.error('[HealthStore][Coherence:Failed] Failed to fetch health summary:', error); update(s => ({ ...s, loading: false })); } } return { subscribe, refresh }; } export const healthStore = createHealthStore(); /** * Derived store for the total count of failing dashboards. * Used for sidebar badges and global notifications. */ export const failingCount = derived(healthStore, $health => $health.fail_count); // [/DEF:health_store:Store]