77 "github.com/iwind/TeaGo/dbs"
88 "github.com/iwind/TeaGo/maps"
99 timeutil "github.com/iwind/TeaGo/utils/time"
10+ "regexp"
1011)
1112
1213type ServerDailyStatDAO dbs.DAO
@@ -30,24 +31,33 @@ func init() {
3031 })
3132}
3233
33- // 提交数据
34+ // SaveStats 提交数据
3435func (this * ServerDailyStatDAO ) SaveStats (tx * dbs.Tx , stats []* pb.ServerDailyStat ) error {
3536 for _ , stat := range stats {
3637 day := timeutil .FormatTime ("Ymd" , stat .CreatedAt )
3738 timeFrom := timeutil .FormatTime ("His" , stat .CreatedAt )
38- timeTo := timeutil .FormatTime ("His" , stat .CreatedAt + 5 * 60 ) // 5分钟
39+ timeTo := timeutil .FormatTime ("His" , stat .CreatedAt + 5 * 60 - 1 ) // 5分钟
3940
4041 _ , _ , err := this .Query (tx ).
4142 Param ("bytes" , stat .Bytes ).
43+ Param ("cachedBytes" , stat .CachedBytes ).
44+ Param ("countRequests" , stat .CountRequests ).
45+ Param ("countCachedRequests" , stat .CountCachedRequests ).
4246 InsertOrUpdate (maps.Map {
43- "serverId" : stat .ServerId ,
44- "regionId" : stat .RegionId ,
45- "bytes" : dbs .SQL ("bytes+:bytes" ),
46- "day" : day ,
47- "timeFrom" : timeFrom ,
48- "timeTo" : timeTo ,
47+ "serverId" : stat .ServerId ,
48+ "regionId" : stat .RegionId ,
49+ "bytes" : dbs .SQL ("bytes+:bytes" ),
50+ "cachedBytes" : dbs .SQL ("cachedBytes+:cachedBytes" ),
51+ "countRequests" : dbs .SQL ("countRequests+:countRequests" ),
52+ "countCachedRequests" : dbs .SQL ("countCachedRequests+:countCachedRequests" ),
53+ "day" : day ,
54+ "timeFrom" : timeFrom ,
55+ "timeTo" : timeTo ,
4956 }, maps.Map {
50- "bytes" : dbs .SQL ("bytes+:bytes" ),
57+ "bytes" : dbs .SQL ("bytes+:bytes" ),
58+ "cachedBytes" : dbs .SQL ("cachedBytes+:cachedBytes" ),
59+ "countRequests" : dbs .SQL ("countRequests+:countRequests" ),
60+ "countCachedRequests" : dbs .SQL ("countCachedRequests+:countCachedRequests" ),
5161 })
5262 if err != nil {
5363 return err
@@ -56,7 +66,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
5666 return nil
5767}
5868
59- // 根据用户计算某月合计
69+ // SumUserMonthly 根据用户计算某月合计
6070// month 格式为YYYYMM
6171func (this * ServerDailyStatDAO ) SumUserMonthly (tx * dbs.Tx , userId int64 , regionId int64 , month string ) (int64 , error ) {
6272 query := this .Query (tx )
@@ -69,7 +79,7 @@ func (this *ServerDailyStatDAO) SumUserMonthly(tx *dbs.Tx, userId int64, regionI
6979 SumInt64 ("bytes" , 0 )
7080}
7181
72- // 获取某月带宽峰值
82+ // SumUserMonthlyPeek 获取某月带宽峰值
7383// month 格式为YYYYMM
7484func (this * ServerDailyStatDAO ) SumUserMonthlyPeek (tx * dbs.Tx , userId int64 , regionId int64 , month string ) (int64 , error ) {
7585 query := this .Query (tx )
@@ -86,7 +96,7 @@ func (this *ServerDailyStatDAO) SumUserMonthlyPeek(tx *dbs.Tx, userId int64, reg
8696 return int64 (max ), nil
8797}
8898
89- // 获取某天流量总和
99+ // SumUserDaily 获取某天流量总和
90100// day 格式为YYYYMMDD
91101func (this * ServerDailyStatDAO ) SumUserDaily (tx * dbs.Tx , userId int64 , regionId int64 , day string ) (int64 , error ) {
92102 query := this .Query (tx )
@@ -100,7 +110,7 @@ func (this *ServerDailyStatDAO) SumUserDaily(tx *dbs.Tx, userId int64, regionId
100110 SumInt64 ("bytes" , 0 )
101111}
102112
103- // 获取某天带宽峰值
113+ // SumUserDailyPeek 获取某天带宽峰值
104114// day 格式为YYYYMMDD
105115func (this * ServerDailyStatDAO ) SumUserDailyPeek (tx * dbs.Tx , userId int64 , regionId int64 , day string ) (int64 , error ) {
106116 query := this .Query (tx )
@@ -117,3 +127,34 @@ func (this *ServerDailyStatDAO) SumUserDailyPeek(tx *dbs.Tx, userId int64, regio
117127 }
118128 return int64 (max ), nil
119129}
130+
131+ // SumHourlyStat 获取 N 小时内的流量
132+ // hour 格式为YYYYMMDDHH
133+ func (this * ServerDailyStatDAO ) SumHourlyStat (tx * dbs.Tx , serverId int64 , hour string ) (stat * pb.ServerDailyStat , err error ) {
134+ stat = & pb.ServerDailyStat {}
135+
136+ if ! regexp .MustCompile (`^\d{10}$` ).MatchString (hour ) {
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" , hour [:8 ]).
144+ Gte ("timeFrom" , hour [8 :]+ "0000" ).
145+ Lte ("timeTo" , hour [8 :]+ "5959" ).
146+ FindOne ()
147+ if err != nil {
148+ return nil , err
149+ }
150+
151+ if one == nil {
152+ return
153+ }
154+
155+ stat .Bytes = one .GetInt64 ("bytes" )
156+ stat .CachedBytes = one .GetInt64 ("cachedBytes" )
157+ stat .CountRequests = one .GetInt64 ("countRequests" )
158+ stat .CountCachedRequests = one .GetInt64 ("countCachedRequests" )
159+ return
160+ }
0 commit comments