|
| 1 | +import { |
| 2 | + Action, Module, Mutation, VuexModule, getModule, |
| 3 | +} from 'vuex-module-decorators' |
| 4 | + |
| 5 | +import store from '@/store' |
| 6 | +import { HealthHistory, HealthSummary } from '@/types/health-monitor' |
| 7 | +import back_axios, { isBackendOffline } from '@/utils/api' |
| 8 | + |
| 9 | +@Module({ dynamic: true, store, name: 'health_monitor' }) |
| 10 | +class HealthMonitorStore extends VuexModule { |
| 11 | + API_URL = '/health-monitor/v1.0/health' |
| 12 | + |
| 13 | + summary: HealthSummary | null = null |
| 14 | + |
| 15 | + history: HealthHistory | null = null |
| 16 | + |
| 17 | + loading = false |
| 18 | + |
| 19 | + error: string | null = null |
| 20 | + |
| 21 | + @Mutation |
| 22 | + setSummary(value: HealthSummary | null): void { |
| 23 | + this.summary = value |
| 24 | + } |
| 25 | + |
| 26 | + @Mutation |
| 27 | + setHistory(value: HealthHistory | null): void { |
| 28 | + this.history = value |
| 29 | + } |
| 30 | + |
| 31 | + @Mutation |
| 32 | + setLoading(value: boolean): void { |
| 33 | + this.loading = value |
| 34 | + } |
| 35 | + |
| 36 | + @Mutation |
| 37 | + setError(message: string | null): void { |
| 38 | + this.error = message |
| 39 | + } |
| 40 | + |
| 41 | + @Action |
| 42 | + async fetchSummary(): Promise<void> { |
| 43 | + this.setLoading(true) |
| 44 | + this.setError(null) |
| 45 | + |
| 46 | + await back_axios({ |
| 47 | + method: 'get', |
| 48 | + url: `${this.API_URL}/summary`, |
| 49 | + timeout: 10000, |
| 50 | + }) |
| 51 | + .then((response) => { |
| 52 | + this.setSummary(response.data as HealthSummary) |
| 53 | + }) |
| 54 | + .catch((error) => { |
| 55 | + this.setSummary(null) |
| 56 | + if (isBackendOffline(error)) { |
| 57 | + return |
| 58 | + } |
| 59 | + this.setError(`Failed to fetch health summary: ${error.message}`) |
| 60 | + }) |
| 61 | + .finally(() => { |
| 62 | + this.setLoading(false) |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + @Action |
| 67 | + async fetchHistory(limit = 200): Promise<void> { |
| 68 | + await back_axios({ |
| 69 | + method: 'get', |
| 70 | + url: `${this.API_URL}/history`, |
| 71 | + params: { limit }, |
| 72 | + timeout: 10000, |
| 73 | + }) |
| 74 | + .then((response) => { |
| 75 | + this.setHistory(response.data as HealthHistory) |
| 76 | + }) |
| 77 | + .catch((error) => { |
| 78 | + this.setHistory(null) |
| 79 | + if (isBackendOffline(error)) { |
| 80 | + return |
| 81 | + } |
| 82 | + this.setError(`Failed to fetch health history: ${error.message}`) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +const health_monitor = getModule(HealthMonitorStore) |
| 88 | + |
| 89 | +export { HealthMonitorStore } |
| 90 | +export default health_monitor |
0 commit comments