diff --git a/internal/db/models/server_bandwidth_stat_dao.go b/internal/db/models/server_bandwidth_stat_dao.go index 049be1b2..591ed511 100644 --- a/internal/db/models/server_bandwidth_stat_dao.go +++ b/internal/db/models/server_bandwidth_stat_dao.go @@ -4,6 +4,8 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs" + "github.com/TeaOSLab/EdgeAPI/internal/utils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" @@ -12,6 +14,7 @@ import ( "github.com/iwind/TeaGo/types" timeutil "github.com/iwind/TeaGo/utils/time" "math" + "strings" "sync" "time" ) @@ -89,6 +92,59 @@ func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, s FindInt64Col(0) } +// FindHourlyBandwidthStats 按小时获取带宽峰值 +func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverId int64, hours int32) (result []*pb.FindHourlyServerBandwidthStatsResponse_Stat, err error) { + if hours <= 0 { + hours = 24 + } + + var timestamp = time.Now().Unix() - int64(hours)*3600 + + ones, _, err := this.Query(tx). + Table(this.partialTable(serverId)). + Result("MAX(bytes) AS bytes", "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime"). + Attr("serverId", serverId). + Gte("CONCAT(day, '.', SUBSTRING(timeAt, 1, 2))", timeutil.FormatTime("Ymd.H", timestamp)). + Group("fullTime"). + FindOnes() + if err != nil { + return nil, err + } + + var m = map[string]*pb.FindHourlyServerBandwidthStatsResponse_Stat{} + for _, one := range ones { + var fullTime = one.GetString("fullTime") + var timePieces = strings.Split(fullTime, ".") + var day = timePieces[0] + var hour = timePieces[1] + + m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{ + Bytes: one.GetInt64("bytes"), + Day: day, + Hour: types.Int32(hour), + } + } + + fullHours, err := utils.RangeHours(timeutil.FormatTime("YmdH", timestamp), timeutil.Format("YmdH")) + if err != nil { + return nil, err + } + for _, fullHour := range fullHours { + stat, ok := m[fullHour] + if ok { + result = append(result, stat) + } else { + result = append(result, &pb.FindHourlyServerBandwidthStatsResponse_Stat{ + Bytes: 0, + Day: fullHour[:8], + Hour: types.Int32(fullHour[8:]), + }) + } + } + + return result, nil +} + // FindDailyPeekBandwidthBytes 获取某天的带宽峰值 // day YYYYMMDD func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) { @@ -99,6 +155,54 @@ func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serv FindInt64Col(0) } +// FindDailyBandwidthStats 按天获取带宽峰值 +func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId int64, days int32) (result []*pb.FindDailyServerBandwidthStatsResponse_Stat, err error) { + if days <= 0 { + days = 14 + } + + var timestamp = time.Now().Unix() - int64(days)*86400 + + ones, _, err := this.Query(tx). + Table(this.partialTable(serverId)). + Result("MAX(bytes) AS bytes", "day"). + Attr("serverId", serverId). + Gte("day", timeutil.FormatTime("Ymd", timestamp)). + Group("day"). + FindOnes() + if err != nil { + return nil, err + } + + var m = map[string]*pb.FindDailyServerBandwidthStatsResponse_Stat{} + for _, one := range ones { + var day = one.GetString("day") + + m[day] = &pb.FindDailyServerBandwidthStatsResponse_Stat{ + Bytes: one.GetInt64("bytes"), + Day: day, + } + } + + allDays, err := utils.RangeDays(timeutil.FormatTime("Ymd", timestamp), timeutil.Format("Ymd")) + if err != nil { + return nil, err + } + for _, day := range allDays { + stat, ok := m[day] + if ok { + result = append(result, stat) + } else { + result = append(result, &pb.FindDailyServerBandwidthStatsResponse_Stat{ + Bytes: 0, + Day: day, + }) + } + } + + return result, nil +} + // FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值 // month YYYYMM func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) { diff --git a/internal/db/models/server_bandwidth_stat_dao_test.go b/internal/db/models/server_bandwidth_stat_dao_test.go index 37186cbd..9bd1e234 100644 --- a/internal/db/models/server_bandwidth_stat_dao_test.go +++ b/internal/db/models/server_bandwidth_stat_dao_test.go @@ -6,6 +6,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "github.com/iwind/TeaGo/bootstrap" "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/rands" timeutil "github.com/iwind/TeaGo/utils/time" "testing" @@ -77,3 +78,24 @@ func TestServerBandwidthStatDAO_Clean(t *testing.T) { } t.Log("ok", time.Since(before).Seconds()*1000, "ms") } + +func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) { + var dao = models.NewServerBandwidthStatDAO() + var tx *dbs.Tx + stats, err := dao.FindHourlyBandwidthStats(tx, 23, 24) + if err != nil { + t.Fatal(err) + } + logs.PrintAsJSON(stats, t) +} + + +func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) { + var dao = models.NewServerBandwidthStatDAO() + var tx *dbs.Tx + stats, err := dao.FindDailyBandwidthStats(tx, 23, 14) + if err != nil { + t.Fatal(err) + } + logs.PrintAsJSON(stats, t) +} \ No newline at end of file diff --git a/internal/rpc/services/service_server_bandwidth_stat.go b/internal/rpc/services/service_server_bandwidth_stat.go index 1c0c8dca..dcbad433 100644 --- a/internal/rpc/services/service_server_bandwidth_stat.go +++ b/internal/rpc/services/service_server_bandwidth_stat.go @@ -161,3 +161,39 @@ func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Con ServerBandwidthStats: pbStats, }, nil } + +// FindHourlyServerBandwidthStats 获取最近N小时峰值带宽 +func (this *ServerBandwidthStatService) FindHourlyServerBandwidthStats(ctx context.Context, req *pb.FindHourlyServerBandwidthStatsRequest) (*pb.FindHourlyServerBandwidthStatsResponse, error) { + _, err := this.ValidateAdmin(ctx) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + stats, err := models.SharedServerBandwidthStatDAO.FindHourlyBandwidthStats(tx, req.ServerId, req.Hours) + if err != nil { + return nil, err + } + + return &pb.FindHourlyServerBandwidthStatsResponse{ + Stats: stats, + }, nil +} + +// FindDailyServerBandwidthStats 获取最近N天峰值带宽 +func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx context.Context, req *pb.FindDailyServerBandwidthStatsRequest) (*pb.FindDailyServerBandwidthStatsResponse, error) { + _, err := this.ValidateAdmin(ctx) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + stats, err := models.SharedServerBandwidthStatDAO.FindDailyBandwidthStats(tx, req.ServerId, req.Days) + if err != nil { + return nil, err + } + + return &pb.FindDailyServerBandwidthStatsResponse{ + Stats: stats, + }, nil +}