mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-05 01:20:25 +08:00
实现峰值带宽和平均带宽两种带宽算法
This commit is contained in:
@@ -62,7 +62,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateServerBandwidth 写入数据
|
// UpdateServerBandwidth 写入数据
|
||||||
func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int64, serverId int64, day string, timeAt string, bytes int64) error {
|
// 暂时不使用region区分
|
||||||
|
func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int64, serverId int64, day string, timeAt string, bytes int64, totalBytes int64) error {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
return errors.New("invalid server id '" + types.String(serverId) + "'")
|
return errors.New("invalid server id '" + types.String(serverId) + "'")
|
||||||
}
|
}
|
||||||
@@ -70,32 +71,37 @@ func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int
|
|||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Param("bytes", bytes).
|
Param("bytes", bytes).
|
||||||
|
Param("totalBytes", totalBytes).
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
"userId": userId,
|
"userId": userId,
|
||||||
"serverId": serverId,
|
"serverId": serverId,
|
||||||
"day": day,
|
"day": day,
|
||||||
"timeAt": timeAt,
|
"timeAt": timeAt,
|
||||||
"bytes": bytes,
|
"bytes": bytes,
|
||||||
|
"totalBytes": totalBytes,
|
||||||
|
"avgBytes": totalBytes / 300,
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"bytes": dbs.SQL("bytes+:bytes"),
|
"bytes": dbs.SQL("bytes+:bytes"),
|
||||||
|
"avgBytes": dbs.SQL("(totalBytes+:totalBytes)/300"), // 因为生成SQL语句时会自动将avgBytes排在totalBytes之前,所以这里不用担心先后顺序的问题
|
||||||
|
"totalBytes": dbs.SQL("totalBytes+:totalBytes"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindMinutelyPeekBandwidthBytes 获取某分钟的带宽峰值
|
// FindMinutelyPeekBandwidthBytes 获取某分钟的带宽峰值
|
||||||
// day YYYYMMDD
|
// day YYYYMMDD
|
||||||
// minute HHII
|
// minute HHII
|
||||||
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string, useAvg bool) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Result("bytes").
|
Result(this.bytesField(useAvg)).
|
||||||
Attr("day", day).
|
Attr("day", day).
|
||||||
Attr("timeAt", minute).
|
Attr("timeAt", minute).
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindHourlyBandwidthStats 按小时获取带宽峰值
|
// FindHourlyBandwidthStats 按小时获取带宽峰值
|
||||||
func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverId int64, hours int32) (result []*pb.FindHourlyServerBandwidthStatsResponse_Stat, err error) {
|
func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverId int64, hours int32, useAvg bool) (result []*pb.FindHourlyServerBandwidthStatsResponse_Stat, err error) {
|
||||||
if hours <= 0 {
|
if hours <= 0 {
|
||||||
hours = 24
|
hours = 24
|
||||||
}
|
}
|
||||||
@@ -105,7 +111,7 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
|||||||
ones, _, err := this.Query(tx).
|
ones, _, err := this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Result("MAX(bytes) AS bytes", "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime").
|
Result(this.maxBytesField(useAvg), "CONCAT(day, '.', SUBSTRING(timeAt, 1, 2)) AS fullTime").
|
||||||
Gte("CONCAT(day, '.', SUBSTRING(timeAt, 1, 2))", timeutil.FormatTime("Ymd.H", timestamp)).
|
Gte("CONCAT(day, '.', SUBSTRING(timeAt, 1, 2))", timeutil.FormatTime("Ymd.H", timestamp)).
|
||||||
Group("fullTime").
|
Group("fullTime").
|
||||||
FindOnes()
|
FindOnes()
|
||||||
@@ -120,6 +126,7 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
|||||||
var day = timePieces[0]
|
var day = timePieces[0]
|
||||||
var hour = timePieces[1]
|
var hour = timePieces[1]
|
||||||
var bytes = one.GetInt64("bytes")
|
var bytes = one.GetInt64("bytes")
|
||||||
|
|
||||||
m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
m[day+hour] = &pb.FindHourlyServerBandwidthStatsResponse_Stat{
|
||||||
Bytes: bytes,
|
Bytes: bytes,
|
||||||
Bits: bytes * 8,
|
Bits: bytes * 8,
|
||||||
@@ -151,26 +158,25 @@ func (this *ServerBandwidthStatDAO) FindHourlyBandwidthStats(tx *dbs.Tx, serverI
|
|||||||
|
|
||||||
// FindDailyPeekBandwidthBytes 获取某天的带宽峰值
|
// FindDailyPeekBandwidthBytes 获取某天的带宽峰值
|
||||||
// day YYYYMMDD
|
// day YYYYMMDD
|
||||||
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, useAvg bool) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Attr("day", day).
|
Attr("day", day).
|
||||||
Result("MAX(bytes)").
|
Result(this.maxBytesField(useAvg)).
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindDailyBandwidthStats 按天获取带宽峰值
|
// FindDailyBandwidthStats 按天获取带宽峰值
|
||||||
func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId int64, days int32) (result []*pb.FindDailyServerBandwidthStatsResponse_Stat, err error) {
|
func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId int64, days int32, useAvg bool) (result []*pb.FindDailyServerBandwidthStatsResponse_Stat, err error) {
|
||||||
if days <= 0 {
|
if days <= 0 {
|
||||||
days = 14
|
days = 14
|
||||||
}
|
}
|
||||||
|
|
||||||
var timestamp = time.Now().Unix() - int64(days)*86400
|
var timestamp = time.Now().Unix() - int64(days)*86400
|
||||||
|
|
||||||
ones, _, err := this.Query(tx).
|
ones, _, err := this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Result("MAX(bytes) AS bytes", "day").
|
Result(this.maxBytesField(useAvg), "day").
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Gte("day", timeutil.FormatTime("Ymd", timestamp)).
|
Gte("day", timeutil.FormatTime("Ymd", timestamp)).
|
||||||
Group("day").
|
Group("day").
|
||||||
@@ -214,7 +220,7 @@ func (this *ServerBandwidthStatDAO) FindDailyBandwidthStats(tx *dbs.Tx, serverId
|
|||||||
// FindBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
// FindBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
||||||
// dayFrom YYYYMMDD
|
// dayFrom YYYYMMDD
|
||||||
// dayTo YYYYMMDD
|
// dayTo YYYYMMDD
|
||||||
func (this *ServerBandwidthStatDAO) FindBandwidthStatsBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
func (this *ServerBandwidthStatDAO) FindBandwidthStatsBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string, useAvg bool) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -232,7 +238,7 @@ func (this *ServerBandwidthStatDAO) FindBandwidthStatsBetweenDays(tx *dbs.Tx, se
|
|||||||
|
|
||||||
ones, _, err := this.Query(tx).
|
ones, _, err := this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Result("bytes", "day", "timeAt").
|
Result(this.bytesField(useAvg), "day", "timeAt").
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
FindOnes()
|
FindOnes()
|
||||||
@@ -292,12 +298,12 @@ func (this *ServerBandwidthStatDAO) FindBandwidthStatsBetweenDays(tx *dbs.Tx, se
|
|||||||
|
|
||||||
// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值
|
// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值
|
||||||
// month YYYYMM
|
// month YYYYMM
|
||||||
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) {
|
func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string, useAvg bool) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Result("MAX(bytes)").
|
Result(this.maxBytesField(useAvg)).
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +311,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, se
|
|||||||
// 参数:
|
// 参数:
|
||||||
// - day YYYYMMDD
|
// - day YYYYMMDD
|
||||||
// - timeAt HHII
|
// - timeAt HHII
|
||||||
func (this *ServerBandwidthStatDAO) FindServerStats(tx *dbs.Tx, serverId int64, day string, timeFrom string, timeTo string) (result []*ServerBandwidthStat, err error) {
|
func (this *ServerBandwidthStatDAO) FindServerStats(tx *dbs.Tx, serverId int64, day string, timeFrom string, timeTo string, useAvg bool) (result []*ServerBandwidthStat, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -313,12 +319,16 @@ func (this *ServerBandwidthStatDAO) FindServerStats(tx *dbs.Tx, serverId int64,
|
|||||||
Between("timeAt", timeFrom, timeTo).
|
Between("timeAt", timeFrom, timeTo).
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
|
|
||||||
|
// 使用平均带宽
|
||||||
|
this.fixServerStats(result, useAvg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllServerStatsWithDay 查找某个服务的当天的所有带宽峰值
|
// FindAllServerStatsWithDay 查找某个服务的当天的所有带宽峰值
|
||||||
// day YYYYMMDD
|
// day YYYYMMDD
|
||||||
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithDay(tx *dbs.Tx, serverId int64, day string) (result []*ServerBandwidthStat, err error) {
|
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithDay(tx *dbs.Tx, serverId int64, day string, useAvg bool) (result []*ServerBandwidthStat, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -326,12 +336,16 @@ func (this *ServerBandwidthStatDAO) FindAllServerStatsWithDay(tx *dbs.Tx, server
|
|||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
|
|
||||||
|
// 使用平均带宽
|
||||||
|
this.fixServerStats(result, useAvg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAllServerStatsWithMonth 查找某个服务的当月的所有带宽峰值
|
// FindAllServerStatsWithMonth 查找某个服务的当月的所有带宽峰值
|
||||||
// month YYYYMM
|
// month YYYYMM
|
||||||
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithMonth(tx *dbs.Tx, serverId int64, month string) (result []*ServerBandwidthStat, err error) {
|
func (this *ServerBandwidthStatDAO) FindAllServerStatsWithMonth(tx *dbs.Tx, serverId int64, month string, useAvg bool) (result []*ServerBandwidthStat, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
@@ -339,11 +353,15 @@ func (this *ServerBandwidthStatDAO) FindAllServerStatsWithMonth(tx *dbs.Tx, serv
|
|||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
|
|
||||||
|
// 使用平均带宽
|
||||||
|
this.fixServerStats(result, useAvg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindMonthlyPercentile 获取某月内百分位
|
// FindMonthlyPercentile 获取某月内百分位
|
||||||
func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId int64, month string, percentile int) (result int64, err error) {
|
func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId int64, month string, percentile int, useAvg bool) (result int64, err error) {
|
||||||
if percentile <= 0 {
|
if percentile <= 0 {
|
||||||
percentile = 95
|
percentile = 95
|
||||||
}
|
}
|
||||||
@@ -353,7 +371,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
result, err = this.Query(tx).
|
result, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Result("bytes").
|
Result(this.bytesField(useAvg)).
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
@@ -384,7 +402,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
result, err = this.Query(tx).
|
result, err = this.Query(tx).
|
||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Result("bytes").
|
Result(this.bytesField(useAvg)).
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
@@ -395,7 +413,7 @@ func (this *ServerBandwidthStatDAO) FindMonthlyPercentile(tx *dbs.Tx, serverId i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindPercentileBetweenDays 获取日期段内内百分位
|
// FindPercentileBetweenDays 获取日期段内内百分位
|
||||||
func (this *ServerBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string, percentile int32) (result *ServerBandwidthStat, err error) {
|
func (this *ServerBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, serverId int64, dayFrom string, dayTo string, percentile int32, useAvg bool) (result *ServerBandwidthStat, err error) {
|
||||||
if dayFrom > dayTo {
|
if dayFrom > dayTo {
|
||||||
dayFrom, dayTo = dayTo, dayFrom
|
dayFrom, dayTo = dayTo, dayFrom
|
||||||
}
|
}
|
||||||
@@ -410,13 +428,13 @@ func (this *ServerBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, server
|
|||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
Desc("bytes").
|
Desc(this.bytesOrderField(useAvg)).
|
||||||
Find()
|
Find()
|
||||||
if err != nil || one == nil {
|
if err != nil || one == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return one.(*ServerBandwidthStat), nil
|
return this.fixServerStat(one.(*ServerBandwidthStat), useAvg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 总数量
|
// 总数量
|
||||||
@@ -443,20 +461,20 @@ func (this *ServerBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, server
|
|||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
Desc("bytes").
|
Desc(this.bytesOrderField(useAvg)).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Find()
|
Find()
|
||||||
if err != nil || one == nil {
|
if err != nil || one == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return one.(*ServerBandwidthStat), nil
|
return this.fixServerStat(one.(*ServerBandwidthStat), useAvg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindPercentileBetweenTimes 获取时间段内内百分位
|
// FindPercentileBetweenTimes 获取时间段内内百分位
|
||||||
// timeFrom 开始时间,格式 YYYYMMDDHHII
|
// timeFrom 开始时间,格式 YYYYMMDDHHII
|
||||||
// timeTo 结束时间,格式 YYYYMMDDHHII
|
// timeTo 结束时间,格式 YYYYMMDDHHII
|
||||||
func (this *ServerBandwidthStatDAO) FindPercentileBetweenTimes(tx *dbs.Tx, serverId int64, timeFrom string, timeTo string, percentile int32) (result *ServerBandwidthStat, err error) {
|
func (this *ServerBandwidthStatDAO) FindPercentileBetweenTimes(tx *dbs.Tx, serverId int64, timeFrom string, timeTo string, percentile int32, useAvg bool) (result *ServerBandwidthStat, err error) {
|
||||||
var reg = regexp.MustCompile(`^\d{12}$`)
|
var reg = regexp.MustCompile(`^\d{12}$`)
|
||||||
if !reg.MatchString(timeFrom) {
|
if !reg.MatchString(timeFrom) {
|
||||||
return nil, errors.New("invalid timeFrom '" + timeFrom + "'")
|
return nil, errors.New("invalid timeFrom '" + timeFrom + "'")
|
||||||
@@ -479,13 +497,13 @@ func (this *ServerBandwidthStatDAO) FindPercentileBetweenTimes(tx *dbs.Tx, serve
|
|||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
||||||
Desc("bytes").
|
Desc(this.bytesOrderField(useAvg)).
|
||||||
Find()
|
Find()
|
||||||
if err != nil || one == nil {
|
if err != nil || one == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return one.(*ServerBandwidthStat), nil
|
return this.fixServerStat(one.(*ServerBandwidthStat), useAvg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 总数量
|
// 总数量
|
||||||
@@ -512,14 +530,14 @@ func (this *ServerBandwidthStatDAO) FindPercentileBetweenTimes(tx *dbs.Tx, serve
|
|||||||
Table(this.partialTable(serverId)).
|
Table(this.partialTable(serverId)).
|
||||||
Attr("serverId", serverId).
|
Attr("serverId", serverId).
|
||||||
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
Between("CONCAT(day, timeAt)", timeFrom, timeTo).
|
||||||
Desc("bytes").
|
Desc(this.bytesOrderField(useAvg)).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Find()
|
Find()
|
||||||
if err != nil || one == nil {
|
if err != nil || one == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return one.(*ServerBandwidthStat), nil
|
return this.fixServerStat(one.(*ServerBandwidthStat), useAvg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean 清理过期数据
|
// Clean 清理过期数据
|
||||||
@@ -559,3 +577,45 @@ func (this *ServerBandwidthStatDAO) runBatch(f func(table string, locker *sync.M
|
|||||||
func (this *ServerBandwidthStatDAO) partialTable(serverId int64) string {
|
func (this *ServerBandwidthStatDAO) partialTable(serverId int64) string {
|
||||||
return this.Table + "_" + types.String(serverId%int64(ServerBandwidthStatTablePartials))
|
return this.Table + "_" + types.String(serverId%int64(ServerBandwidthStatTablePartials))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取字节字段
|
||||||
|
func (this *ServerBandwidthStatDAO) bytesField(useAvg bool) string {
|
||||||
|
if useAvg {
|
||||||
|
return "avgBytes AS bytes"
|
||||||
|
}
|
||||||
|
return "bytes"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取最大字节字段
|
||||||
|
func (this *ServerBandwidthStatDAO) maxBytesField(useAvg bool) string {
|
||||||
|
if useAvg {
|
||||||
|
return "MAX(avgBytes) AS bytes"
|
||||||
|
}
|
||||||
|
return "MAX(bytes) AS bytes"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取排序字段
|
||||||
|
func (this *ServerBandwidthStatDAO) bytesOrderField(useAvg bool) string {
|
||||||
|
if useAvg {
|
||||||
|
return "avgBytes"
|
||||||
|
}
|
||||||
|
return "bytes"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ServerBandwidthStatDAO) fixServerStat(stat *ServerBandwidthStat, useAvg bool) *ServerBandwidthStat {
|
||||||
|
if stat == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if useAvg {
|
||||||
|
stat.Bytes = stat.AvgBytes
|
||||||
|
}
|
||||||
|
return stat
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ServerBandwidthStatDAO) fixServerStats(stats []*ServerBandwidthStat, useAvg bool) {
|
||||||
|
if useAvg {
|
||||||
|
for _, stat := range stats {
|
||||||
|
stat.Bytes = stat.AvgBytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
func TestServerBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
|
func TestServerBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := dao.UpdateServerBandwidth(tx, 1, 1, timeutil.Format("Ymd"), timeutil.Format("Hi"), 1024)
|
err := dao.UpdateServerBandwidth(tx, 1, 1, timeutil.Format("Ymd"), timeutil.FormatTime("Hi", time.Now().Unix()/300*300), 1024, 300)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ func TestSeverBandwidthStatDAO_InsertManyStats(t *testing.T) {
|
|||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -rands.Int(0, 200)))
|
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -rands.Int(0, 200)))
|
||||||
var minute = fmt.Sprintf("%02d%02d", rands.Int(0, 23), rands.Int(0, 59))
|
var minute = fmt.Sprintf("%02d%02d", rands.Int(0, 23), rands.Int(0, 59))
|
||||||
err := dao.UpdateServerBandwidth(tx, 1, 1, day, minute, 1024)
|
err := dao.UpdateServerBandwidth(tx, 1, 1, day, minute, 1024, 300)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -41,13 +41,14 @@ func TestSeverBandwidthStatDAO_InsertManyStats(t *testing.T) {
|
|||||||
func TestServerBandwidthStatDAO_FindMonthlyPercentile(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindMonthlyPercentile(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95))
|
t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95, false))
|
||||||
|
t.Log(dao.FindMonthlyPercentile(tx, 23, timeutil.Format("Ym"), 95, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServerBandwidthStatDAO_FindAllServerStatsWithMonth(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindAllServerStatsWithMonth(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindAllServerStatsWithMonth(tx, 23, timeutil.Format("Ym"))
|
stats, err := dao.FindAllServerStatsWithMonth(tx, 23, timeutil.Format("Ym"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -59,7 +60,7 @@ func TestServerBandwidthStatDAO_FindAllServerStatsWithMonth(t *testing.T) {
|
|||||||
func TestServerBandwidthStatDAO_FindAllServerStatsWithDay(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindAllServerStatsWithDay(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindAllServerStatsWithDay(tx, 23, timeutil.Format("Ymd"))
|
stats, err := dao.FindAllServerStatsWithDay(tx, 23, timeutil.Format("Ymd"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ func TestServerBandwidthStatDAO_Clean(t *testing.T) {
|
|||||||
func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindHourlyBandwidthStats(tx, 23, 24)
|
stats, err := dao.FindHourlyBandwidthStats(tx, 23, 24, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -92,7 +93,7 @@ func TestServerBandwidthStatDAO_FindHourlyBandwidthStats(t *testing.T) {
|
|||||||
func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindDailyBandwidthStats(tx, 23, 14)
|
stats, err := dao.FindDailyBandwidthStats(tx, 23, 14, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -102,7 +103,7 @@ func TestServerBandwidthStatDAO_FindDailyBandwidthStats(t *testing.T) {
|
|||||||
func TestServerBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
func TestServerBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
||||||
var dao = models.NewServerBandwidthStatDAO()
|
var dao = models.NewServerBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindBandwidthStatsBetweenDays(tx, 23, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"))
|
stats, err := dao.FindBandwidthStatsBetweenDays(tx, 23, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ type ServerBandwidthStat struct {
|
|||||||
Day string `field:"day"` // 日期YYYYMMDD
|
Day string `field:"day"` // 日期YYYYMMDD
|
||||||
TimeAt string `field:"timeAt"` // 时间点HHMM
|
TimeAt string `field:"timeAt"` // 时间点HHMM
|
||||||
Bytes uint64 `field:"bytes"` // 带宽字节
|
Bytes uint64 `field:"bytes"` // 带宽字节
|
||||||
|
AvgBytes uint64 `field:"avgBytes"` // 平均流量
|
||||||
|
TotalBytes uint64 `field:"totalBytes"` // 总流量
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerBandwidthStatOperator struct {
|
type ServerBandwidthStatOperator struct {
|
||||||
@@ -19,6 +21,8 @@ type ServerBandwidthStatOperator struct {
|
|||||||
Day any // 日期YYYYMMDD
|
Day any // 日期YYYYMMDD
|
||||||
TimeAt any // 时间点HHMM
|
TimeAt any // 时间点HHMM
|
||||||
Bytes any // 带宽字节
|
Bytes any // 带宽字节
|
||||||
|
AvgBytes any // 平均流量
|
||||||
|
TotalBytes any // 总流量
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerBandwidthStatOperator() *ServerBandwidthStatOperator {
|
func NewServerBandwidthStatOperator() *ServerBandwidthStatOperator {
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUserBandwidth 写入数据
|
// UpdateUserBandwidth 写入数据
|
||||||
func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64, regionId int64, day string, timeAt string, bytes int64) error {
|
func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64, regionId int64, day string, timeAt string, bytes int64, totalBytes int64) error {
|
||||||
if userId <= 0 {
|
if userId <= 0 {
|
||||||
// 如果用户ID不大于0,则说明服务不属于任何用户,此时不需要处理
|
// 如果用户ID不大于0,则说明服务不属于任何用户,此时不需要处理
|
||||||
return nil
|
return nil
|
||||||
@@ -70,23 +70,28 @@ func (this *UserBandwidthStatDAO) UpdateUserBandwidth(tx *dbs.Tx, userId int64,
|
|||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Table(this.partialTable(userId)).
|
Table(this.partialTable(userId)).
|
||||||
Param("bytes", bytes).
|
Param("bytes", bytes).
|
||||||
|
Param("totalBytes", totalBytes).
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
InsertOrUpdateQuickly(maps.Map{
|
||||||
"userId": userId,
|
"userId": userId,
|
||||||
"regionId": regionId,
|
"regionId": regionId,
|
||||||
"day": day,
|
"day": day,
|
||||||
"timeAt": timeAt,
|
"timeAt": timeAt,
|
||||||
"bytes": bytes,
|
"bytes": bytes,
|
||||||
|
"totalBytes": totalBytes,
|
||||||
|
"avgBytes": totalBytes / 300,
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"bytes": dbs.SQL("bytes+:bytes"),
|
"bytes": dbs.SQL("bytes+:bytes"),
|
||||||
|
"avgBytes": dbs.SQL("(totalBytes+:totalBytes)/300"), // 因为生成SQL语句时会自动将avgBytes排在totalBytes之前,所以这里不用担心先后顺序的问题
|
||||||
|
"totalBytes": dbs.SQL("totalBytes+:totalBytes"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindUserPeekBandwidthInMonth 读取某月带宽峰值
|
// FindUserPeekBandwidthInMonth 读取某月带宽峰值
|
||||||
// month YYYYMM
|
// month YYYYMM
|
||||||
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInMonth(tx *dbs.Tx, userId int64, month string) (*UserBandwidthStat, error) {
|
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInMonth(tx *dbs.Tx, userId int64, month string, useAvg bool) (*UserBandwidthStat, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Table(this.partialTable(userId)).
|
Table(this.partialTable(userId)).
|
||||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", this.sumBytesField(useAvg)).
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
Between("day", month+"01", month+"31").
|
Between("day", month+"01", month+"31").
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
@@ -101,7 +106,7 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInMonth(tx *dbs.Tx, userI
|
|||||||
|
|
||||||
// FindPercentileBetweenDays 获取日期段内内百分位
|
// FindPercentileBetweenDays 获取日期段内内百分位
|
||||||
// regionId 如果为 -1 表示没有区域的带宽;如果为 0 表示所有区域的带宽
|
// regionId 如果为 -1 表示没有区域的带宽;如果为 0 表示所有区域的带宽
|
||||||
func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string, percentile int32) (result *UserBandwidthStat, err error) {
|
func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string, percentile int32, useAvg bool) (result *UserBandwidthStat, err error) {
|
||||||
if dayFrom > dayTo {
|
if dayFrom > dayTo {
|
||||||
dayFrom, dayTo = dayTo, dayFrom
|
dayFrom, dayTo = dayTo, dayFrom
|
||||||
}
|
}
|
||||||
@@ -120,7 +125,7 @@ func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId i
|
|||||||
query.Attr("regionId", 0)
|
query.Attr("regionId", 0)
|
||||||
}
|
}
|
||||||
one, err := query.
|
one, err := query.
|
||||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", this.sumBytesField(useAvg)).
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
@@ -168,7 +173,7 @@ func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId i
|
|||||||
query.Attr("regionId", 0)
|
query.Attr("regionId", 0)
|
||||||
}
|
}
|
||||||
one, err := query.
|
one, err := query.
|
||||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", "SUM(bytes) AS bytes").
|
Result("MIN(id) AS id", "MIN(userId) AS userId", "day", "timeAt", this.sumBytesField(useAvg)).
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
@@ -185,10 +190,10 @@ func (this *UserBandwidthStatDAO) FindPercentileBetweenDays(tx *dbs.Tx, userId i
|
|||||||
|
|
||||||
// FindUserPeekBandwidthInDay 读取某日带宽峰值
|
// FindUserPeekBandwidthInDay 读取某日带宽峰值
|
||||||
// day YYYYMMDD
|
// day YYYYMMDD
|
||||||
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId int64, day string) (*UserBandwidthStat, error) {
|
func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId int64, day string, useAvg bool) (*UserBandwidthStat, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Table(this.partialTable(userId)).
|
Table(this.partialTable(userId)).
|
||||||
Result("MIN(id) AS id", "MIN(userId) AS userId", "MIN(day) AS day", "timeAt", "SUM(bytes) AS bytes").
|
Result("MIN(id) AS id", "MIN(userId) AS userId", "MIN(day) AS day", "timeAt", this.sumBytesField(useAvg)).
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
Attr("day", day).
|
Attr("day", day).
|
||||||
Desc("bytes").
|
Desc("bytes").
|
||||||
@@ -203,7 +208,7 @@ func (this *UserBandwidthStatDAO) FindUserPeekBandwidthInDay(tx *dbs.Tx, userId
|
|||||||
// FindUserBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
// FindUserBandwidthStatsBetweenDays 查找日期段内的带宽峰值
|
||||||
// dayFrom YYYYMMDD
|
// dayFrom YYYYMMDD
|
||||||
// dayTo YYYYMMDD
|
// dayTo YYYYMMDD
|
||||||
func (this *UserBandwidthStatDAO) FindUserBandwidthStatsBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
func (this *UserBandwidthStatDAO) FindUserBandwidthStatsBetweenDays(tx *dbs.Tx, userId int64, regionId int64, dayFrom string, dayTo string, useAvg bool) (result []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat, err error) {
|
||||||
if userId <= 0 {
|
if userId <= 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -225,7 +230,7 @@ func (this *UserBandwidthStatDAO) FindUserBandwidthStatsBetweenDays(tx *dbs.Tx,
|
|||||||
query.Attr("regionId", regionId)
|
query.Attr("regionId", regionId)
|
||||||
}
|
}
|
||||||
ones, _, err := query.
|
ones, _, err := query.
|
||||||
Result("SUM(bytes) AS bytes", "day", "timeAt").
|
Result(this.sumBytesField(useAvg), "day", "timeAt").
|
||||||
Attr("userId", userId).
|
Attr("userId", userId).
|
||||||
Between("day", dayFrom, dayTo).
|
Between("day", dayFrom, dayTo).
|
||||||
Group("day").
|
Group("day").
|
||||||
@@ -352,3 +357,21 @@ func (this *UserBandwidthStatDAO) runBatch(f func(table string, locker *sync.Mut
|
|||||||
func (this *UserBandwidthStatDAO) partialTable(userId int64) string {
|
func (this *UserBandwidthStatDAO) partialTable(userId int64) string {
|
||||||
return this.Table + "_" + types.String(userId%int64(UserBandwidthStatTablePartials))
|
return this.Table + "_" + types.String(userId%int64(UserBandwidthStatTablePartials))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取总数字段
|
||||||
|
func (this *UserBandwidthStatDAO) sumBytesField(useAvg bool) string {
|
||||||
|
if useAvg {
|
||||||
|
return "SUM(avgBytes) AS bytes"
|
||||||
|
}
|
||||||
|
return "SUM(bytes) AS bytes"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *UserBandwidthStatDAO) fixUserStat(stat *UserBandwidthStat, useAvg bool) *UserBandwidthStat {
|
||||||
|
if stat == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if useAvg {
|
||||||
|
stat.Bytes = stat.AvgBytes
|
||||||
|
}
|
||||||
|
return stat
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ import (
|
|||||||
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInMonth(t *testing.T) {
|
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInMonth(t *testing.T) {
|
||||||
var dao = models.NewUserBandwidthStatDAO()
|
var dao = models.NewUserBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stat, err := dao.FindUserPeekBandwidthInMonth(tx, 1, timeutil.Format("Ym"))
|
|
||||||
|
// max
|
||||||
|
{
|
||||||
|
stat, err := dao.FindUserPeekBandwidthInMonth(tx, 1, timeutil.Format("Ym"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -22,10 +25,21 @@ func TestUserBandwidthStatDAO_FindUserPeekBandwidthInMonth(t *testing.T) {
|
|||||||
logs.PrintAsJSON(stat, t)
|
logs.PrintAsJSON(stat, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// avg
|
||||||
|
{
|
||||||
|
stat, err := dao.FindUserPeekBandwidthInMonth(tx, 1, timeutil.Format("Ym"), true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logs.PrintAsJSON(stat, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInDay(t *testing.T) {
|
func TestUserBandwidthStatDAO_FindUserPeekBandwidthInDay(t *testing.T) {
|
||||||
var dao = models.NewUserBandwidthStatDAO()
|
var dao = models.NewUserBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stat, err := dao.FindUserPeekBandwidthInDay(tx, 1, timeutil.Format("Ymd"))
|
stat, err := dao.FindUserPeekBandwidthInDay(tx, 1, timeutil.Format("Ymd"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -36,7 +50,7 @@ func TestUserBandwidthStatDAO_FindUserPeekBandwidthInDay(t *testing.T) {
|
|||||||
func TestUserBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
|
func TestUserBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
|
||||||
var dao = models.NewUserBandwidthStatDAO()
|
var dao = models.NewUserBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := dao.UpdateUserBandwidth(tx, 1, 0, timeutil.Format("Ymd"), timeutil.Format("Hi"), 1024)
|
err := dao.UpdateUserBandwidth(tx, 1, 0, timeutil.Format("Ymd"), timeutil.FormatTime("Hi", time.Now().Unix()/300*300), 1024, 300)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -56,7 +70,7 @@ func TestUserBandwidthStatDAO_Clean(t *testing.T) {
|
|||||||
func TestUserBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
func TestUserBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
||||||
var dao = models.NewUserBandwidthStatDAO()
|
var dao = models.NewUserBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stats, err := dao.FindUserBandwidthStatsBetweenDays(tx, 1, 0, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"))
|
stats, err := dao.FindUserBandwidthStatsBetweenDays(tx, 1, 0, timeutil.Format("Ymd", time.Now().AddDate(0, 0, -2)), timeutil.Format("Ymd"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -73,7 +87,7 @@ func TestUserBandwidthStatDAO_FindBandwidthStatsBetweenDays(t *testing.T) {
|
|||||||
func TestUserBandwidthStatDAO_FindPercentileBetweenDays(t *testing.T) {
|
func TestUserBandwidthStatDAO_FindPercentileBetweenDays(t *testing.T) {
|
||||||
var dao = models.NewUserBandwidthStatDAO()
|
var dao = models.NewUserBandwidthStatDAO()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
stat, err := dao.FindPercentileBetweenDays(tx, 1, 0, timeutil.Format("Ymd"), timeutil.Format("Ymd"), 95)
|
stat, err := dao.FindPercentileBetweenDays(tx, 1, 0, timeutil.Format("Ymd"), timeutil.Format("Ymd"), 95, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,19 +4,23 @@ package models
|
|||||||
type UserBandwidthStat struct {
|
type UserBandwidthStat struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
UserId uint64 `field:"userId"` // 用户ID
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
RegionId uint32 `field:"regionId"` // 区域ID
|
||||||
Day string `field:"day"` // 日期YYYYMMDD
|
Day string `field:"day"` // 日期YYYYMMDD
|
||||||
TimeAt string `field:"timeAt"` // 时间点HHII
|
TimeAt string `field:"timeAt"` // 时间点HHII
|
||||||
Bytes uint64 `field:"bytes"` // 带宽
|
Bytes uint64 `field:"bytes"` // 带宽
|
||||||
RegionId uint32 `field:"regionId"` // 区域ID
|
TotalBytes uint64 `field:"totalBytes"` // 总流量
|
||||||
|
AvgBytes uint64 `field:"avgBytes"` // 平均流量
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserBandwidthStatOperator struct {
|
type UserBandwidthStatOperator struct {
|
||||||
Id any // ID
|
Id any // ID
|
||||||
UserId any // 用户ID
|
UserId any // 用户ID
|
||||||
|
RegionId any // 区域ID
|
||||||
Day any // 日期YYYYMMDD
|
Day any // 日期YYYYMMDD
|
||||||
TimeAt any // 时间点HHII
|
TimeAt any // 时间点HHII
|
||||||
Bytes any // 带宽
|
Bytes any // 带宽
|
||||||
RegionId any // 区域ID
|
TotalBytes any // 总流量
|
||||||
|
AvgBytes any // 平均流量
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserBandwidthStatOperator() *UserBandwidthStatOperator {
|
func NewUserBandwidthStatOperator() *UserBandwidthStatOperator {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -245,7 +246,7 @@ func (this *UserDAO) CreateUser(tx *dbs.Tx, username string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUser 修改用户
|
// UpdateUser 修改用户
|
||||||
func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, password string, fullname string, mobile string, tel string, email string, remark string, isOn bool, nodeClusterId int64) error {
|
func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, password string, fullname string, mobile string, tel string, email string, remark string, isOn bool, nodeClusterId int64, bandwidthAlgo systemconfigs.BandwidthAlgo) error {
|
||||||
if userId <= 0 {
|
if userId <= 0 {
|
||||||
return errors.New("invalid userId")
|
return errors.New("invalid userId")
|
||||||
}
|
}
|
||||||
@@ -262,6 +263,7 @@ func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, passw
|
|||||||
op.Email = email
|
op.Email = email
|
||||||
op.Remark = remark
|
op.Remark = remark
|
||||||
op.ClusterId = nodeClusterId
|
op.ClusterId = nodeClusterId
|
||||||
|
op.BandwidthAlgo = bandwidthAlgo
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -682,6 +684,34 @@ func (this *UserDAO) UpdateUserVerifiedEmail(tx *dbs.Tx, userId int64, verifiedE
|
|||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindUserBandwidthAlgoForView 获取用户浏览用的带宽算法
|
||||||
|
func (this *UserDAO) FindUserBandwidthAlgoForView(tx *dbs.Tx, userId int64, uiConfig *systemconfigs.UserUIConfig) (bandwidthAlgo string, err error) {
|
||||||
|
bandwidthAlgo, err = this.Query(tx).
|
||||||
|
Pk(userId).
|
||||||
|
Result("bandwidthAlgo").
|
||||||
|
FindStringCol("")
|
||||||
|
if len(bandwidthAlgo) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if uiConfig == nil {
|
||||||
|
uiConfig, err = SharedSysSettingDAO.ReadUserUIConfig(tx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if uiConfig == nil {
|
||||||
|
return systemconfigs.BandwidthAlgoSecondly, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if uiConfig != nil &&
|
||||||
|
len(uiConfig.TrafficStats.BandwidthAlgo) > 0 {
|
||||||
|
return uiConfig.TrafficStats.BandwidthAlgo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return systemconfigs.BandwidthAlgoSecondly, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 用户变更通知
|
// NotifyUpdate 用户变更通知
|
||||||
func (this *UserDAO) NotifyUpdate(tx *dbs.Tx, userId int64) error {
|
func (this *UserDAO) NotifyUpdate(tx *dbs.Tx, userId int64) error {
|
||||||
if userId <= 0 {
|
if userId <= 0 {
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ type User struct {
|
|||||||
PricePeriod string `field:"pricePeriod"` // 结算周期
|
PricePeriod string `field:"pricePeriod"` // 结算周期
|
||||||
ServersEnabled uint8 `field:"serversEnabled"` // 是否禁用所有服务
|
ServersEnabled uint8 `field:"serversEnabled"` // 是否禁用所有服务
|
||||||
Notification dbs.JSON `field:"notification"` // 通知设置
|
Notification dbs.JSON `field:"notification"` // 通知设置
|
||||||
|
BandwidthAlgo string `field:"bandwidthAlgo"` // 带宽算法
|
||||||
|
BandwidthModifier float64 `field:"bandwidthModifier"` // 带宽修正值
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserOperator struct {
|
type UserOperator struct {
|
||||||
@@ -67,6 +69,8 @@ type UserOperator struct {
|
|||||||
PricePeriod any // 结算周期
|
PricePeriod any // 结算周期
|
||||||
ServersEnabled any // 是否禁用所有服务
|
ServersEnabled any // 是否禁用所有服务
|
||||||
Notification any // 通知设置
|
Notification any // 通知设置
|
||||||
|
BandwidthAlgo any // 带宽算法
|
||||||
|
BandwidthModifier any // 带宽修正值
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserOperator() *UserOperator {
|
func NewUserOperator() *UserOperator {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func init() {
|
|||||||
for _, stat := range m {
|
for _, stat := range m {
|
||||||
// 更新服务的带宽峰值
|
// 更新服务的带宽峰值
|
||||||
if stat.ServerId > 0 {
|
if stat.ServerId > 0 {
|
||||||
err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes)
|
err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
|
remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ func init() {
|
|||||||
|
|
||||||
// 更新用户的带宽峰值
|
// 更新用户的带宽峰值
|
||||||
if stat.UserId > 0 {
|
if stat.UserId > 0 {
|
||||||
err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes)
|
err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("SharedUserBandwidthStatDAO", "dump bandwidth stats failed: "+err.Error())
|
remotelogs.Error("SharedUserBandwidthStatDAO", "dump bandwidth stats failed: "+err.Error())
|
||||||
}
|
}
|
||||||
@@ -126,6 +126,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
|
|||||||
oldStat, ok := serverBandwidthStatsMap[key]
|
oldStat, ok := serverBandwidthStatsMap[key]
|
||||||
if ok {
|
if ok {
|
||||||
oldStat.Bytes += stat.Bytes
|
oldStat.Bytes += stat.Bytes
|
||||||
|
oldStat.TotalBytes += stat.TotalBytes
|
||||||
} else {
|
} else {
|
||||||
serverBandwidthStatsMap[key] = &pb.ServerBandwidthStat{
|
serverBandwidthStatsMap[key] = &pb.ServerBandwidthStat{
|
||||||
Id: 0,
|
Id: 0,
|
||||||
@@ -135,6 +136,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
|
|||||||
Day: stat.Day,
|
Day: stat.Day,
|
||||||
TimeAt: stat.TimeAt,
|
TimeAt: stat.TimeAt,
|
||||||
Bytes: stat.Bytes,
|
Bytes: stat.Bytes,
|
||||||
|
TotalBytes: stat.TotalBytes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serverBandwidthStatsLocker.Unlock()
|
serverBandwidthStatsLocker.Unlock()
|
||||||
@@ -150,12 +152,26 @@ func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Con
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var stats = []*models.ServerBandwidthStat{}
|
|
||||||
var tx = this.NullTx()
|
var tx = this.NullTx()
|
||||||
|
|
||||||
|
// 带宽算法
|
||||||
|
if len(req.Algo) == 0 {
|
||||||
|
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Algo = bandwidthAlgo
|
||||||
|
}
|
||||||
|
|
||||||
|
var stats = []*models.ServerBandwidthStat{}
|
||||||
if len(req.Day) > 0 {
|
if len(req.Day) > 0 {
|
||||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day)
|
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
} else if len(req.Month) > 0 {
|
} else if len(req.Month) > 0 {
|
||||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithMonth(tx, req.ServerId, req.Month)
|
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithMonth(tx, req.ServerId, req.Month, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
} else {
|
} else {
|
||||||
// 默认返回空
|
// 默认返回空
|
||||||
return nil, errors.New("'month' or 'day' parameter is needed")
|
return nil, errors.New("'month' or 'day' parameter is needed")
|
||||||
@@ -188,12 +204,26 @@ func (this *ServerBandwidthStatService) FindHourlyServerBandwidthStats(ctx conte
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tx = this.NullTx()
|
||||||
|
|
||||||
|
// 带宽算法
|
||||||
|
if len(req.Algo) == 0 {
|
||||||
|
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Algo = bandwidthAlgo
|
||||||
|
}
|
||||||
|
|
||||||
if req.Hours <= 0 {
|
if req.Hours <= 0 {
|
||||||
req.Hours = 12
|
req.Hours = 12
|
||||||
}
|
}
|
||||||
|
|
||||||
var tx = this.NullTx()
|
stats, err := models.SharedServerBandwidthStatDAO.FindHourlyBandwidthStats(tx, req.ServerId, req.Hours, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
stats, err := models.SharedServerBandwidthStatDAO.FindHourlyBandwidthStats(tx, req.ServerId, req.Hours)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -210,7 +240,7 @@ func (this *ServerBandwidthStatService) FindHourlyServerBandwidthStats(ctx conte
|
|||||||
var timeTo = timeutil.Format("YmdHi")
|
var timeTo = timeutil.Format("YmdHi")
|
||||||
|
|
||||||
var pbNthStat *pb.FindHourlyServerBandwidthStatsResponse_Stat
|
var pbNthStat *pb.FindHourlyServerBandwidthStatsResponse_Stat
|
||||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile)
|
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -239,6 +269,19 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
|||||||
|
|
||||||
var tx = this.NullTx()
|
var tx = this.NullTx()
|
||||||
|
|
||||||
|
// 带宽算法
|
||||||
|
if len(req.Algo) == 0 {
|
||||||
|
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Algo = bandwidthAlgo
|
||||||
|
}
|
||||||
|
|
||||||
if req.Days <= 0 {
|
if req.Days <= 0 {
|
||||||
req.Days = 30
|
req.Days = 30
|
||||||
}
|
}
|
||||||
@@ -247,7 +290,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
|||||||
var dayFrom = timeutil.FormatTime("Ymd", timestamp)
|
var dayFrom = timeutil.FormatTime("Ymd", timestamp)
|
||||||
var dayTo = timeutil.Format("Ymd")
|
var dayTo = timeutil.Format("Ymd")
|
||||||
|
|
||||||
stats, err := models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, dayFrom, dayTo)
|
stats, err := models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, dayFrom, dayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -268,7 +311,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
|||||||
}
|
}
|
||||||
|
|
||||||
var pbNthStat = &pb.FindDailyServerBandwidthStatsResponse_Stat{}
|
var pbNthStat = &pb.FindDailyServerBandwidthStatsResponse_Stat{}
|
||||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, dayFrom, dayTo, percentile)
|
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, dayFrom, dayTo, percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -308,6 +351,27 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 带宽算法
|
||||||
|
if len(req.Algo) == 0 {
|
||||||
|
var bandwidthUserId = userId
|
||||||
|
if bandwidthUserId <= 0 {
|
||||||
|
if req.UserId > 0 {
|
||||||
|
bandwidthUserId = req.UserId
|
||||||
|
} else if req.ServerId > 0 {
|
||||||
|
bandwidthUserId, err = models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bandwidthUserId > 0 {
|
||||||
|
req.Algo, err = models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, bandwidthUserId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if req.UserId <= 0 && req.ServerId <= 0 {
|
if req.UserId <= 0 && req.ServerId <= 0 {
|
||||||
return &pb.FindDailyServerBandwidthStatsBetweenDaysResponse{
|
return &pb.FindDailyServerBandwidthStatsBetweenDaysResponse{
|
||||||
Stats: nil,
|
Stats: nil,
|
||||||
@@ -327,10 +391,10 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
|||||||
var pbStats = []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
|
var pbStats = []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
|
||||||
var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
|
var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
|
||||||
if req.ServerId > 0 { // 服务统计
|
if req.ServerId > 0 { // 服务统计
|
||||||
pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo)
|
pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
|
|
||||||
// nth
|
// nth
|
||||||
stat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Percentile)
|
stat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -343,10 +407,10 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { // 用户统计
|
} else { // 用户统计
|
||||||
pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo)
|
pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
|
|
||||||
// nth
|
// nth
|
||||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Percentile)
|
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -409,6 +409,19 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
var result = &pb.ComposeServerStatBoardResponse{}
|
var result = &pb.ComposeServerStatBoardResponse{}
|
||||||
var tx = this.NullTx()
|
var tx = this.NullTx()
|
||||||
|
|
||||||
|
// 用户ID
|
||||||
|
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var bandwidthAglo = ""
|
||||||
|
if userId > 0 {
|
||||||
|
bandwidthAglo, err = models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 带宽统计
|
// 带宽统计
|
||||||
{
|
{
|
||||||
var month = timeutil.Format("Ym")
|
var month = timeutil.Format("Ym")
|
||||||
@@ -423,7 +436,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
var minute3 = timeutil.FormatTime("Hi", timestamp-300*2)
|
var minute3 = timeutil.FormatTime("Hi", timestamp-300*2)
|
||||||
|
|
||||||
for _, minute := range []string{minute1, minute2, minute3} {
|
for _, minute := range []string{minute1, minute2, minute3} {
|
||||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMinutelyPeekBandwidthBytes(tx, req.ServerId, day, minute)
|
bytes, err := models.SharedServerBandwidthStatDAO.FindMinutelyPeekBandwidthBytes(tx, req.ServerId, day, minute, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -437,7 +450,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
|
|
||||||
// 当天
|
// 当天
|
||||||
{
|
{
|
||||||
bytes, err := models.SharedServerBandwidthStatDAO.FindDailyPeekBandwidthBytes(tx, req.ServerId, day)
|
bytes, err := models.SharedServerBandwidthStatDAO.FindDailyPeekBandwidthBytes(tx, req.ServerId, day, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -446,7 +459,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
|
|
||||||
// 当月
|
// 当月
|
||||||
{
|
{
|
||||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, month)
|
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, month, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -455,7 +468,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
|
|
||||||
// 上月
|
// 上月
|
||||||
{
|
{
|
||||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, timeutil.Format("Ym", time.Now().AddDate(0, -1, 0)))
|
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, timeutil.Format("Ym", time.Now().AddDate(0, -1, 0)), bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -476,7 +489,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
timeTo = r.Day + r.MinuteTo
|
timeTo = r.Day + r.MinuteTo
|
||||||
}
|
}
|
||||||
|
|
||||||
bandwidthStats, err := models.SharedServerBandwidthStatDAO.FindServerStats(tx, req.ServerId, r.Day, r.MinuteFrom, r.MinuteTo)
|
bandwidthStats, err := models.SharedServerBandwidthStatDAO.FindServerStats(tx, req.ServerId, r.Day, r.MinuteFrom, r.MinuteTo, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -519,7 +532,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
|||||||
}
|
}
|
||||||
result.BandwidthPercentile = percentile
|
result.BandwidthPercentile = percentile
|
||||||
|
|
||||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile)
|
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func (this *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserReque
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = models.SharedUserDAO.UpdateUser(tx, req.UserId, req.Username, req.Password, req.Fullname, req.Mobile, req.Tel, req.Email, req.Remark, req.IsOn, req.NodeClusterId)
|
err = models.SharedUserDAO.UpdateUser(tx, req.UserId, req.Username, req.Password, req.Fullname, req.Mobile, req.Tel, req.Email, req.Remark, req.IsOn, req.NodeClusterId, req.BandwidthAlgo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -259,6 +259,7 @@ func (this *UserService) FindEnabledUser(ctx context.Context, req *pb.FindEnable
|
|||||||
NodeCluster: pbCluster,
|
NodeCluster: pbCluster,
|
||||||
IsIndividualIdentified: isIndividualIdentified,
|
IsIndividualIdentified: isIndividualIdentified,
|
||||||
IsEnterpriseIdentified: isEnterpriseIdentified,
|
IsEnterpriseIdentified: isEnterpriseIdentified,
|
||||||
|
BandwidthAlgo: user.BandwidthAlgo,
|
||||||
OtpLogin: pbOtpAuth,
|
OtpLogin: pbOtpAuth,
|
||||||
}}, nil
|
}}, nil
|
||||||
}
|
}
|
||||||
@@ -404,6 +405,11 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
|
|
||||||
var tx = this.NullTx()
|
var tx = this.NullTx()
|
||||||
|
|
||||||
|
bandwidthAglo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, req.UserId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// 网站数量
|
// 网站数量
|
||||||
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(tx, 0, "", req.UserId, 0, configutils.BoolStateAll, []string{})
|
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(tx, 0, "", req.UserId, 0, configutils.BoolStateAll, []string{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -423,7 +429,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
// 本月带宽峰值
|
// 本月带宽峰值
|
||||||
var monthlyPeekBandwidthBytes int64 = 0
|
var monthlyPeekBandwidthBytes int64 = 0
|
||||||
{
|
{
|
||||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInMonth(tx, req.UserId, currentMonth)
|
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInMonth(tx, req.UserId, currentMonth, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -435,7 +441,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
// 本日带宽峰值
|
// 本日带宽峰值
|
||||||
var dailyPeekBandwidthBytes int64 = 0
|
var dailyPeekBandwidthBytes int64 = 0
|
||||||
{
|
{
|
||||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, currentDay)
|
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, currentDay, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -478,7 +484,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 峰值带宽
|
// 峰值带宽
|
||||||
peekBandwidthBytesStat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, day)
|
peekBandwidthBytesStat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, day, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -515,7 +521,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
bandwidthPercentile = userConfig.TrafficStats.BandwidthPercentile
|
bandwidthPercentile = userConfig.TrafficStats.BandwidthPercentile
|
||||||
}
|
}
|
||||||
result.BandwidthPercentile = bandwidthPercentile
|
result.BandwidthPercentile = bandwidthPercentile
|
||||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, 0, dayFrom, dayTo, bandwidthPercentile)
|
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, 0, dayFrom, dayTo, bandwidthPercentile, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user