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
This commit is contained in:
wood chen 2025-02-15 12:01:28 +08:00
parent b5a6928a3a
commit eabb1f1f9a
2 changed files with 10 additions and 8 deletions

View File

@ -7,12 +7,12 @@ import (
// RequestLog 请求日志 // RequestLog 请求日志
type RequestLog struct { type RequestLog struct {
Time time.Time `json:"time"` Time time.Time `json:"Time"`
Path string `json:"path"` Path string `json:"Path"`
Status int `json:"status"` Status int `json:"Status"`
Latency int64 `json:"latency"` Latency int64 `json:"Latency"`
BytesSent int64 `json:"bytes_sent"` BytesSent int64 `json:"BytesSent"`
ClientIP string `json:"client_ip"` ClientIP string `json:"ClientIP"`
} }
// PathMetrics 路径指标 // PathMetrics 路径指标

View File

@ -279,7 +279,7 @@ export default function DashboardPage() {
} }
function formatBytes(bytes: number) { function formatBytes(bytes: number) {
if (bytes === 0) return "0 B" if (!bytes || isNaN(bytes)) return "0 B"
const k = 1024 const k = 1024
const sizes = ["B", "KB", "MB", "GB"] const sizes = ["B", "KB", "MB", "GB"]
const i = Math.floor(Math.log(bytes) / Math.log(k)) const i = Math.floor(Math.log(bytes) / Math.log(k))
@ -287,11 +287,13 @@ function formatBytes(bytes: number) {
} }
function formatDate(dateStr: string) { function formatDate(dateStr: string) {
if (!dateStr) return "-"
const date = new Date(dateStr) const date = new Date(dateStr)
return date.toLocaleTimeString() return date.toLocaleString()
} }
function formatLatency(nanoseconds: number) { function formatLatency(nanoseconds: number) {
if (!nanoseconds || isNaN(nanoseconds)) return "-"
if (nanoseconds < 1000) { if (nanoseconds < 1000) {
return nanoseconds + " ns" return nanoseconds + " ns"
} else if (nanoseconds < 1000000) { } else if (nanoseconds < 1000000) {