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"
|
2020-12-11 17:28:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 服务统计相关服务
|
|
|
|
|
type ServerDailyStatService struct {
|
|
|
|
|
BaseService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上传统计
|
|
|
|
|
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()
|
|
|
|
|
}
|