202 lines
5.5 KiB
TypeScript
202 lines
5.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api } from './client'
|
|
|
|
export interface DashboardScope {
|
|
city?: string
|
|
client?: string
|
|
object_ids?: number[]
|
|
tag_ids?: number[]
|
|
}
|
|
|
|
export interface DashboardCheckDefinition {
|
|
check_key: string
|
|
enabled: boolean
|
|
interval_sec_default: number
|
|
plugin_action: string
|
|
categories: string[]
|
|
timeout_sec: number
|
|
concurrency: number
|
|
updated_at: string
|
|
}
|
|
|
|
export interface DashboardObjectOverride {
|
|
object_id: number
|
|
check_key: string
|
|
enabled_override: boolean | null
|
|
interval_sec_override: number | null
|
|
categories_override: string[] | null
|
|
updated_at: string
|
|
}
|
|
|
|
export interface DashboardMonitorStatus {
|
|
status: string
|
|
run_type: string | null
|
|
scope: Record<string, unknown> | null
|
|
started_at: string | null
|
|
finished_at: string | null
|
|
last_success_at: string | null
|
|
last_error: string | null
|
|
last_counts: Record<string, unknown> | null
|
|
}
|
|
|
|
export interface DashboardObjectPolicy {
|
|
object_id: number
|
|
mode: 'inherit' | 'include' | 'exclude'
|
|
updated_at: string
|
|
}
|
|
|
|
export interface DashboardTagPolicy {
|
|
tag_id: number
|
|
enabled: boolean
|
|
updated_at: string
|
|
}
|
|
|
|
export interface DashboardPolicyResponse {
|
|
object_policies: DashboardObjectPolicy[]
|
|
tag_policies: DashboardTagPolicy[]
|
|
}
|
|
|
|
export interface DashboardObjectCheckSummary {
|
|
status_counts: Record<string, number>
|
|
last_checked_at: string | null
|
|
max_age_sec: number | null
|
|
}
|
|
|
|
export interface DashboardObjectSummary {
|
|
object_id: number
|
|
object_name: string
|
|
city: string | null
|
|
client: string | null
|
|
allowed: boolean
|
|
monitor_mode: 'inherit' | 'include' | 'exclude'
|
|
device_count: number
|
|
checks: Record<string, DashboardObjectCheckSummary>
|
|
health_score: number | null
|
|
}
|
|
|
|
export interface DashboardHomeSummary {
|
|
generated_at: string
|
|
scope: DashboardScope
|
|
totals: Record<string, number>
|
|
checks_meta: DashboardCheckDefinition[]
|
|
objects: DashboardObjectSummary[]
|
|
problem_objects: DashboardObjectSummary[]
|
|
}
|
|
|
|
export interface DashboardChecksResponse {
|
|
checks: DashboardCheckDefinition[]
|
|
object_overrides: DashboardObjectOverride[]
|
|
}
|
|
|
|
export const useDashboardHomeSummary = (scope: DashboardScope) =>
|
|
useQuery({
|
|
queryKey: ['dashboard-home-summary', scope],
|
|
queryFn: () =>
|
|
api
|
|
.get<DashboardHomeSummary>('/api/v1/dashboard/home-summary', { params: scope })
|
|
.then((r) => r.data),
|
|
refetchInterval: 30_000,
|
|
})
|
|
|
|
export const useDashboardMonitorStatus = () =>
|
|
useQuery({
|
|
queryKey: ['dashboard-monitor-status'],
|
|
queryFn: () =>
|
|
api
|
|
.get<DashboardMonitorStatus>('/api/v1/dashboard/monitor/status')
|
|
.then((r) => r.data),
|
|
refetchInterval: 10_000,
|
|
})
|
|
|
|
export const useDashboardRunNow = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: DashboardScope & { force?: boolean }) =>
|
|
api.post('/api/v1/dashboard/monitor/run', body).then((r) => r.data),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['dashboard-monitor-status'] })
|
|
qc.invalidateQueries({ queryKey: ['dashboard-home-summary'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useDashboardPolicy = () =>
|
|
useQuery({
|
|
queryKey: ['dashboard-policy'],
|
|
queryFn: () =>
|
|
api
|
|
.get<DashboardPolicyResponse>('/api/v1/dashboard/monitor/policy')
|
|
.then((r) => r.data),
|
|
})
|
|
|
|
export const usePatchDashboardObjectPolicy = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: Array<{ object_id: number; mode: 'inherit' | 'include' | 'exclude' }>) =>
|
|
api.patch('/api/v1/dashboard/monitor/policy/objects', body).then((r) => r.data),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['dashboard-policy'] })
|
|
qc.invalidateQueries({ queryKey: ['dashboard-home-summary'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const usePatchDashboardTagPolicy = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: Array<{ tag_id: number; enabled: boolean }>) =>
|
|
api.patch('/api/v1/dashboard/monitor/policy/tags', body).then((r) => r.data),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['dashboard-policy'] })
|
|
qc.invalidateQueries({ queryKey: ['dashboard-home-summary'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const useDashboardChecks = () =>
|
|
useQuery({
|
|
queryKey: ['dashboard-checks'],
|
|
queryFn: () =>
|
|
api
|
|
.get<DashboardChecksResponse>('/api/v1/dashboard/checks')
|
|
.then((r) => r.data),
|
|
})
|
|
|
|
export const usePatchDashboardChecks = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (
|
|
body: Array<{
|
|
check_key: string
|
|
enabled?: boolean
|
|
interval_sec_default?: number
|
|
categories?: string[]
|
|
timeout_sec?: number
|
|
concurrency?: number
|
|
}>
|
|
) => api.patch('/api/v1/dashboard/checks', body).then((r) => r.data),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['dashboard-checks'] })
|
|
qc.invalidateQueries({ queryKey: ['dashboard-home-summary'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export const usePatchDashboardObjectOverrides = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (
|
|
body: Array<{
|
|
object_id: number
|
|
check_key: string
|
|
enabled_override?: boolean | null
|
|
interval_sec_override?: number | null
|
|
categories_override?: string[] | null
|
|
}>
|
|
) => api.patch('/api/v1/dashboard/checks/object-overrides', body).then((r) => r.data),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['dashboard-checks'] })
|
|
qc.invalidateQueries({ queryKey: ['dashboard-home-summary'] })
|
|
},
|
|
})
|
|
}
|