// 自定义监控仪表盘 API // Logan.Li import { http } from '@/utils/request'; import type { GrafanaDashboard } from '@/types/monitor'; export const dashboardApi = { getDashboards: async (): Promise<{ data: GrafanaDashboard[]; total: number }> => { const res = await http.get('/custom-dashboards'); // http 自动解包 .data,后端返回 {list, total} const body = (res as Record) ?? {}; const list = (body.list as GrafanaDashboard[]) ?? []; const total = (body.total as number) ?? list.length; return { data: list, total }; }, getDatasources: async (): Promise<{ data: Record[]; total: number }> => { // 复用 Prometheus 数据源列表 const res = await http.get('/prometheus-datasources'); const arr = (res as Record[]) ?? []; return { data: arr, total: arr.length }; }, createDashboard: async (data: Partial): Promise<{ success: boolean }> => { // 将 panels 数组序列化为 JSON 字符串 const payload: Record = { name: data.name ?? '', description: data.description ?? '', datasource: data.datasource ?? 'prometheus', datasource_url: data.datasourceUrl ?? '', panels: JSON.stringify(data.panels ?? []), is_default: data.isDefault ?? false, }; await http.post('/custom-dashboards', payload); return { success: true }; }, updateDashboard: async (id: number | string, data: Partial): Promise<{ success: boolean }> => { const payload: Record = {}; if (data.name !== undefined) payload.name = data.name; if (data.description !== undefined) payload.description = data.description; if (data.datasource !== undefined) payload.datasource = data.datasource; if (data.datasourceUrl !== undefined) payload.datasource_url = data.datasourceUrl; if (data.panels !== undefined) payload.panels = JSON.stringify(data.panels); if (data.isDefault !== undefined) payload.is_default = data.isDefault; await http.put(`/custom-dashboards/${id}`, payload); return { success: true }; }, deleteDashboard: async (id: number | string): Promise<{ success: boolean }> => { await http.delete(`/custom-dashboards/${id}`); return { success: true }; }, setDefault: async (id: number | string): Promise<{ success: boolean }> => { await http.post(`/custom-dashboards/${id}/default`); return { success: true }; }, createDatasource: async (_data: Record): Promise<{ success: boolean }> => { return { success: false }; }, };