Skip to content

Commit b651d94

Browse files
committed
请求统计增加即时、按天
1 parent b61433c commit b651d94

3 files changed

Lines changed: 166 additions & 8 deletions

File tree

internal/db/models/server_daily_stat_dao.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,37 @@ func (this *ServerDailyStatDAO) SumUserDailyPeek(tx *dbs.Tx, userId int64, regio
128128
return int64(max), nil
129129
}
130130

131-
// SumHourlyStat 获取 N 小时内的流量
131+
// SumMinutelyStat 获取某个分钟内的流量
132+
// minute 格式为YYYYMMDDHHMM,并且已经格式化成每5分钟一个值
133+
func (this *ServerDailyStatDAO) SumMinutelyStat(tx *dbs.Tx, serverId int64, minute string) (stat *pb.ServerDailyStat, err error) {
134+
stat = &pb.ServerDailyStat{}
135+
136+
if !regexp.MustCompile(`^\d{12}$`).MatchString(minute) {
137+
return
138+
}
139+
140+
one, _, err := this.Query(tx).
141+
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
142+
Attr("serverId", serverId).
143+
Attr("day", minute[:8]).
144+
Attr("timeFrom", minute[8:]+"00").
145+
FindOne()
146+
if err != nil {
147+
return nil, err
148+
}
149+
150+
if one == nil {
151+
return
152+
}
153+
154+
stat.Bytes = one.GetInt64("bytes")
155+
stat.CachedBytes = one.GetInt64("cachedBytes")
156+
stat.CountRequests = one.GetInt64("countRequests")
157+
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
158+
return
159+
}
160+
161+
// SumHourlyStat 获取某个小时内的流量
132162
// hour 格式为YYYYMMDDHH
133163
func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour string) (stat *pb.ServerDailyStat, err error) {
134164
stat = &pb.ServerDailyStat{}
@@ -158,3 +188,32 @@ func (this *ServerDailyStatDAO) SumHourlyStat(tx *dbs.Tx, serverId int64, hour s
158188
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
159189
return
160190
}
191+
192+
// SumDailyStat 获取某天内的流量
193+
// day 格式为YYYYMMDD
194+
func (this *ServerDailyStatDAO) SumDailyStat(tx *dbs.Tx, serverId int64, day string) (stat *pb.ServerDailyStat, err error) {
195+
stat = &pb.ServerDailyStat{}
196+
197+
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
198+
return
199+
}
200+
201+
one, _, err := this.Query(tx).
202+
Result("SUM(bytes) AS bytes, SUM(cachedBytes) AS cachedBytes, SUM(countRequests) AS countRequests, SUM(countCachedRequests) AS countCachedRequests").
203+
Attr("serverId", serverId).
204+
Attr("day", day).
205+
FindOne()
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
if one == nil {
211+
return
212+
}
213+
214+
stat.Bytes = one.GetInt64("bytes")
215+
stat.CachedBytes = one.GetInt64("cachedBytes")
216+
stat.CountRequests = one.GetInt64("countRequests")
217+
stat.CountCachedRequests = one.GetInt64("countCachedRequests")
218+
return
219+
}

internal/db/models/server_daily_stat_dao_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ func TestServerDailyStatDAO_SumHourlyRequests(t *testing.T) {
5757
dbs.NotifyReady()
5858
var tx *dbs.Tx
5959

60-
stats, err := NewServerDailyStatDAO().SumHourlyStats(tx, 23, timeutil.Format("YmdH"))
60+
stat, err := NewServerDailyStatDAO().SumHourlyStat(tx, 23, timeutil.Format("YmdH"))
6161
if err != nil {
6262
t.Fatal(err)
6363
}
64-
logs.PrintAsJSON(stats, t)
64+
logs.PrintAsJSON(stat, t)
65+
}
66+
67+
func TestServerDailyStatDAO_SumMinutelyRequests(t *testing.T) {
68+
dbs.NotifyReady()
69+
var tx *dbs.Tx
70+
71+
stat, err := NewServerDailyStatDAO().SumMinutelyStat(tx, 23, timeutil.Format("Ymd") + "1435")
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
logs.PrintAsJSON(stat, t)
6576
}

internal/rpc/services/service_server_daily_stat.go

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ func (this *ServerDailyStatService) UploadServerDailyStats(ctx context.Context,
6767
return this.Success()
6868
}
6969

