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
+33
View File
@@ -67,3 +67,36 @@ func UpsertTrafficAggregateDaily(peerId int64, dayStart int64, rxBytes int64, tx
func gormExprAdd(column string, delta int64) interface{} { func gormExprAdd(column string, delta int64) interface{} {
return gorm.Expr(fmt.Sprintf("%s + ?", column), delta) return gorm.Expr(fmt.Sprintf("%s + ?", column), delta)
} }
func CleanupTrafficSample(olderThanMs int64) error {
if !tableExists("traffic_sample") {
return nil
}
if tx := sqliteDB.Exec("DELETE FROM traffic_sample WHERE sampled_at < ?", olderThanMs); tx.Error != nil {
logrus.Errorf("%v", tx.Error)
return errors.New(constant.SysError)
}
return nil
}
func CleanupTrafficAggregateHourly(olderThanMs int64) error {
if !tableExists("traffic_aggregate_hourly") {
return nil
}
if tx := sqliteDB.Exec("DELETE FROM traffic_aggregate_hourly WHERE hour_start < ?", olderThanMs); tx.Error != nil {
logrus.Errorf("%v", tx.Error)
return errors.New(constant.SysError)
}
return nil
}
func CleanupTrafficAggregateDaily(olderThanMs int64) error {
if !tableExists("traffic_aggregate_daily") {
return nil
}
if tx := sqliteDB.Exec("DELETE FROM traffic_aggregate_daily WHERE day_start < ?", olderThanMs); tx.Error != nil {
logrus.Errorf("%v", tx.Error)
return errors.New(constant.SysError)
}
return nil
}
@@ -47,7 +47,6 @@
<el-radio-button label="1h">1h</el-radio-button> <el-radio-button label="1h">1h</el-radio-button>
<el-radio-button label="24h">24h</el-radio-button> <el-radio-button label="24h">24h</el-radio-button>
<el-radio-button label="7d">7d</el-radio-button> <el-radio-button label="7d">7d</el-radio-button>
<el-radio-button label="30d">30d</el-radio-button>
</el-radio-group> </el-radio-group>
</div> </div>
</template> </template>
+3 -3
View File
@@ -23,10 +23,10 @@ func InitCron() error {
logrus.Errorf("cron add func CollectMetricsSnapshot err: %v", err) logrus.Errorf("cron add func CollectMetricsSnapshot err: %v", err)
return errors.New("cron add func CollectMetricsSnapshot err") return errors.New("cron add func CollectMetricsSnapshot err")
} }
_, err = c.AddFunc("@every 10m", service.CleanupMetricsRetention) _, err = c.AddFunc("@every 1h", service.CleanupStatsRetention)
if err != nil { if err != nil {
logrus.Errorf("cron add func CleanupMetricsRetention err: %v", err) logrus.Errorf("cron add func CleanupStatsRetention err: %v", err)
return errors.New("cron add func CleanupMetricsRetention err") return errors.New("cron add func CleanupStatsRetention err")
} }
resetTrafficCron, err := dao.GetConfig("key = ?", constant.ResetTrafficCron) resetTrafficCron, err := dao.GetConfig("key = ?", constant.ResetTrafficCron)
if err != nil { if err != nil {
-2
View File
@@ -72,7 +72,6 @@ func saveAccountTraffic(apiPort int64, trafficStatsSecret string) {
nowMs := time.Now().UnixMilli() nowMs := time.Now().UnixMilli()
hourStart := nowMs - (nowMs % int64(time.Hour/time.Millisecond)) hourStart := nowMs - (nowMs % int64(time.Hour/time.Millisecond))
dayStart := nowMs - (nowMs % int64(24*time.Hour/time.Millisecond))
for key, traffic := range users { for key, traffic := range users {
rxBytes := traffic.Rx rxBytes := traffic.Rx
@@ -115,7 +114,6 @@ func saveAccountTraffic(apiPort int64, trafficStatsSecret string) {
} }
_ = dao.UpsertTrafficAggregateHourly(*peer.Id, hourStart, rxBytes, txBytes) _ = dao.UpsertTrafficAggregateHourly(*peer.Id, hourStart, rxBytes, txBytes)
_ = dao.UpsertTrafficAggregateDaily(*peer.Id, dayStart, rxBytes, txBytes)
} }
} }
func kickAccount(apiPort int64, trafficStatsSecret string) { 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) fromMs := nowMs - int64(24*time.Hour/time.Millisecond)
bucketMs := int64(5 * time.Minute / time.Millisecond) bucketMs := int64(5 * time.Minute / time.Millisecond)
source := "sample" source := "sample"
r := strings.TrimSpace(rangeKey) r := "24h"
if r == "1h" {
switch strings.TrimSpace(rangeKey) {
case "1h":
fromMs = nowMs - int64(time.Hour/time.Millisecond) fromMs = nowMs - int64(time.Hour/time.Millisecond)
bucketMs = int64(time.Minute / time.Millisecond) bucketMs = int64(time.Minute / time.Millisecond)
source = "sample" source = "sample"
} else if r == "7d" { r = "1h"
case "7d":
fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond) fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond)
bucketMs = int64(time.Hour / time.Millisecond) bucketMs = int64(time.Hour / time.Millisecond)
source = "hourly" source = "hourly"
} else if r == "30d" { r = "7d"
fromMs = nowMs - int64(30*24*time.Hour/time.Millisecond) case "24h", "":
bucketMs = int64(24 * time.Hour / time.Millisecond) fromMs = nowMs - int64(24*time.Hour/time.Millisecond)
source = "daily"
r = "30d"
} else if r == "" {
r = "24h"
bucketMs = int64(5 * time.Minute / time.Millisecond) bucketMs = int64(5 * time.Minute / time.Millisecond)
source = "sample" source = "sample"
} else {
r = "24h" r = "24h"
default:
fromMs = nowMs - int64(24*time.Hour/time.Millisecond)
bucketMs = int64(5 * time.Minute / time.Millisecond) bucketMs = int64(5 * time.Minute / time.Millisecond)
source = "sample" source = "sample"
r = "24h"
} }
traffic, err := dao.DashboardTrafficTimeseries(fromMs, nowMs, bucketMs, source) 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) fromMs := nowMs - int64(24*time.Hour/time.Millisecond)
if strings.TrimSpace(rangeKey) == "7d" { if strings.TrimSpace(rangeKey) == "7d" {
fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond) 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) return dao.DashboardTopPeers(fromMs, nowMs, limit)
} }
+14 -3
View File
@@ -14,6 +14,7 @@ import (
) )
const hysteriaVersionCacheTTL = 15 * time.Minute const hysteriaVersionCacheTTL = 15 * time.Minute
const statsRetention = 7 * 24 * time.Hour
var ansiRe = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`) var ansiRe = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
var hysteriaVersionRe = regexp.MustCompile(`(?i)v?\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?`) var hysteriaVersionRe = regexp.MustCompile(`(?i)v?\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?`)
@@ -150,9 +151,19 @@ func CollectMetricsSnapshot() {
_ = dao.SaveMetricSample(metric) _ = dao.SaveMetricSample(metric)
} }
func CleanupMetricsRetention() { func CleanupStatsRetention() {
cutoff := time.Now().Add(-72 * time.Hour).UnixMilli() now := time.Now()
_ = dao.CleanupMetricSample(cutoff) 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 { func DashboardSnapshot() metricsSnapshot {