实现总体流量按天统计、按小时统计,提供管理平台Dashboard API

This commit is contained in:
GoEdgeLab
2021-01-21 18:55:34 +08:00
parent f914f45098
commit f947d769f7
15 changed files with 489 additions and 4 deletions

View File

@@ -41,7 +41,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs(t *testing.T) {
t.Fatal(err)
}
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
if err != nil {
t.Fatal(err)
}
@@ -68,7 +68,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page(t *testing.T) {
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
@@ -99,7 +99,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Reverse(t *testing.T) {
}
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0)
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
@@ -124,7 +124,7 @@ func TestHTTPAccessLogDAO_ListAccessLogs_Page_NotExists(t *testing.T) {
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0)
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0)
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)

View File

@@ -163,6 +163,8 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId
func (this *NodeDAO) CountAllEnabledNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(NodeStateEnabled).
Where("clusterId IN (SELECT id FROM "+SharedNodeClusterDAO.Table+" WHERE state=:clusterState)").
Param("clusterState", NodeClusterStateEnabled).
Count()
}

View File

@@ -592,6 +592,13 @@ func (this *ServerDAO) UpdateServerReverseProxy(tx *dbs.Tx, serverId int64, conf
return this.NotifyUpdate(tx, serverId)
}
// 计算所有可用服务数量
func (this *ServerDAO) CountAllEnabledServers(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(ServerStateEnabled).
Count()
}
// 计算所有可用服务数量
func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag configutils.BoolState, protocolFamily string) (int64, error) {
query := this.Query(tx).

View File

@@ -0,0 +1,75 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type TrafficDailyStatDAO dbs.DAO
func NewTrafficDailyStatDAO() *TrafficDailyStatDAO {
return dbs.NewDAO(&TrafficDailyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeTrafficDailyStats",
Model: new(TrafficDailyStat),
PkName: "id",
},
}).(*TrafficDailyStatDAO)
}
var SharedTrafficDailyStatDAO *TrafficDailyStatDAO
func init() {
dbs.OnReady(func() {
SharedTrafficDailyStatDAO = NewTrafficDailyStatDAO()
})
}
// 增加流量
func (this *TrafficDailyStatDAO) IncreaseDailyBytes(tx *dbs.Tx, day string, bytes int64) error {
if len(day) != 8 {
return errors.New("invalid day '" + day + "'")
}
err := this.Query(tx).
Param("bytes", bytes).
InsertOrUpdateQuickly(maps.Map{
"day": day,
"bytes": bytes,
}, maps.Map{
"bytes": dbs.SQL("bytes+:bytes"),
})
if err != nil {
return err
}
return nil
}
// 获取日期之间统计
func (this *TrafficDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*TrafficDailyStat, err error) {
ones, err := this.Query(tx).
Between("day", dayFrom, dayTo).
FindAll()
dayMap := map[string]*TrafficDailyStat{} // day => Stat
for _, one := range ones {
stat := one.(*TrafficDailyStat)
dayMap[stat.Day] = stat
}
days, err := utils.RangeDays(dayFrom, dayTo)
if err != nil {
return nil, err
}
for _, day := range days {
stat, ok := dayMap[day]
if ok {
result = append(result, stat)
} else {
result = append(result, &TrafficDailyStat{Day: day})
}
}
return result, nil
}

View File

@@ -0,0 +1,20 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
"time"
)
func TestTrafficDailyStatDAO_IncreaseDayBytes(t *testing.T) {
dbs.NotifyReady()
now := time.Now()
err := SharedTrafficDailyStatDAO.IncreaseDayBytes(nil, timeutil.Format("Ymd"), 1)
if err != nil {
t.Fatal(err)
}
t.Log("ok", time.Since(now).Seconds()*1000, "ms")
}

View File

@@ -0,0 +1,18 @@
package models
// 总的流量统计
type TrafficDailyStat struct {
Id uint64 `field:"id"` // ID
Day string `field:"day"` // YYYYMMDD
Bytes uint64 `field:"bytes"` // 流量字节
}
type TrafficDailyStatOperator struct {
Id interface{} // ID
Day interface{} // YYYYMMDD
Bytes interface{} // 流量字节
}
func NewTrafficDailyStatOperator() *TrafficDailyStatOperator {
return &TrafficDailyStatOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -0,0 +1,75 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
)
type TrafficHourlyStatDAO dbs.DAO
func NewTrafficHourlyStatDAO() *TrafficHourlyStatDAO {
return dbs.NewDAO(&TrafficHourlyStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeTrafficHourlyStats",
Model: new(TrafficHourlyStat),
PkName: "id",
},
}).(*TrafficHourlyStatDAO)
}
var SharedTrafficHourlyStatDAO *TrafficHourlyStatDAO
func init() {
dbs.OnReady(func() {
SharedTrafficHourlyStatDAO = NewTrafficHourlyStatDAO()
})
}
// 增加流量
func (this *TrafficHourlyStatDAO) IncreaseHourlyBytes(tx *dbs.Tx, hour string, bytes int64) error {
if len(hour) != 10 {
return errors.New("invalid hour '" + hour + "'")
}
err := this.Query(tx).
Param("bytes", bytes).
InsertOrUpdateQuickly(maps.Map{
"hour": hour,
"bytes": bytes,
}, maps.Map{
"bytes": dbs.SQL("bytes+:bytes"),
})
if err != nil {
return err
}
return nil
}
// 获取日期之间统计
func (this *TrafficHourlyStatDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, hourTo string) (result []*TrafficHourlyStat, err error) {
ones, err := this.Query(tx).
Between("hour", hourFrom, hourTo).
FindAll()
hourMap := map[string]*TrafficHourlyStat{} // hour => Stat
for _, one := range ones {
stat := one.(*TrafficHourlyStat)
hourMap[stat.Hour] = stat
}
hours, err := utils.RangeHours(hourFrom, hourTo)
if err != nil {
return nil, err
}
for _, hour := range hours {
stat, ok := hourMap[hour]
if ok {
result = append(result, stat)
} else {
result = append(result, &TrafficHourlyStat{Hour: hour})
}
}
return result, nil
}

View File

@@ -0,0 +1,20 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
"time"
)
func TestTrafficHourlyStatDAO_IncreaseDayBytes(t *testing.T) {
dbs.NotifyReady()
now := time.Now()
err := SharedTrafficHourlyStatDAO.IncreaseDayBytes(nil, timeutil.Format("YmdH"), 1)
if err != nil {
t.Fatal(err)
}
t.Log("ok", time.Since(now).Seconds()*1000, "ms")
}

View File

@@ -0,0 +1,18 @@
package models
// 总的流量统计(按小时)
type TrafficHourlyStat struct {
Id uint64 `field:"id"` // ID
Hour string `field:"hour"` // YYYYMMDDHH
Bytes uint64 `field:"bytes"` // 流量字节
}
type TrafficHourlyStatOperator struct {
Id interface{} // ID
Hour interface{} // YYYYMMDDHH
Bytes interface{} // 流量字节
}
func NewTrafficHourlyStatOperator() *TrafficHourlyStatOperator {
return &TrafficHourlyStatOperator{}
}

View File

@@ -0,0 +1 @@
package models