请求统计增加即时、按天

This commit is contained in:
刘祥超
2021-06-08 15:10:08 +08:00
parent b61433c5f8
commit b651d94bf5
3 changed files with 166 additions and 8 deletions

View File

@@ -128,7 +128,37 @@ func (this *ServerDailyStatDAO) SumUserDailyPeek(tx *dbs.Tx, userId int64, regio
return int64(max), nil
}
// SumHourlyStat 获取 N 小时内的流量
// SumMinutelyStat 获取某个分钟内的流量
// minute 格式为YYYYMMDDHHMM并且已经格式化成每5分钟一个值
func (this *ServerDailyStatDAO) SumMinutelyStat(tx *dbs.Tx, serverId int64, minute string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{12}$`).MatchString(minute) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
Attr("serverId", serverId).
Attr("day", minute[:8]).
Attr("timeFrom", minute[8:]+"00").
FindOne()
if err != nil {
return nil, err
}
if one == nil {
return
}
stat.Bytes = one.GetInt64("bytes")
stat.CachedBytes = one.GetInt64("cachedBytes")
stat.CountRequests = one.GetInt64("countRequests")
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
return
}
// SumHourlyStat 获取某个小时内的流量
// hour 格式为YYYYMMDDHH
func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
@@ -158,3 +188,32 @@ func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour s
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
return
}
// SumDailyStat 获取某天内的流量
// day 格式为YYYYMMDD
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day string) (stat *pb.ServerDailyStat, err error) {
stat = &pb.ServerDailyStat{}
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
return
}
one, _, err := this.Query(tx).
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
Attr("serverId", serverId).
Attr("day", day).
FindOne()
if err != nil {
return nil, err
}
if one == nil {
return
}
stat.Bytes = one.GetInt64("bytes")
stat.CachedBytes = one.GetInt64("cachedBytes")
stat.CountRequests = one.GetInt64("countRequests")
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
return
}

View File

@@ -57,9 +57,20 @@ func TestServerDailyStatDAO_SumHourlyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
stats, err := NewServerDailyStatDAO().SumHourlyStats(tx, 23, timeutil.Format("YmdH"))
stat, err := NewServerDailyStatDAO().SumHourlyStat(tx, 23, timeutil.Format("YmdH"))
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stats, t)
logs.PrintAsJSON(stat, t)
}
func TestServerDailyStatDAO_SumMinutelyRequests(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
stat, err := NewServerDailyStatDAO().SumMinutelyStat(tx, 23, timeutil.Format("Ymd") + "1435")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(stat, t)
}

View File

@@ -67,8 +67,8 @@ func (this *ServerDailyStatService) UploadServerDailyStats(ctx context.Context,
return this.Success()
}
// FindServerHourlyStats 按小时读取统计数据
func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, req *pb.FindServerHourlyStatsRequest) (*pb.FindServerHourlyStatsResponse, error) {
// FindLatestServerHourlyStats 按小时读取统计数据
func (this *ServerDailyStatService) FindLatestServerHourlyStats(ctx context.Context, req *pb.FindLatestServerHourlyStatsRequest) (*pb.FindLatestServerHourlyStatsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
@@ -76,7 +76,7 @@ func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, r
tx := this.NullTx()
result := []*pb.FindServerHourlyStatsResponse_HourlyStat{}
result := []*pb.FindLatestServerHourlyStatsResponse_HourlyStat{}
if req.Hours > 0 {
for i := int32(0); i < req.Hours; i++ {
hourString := timeutil.Format("YmdH", time.Now().Add(-time.Duration(i)*time.Hour))
@@ -85,7 +85,7 @@ func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, r
return nil, err
}
if stat != nil {
result = append(result, &pb.FindServerHourlyStatsResponse_HourlyStat{
result = append(result, &pb.FindLatestServerHourlyStatsResponse_HourlyStat{
Hour: hourString,
Bytes: stat.Bytes,
CachedBytes: stat.CachedBytes,
@@ -95,5 +95,93 @@ func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, r
}
}
}
return &pb.FindServerHourlyStatsResponse{Stats: result}, nil
return &pb.FindLatestServerHourlyStatsResponse{Stats: result}, nil
}
// FindLatestServerMinutelyStats 按分钟读取统计数据
func (this *ServerDailyStatService) FindLatestServerMinutelyStats(ctx context.Context, req *pb.FindLatestServerMinutelyStatsRequest) (*pb.FindLatestServerMinutelyStatsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
tx := this.NullTx()
result := []*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{}
cache := map[string]*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{} // minute => stat
var avgRatio int64 = 5 * 60 // 因为每5分钟记录一次
if req.Minutes > 0 {
for i := int32(0); i < req.Minutes; i++ {
date := time.Now().Add(-time.Duration(i) * time.Minute)
minuteString := timeutil.Format("YmdHi", date)
minute := date.Minute()
roundMinute := minute - minute%5
if roundMinute != minute {
date = date.Add(-time.Duration(minute-roundMinute) * time.Minute)
}
queryMinuteString := timeutil.Format("YmdHi", date)
pbStat, ok := cache[queryMinuteString]
if ok {
result = append(result, &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
Minute: minuteString,
Bytes: pbStat.Bytes,
CachedBytes: pbStat.CachedBytes,
CountRequests: pbStat.CountRequests,
CountCachedRequests: pbStat.CountCachedRequests,
})
continue
}
stat, err := models.SharedServerDailyStatDAO.SumMinutelyStat(tx, req.ServerId, queryMinuteString)
if err != nil {
return nil, err
}
if stat != nil {
pbStat = &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
Minute: minuteString,
Bytes: stat.Bytes / avgRatio,
CachedBytes: stat.CachedBytes / avgRatio,
CountRequests: stat.CountRequests / avgRatio,
CountCachedRequests: stat.CountCachedRequests / avgRatio,
}
result = append(result, pbStat)
cache[queryMinuteString] = pbStat
}
}
}
return &pb.FindLatestServerMinutelyStatsResponse{Stats: result}, nil
}
// FindLatestServerDailyStats 按天读取统计数据
func (this *ServerDailyStatService) FindLatestServerDailyStats(ctx context.Context, req *pb.FindLatestServerDailyStatsRequest) (*pb.FindLatestServerDailyStatsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
tx := this.NullTx()
result := []*pb.FindLatestServerDailyStatsResponse_DailyStat{}
if req.Days > 0 {
for i := int32(0); i < req.Days; i++ {
dayString := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -int(i)))
stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.ServerId, dayString)
if err != nil {
return nil, err
}
if stat != nil {
result = append(result, &pb.FindLatestServerDailyStatsResponse_DailyStat{
Day: dayString,
Bytes: stat.Bytes,
CachedBytes: stat.CachedBytes,
CountRequests: stat.CountRequests,
CountCachedRequests: stat.CountCachedRequests,
})
}
}
}
return &pb.FindLatestServerDailyStatsResponse{Stats: result}, nil
}