- 公网反代:Nginx 状态卡片(版本 / 运行状态 / 配置校验 / 端口监听) - 部署设置第一、二梯队(HTTPS 开关、安全头、限流、连接数、仅中国大陆 IP) - 隧道检测兼容 frps v0.68 / v0.71(V2 与旧版端点) - 恢复被清空的 internal/frp/store.go、internal/api/ddns_handler.go、web/src/views/DdnsView.vue
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package logstore
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHourlyAccessCountsCalendarDay(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "access.log")
|
|
now := time.Now()
|
|
todayMorning := time.Date(now.Year(), now.Month(), now.Day(), 8, 15, 0, 0, now.Location())
|
|
todayNoon := time.Date(now.Year(), now.Month(), now.Day(), 11, 30, 0, 0, now.Location())
|
|
yesterday := todayMorning.Add(-24 * time.Hour)
|
|
lines := []string{
|
|
formatAccessLine(todayMorning, "/"),
|
|
formatAccessLine(todayMorning.Add(30*time.Minute), "/"),
|
|
formatAccessLine(todayNoon, "/"),
|
|
formatAccessLine(yesterday, "/"),
|
|
}
|
|
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, labels := HourlyAccessCounts(path)
|
|
if len(got) != 12 || len(labels) != 12 {
|
|
t.Fatalf("expected 12 buckets, got %d counts / %d labels", len(got), len(labels))
|
|
}
|
|
if labels[0] != "00:00" || labels[11] != "22:00" {
|
|
t.Fatalf("unexpected labels: %v", labels)
|
|
}
|
|
sum := 0
|
|
for _, n := range got {
|
|
sum += n
|
|
}
|
|
if sum != 3 {
|
|
t.Fatalf("expected 3 requests today, got %d (%v)", sum, got)
|
|
}
|
|
currentBucket := now.Hour() / 2
|
|
for i := currentBucket + 1; i < 12; i++ {
|
|
if got[i] != 0 {
|
|
t.Fatalf("future bucket %d should be 0, got %d", i, got[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func formatAccessLine(at time.Time, path string) string {
|
|
return at.Format(time.RFC3339) + " app.example.com GET " + path + " 200 0.010 1.2.3.4 127.0.0.1:8080"
|
|
}
|