fix: полный продакшен-фикс dashboard/peer/hysteria по fix35
This commit is contained in:
+43
-7
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user