fix: полный продакшен-фикс dashboard/peer/hysteria по fix35

This commit is contained in:
2026-05-09 15:38:30 +05:00
parent a4157d1363
commit 78656e74b0
18 changed files with 190 additions and 85 deletions
+43 -7
View File
@@ -6,6 +6,7 @@ import (
"hy2xs-admin/model/constant"
"hy2xs-admin/model/entity"
"hy2xs-admin/model/vo"
"strings"
"time"
)
@@ -119,20 +120,55 @@ func DashboardTopPeers(fromMs int64, toMs int64, limit int) ([]vo.DashboardTopPe
return rows, nil
}
func DashboardTrafficTimeseries(fromMs int64, toMs int64) ([]vo.DashboardSeriesPointVo, error) {
func DashboardTrafficTimeseries(fromMs int64, toMs int64, bucketMs int64, source string) ([]vo.DashboardSeriesPointVo, error) {
rows := make([]vo.DashboardSeriesPointVo, 0)
if toMs < fromMs {
return rows, nil
}
if bucketMs <= 0 {
bucketMs = int64(time.Minute / time.Millisecond)
}
sourceTable := "traffic_sample"
fromColumn := "sampled_at"
switch strings.TrimSpace(strings.ToLower(source)) {
case "hourly":
sourceTable = "traffic_aggregate_hourly"
fromColumn = "hour_start"
case "daily":
sourceTable = "traffic_aggregate_daily"
fromColumn = "day_start"
default:
sourceTable = "traffic_sample"
fromColumn = "sampled_at"
}
alignedFrom := fromMs - (fromMs % bucketMs)
if tx := sqliteDB.Raw(`SELECT
hour_start AS ts,
(? + CAST((`+fromColumn+` - ?) / ? AS INTEGER) * ?) AS ts,
COALESCE(SUM(rx_bytes),0) AS download,
COALESCE(SUM(tx_bytes),0) AS upload
FROM traffic_aggregate_hourly
WHERE hour_start BETWEEN ? AND ?
GROUP BY hour_start
ORDER BY hour_start ASC`, fromMs, toMs).Scan(&rows); tx.Error != nil {
FROM `+sourceTable+`
WHERE `+fromColumn+` BETWEEN ? AND ?
GROUP BY ts
ORDER BY ts ASC`, alignedFrom, alignedFrom, bucketMs, bucketMs, fromMs, toMs).Scan(&rows); tx.Error != nil {
logrus.Errorf("%v", tx.Error)
return rows, errors.New(constant.SysError)
}
return rows, nil
rowMap := make(map[int64]vo.DashboardSeriesPointVo, len(rows))
for _, item := range rows {
rowMap[item.Ts] = item
}
filled := make([]vo.DashboardSeriesPointVo, 0, int((toMs-alignedFrom)/bucketMs)+1)
for ts := alignedFrom; ts <= toMs; ts += bucketMs {
if item, ok := rowMap[ts]; ok {
filled = append(filled, item)
continue
}
filled = append(filled, vo.DashboardSeriesPointVo{Ts: ts, Download: 0, Upload: 0})
}
return filled, nil
}
func DashboardSystemTimeseries(fromMs int64, toMs int64) ([]vo.DashboardSeriesPointVo, error) {
+4 -9
View File
@@ -8,7 +8,6 @@ import (
"gorm.io/gorm/clause"
"hy2xs-admin/model/constant"
"hy2xs-admin/model/entity"
"time"
)
func SaveTrafficSample(sample entity.TrafficSample) error {
@@ -29,13 +28,11 @@ func UpsertTrafficAggregateHourly(peerId int64, hourStart int64, rxBytes int64,
RxBytes: &rxBytes,
TxBytes: &txBytes,
}
now := time.Now()
if tx := sqliteDB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "peer_id"}, {Name: "hour_start"}},
DoUpdates: clause.Assignments(map[string]interface{}{
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
"update_time": now,
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
}),
}).Create(&agg); tx.Error != nil {
logrus.Errorf("%v", tx.Error)
@@ -54,13 +51,11 @@ func UpsertTrafficAggregateDaily(peerId int64, dayStart int64, rxBytes int64, tx
RxBytes: &rxBytes,
TxBytes: &txBytes,
}
now := time.Now()
if tx := sqliteDB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "peer_id"}, {Name: "day_start"}},
DoUpdates: clause.Assignments(map[string]interface{}{
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
"update_time": now,
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
}),
}).Create(&agg); tx.Error != nil {
logrus.Errorf("%v", tx.Error)