2020-12-11 17:28:20 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
2021-01-21 20:22:58 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
|
2020-12-11 17:28:20 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-01-21 18:55:34 +08:00
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
2021-06-08 11:18:27 +08:00
|
|
|
"time"
|
2020-12-11 17:28:20 +08:00
|
|
|
)
|
|
|
|
|
|
2021-06-08 11:18:27 +08:00
|
|
|
// ServerDailyStatService 服务统计相关服务
|
2020-12-11 17:28:20 +08:00
|
|
|
type ServerDailyStatService struct {
|
|
|
|
|
BaseService
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 11:18:27 +08:00
|
|
|
// UploadServerDailyStats 上传统计
|
2020-12-11 17:28:20 +08:00
|
|
|
func (this *ServerDailyStatService) UploadServerDailyStats(ctx context.Context, req *pb.UploadServerDailyStatsRequest) (*pb.RPCSuccess, error) {
|
2021-01-21 20:22:58 +08:00
|
|
|
nodeId, err := this.ValidateNode(ctx)
|
2020-12-11 17:28:20 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
err = models.SharedServerDailyStatDAO.SaveStats(tx, req.Stats)
|
2020-12-11 17:28:20 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-01-21 18:55:34 +08:00
|
|
|
|
|
|
|
|
// 写入其他统计表
|
|
|
|
|
// TODO 将来改成每小时入库一次
|
|
|
|
|
for _, stat := range req.Stats {
|
|
|
|
|
// 总体流量(按天)
|
2021-01-21 20:22:58 +08:00
|
|
|
err = stats.SharedTrafficDailyStatDAO.IncreaseDailyBytes(tx, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes)
|
2021-01-21 18:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 总体统计(按小时)
|
2021-01-21 20:22:58 +08:00
|
|
|
err = stats.SharedTrafficHourlyStatDAO.IncreaseHourlyBytes(tx, timeutil.FormatTime("YmdH", stat.CreatedAt), stat.Bytes)
|
2021-01-21 18:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 20:22:58 +08:00
|
|
|
// 节点流量
|
|
|
|
|
if nodeId > 0 {
|
|
|
|
|
err = stats.SharedNodeTrafficDailyStatDAO.IncreaseDailyBytes(tx, nodeId, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 集群流量
|
|
|
|
|
clusterId, err := models.SharedNodeDAO.FindNodeClusterId(tx, nodeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if clusterId > 0 {
|
|
|
|
|
err = stats.SharedNodeClusterTrafficDailyStatDAO.IncreaseDailyBytes(tx, clusterId, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-21 18:55:34 +08:00
|
|
|
|
2020-12-11 17:28:20 +08:00
|
|
|
return this.Success()
|
|
|
|
|
}
|
2021-06-08 11:18:27 +08:00
|
|
|
|
|
|
|
|
// FindServerHourlyStats 按小时读取统计数据
|
|
|
|
|
func (this *ServerDailyStatService) FindServerHourlyStats(ctx context.Context, req *pb.FindServerHourlyStatsRequest) (*pb.FindServerHourlyStatsResponse, error) {
|
|
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
result := []*pb.FindServerHourlyStatsResponse_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))
|
|
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumHourlyStat(tx, req.ServerId, hourString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
result = append(result, &pb.FindServerHourlyStatsResponse_HourlyStat{
|
|
|
|
|
Hour: hourString,
|
|
|
|
|
Bytes: stat.Bytes,
|
|
|
|
|
CachedBytes: stat.CachedBytes,
|
|
|
|
|
CountRequests: stat.CountRequests,
|
|
|
|
|
CountCachedRequests: stat.CountCachedRequests,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &pb.FindServerHourlyStatsResponse{Stats: result}, nil
|
|
|
|
|
}
|