feat: 优化网站统计性能

This commit is contained in:
耗子
2026-02-21 23:05:14 +08:00
parent 42055275ca
commit 04b4352abd
2 changed files with 50 additions and 29 deletions
+37 -29
View File
@@ -118,6 +118,7 @@ func (a *Aggregator) Record(entry *LogEntry) {
uvKey := entry.IP + "|" + entry.UA
isErr := entry.Status >= 400 && entry.Status < 600
isPV := IsPageView(entry)
isStatic := IsStaticResource(entry.URI)
clientKey := browser + "|" + os
a.mu.Lock()
@@ -206,8 +207,10 @@ func (a *Aggregator) Record(entry *LogEntry) {
sd.spiders++
hb.spiders++
// 蜘蛛详细统计
sd.spiderCounts[spiderName]++
} else {
if !isStatic {
sd.spiderCounts[spiderName]++
}
} else if !isStatic {
// 非蜘蛛请求才统计客户端(浏览器/OS)
if cc, exists := sd.clientCounts[clientKey]; exists {
cc.requests++
@@ -217,43 +220,48 @@ func (a *Aggregator) Record(entry *LogEntry) {
}
// IP 详细统计
if ic, exists := sd.ipCounts[entry.IP]; exists {
ic.requests++
ic.bandwidth += entry.Bytes
} else if len(sd.ipCounts) < a.DetailMaxKeys {
sd.ipCounts[entry.IP] = &ipCount{requests: 1, bandwidth: entry.Bytes}
if !isStatic {
if ic, exists := sd.ipCounts[entry.IP]; exists {
ic.requests++
ic.bandwidth += entry.Bytes
} else if len(sd.ipCounts) < a.DetailMaxKeys {
sd.ipCounts[entry.IP] = &ipCount{requests: 1, bandwidth: entry.Bytes}
}
}
// URI 详细统计
if uc, exists := sd.uriCounts[entry.URI]; exists {
uc.requests++
uc.bandwidth += entry.Bytes
if isErr {
uc.errors++
if !isStatic {
if uc, exists := sd.uriCounts[entry.URI]; exists {
uc.requests++
uc.bandwidth += entry.Bytes
if isErr {
uc.errors++
}
if entry.RequestTime > 0 {
ms := uint64(entry.RequestTime * 1000)
uc.requestTimeSum += ms
uc.requestTimeCount++
}
} else if len(sd.uriCounts) < a.DetailMaxKeys {
uc := &uriCount{requests: 1, bandwidth: entry.Bytes}
if isErr {
uc.errors = 1
}
if entry.RequestTime > 0 {
ms := uint64(entry.RequestTime * 1000)
uc.requestTimeSum = ms
uc.requestTimeCount = 1
}
sd.uriCounts[entry.URI] = uc
}
if entry.RequestTime > 0 {
ms := uint64(entry.RequestTime * 1000)
uc.requestTimeSum += ms
uc.requestTimeCount++
}
} else if len(sd.uriCounts) < a.DetailMaxKeys {
uc := &uriCount{requests: 1, bandwidth: entry.Bytes}
if isErr {
uc.errors = 1
}
if entry.RequestTime > 0 {
ms := uint64(entry.RequestTime * 1000)
uc.requestTimeSum = ms
uc.requestTimeCount = 1
}
sd.uriCounts[entry.URI] = uc
}
// 错误计数
if isErr {
sd.errors++
hb.errors++
if len(a.errBuf) < a.ErrBufMaxSize {
// 错误日志详细记录
if !isStatic && len(a.errBuf) < a.ErrBufMaxSize {
errEntry := &ErrorEntry{
Site: entry.Site,
URI: entry.URI,
+13
View File
@@ -15,6 +15,19 @@ var staticExts = map[string]struct{}{
".7z": {}, ".bz2": {}, ".xz": {}, ".swf": {}, ".flv": {},
}
// IsStaticResource 判定 URI 是否为静态资源(js/css/图片/字体等)
func IsStaticResource(uri string) bool {
if i := strings.IndexByte(uri, '?'); i >= 0 {
uri = uri[:i]
}
ext := strings.ToLower(path.Ext(uri))
if ext == "" {
return false
}
_, ok := staticExts[ext]
return ok
}
// IsPageView 判定请求是否为页面浏览(PV)
func IsPageView(entry *LogEntry) bool {
// 优先使用 Content-Type 判定