From 1db7f24d5cc786a685983ec95c6845e746830d96 Mon Sep 17 00:00:00 2001 From: Crimson Date: Sat, 9 May 2026 21:31:03 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=BD=D0=B0=D1=8F=20=D0=B0=D0=B3=D1=80=D0=B5=D0=B3=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20top=20peers=20=D0=BF=D0=BE=20=D0=B4=D0=B8?= =?UTF-8?q?=D0=B0=D0=BF=D0=B0=D0=B7=D0=BE=D0=BD=D0=B0=D0=BC=20=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=87=D0=B8=D0=BD=D0=BA=D0=B0=20hourly=20bucket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/dao/dashboard.go | 22 +++++++++++++++---- apps/model/entity/traffic_aggregate_daily.go | 8 +++---- apps/model/entity/traffic_aggregate_hourly.go | 8 +++---- apps/service/dashboard.go | 18 +++++++++++++-- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/apps/dao/dashboard.go b/apps/dao/dashboard.go index 15380ab..cd729e9 100644 --- a/apps/dao/dashboard.go +++ b/apps/dao/dashboard.go @@ -96,10 +96,20 @@ func DashboardTrafficSummary() (vo.DashboardTrafficVo, error) { return result, nil } -func DashboardTopPeers(fromMs int64, toMs int64, limit int) ([]vo.DashboardTopPeerVo, error) { +func DashboardTopPeers(fromMs int64, toMs int64, limit int, source string) ([]vo.DashboardTopPeerVo, error) { if limit <= 0 { limit = 10 } + sourceTable := "traffic_sample" + timeColumn := "sampled_at" + switch strings.TrimSpace(strings.ToLower(source)) { + case "hourly": + sourceTable = "traffic_aggregate_hourly" + timeColumn = "hour_start" + default: + sourceTable = "traffic_sample" + timeColumn = "sampled_at" + } rows := make([]vo.DashboardTopPeerVo, 0) if tx := sqliteDB.Raw(`SELECT p.id AS peer_id, @@ -108,9 +118,9 @@ func DashboardTopPeers(fromMs int64, toMs int64, limit int) ([]vo.DashboardTopPe COALESCE(SUM(ts.rx_bytes),0) AS download, COALESCE(SUM(ts.tx_bytes),0) AS upload, COALESCE(SUM(ts.rx_bytes + ts.tx_bytes),0) AS total - FROM traffic_sample ts + FROM `+sourceTable+` ts JOIN peer p ON p.id = ts.peer_id - WHERE ts.sampled_at BETWEEN ? AND ? + WHERE ts.`+timeColumn+` BETWEEN ? AND ? GROUP BY p.id, p.name, p.remark ORDER BY total DESC LIMIT ?`, fromMs, toMs, limit).Scan(&rows); tx.Error != nil { @@ -141,6 +151,10 @@ func DashboardTrafficTimeseries(fromMs int64, toMs int64, bucketMs int64, source } alignedFrom := fromMs - (fromMs % bucketMs) + queryFrom := fromMs + if sourceTable == "traffic_aggregate_hourly" { + queryFrom = alignedFrom + } if tx := sqliteDB.Raw(`SELECT (? + CAST((`+fromColumn+` - ?) / ? AS INTEGER) * ?) AS ts, COALESCE(SUM(rx_bytes),0) AS download, @@ -148,7 +162,7 @@ func DashboardTrafficTimeseries(fromMs int64, toMs int64, bucketMs int64, source FROM `+sourceTable+` WHERE `+fromColumn+` BETWEEN ? AND ? GROUP BY ts - ORDER BY ts ASC`, alignedFrom, alignedFrom, bucketMs, bucketMs, fromMs, toMs).Scan(&rows); tx.Error != nil { + ORDER BY ts ASC`, alignedFrom, alignedFrom, bucketMs, bucketMs, queryFrom, toMs).Scan(&rows); tx.Error != nil { logrus.Errorf("%v", tx.Error) return rows, errors.New(constant.SysError) } diff --git a/apps/model/entity/traffic_aggregate_daily.go b/apps/model/entity/traffic_aggregate_daily.go index d732283..4a39326 100644 --- a/apps/model/entity/traffic_aggregate_daily.go +++ b/apps/model/entity/traffic_aggregate_daily.go @@ -1,10 +1,10 @@ package entity type TrafficAggregateDaily struct { - PeerId *int64 `gorm:"column:peer_id;default:0;primaryKey" json:"peerId"` - DayStart *int64 `gorm:"column:day_start;default:0;primaryKey" json:"dayStart"` - RxBytes *int64 `gorm:"column:rx_bytes;default:0" json:"rxBytes"` - TxBytes *int64 `gorm:"column:tx_bytes;default:0" json:"txBytes"` + PeerId *int64 `gorm:"column:peer_id;default:0;primaryKey" json:"peerId"` + DayStart *int64 `gorm:"column:day_start;default:0;primaryKey" json:"dayStart"` + RxBytes *int64 `gorm:"column:rx_bytes;default:0" json:"rxBytes"` + TxBytes *int64 `gorm:"column:tx_bytes;default:0" json:"txBytes"` } func (TrafficAggregateDaily) TableName() string { diff --git a/apps/model/entity/traffic_aggregate_hourly.go b/apps/model/entity/traffic_aggregate_hourly.go index bda2690..d7e1955 100644 --- a/apps/model/entity/traffic_aggregate_hourly.go +++ b/apps/model/entity/traffic_aggregate_hourly.go @@ -1,10 +1,10 @@ package entity type TrafficAggregateHourly struct { - PeerId *int64 `gorm:"column:peer_id;default:0;primaryKey" json:"peerId"` - HourStart *int64 `gorm:"column:hour_start;default:0;primaryKey" json:"hourStart"` - RxBytes *int64 `gorm:"column:rx_bytes;default:0" json:"rxBytes"` - TxBytes *int64 `gorm:"column:tx_bytes;default:0" json:"txBytes"` + PeerId *int64 `gorm:"column:peer_id;default:0;primaryKey" json:"peerId"` + HourStart *int64 `gorm:"column:hour_start;default:0;primaryKey" json:"hourStart"` + RxBytes *int64 `gorm:"column:rx_bytes;default:0" json:"rxBytes"` + TxBytes *int64 `gorm:"column:tx_bytes;default:0" json:"txBytes"` } func (TrafficAggregateHourly) TableName() string { diff --git a/apps/service/dashboard.go b/apps/service/dashboard.go index e61682f..3a10772 100644 --- a/apps/service/dashboard.go +++ b/apps/service/dashboard.go @@ -90,11 +90,25 @@ func DashboardTimeseries(rangeKey string) (vo.DashboardTimeseriesVo, error) { func DashboardTopPeers(rangeKey string, limit int) ([]vo.DashboardTopPeerVo, error) { nowMs := time.Now().UnixMilli() + normalizedRange := strings.TrimSpace(strings.ToLower(rangeKey)) fromMs := nowMs - int64(24*time.Hour/time.Millisecond) - if strings.TrimSpace(rangeKey) == "7d" { + source := "sample" + + switch normalizedRange { + case "1h": + fromMs = nowMs - int64(time.Hour/time.Millisecond) + source = "sample" + case "7d": fromMs = nowMs - int64(7*24*time.Hour/time.Millisecond) + source = "hourly" + case "24h", "": + fromMs = nowMs - int64(24*time.Hour/time.Millisecond) + source = "sample" + default: + fromMs = nowMs - int64(24*time.Hour/time.Millisecond) + source = "sample" } - return dao.DashboardTopPeers(fromMs, nowMs, limit) + return dao.DashboardTopPeers(fromMs, nowMs, limit, source) } func DashboardSecurity() ([]vo.SecurityRiskVo, error) {