fix: 修复历史报表交互状态

This commit is contained in:
zxr
2026-08-04 01:02:17 +08:00
parent 913e9e63ad
commit 24dd0c17da
3 changed files with 68 additions and 145 deletions

View File

@@ -1,5 +1,5 @@
import { request } from "@/api/request"
import SafeStorage, { AppStorageKey } from "@/utils/safeStorage"
import { request } from '@/api/request'
import SafeStorage, { AppStorageKey } from '@/utils/safeStorage'
// ============ 通用响应类型 ============
@@ -136,9 +136,7 @@ export interface NetworkDeviceReportParams {
export interface StatisticsReportParams {
data_source: 'dc-host' | 'dc-network' | 'dc-database' | 'dc-middleware'
/** 与 metric_name 二选一;网络等指标优先用 metric_id */
metric_id?: string
metric_name?: string
metric_id: string
target_identities: string[]
start_time: string
end_time: string
@@ -161,16 +159,13 @@ export interface HistoryReportParams {
export interface TopNReportParams {
data_source: 'dc-host' | 'dc-network' | 'dc-database' | 'dc-middleware' | 'alert'
metric_id?: string
metric_name?: string
metric_id: string
target_identities: string[]
start_time: string
end_time: string
n?: number
rank_aggregate?: 'avg' | 'max' | 'min' | 'last'
order?: 'desc' | 'asc'
metric_type?: 'cpu' | 'disk' | 'io' | 'memory' | 'network'
collector_identity?: string
}
/** 证据导出事件行 */
@@ -239,7 +234,8 @@ export const fetchReportMetricsAvailable = (params: {
identities?: string
keyword?: string
limit?: number
}) => request.get<ApiResponse<{ data_source: string; items: any[]; registry: any[] }>>('/DC-Control/v1/reports/metrics/available', { params })
}) =>
request.get<ApiResponse<{ data_source: string; items: any[]; registry: any[] }>>('/DC-Control/v1/reports/metrics/available', { params })
/** 逻辑指标目录(Registry) */
export const fetchReportMetricsRegistry = (params: { data_source: string }) =>
@@ -275,21 +271,14 @@ function exportBufferToBlob(ab: ArrayBuffer, format: ReportExportFormat, content
return new Blob([ab], { type: mime })
}
if (format === 'pdf') {
const okPdf =
u8.length >= 5 &&
u8[0] === 0x25 &&
u8[1] === 0x50 &&
u8[2] === 0x44 &&
u8[3] === 0x46 &&
u8[4] === 0x2d
const okPdf = u8.length >= 5 && u8[0] === 0x25 && u8[1] === 0x50 && u8[2] === 0x44 && u8[3] === 0x46 && u8[4] === 0x2d
if (!okPdf) {
throw new Error('导出失败:服务器返回的不是有效的 PDF。')
}
const mime = contentType && /pdf/i.test(contentType) ? contentType : 'application/pdf'
return new Blob([ab], { type: mime })
}
const mime =
contentType && /csv|text|plain/i.test(contentType) ? contentType : 'text/csv;charset=utf-8'
const mime = contentType && /csv|text|plain/i.test(contentType) ? contentType : 'text/csv;charset=utf-8'
return new Blob([ab], { type: mime })
}
@@ -308,11 +297,7 @@ function parseContentDispositionFileName(value: string | null): string | undefin
return value.match(/filename="([^"]+)"/i)?.[1]
}
function normalizeReportFileName(
fileName: string | undefined,
id: number,
format: ReportExportFormat,
): string {
function normalizeReportFileName(fileName: string | undefined, id: number, format: ReportExportFormat): string {
const defaultName = `report_${id}.${format}`
if (!fileName) return defaultName
@@ -340,10 +325,7 @@ function normalizeReportFileName(
* 报表文件导出:使用 fetch + arrayBuffer,绕过 axios 拦截器与 Blob 中间态,
* 避免网络面板里已是合法 xlsx(PK…)但落盘文件损坏的情况。
*/
export const exportReport = async (
id: number,
format: ReportExportFormat = 'csv',
): Promise<DownloadedReport> => {
export const exportReport = async (id: number, format: ReportExportFormat = 'csv'): Promise<DownloadedReport> => {
const base = String(import.meta.env.VITE_API_BASE_URL || '').replace(/\/$/, '')
const url = `${base}/DC-Control/v1/reports/${id}/export?format=${encodeURIComponent(format)}`
const token = SafeStorage.get(AppStorageKey.TOKEN)
@@ -367,11 +349,7 @@ export const exportReport = async (
}
return {
blob: exportBufferToBlob(ab, format, r.headers.get('content-type')),
fileName: normalizeReportFileName(
parseContentDispositionFileName(r.headers.get('content-disposition')),
id,
format,
),
fileName: normalizeReportFileName(parseContentDispositionFileName(r.headers.get('content-disposition')), id, format),
}
}