Исправляет ложные «Ошибки проверок»: GET /v1/ready отдаёт строки ok/memory, а не boolean. Уплотнён ReUI Frame/donut layout на вкладке Система. Co-authored-by: Cursor <[email protected]>
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { queryOptions } from '@tanstack/react-query'
|
|
import { apiFetch, apiJSON, apiMutate } from '@/lib/api-client'
|
|
|
|
export interface HealthStatus {
|
|
ok: boolean
|
|
status?: string
|
|
error?: string
|
|
}
|
|
|
|
export interface ReadyStatus {
|
|
status?: string
|
|
/** Backend: string ("ok"|"memory"|"unavailable"), boolean, or { ok, error }. */
|
|
checks?: Record<string, boolean | string | { ok?: boolean; error?: string }>
|
|
}
|
|
|
|
export interface VersionInfo {
|
|
version?: string
|
|
app?: string
|
|
git_sha?: string
|
|
build_time?: string
|
|
}
|
|
|
|
export const monitoringKeys = {
|
|
all: ['monitoring'] as const,
|
|
health: () => [...monitoringKeys.all, 'health'] as const,
|
|
ready: () => [...monitoringKeys.all, 'ready'] as const,
|
|
version: () => [...monitoringKeys.all, 'version'] as const,
|
|
}
|
|
|
|
async function fetchHealth(): Promise<HealthStatus> {
|
|
const res = await apiFetch('/v1/health', { method: 'GET' })
|
|
let body: { status?: string } = {}
|
|
try {
|
|
body = (await res.json()) as { status?: string }
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
return { ok: res.ok, status: body.status, error: res.ok ? undefined : `HTTP ${res.status}` }
|
|
}
|
|
|
|
export function monitoringHealthQueryOptions() {
|
|
return queryOptions<HealthStatus>({
|
|
queryKey: monitoringKeys.health(),
|
|
queryFn: fetchHealth,
|
|
staleTime: 15_000,
|
|
})
|
|
}
|
|
|
|
export function monitoringReadyQueryOptions() {
|
|
return queryOptions<ReadyStatus>({
|
|
queryKey: monitoringKeys.ready(),
|
|
queryFn: () => apiJSON<ReadyStatus>('/v1/ready'),
|
|
staleTime: 15_000,
|
|
})
|
|
}
|
|
|
|
export function monitoringVersionQueryOptions() {
|
|
return queryOptions<VersionInfo>({
|
|
queryKey: monitoringKeys.version(),
|
|
queryFn: () => apiJSON<VersionInfo>('/v1/version'),
|
|
staleTime: 60_000,
|
|
})
|
|
}
|
|
|
|
export async function fetchLog(endpoint: string): Promise<string> {
|
|
const res = await apiFetch(endpoint, { method: 'GET' })
|
|
return await res.text()
|
|
}
|
|
|
|
export async function clearLog(endpoint: string): Promise<void> {
|
|
await apiMutate(endpoint, 'DELETE', {})
|
|
}
|