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"
|
2021-07-05 11:37:22 +08:00
|
|
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
2020-12-11 17:28:20 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2022-04-15 12:14:59 +08:00
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2021-01-21 18:55:34 +08:00
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
2021-06-17 21:17:53 +08:00
|
|
|
"math"
|
2022-06-18 14:26:43 +08:00
|
|
|
"regexp"
|
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-07-05 11:37:22 +08:00
|
|
|
role, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeDNS)
|
2020-12-11 17:28:20 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:05:30 +08:00
|
|
|
var tx = this.NullTx()
|
2021-01-01 23:31:30 +08:00
|
|
|
|
2021-10-21 17:10:53 +08:00
|
|
|
// 保存统计数据
|
2021-01-01 23:31:30 +08:00
|
|
|
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
|
|
|
|
2021-07-05 11:37:22 +08:00
|
|
|
var clusterId int64
|
|
|
|
|
switch role {
|
|
|
|
|
case rpcutils.UserTypeDNS:
|
2021-08-08 15:47:48 +08:00
|
|
|
clusterId, err = models.SharedNSNodeDAO.FindNodeClusterId(tx, nodeId)
|
2021-07-05 11:37:22 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 18:55:34 +08:00
|
|
|
// 写入其他统计表
|
|
|
|
|
// TODO 将来改成每小时入库一次
|
|
|
|
|
for _, stat := range req.Stats {
|
2021-08-01 11:13:46 +08:00
|
|
|
if role == rpcutils.UserTypeNode {
|
|
|
|
|
clusterId, err = models.SharedServerDAO.FindServerClusterId(tx, stat.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 18:55:34 +08:00
|
|
|
// 总体流量(按天)
|
2021-07-13 11:04:45 +08:00
|
|
|
err = stats.SharedTrafficDailyStatDAO.IncreaseDailyStat(tx, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
2021-01-21 18:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 总体统计(按小时)
|
2021-07-13 11:04:45 +08:00
|
|
|
err = stats.SharedTrafficHourlyStatDAO.IncreaseHourlyStat(tx, timeutil.FormatTime("YmdH", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
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 {
|
2021-07-13 11:04:45 +08:00
|
|
|
err = stats.SharedNodeTrafficDailyStatDAO.IncreaseDailyStat(tx, clusterId, role, nodeId, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
2021-01-21 20:22:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 11:04:45 +08:00
|
|
|
err = stats.SharedNodeTrafficHourlyStatDAO.IncreaseHourlyStat(tx, clusterId, role, nodeId, timeutil.FormatTime("YmdH", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
2021-01-21 20:22:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-07-05 11:37:22 +08:00
|
|
|
|
|
|
|
|
// 集群流量
|
2021-01-21 20:22:58 +08:00
|
|
|
if clusterId > 0 {
|
2021-07-13 11:04:45 +08:00
|
|
|
err = stats.SharedNodeClusterTrafficDailyStatDAO.IncreaseDailyStat(tx, clusterId, timeutil.FormatTime("Ymd", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
2021-01-21 20:22:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-21 18:55:34 +08:00
|
|
|
|
2021-07-05 11:37:22 +08:00
|
|
|
// 域名统计
|
|
|
|
|
for _, stat := range req.DomainStats {
|
2021-08-01 11:13:46 +08:00
|
|
|
if role == rpcutils.UserTypeNode {
|
|
|
|
|
clusterId, err = models.SharedServerDAO.FindServerClusterId(tx, stat.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 11:04:45 +08:00
|
|
|
err := stats.SharedServerDomainHourlyStatDAO.IncreaseHourlyStat(tx, clusterId, nodeId, stat.ServerId, stat.Domain, timeutil.FormatTime("YmdH", stat.CreatedAt), stat.Bytes, stat.CachedBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.AttackBytes)
|
2021-07-05 11:37:22 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 17:28:20 +08:00
|
|
|
return this.Success()
|
|
|
|
|
}
|
2021-06-08 11:18:27 +08:00
|
|
|
|
2021-06-08 15:10:08 +08:00
|
|
|
// FindLatestServerHourlyStats 按小时读取统计数据
|
|
|
|
|
func (this *ServerDailyStatService) FindLatestServerHourlyStats(ctx context.Context, req *pb.FindLatestServerHourlyStatsRequest) (*pb.FindLatestServerHourlyStatsResponse, error) {
|
2022-07-22 14:35:17 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx)
|
2021-06-08 11:18:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:05:30 +08:00
|
|
|
var tx = this.NullTx()
|
2021-06-08 11:18:27 +08:00
|
|
|
|
2021-06-08 15:10:08 +08:00
|
|
|
result := []*pb.FindLatestServerHourlyStatsResponse_HourlyStat{}
|
2021-06-08 11:18:27 +08:00
|
|
|
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 {
|
2021-06-08 15:10:08 +08:00
|
|
|
result = append(result, &pb.FindLatestServerHourlyStatsResponse_HourlyStat{
|
2021-06-08 11:18:27 +08:00
|
|
|
Hour: hourString,
|
|
|
|
|
Bytes: stat.Bytes,
|
|
|
|
|
CachedBytes: stat.CachedBytes,
|
|
|
|
|
CountRequests: stat.CountRequests,
|
|
|
|
|
CountCachedRequests: stat.CountCachedRequests,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-08 15:10:08 +08:00
|
|
|
return &pb.FindLatestServerHourlyStatsResponse{Stats: result}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindLatestServerMinutelyStats 按分钟读取统计数据
|
|
|
|
|
func (this *ServerDailyStatService) FindLatestServerMinutelyStats(ctx context.Context, req *pb.FindLatestServerMinutelyStatsRequest) (*pb.FindLatestServerMinutelyStatsResponse, error) {
|
2022-07-22 14:35:17 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx)
|
2021-06-08 15:10:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:05:30 +08:00
|
|
|
var tx = this.NullTx()
|
2021-06-08 15:10:08 +08:00
|
|
|
|
|
|
|
|
result := []*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{}
|
|
|
|
|
cache := map[string]*pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{} // minute => stat
|
|
|
|
|
|
|
|
|
|
var avgRatio int64 = 5 * 60 // 因为每5分钟记录一次
|
|
|
|
|
|
|
|
|
|
if req.Minutes > 0 {
|
|
|
|
|
for i := int32(0); i < req.Minutes; i++ {
|
|
|
|
|
date := time.Now().Add(-time.Duration(i) * time.Minute)
|
|
|
|
|
minuteString := timeutil.Format("YmdHi", date)
|
|
|
|
|
|
|
|
|
|
minute := date.Minute()
|
|
|
|
|
roundMinute := minute - minute%5
|
|
|
|
|
if roundMinute != minute {
|
|
|
|
|
date = date.Add(-time.Duration(minute-roundMinute) * time.Minute)
|
|
|
|
|
}
|
|
|
|
|
queryMinuteString := timeutil.Format("YmdHi", date)
|
|
|
|
|
pbStat, ok := cache[queryMinuteString]
|
|
|
|
|
if ok {
|
|
|
|
|
result = append(result, &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
|
|
|
|
|
Minute: minuteString,
|
|
|
|
|
Bytes: pbStat.Bytes,
|
|
|
|
|
CachedBytes: pbStat.CachedBytes,
|
|
|
|
|
CountRequests: pbStat.CountRequests,
|
|
|
|
|
CountCachedRequests: pbStat.CountCachedRequests,
|
|
|
|
|
})
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumMinutelyStat(tx, req.ServerId, queryMinuteString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
pbStat = &pb.FindLatestServerMinutelyStatsResponse_MinutelyStat{
|
|
|
|
|
Minute: minuteString,
|
|
|
|
|
Bytes: stat.Bytes / avgRatio,
|
|
|
|
|
CachedBytes: stat.CachedBytes / avgRatio,
|
2021-06-17 21:17:53 +08:00
|
|
|
CountRequests: int64(math.Ceil(float64(stat.CountRequests) / float64(avgRatio))),
|
|
|
|
|
CountCachedRequests: int64(math.Ceil(float64(stat.CountCachedRequests) / float64(avgRatio))),
|
2021-06-08 15:10:08 +08:00
|
|
|
}
|
|
|
|
|
result = append(result, pbStat)
|
|
|
|
|
cache[queryMinuteString] = pbStat
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &pb.FindLatestServerMinutelyStatsResponse{Stats: result}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindLatestServerDailyStats 按天读取统计数据
|
|
|
|
|
func (this *ServerDailyStatService) FindLatestServerDailyStats(ctx context.Context, req *pb.FindLatestServerDailyStatsRequest) (*pb.FindLatestServerDailyStatsResponse, error) {
|
2022-07-22 14:35:17 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx)
|
2021-06-08 15:10:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:05:30 +08:00
|
|
|
var tx = this.NullTx()
|
2021-06-08 15:10:08 +08:00
|
|
|
|
|
|
|
|
result := []*pb.FindLatestServerDailyStatsResponse_DailyStat{}
|
|
|
|
|
if req.Days > 0 {
|
|
|
|
|
for i := int32(0); i < req.Days; i++ {
|
|
|
|
|
dayString := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -int(i)))
|
|
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.ServerId, dayString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
result = append(result, &pb.FindLatestServerDailyStatsResponse_DailyStat{
|
|
|
|
|
Day: dayString,
|
|
|
|
|
Bytes: stat.Bytes,
|
|
|
|
|
CachedBytes: stat.CachedBytes,
|
|
|
|
|
CountRequests: stat.CountRequests,
|
|
|
|
|
CountCachedRequests: stat.CountCachedRequests,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &pb.FindLatestServerDailyStatsResponse{Stats: result}, nil
|
2021-06-08 11:18:27 +08:00
|
|
|
}
|
2022-04-15 12:14:59 +08:00
|
|
|
|
|
|
|
|
// SumCurrentServerDailyStats 查找单个服务当前统计数据
|
|
|
|
|
func (this *ServerDailyStatService) SumCurrentServerDailyStats(ctx context.Context, req *pb.SumCurrentServerDailyStatsRequest) (*pb.SumCurrentServerDailyStatsResponse, error) {
|
2022-07-22 15:05:30 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx)
|
2022-04-15 12:14:59 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 14:26:43 +08:00
|
|
|
var tx *dbs.Tx = this.NullTx()
|
|
|
|
|
|
|
|
|
|
// 检查用户
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按日
|
2022-04-15 12:14:59 +08:00
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumCurrentDailyStat(tx, req.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
Bytes: int64(stat.Bytes),
|
|
|
|
|
CachedBytes: int64(stat.CachedBytes),
|
|
|
|
|
CountRequests: int64(stat.CountRequests),
|
|
|
|
|
CountCachedRequests: int64(stat.CountCachedRequests),
|
|
|
|
|
CountAttackRequests: int64(stat.CountAttackRequests),
|
|
|
|
|
AttackBytes: int64(stat.AttackBytes),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 14:26:43 +08:00
|
|
|
return &pb.SumCurrentServerDailyStatsResponse{ServerDailyStat: pbStat}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SumServerDailyStats 计算单个服务的日统计
|
|
|
|
|
func (this *ServerDailyStatService) SumServerDailyStats(ctx context.Context, req *pb.SumServerDailyStatsRequest) (*pb.SumServerDailyStatsResponse, error) {
|
2022-07-22 15:05:30 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx)
|
2022-06-18 14:26:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = this.NullTx()
|
|
|
|
|
|
|
|
|
|
// 检查用户
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 某日统计
|
|
|
|
|
var day = timeutil.Format("Ymd")
|
|
|
|
|
if regexp.MustCompile(`^\d{8}$`).MatchString(req.Day) {
|
|
|
|
|
day = req.Day
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.ServerId, day)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
Bytes: stat.Bytes,
|
|
|
|
|
CachedBytes: stat.CachedBytes,
|
|
|
|
|
CountRequests: stat.CountRequests,
|
|
|
|
|
CountCachedRequests: stat.CountCachedRequests,
|
|
|
|
|
CountAttackRequests: stat.CountAttackRequests,
|
|
|
|
|
AttackBytes: stat.AttackBytes,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &pb.SumServerDailyStatsResponse{ServerDailyStat: pbStat}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SumServerMonthlyStats 计算单个服务的月统计
|
|
|
|
|
func (this *ServerDailyStatService) SumServerMonthlyStats(ctx context.Context, req *pb.SumServerMonthlyStatsRequest) (*pb.SumServerMonthlyStatsResponse, error) {
|
2022-07-22 15:05:30 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx)
|
2022-06-18 14:26:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = this.NullTx()
|
|
|
|
|
|
|
|
|
|
// 检查用户
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 某月统计
|
|
|
|
|
var month = timeutil.Format("Ym")
|
|
|
|
|
if regexp.MustCompile(`^\d{6}$`).MatchString(req.Month) {
|
|
|
|
|
month = req.Month
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按月
|
|
|
|
|
stat, err := models.SharedServerDailyStatDAO.SumMonthlyStat(tx, req.ServerId, month)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
}
|
|
|
|
|
if stat != nil {
|
|
|
|
|
pbStat = &pb.ServerDailyStat{
|
|
|
|
|
ServerId: req.ServerId,
|
|
|
|
|
Bytes: stat.Bytes,
|
|
|
|
|
CachedBytes: stat.CachedBytes,
|
|
|
|
|
CountRequests: stat.CountRequests,
|
|
|
|
|
CountCachedRequests: stat.CountCachedRequests,
|
|
|
|
|
CountAttackRequests: stat.CountAttackRequests,
|
|
|
|
|
AttackBytes: stat.AttackBytes,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.SumServerMonthlyStatsResponse{ServerMonthlyStat: pbStat}, nil
|
2022-04-15 12:14:59 +08:00
|
|
|
}
|