mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
增加统计指标自动清理
This commit is contained in:
@@ -4,11 +4,30 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MetricSumStatDAO dbs.DAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
err := SharedMetricSumStatDAO.Clean(nil, 30) // 只保留30天
|
||||
if err != nil {
|
||||
logs.Println("SharedMetricSumStatDAO: clean expired data failed: " + err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func NewMetricSumStatDAO() *MetricSumStatDAO {
|
||||
return dbs.NewDAO(&MetricSumStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
@@ -32,14 +51,15 @@ func init() {
|
||||
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
||||
return this.Query(tx).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"clusterId": clusterId,
|
||||
"nodeId": nodeId,
|
||||
"serverId": serverId,
|
||||
"itemId": itemId,
|
||||
"version": version,
|
||||
"time": time,
|
||||
"count": count,
|
||||
"total": total,
|
||||
"clusterId": clusterId,
|
||||
"nodeId": nodeId,
|
||||
"serverId": serverId,
|
||||
"itemId": itemId,
|
||||
"version": version,
|
||||
"time": time,
|
||||
"count": count,
|
||||
"total": total,
|
||||
"createdDay": timeutil.Format("Ymd"),
|
||||
}, maps.Map{
|
||||
"count": count,
|
||||
"total": total,
|
||||
@@ -134,3 +154,15 @@ func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string,
|
||||
}
|
||||
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
||||
}
|
||||
|
||||
// Clean 清理数据
|
||||
func (this *MetricSumStatDAO) Clean(tx *dbs.Tx, days int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Where("(createdDay IS NULL OR createdDay<:day)").
|
||||
Param("day", timeutil.FormatTime("Ymd", time.Now().Unix()-days*86400)).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,4 +3,12 @@ package models
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMetricSumStatDAO_Clean(t *testing.T) {
|
||||
err := NewMetricSumStatDAO().Clean(nil, 20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,29 @@ package models
|
||||
|
||||
// MetricSumStat 指标统计总和数据
|
||||
type MetricSumStat struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
NodeId uint32 `field:"nodeId"` // 节点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"` // 版本号
|
||||
Id uint64 `field:"id"` // ID
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
NodeId uint32 `field:"nodeId"` // 节点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"` // 版本号
|
||||
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||
}
|
||||
|
||||
type MetricSumStatOperator struct {
|
||||
Id interface{} // ID
|
||||
ClusterId interface{} // 集群ID
|
||||
NodeId interface{} // 节点ID
|
||||
ServerId interface{} // 服务ID
|
||||
ItemId interface{} // 指标
|
||||
Count interface{} // 数量
|
||||
Total interface{} // 总和
|
||||
Time interface{} // 分钟值YYYYMMDDHHII
|
||||
Version interface{} // 版本号
|
||||
Id interface{} // ID
|
||||
ClusterId interface{} // 集群ID
|
||||
NodeId interface{} // 节点ID
|
||||
ServerId interface{} // 服务ID
|
||||
ItemId interface{} // 指标
|
||||
Count interface{} // 数量
|
||||
Total interface{} // 总和
|
||||
Time interface{} // 分钟值YYYYMMDDHHII
|
||||
Version interface{} // 版本号
|
||||
CreatedDay interface{} // 创建日期YYYYMMDD
|
||||
}
|
||||
|
||||
func NewMetricSumStatOperator() *MetricSumStatOperator {
|
||||
|
||||
@@ -100,7 +100,7 @@ func TestServerDAO_CheckPortIsUsing(t *testing.T) {
|
||||
// t.Log("isUsing:", isUsing)
|
||||
//}
|
||||
{
|
||||
isUsing, err := SharedServerDAO.CheckTCPPortIsUsing(tx, 18, 3306, 0, "tcp")
|
||||
isUsing, err := SharedServerDAO.CheckPortIsUsing(tx, 18, "tcp", 3306, 0, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user