fix: ограничить статистику 7 днями и убрать 30d из дашборда

This commit is contained in:
2026-05-09 16:07:54 +05:00
parent 78656e74b0
commit 44c01d39d0
6 changed files with 62 additions and 22 deletions
-2
View File
@@ -72,7 +72,6 @@ func saveAccountTraffic(apiPort int64, trafficStatsSecret string) {
nowMs := time.Now().UnixMilli()
hourStart := nowMs - (nowMs % int64(time.Hour/time.Millisecond))
dayStart := nowMs - (nowMs % int64(24*time.Hour/time.Millisecond))
for key, traffic := range users {
rxBytes := traffic.Rx
@@ -115,7 +114,6 @@ func saveAccountTraffic(apiPort int64, trafficStatsSecret string) {
}
_ = dao.UpsertTrafficAggregateHourly(*peer.Id, hourStart, rxBytes, txBytes)
_ = dao.UpsertTrafficAggregateDaily(*peer.Id, dayStart, rxBytes, txBytes)
}
}
func kickAccount(apiPort int64, trafficStatsSecret string) {
+12 -13
View File
@@ -46,28 +46,29 @@ func DashboardTimeseries(rangeKey string) (vo.DashboardTimeseriesVo, error) {
fromMs := nowMs - int64(24*time.Hour/time.Millisecond)
bucketMs := int64(5 * time.Minute / time.Millisecond)
source := "sample"
r := strings.TrimSpace(rangeKey)
if r == "1h" {
r := "24h"
switch strings.TrimSpace(rangeKey) {
case "1h":
fromMs = nowMs - int64(time.Hour/time.Millisecond)
bucketMs = int64(time.Minute / time.Millisecond)
source = "sample"
} else if r == "7d" {
r = "1h"
case "7d":
fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond)
bucketMs = int64(time.Hour / time.Millisecond)
source = "hourly"
} else if r == "30d" {
fromMs = nowMs - int64(30*24*time.Hour/time.Millisecond)
bucketMs = int64(24 * time.Hour / time.Millisecond)
source = "daily"
r = "30d"
} else if r == "" {
r = "24h"
r = "7d"
case "24h", "":
fromMs = nowMs - int64(24*time.Hour/time.Millisecond)
bucketMs = int64(5 * time.Minute / time.Millisecond)
source = "sample"
} else {
r = "24h"
default:
fromMs = nowMs - int64(24*time.Hour/time.Millisecond)
bucketMs = int64(5 * time.Minute / time.Millisecond)
source = "sample"
r = "24h"
}
traffic, err := dao.DashboardTrafficTimeseries(fromMs, nowMs, bucketMs, source)
@@ -92,8 +93,6 @@ func DashboardTopPeers(rangeKey string, limit int) ([]vo.DashboardTopPeerVo, err
fromMs := nowMs - int64(24*time.Hour/time.Millisecond)
if strings.TrimSpace(rangeKey) == "7d" {
fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond)
} else if strings.TrimSpace(rangeKey) == "30d" {
fromMs = nowMs - int64(30*24*time.Hour/time.Millisecond)
}
return dao.DashboardTopPeers(fromMs, nowMs, limit)
}
+14 -3
View File
@@ -14,6 +14,7 @@ import (
)
const hysteriaVersionCacheTTL = 15 * time.Minute
const statsRetention = 7 * 24 * time.Hour
var ansiRe = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
var hysteriaVersionRe = regexp.MustCompile(`(?i)v?\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?`)
@@ -150,9 +151,19 @@ func CollectMetricsSnapshot() {
_ = dao.SaveMetricSample(metric)
}
func CleanupMetricsRetention() {
cutoff := time.Now().Add(-72 * time.Hour).UnixMilli()
_ = dao.CleanupMetricSample(cutoff)
func CleanupStatsRetention() {
now := time.Now()
cutoffMs := now.Add(-statsRetention).UnixMilli()
hourMs := int64(time.Hour / time.Millisecond)
dayMs := int64(24 * time.Hour / time.Millisecond)
cutoffHour := cutoffMs - (cutoffMs % hourMs)
cutoffDay := cutoffMs - (cutoffMs % dayMs)
_ = dao.CleanupMetricSample(cutoffMs)
_ = dao.CleanupTrafficSample(cutoffMs)
_ = dao.CleanupTrafficAggregateHourly(cutoffHour)
_ = dao.CleanupTrafficAggregateDaily(cutoffDay)
}
func DashboardSnapshot() metricsSnapshot {