mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
refactor(monitoring, public): remove recent requests tracking and update metrics display
- Removed the RecentRequests field from SystemMetrics and its associated logic in LogRequest to streamline metrics collection. - Updated the HTML to eliminate the recent requests section, focusing on the top referers display for improved performance and clarity. - Enhanced the overall metrics presentation by ensuring only relevant data is displayed, contributing to a cleaner user interface.
This commit is contained in:
parent
f92b8385d8
commit
8d0d502910
@ -39,9 +39,6 @@ type SystemMetrics struct {
|
||||
// 路径延迟统计
|
||||
PathLatencies map[string]float64 `json:"path_latencies"`
|
||||
|
||||
// 最近请求
|
||||
RecentRequests []RequestLog `json:"recent_requests"`
|
||||
|
||||
// 热门引用来源
|
||||
TopReferers map[string]int64 `json:"top_referers"`
|
||||
|
||||
@ -76,7 +73,6 @@ func init() {
|
||||
metrics.StatusCodes = make(map[int]int64)
|
||||
metrics.PathLatencies = make(map[string]float64)
|
||||
metrics.TopReferers = make(map[string]int64)
|
||||
metrics.RecentRequests = make([]RequestLog, 0, 100)
|
||||
}
|
||||
|
||||
func CollectMetrics() *SystemMetrics {
|
||||
@ -103,11 +99,6 @@ func CollectMetrics() *SystemMetrics {
|
||||
func LogRequest(log RequestLog) {
|
||||
metrics.RequestCount.Add(1)
|
||||
|
||||
// 使用分段锁减少锁竞争
|
||||
bucket := getBucket(log.Path)
|
||||
bucket.mu.Lock()
|
||||
defer bucket.mu.Unlock()
|
||||
|
||||
mu.Lock() // 添加全局锁保护 map 写入
|
||||
metrics.StatusCodes[log.StatusCode]++
|
||||
|
||||
@ -130,14 +121,6 @@ func LogRequest(log RequestLog) {
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// 保存最近请求记录
|
||||
mu.Lock() // 保护 RecentRequests
|
||||
metrics.RecentRequests = append([]RequestLog{log}, metrics.RecentRequests...)
|
||||
if len(metrics.RecentRequests) > 100 {
|
||||
metrics.RecentRequests = metrics.RecentRequests[:100]
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// 更新平均延迟
|
||||
count := metrics.RequestCount.Load()
|
||||
if count > 1 {
|
||||
|
@ -229,28 +229,6 @@
|
||||
return `${(microseconds/1000000).toFixed(3)}s`;
|
||||
}
|
||||
|
||||
// 最近请求
|
||||
const recentRequestsHtml = metrics.recent_requests.map(req => `
|
||||
<tr>
|
||||
<td>${new Date(req.time).toLocaleString()}</td>
|
||||
<td>${req.path}</td>
|
||||
<td>${req.method}</td>
|
||||
<td>${req.status_code}</td>
|
||||
<td>${formatLatency(req.latency)}</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
|
||||
// 热门引用来源
|
||||
const topReferersHtml = Object.entries(metrics.top_referers)
|
||||
.sort(([, a], [, b]) => b - a)
|
||||
.slice(0, 10)
|
||||
.map(([referer, count]) => `
|
||||
<div class="referer-item">
|
||||
<span class="referer">${referer}</span>
|
||||
<span class="count">${count}</span>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
const metricsHtml = `
|
||||
<div class="metrics-container">
|
||||
<div class="metrics-section">
|
||||
@ -292,30 +270,18 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="metrics-section">
|
||||
<h3>最近请求</h3>
|
||||
<div class="recent-requests">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>时间</th>
|
||||
<th>路径</th>
|
||||
<th>方法</th>
|
||||
<th>状态</th>
|
||||
<th>延迟</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${recentRequestsHtml}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="metrics-section">
|
||||
<h3>热门引用来源</h3>
|
||||
<div class="top-referers">
|
||||
${topReferersHtml}
|
||||
${Object.entries(metrics.top_referers)
|
||||
.sort(([, a], [, b]) => b - a)
|
||||
.slice(0, 10)
|
||||
.map(([referer, count]) => `
|
||||
<div class="referer-item">
|
||||
<span class="referer">${referer}</span>
|
||||
<span class="count">${count}</span>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user