diff --git a/internal/db/models/server_bandwidth_stat_dao.go b/internal/db/models/server_bandwidth_stat_dao.go index eeb2f699..049be1b2 100644 --- a/internal/db/models/server_bandwidth_stat_dao.go +++ b/internal/db/models/server_bandwidth_stat_dao.go @@ -124,6 +124,32 @@ func (this *ServerBandwidthStatDAO) FindServerStats(tx *dbs.Tx, serverId int64, return } +// FindAllServerStatsWithDay 查找某个服务的当天的所有带宽峰值 +// day YYYYMMDD +func (this *ServerBandwidthStatDAO) FindAllServerStatsWithDay(tx *dbs.Tx, serverId int64, day string) (result []*ServerBandwidthStat, err error) { + _, err = this.Query(tx). + Table(this.partialTable(serverId)). + Attr("serverId", serverId). + Attr("day", day). + AscPk(). + Slice(&result). + FindAll() + return +} + +// FindAllServerStatsWithMonth 查找某个服务的当月的所有带宽峰值 +// month YYYYMM +func (this *ServerBandwidthStatDAO) FindAllServerStatsWithMonth(tx *dbs.Tx, serverId int64, month string) (result []*ServerBandwidthStat, err error) { + _, err = this.Query(tx). + Table(this.partialTable(serverId)). + Attr("serverId", serverId). + Between("day", month+"01", month+"31"). + AscPk(). + Slice(&result). + FindAll() + return +} + // FindMonthlyPercentile 获取某月内百分位 func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId int64, month string, percentile int) (result int64, err error) { if percentile <= 0 { diff --git a/internal/db/models/server_bandwidth_stat_dao_test.go b/internal/db/models/server_bandwidth_stat_dao_test.go index 9ec8cd60..37186cbd 100644 --- a/internal/db/models/server_bandwidth_stat_dao_test.go +++ b/internal/db/models/server_bandwidth_stat_dao_test.go @@ -43,6 +43,30 @@ func TestServerBandwidthStatDAO_FindMonthlyPercentile(t *testing.T) { t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95)) } +func TestServerBandwidthStatDAO_FindAllServerStatsWithMonth(t *testing.T) { + var dao = models.NewServerBandwidthStatDAO() + var tx *dbs.Tx + stats, err := dao.FindAllServerStatsWithMonth(tx, 23, timeutil.Format("Ym")) + if err != nil { + t.Fatal(err) + } + for _, stat := range stats { + t.Logf("%+v", stat) + } +} + +func TestServerBandwidthStatDAO_FindAllServerStatsWithDay(t *testing.T) { + var dao = models.NewServerBandwidthStatDAO() + var tx *dbs.Tx + stats, err := dao.FindAllServerStatsWithDay(tx, 23, timeutil.Format("Ymd")) + if err != nil { + t.Fatal(err) + } + for _, stat := range stats { + t.Logf("%+v", stat) + } +} + func TestServerBandwidthStatDAO_Clean(t *testing.T) { var dao = models.NewServerBandwidthStatDAO() var tx *dbs.Tx diff --git a/internal/rpc/services/service_server_bandwidth_stat.go b/internal/rpc/services/service_server_bandwidth_stat.go index b07f1fba..5b9a34b8 100644 --- a/internal/rpc/services/service_server_bandwidth_stat.go +++ b/internal/rpc/services/service_server_bandwidth_stat.go @@ -4,6 +4,7 @@ package services import ( "context" + "errors" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs" @@ -105,3 +106,41 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C return this.Success() } + +// FindServerBandwidthStats 获取服务的峰值带宽 +func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Context, req *pb.FindServerBandwidthStatsRequest) (*pb.FindServerBandwidthStatsResponse, error) { + _, err := this.ValidateAdmin(ctx) + if err != nil { + return nil, err + } + + var stats = []*models.ServerBandwidthStat{} + var tx = this.NullTx() + if len(req.Day) > 0 { + stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day) + } else if len(req.Month) > 0 { + stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithMonth(tx, req.ServerId, req.Month) + } else { + // 默认返回空 + return nil, errors.New("'month' or 'day' parameter is needed") + } + + if err != nil { + return nil, err + } + + var pbStats = []*pb.ServerBandwidthStat{} + for _, stat := range stats { + pbStats = append(pbStats, &pb.ServerBandwidthStat{ + Id: int64(stat.Id), + UserId: int64(stat.UserId), + ServerId: int64(stat.ServerId), + Day: stat.Day, + TimeAt: stat.TimeAt, + Bytes: int64(stat.Bytes), + }) + } + return &pb.FindServerBandwidthStatsResponse{ + ServerBandwidthStats: pbStats, + }, nil +}