70-
// FindServerHourlyStats 按小时读取统计数据
71-
func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, req *pb.FindServerHourlyStatsRequest) (*pb.FindServerHourlyStatsResponse, error) {
70+
// FindLatestServerHourlyStats 按小时读取统计数据
71+
func (this *ServerDailyStatService) FindLatestServerHourlyStats(ctx context.Context, req *pb.FindLatestServerHourlyStatsRequest) (*pb.FindLatestServerHourlyStatsResponse, error) {
7272
_, err := this.ValidateAdmin(ctx, 0)
7373
if err != nil {
7474
return nil, err
7575
}
7676

7777
tx := this.NullTx()
7878

79-
result := []*pb.FindServerHourlyStatsResponse_HourlyStat{}
79+
result := []*pb.FindLatestServerHourlyStatsResponse_HourlyStat{}
8080
if req.Hours > 0 {
8181
for i := int32(0); i < req.Hours; i++ {
8282
hourString := timeutil.Format("YmdH", time.Now().Add(-time.Duration(i)*time.Hour))
@@ -85,7 +85,7 @@ func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, r
8585
return nil, err
8686
}
8787
if stat != nil {
88-
result = append(result, &pb.FindServerHourlyStatsResponse_HourlyStat{
88+
result = append(result, &pb.FindLatestServerHourlyStatsResponse_HourlyStat{
8989
Hour: hourString,
9090
Bytes: stat.Bytes,
9191
CachedBytes: stat.CachedBytes,
@@ -95,5 +95,93 @@ func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, r
9595
}
9696
}
9797
}
98-
return &pb.FindServerHourlyStatsResponse{Stats: result}, nil
98+
return &pb.FindLatestServerHourlyStatsResponse{Stats: result}, nil
99+
}
100+
101+
// FindLatestServerMinutelyStats 按分钟读取统计数据
102+
func (this *ServerDailyStatService) FindLatestServerMinutelyStats(ctx context.Context, req *pb.FindLatestServerMinutelyStatsRequest) (*pb.FindLatestServerMinutelyStatsResponse, error) {
103+
_, err := this.ValidateAdmin(ctx, 0)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
tx := this.NullTx()
109+
110+
result := []*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{}
111+
cache := map[string]*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{} // minute => stat
112+
113+
var avgRatio int64 = 5 * 60 // 因为每5分钟记录一次
114+
115+
if req.Minutes > 0 {
116+
for i := int32(0); i < req.Minutes; i++ {
117+
date := time.Now().Add(-time.Duration(i) * time.Minute)
118+
minuteString := timeutil.Format("YmdHi", date)
119+
120+
minute := date.Minute()
121+
roundMinute := minute - minute%5
122+
if roundMinute != minute {
123+
date = date.Add(-time.Duration(minute-roundMinute) * time.Minute)
124+
}
125+
queryMinuteString := timeutil.Format("YmdHi", date)
126+
pbStat, ok := cache[queryMinuteString]
127+
if ok {
128+
result = append(result, &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
129+
Minute: minuteString,
130+
Bytes: pbStat.Bytes,
131+
CachedBytes: pbStat.CachedBytes,
132+
CountRequests: pbStat.CountRequests,
133+
CountCachedRequests: pbStat.CountCachedRequests,
134+
})
135+
continue
136+
}
137+
138+
stat, err := models.SharedServerDailyStatDAO.SumMinutelyStat(tx, req.ServerId, queryMinuteString)
139+
if err != nil {
140+
return nil, err
141+
}
142+
if stat != nil {
143+
pbStat = &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
144+
Minute: minuteString,
145+
Bytes: stat.Bytes / avgRatio,
146+
CachedBytes: stat.CachedBytes / avgRatio,
147+
CountRequests: stat.CountRequests / avgRatio,
148+
CountCachedRequests: stat.CountCachedRequests / avgRatio,
149+
}
150+
result = append(result, pbStat)
151+
cache[queryMinuteString] = pbStat
152+
}
153+
}
154+
}
155+
return &pb.FindLatestServerMinutelyStatsResponse{Stats: result}, nil
156+
}
157+
158+
// FindLatestServerDailyStats 按天读取统计数据
159+
func (this *ServerDailyStatService) FindLatestServerDailyStats(ctx context.Context, req *pb.FindLatestServerDailyStatsRequest) (*pb.FindLatestServerDailyStatsResponse, error) {
160+
_, err := this.ValidateAdmin(ctx, 0)
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
tx := this.NullTx()
166+
167+
result := []*pb.FindLatestServerDailyStatsResponse_DailyStat{}
168+
if req.Days > 0 {
169+
for i := int32(0); i < req.Days; i++ {
170+
dayString := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -int(i)))
171+
stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.ServerId, dayString)
172+
if err != nil {
173+
return nil, err
174+
}
175+
if stat != nil {
176+
result = append(result, &pb.FindLatestServerDailyStatsResponse_DailyStat{
177+
Day: dayString,
178+
Bytes: stat.Bytes,
179+
CachedBytes: stat.CachedBytes,
180+
CountRequests: stat.CountRequests,
181+
CountCachedRequests: stat.CountCachedRequests,
182+
})
183+
}
184+
}
185+
}
186+
return &pb.FindLatestServerDailyStatsResponse{Stats: result}, nil
99187
}

0 commit comments

Comments
 (0)