From d3933374af20f79b04ff3e92d76d5303cbaa044d Mon Sep 17 00:00:00 2001 From: wood chen Date: Mon, 2 Dec 2024 05:35:24 +0800 Subject: [PATCH] refactor(middleware, monitoring, public): streamline metrics handling and remove top referers display - Simplified MetricsMiddleware by removing unnecessary comments and focusing on essential request logging. - Updated RequestLog structure to exclude the Referer field from JSON serialization, enhancing data privacy. - Removed top referers tracking and associated HTML/CSS elements to declutter the user interface and improve performance. - Cleaned up CSS styles related to referers for a more streamlined design. --- middleware/metrics.go | 4 ---- monitoring/metrics.go | 51 +------------------------------------------ public/css/main.css | 36 ------------------------------ public/index.html | 18 --------------- 4 files changed, 1 insertion(+), 108 deletions(-) diff --git a/middleware/metrics.go b/middleware/metrics.go index 680366e..93e95ea 100644 --- a/middleware/metrics.go +++ b/middleware/metrics.go @@ -15,16 +15,13 @@ func MetricsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() - // 创建自定义的ResponseWriter来捕获状态码 rw := &responseWriter{ ResponseWriter: w, statusCode: http.StatusOK, } - // 处理请求 next.ServeHTTP(rw, r) - // 记录请求数据 duration := time.Since(start) monitoring.LogRequest(monitoring.RequestLog{ Time: time.Now().Unix(), @@ -33,7 +30,6 @@ func MetricsMiddleware(next http.Handler) http.Handler { StatusCode: rw.statusCode, Latency: float64(duration.Microseconds()) / 1000, IP: utils.GetRealIP(r), - Referer: r.Referer(), }) }) } diff --git a/monitoring/metrics.go b/monitoring/metrics.go index 5e4adc9..4396ab2 100644 --- a/monitoring/metrics.go +++ b/monitoring/metrics.go @@ -1,10 +1,8 @@ package monitoring import ( - "encoding/json" "runtime" "strings" - "sync" "time" ) @@ -21,9 +19,6 @@ type SystemMetrics struct { HeapAlloc uint64 `json:"heap_alloc"` HeapSys uint64 `json:"heap_sys"` } `json:"memory_stats"` - - // 热门引用来源 - TopReferers sync.Map `json:"-"` // 内部使用 sync.Map } type RequestLog struct { @@ -33,7 +28,7 @@ type RequestLog struct { StatusCode int `json:"status_code"` Latency float64 `json:"latency"` IP string `json:"ip"` - Referer string `json:"referer"` + Referer string `json:"-"` } var ( @@ -41,32 +36,7 @@ var ( startTime = time.Now() ) -func init() { - // 定期清理引用来源 - go func() { - ticker := time.NewTicker(5 * time.Minute) - for range ticker.C { - metrics.TopReferers = sync.Map{} // 直接重置 - } - }() -} - func LogRequest(log RequestLog) { - // 更新引用来源 - if log.Referer != "direct" { - if val, ok := metrics.TopReferers.Load(log.Referer); ok { - metrics.TopReferers.Store(log.Referer, val.(int64)+1) - } else { - metrics.TopReferers.Store(log.Referer, int64(1)) - } - } else { - if val, ok := metrics.TopReferers.Load("直接访问"); ok { - metrics.TopReferers.Store("直接访问", val.(int64)+1) - } else { - metrics.TopReferers.Store("直接访问", int64(1)) - } - } - // 更新平均延迟 (只关心 API 请求) if strings.HasPrefix(log.Path, "/pic/") || strings.HasPrefix(log.Path, "/video/") { metrics.AverageLatency = (metrics.AverageLatency + log.Latency) / 2 @@ -86,22 +56,3 @@ func CollectMetrics() *SystemMetrics { return &metrics } - -// 添加 MarshalJSON 方法来正确序列化 TopReferers -func (m *SystemMetrics) MarshalJSON() ([]byte, error) { - type Alias SystemMetrics - referers := make(map[string]int64) - - m.TopReferers.Range(func(key, value interface{}) bool { - referers[key.(string)] = value.(int64) - return true - }) - - return json.Marshal(&struct { - *Alias - TopReferers map[string]int64 `json:"top_referers"` - }{ - Alias: (*Alias)(m), - TopReferers: referers, - }) -} diff --git a/public/css/main.css b/public/css/main.css index 1bbaa80..5fc794e 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -352,38 +352,6 @@ td a:hover { color: #2196f3; } -.referers-list { - margin-top: 15px; - display: grid; - gap: 8px; - width: 100%; -} - -.referer-item { - background: rgba(255, 255, 255, 0.05); - padding: 10px 15px; - border-radius: 6px; - display: flex; - justify-content: space-between; - align-items: center; -} - -.referer { - color: #999; - max-width: 70%; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -.count { - color: #2196f3; - font-weight: 500; - padding: 2px 8px; - background: rgba(33, 150, 243, 0.1); - border-radius: 4px; -} - .error-message { background: rgba(255, 0, 0, 0.1); color: #ff4444; @@ -412,10 +380,6 @@ td a:hover { .metric-value { font-size: 1em; } - - .referer { - max-width: 60%; - } } .main-title { diff --git a/public/index.html b/public/index.html index f2fadb8..7d00fe5 100644 --- a/public/index.html +++ b/public/index.html @@ -286,24 +286,6 @@

💻 系统状态

- - ${Object.keys(data.top_referers).length > 0 ? ` -
-
-

🔗 访问来源

-
-
- ${Object.entries(data.top_referers) - .sort(([,a], [,b]) => b - a) - .map(([referer, count]) => ` -
- ${referer || '直接访问'} - ${count} -
- `).join('')} -
-
- ` : ''} `;