From eabb1f1f9a6d203b475648783be70a157882af59 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 15 Feb 2025 12:01:28 +0800 Subject: [PATCH] refactor(models,web): Update JSON serialization and formatting utilities - Modify RequestLog struct to use PascalCase JSON tags - Enhance formatBytes function to handle undefined or NaN inputs - Update formatDate to return full datetime and handle empty inputs - Add null/NaN checks to formatLatency for more robust display --- internal/models/request.go | 12 ++++++------ web/app/dashboard/page.tsx | 6 ++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/models/request.go b/internal/models/request.go index ccf95c2..77d7f2c 100644 --- a/internal/models/request.go +++ b/internal/models/request.go @@ -7,12 +7,12 @@ import ( // RequestLog 请求日志 type RequestLog struct { - Time time.Time `json:"time"` - Path string `json:"path"` - Status int `json:"status"` - Latency int64 `json:"latency"` - BytesSent int64 `json:"bytes_sent"` - ClientIP string `json:"client_ip"` + Time time.Time `json:"Time"` + Path string `json:"Path"` + Status int `json:"Status"` + Latency int64 `json:"Latency"` + BytesSent int64 `json:"BytesSent"` + ClientIP string `json:"ClientIP"` } // PathMetrics 路径指标 diff --git a/web/app/dashboard/page.tsx b/web/app/dashboard/page.tsx index 2dd1723..66272bc 100644 --- a/web/app/dashboard/page.tsx +++ b/web/app/dashboard/page.tsx @@ -279,7 +279,7 @@ export default function DashboardPage() { } function formatBytes(bytes: number) { - if (bytes === 0) return "0 B" + if (!bytes || isNaN(bytes)) return "0 B" const k = 1024 const sizes = ["B", "KB", "MB", "GB"] const i = Math.floor(Math.log(bytes) / Math.log(k)) @@ -287,11 +287,13 @@ function formatBytes(bytes: number) { } function formatDate(dateStr: string) { + if (!dateStr) return "-" const date = new Date(dateStr) - return date.toLocaleTimeString() + return date.toLocaleString() } function formatLatency(nanoseconds: number) { + if (!nanoseconds || isNaN(nanoseconds)) return "-" if (nanoseconds < 1000) { return nanoseconds + " ns" } else if (nanoseconds < 1000000) {