mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-14 08:20:25 +08:00
指标数据增加总和数据
This commit is contained in:
@@ -228,7 +228,7 @@ func (this *MetricItemDAO) ComposeItemConfig(tx *dbs.Tx, itemId int64) (*serverc
|
||||
Category: item.Category,
|
||||
Value: item.Value,
|
||||
Keys: item.DecodeKeys(),
|
||||
Version: types.Int(item.Version),
|
||||
Version: types.Int32(item.Version),
|
||||
}
|
||||
|
||||
return config, nil
|
||||
|
||||
@@ -31,7 +31,7 @@ func init() {
|
||||
}
|
||||
|
||||
// CreateStat 创建统计数据
|
||||
func (this *MetricStatDAO) CreateStat(tx *dbs.Tx, hash string, clusterId int64, nodeId int64, serverId int64, itemId int64, keys []string, value float64, time string, version int) error {
|
||||
func (this *MetricStatDAO) CreateStat(tx *dbs.Tx, hash string, clusterId int64, nodeId int64, serverId int64, itemId int64, keys []string, value float64, time string, version int32) error {
|
||||
hash += "@" + strconv.FormatInt(nodeId, 10)
|
||||
var keysString string
|
||||
if len(keys) > 0 {
|
||||
@@ -92,7 +92,9 @@ func (this *MetricStatDAO) ListItemStats(tx *dbs.Tx, itemId int64, version int32
|
||||
Attr("version", version).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
DescPk().
|
||||
Desc("time").
|
||||
Desc("serverId").
|
||||
Desc("value").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
|
||||
62
internal/db/models/metric_sum_stat_dao.go
Normal file
62
internal/db/models/metric_sum_stat_dao.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type MetricSumStatDAO dbs.DAO
|
||||
|
||||
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
||||
return dbs.NewDAO(&MetricSumStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeMetricSumStats",
|
||||
Model: new(MetricSumStat),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*MetricSumStatDAO)
|
||||
}
|
||||
|
||||
var SharedMetricSumStatDAO *MetricSumStatDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedMetricSumStatDAO = NewMetricSumStatDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateSum 更新统计数据
|
||||
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
||||
return this.Query(tx).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"serverId": serverId,
|
||||
"itemId": itemId,
|
||||
"version": version,
|
||||
"time": time,
|
||||
"count": count,
|
||||
"total": total,
|
||||
}, maps.Map{
|
||||
"count": count,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
// FindSum 查找统计数据
|
||||
func (this *MetricSumStatDAO) FindSum(tx *dbs.Tx, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("serverId", serverId).
|
||||
Attr("time", time).
|
||||
Attr("itemId", itemId).
|
||||
Attr("version", version).
|
||||
Find()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if one == nil {
|
||||
return
|
||||
}
|
||||
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
||||
}
|
||||
6
internal/db/models/metric_sum_stat_dao_test.go
Normal file
6
internal/db/models/metric_sum_stat_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
26
internal/db/models/metric_sum_stat_model.go
Normal file
26
internal/db/models/metric_sum_stat_model.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
// MetricSumStat 指标统计总和数据
|
||||
type MetricSumStat struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
ServerId uint32 `field:"serverId"` // 服务ID
|
||||
ItemId uint64 `field:"itemId"` // 指标
|
||||
Count uint64 `field:"count"` // 数量
|
||||
Total float64 `field:"total"` // 总和
|
||||
Time string `field:"time"` // 分钟值YYYYMMDDHHII
|
||||
Version uint32 `field:"version"` // 版本号
|
||||
}
|
||||
|
||||
type MetricSumStatOperator struct {
|
||||
Id interface{} // ID
|
||||
ServerId interface{} // 服务ID
|
||||
ItemId interface{} // 指标
|
||||
Count interface{} // 数量
|
||||
Total interface{} // 总和
|
||||
Time interface{} // 分钟值YYYYMMDDHHII
|
||||
Version interface{} // 版本号
|
||||
}
|
||||
|
||||
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
||||
return &MetricSumStatOperator{}
|
||||
}
|
||||
1
internal/db/models/metric_sum_stat_model_ext.go
Normal file
1
internal/db/models/metric_sum_stat_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
@@ -28,12 +28,18 @@ func (this *MetricStatService) UploadMetricStats(ctx context.Context, req *pb.Up
|
||||
}
|
||||
|
||||
for _, stat := range req.MetricStats {
|
||||
err := models.SharedMetricStatDAO.CreateStat(tx, stat.Hash, clusterId, nodeId, stat.ServerId, stat.ItemId, stat.Keys, float64(stat.Value), stat.Time, int(stat.Version))
|
||||
err := models.SharedMetricStatDAO.CreateStat(tx, stat.Hash, clusterId, nodeId, req.ServerId, req.ItemId, stat.Keys, float64(stat.Value), req.Time, req.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 保存总和
|
||||
err = models.SharedMetricSumStatDAO.UpdateSum(tx, req.ServerId, req.Time, req.ItemId, req.Version, req.Count, req.Total)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
@@ -90,6 +96,12 @@ func (this *MetricStatService) ListMetricStats(ctx context.Context, req *pb.List
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 查找sum值
|
||||
count, total, err := models.SharedMetricSumStatDAO.FindSum(tx, int64(stat.ServerId), stat.Time, int64(stat.ItemId), types.Int32(stat.Version))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbStats = append(pbStats, &pb.MetricStat{
|
||||
Id: int64(stat.Id),
|
||||
Hash: stat.Hash,
|
||||
@@ -102,6 +114,8 @@ func (this *MetricStatService) ListMetricStats(ctx context.Context, req *pb.List
|
||||
NodeCluster: &pb.NodeCluster{Id: int64(stat.ClusterId), Name: clusterName},
|
||||
Node: &pb.Node{Id: int64(stat.NodeId), Name: nodeName},
|
||||
Server: &pb.Server{Id: int64(stat.ServerId), Name: serverName},
|
||||
SumCount: count,
|
||||
SumTotal: total,
|
||||
})
|
||||
}
|
||||
return &pb.ListMetricStatsResponse{MetricStats: pbStats}, nil
|
||||
|
||||
Reference in New Issue
Block a user