2021-07-01 10:39:42 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2021-12-03 10:57:40 +08:00
|
|
|
"github.com/iwind/TeaGo/logs"
|
2021-07-01 10:39:42 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-12-03 10:57:40 +08:00
|
|
|
"github.com/iwind/TeaGo/rands"
|
|
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
|
|
|
"time"
|
2021-07-01 10:39:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MetricSumStatDAO dbs.DAO
|
|
|
|
|
|
2021-12-03 10:57:40 +08:00
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 10:39:42 +08:00
|
|
|
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 更新统计数据
|
2021-07-05 16:37:53 +08:00
|
|
|
func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error {
|
2021-07-01 10:39:42 +08:00
|
|
|
return this.Query(tx).
|
|
|
|
|
InsertOrUpdateQuickly(maps.Map{
|
2021-12-03 10:57:40 +08:00
|
|
|
"clusterId": clusterId,
|
|
|
|
|
"nodeId": nodeId,
|
|
|
|
|
"serverId": serverId,
|
|
|
|
|
"itemId": itemId,
|
|
|
|
|
"version": version,
|
|
|
|
|
"time": time,
|
|
|
|
|
"count": count,
|
|
|
|
|
"total": total,
|
|
|
|
|
"createdDay": timeutil.Format("Ymd"),
|
2021-07-01 10:39:42 +08:00
|
|
|
}, maps.Map{
|
|
|
|
|
"count": count,
|
|
|
|
|
"total": total,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 19:55:37 +08:00
|
|
|
// FindNodeServerSum 查找某个服务在某个节点上的统计数据
|
|
|
|
|
func (this *MetricSumStatDAO) FindNodeServerSum(tx *dbs.Tx, nodeId int64, serverId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
2021-07-01 10:39:42 +08:00
|
|
|
one, err := this.Query(tx).
|
2021-07-05 16:37:53 +08:00
|
|
|
Attr("nodeId", nodeId).
|
2021-07-01 10:39:42 +08:00
|
|
|
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
|
|
|
|
|
}
|
2021-07-05 16:37:53 +08:00
|
|
|
|
2021-07-19 17:58:16 +08:00
|
|
|
// FindSumAtTime 查找某个时间的统计数据
|
|
|
|
|
func (this *MetricSumStatDAO) FindSumAtTime(tx *dbs.Tx, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
|
|
|
|
one, err := this.Query(tx).
|
|
|
|
|
Attr("time", time).
|
|
|
|
|
Attr("itemId", itemId).
|
|
|
|
|
Attr("version", version).
|
|
|
|
|
Result("SUM(count) AS `count`, SUM(total) AS total").
|
|
|
|
|
Find()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
if one == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 19:55:37 +08:00
|
|
|
// FindServerSum 查找某个服务的统计数据
|
|
|
|
|
func (this *MetricSumStatDAO) FindServerSum(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).
|
|
|
|
|
Result("SUM(count) AS `count`, SUM(total) AS total").
|
|
|
|
|
Find()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
if one == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 16:37:53 +08:00
|
|
|
// FindClusterSum 查找集群上的统计数据
|
|
|
|
|
func (this *MetricSumStatDAO) FindClusterSum(tx *dbs.Tx, clusterId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
|
|
|
|
one, err := this.Query(tx).
|
|
|
|
|
Attr("clusterId", clusterId).
|
|
|
|
|
Attr("time", time).
|
|
|
|
|
Attr("itemId", itemId).
|
|
|
|
|
Attr("version", version).
|
|
|
|
|
Result("SUM(count) AS `count`, SUM(total) AS total").
|
|
|
|
|
Find()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
if one == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
|
|
|
|
}
|
2021-07-06 20:06:34 +08:00
|
|
|
|
|
|
|
|
// FindNodeSum 查找节点上的统计数据
|
|
|
|
|
func (this *MetricSumStatDAO) FindNodeSum(tx *dbs.Tx, nodeId int64, time string, itemId int64, version int32) (count int64, total float32, err error) {
|
|
|
|
|
one, err := this.Query(tx).
|
|
|
|
|
Attr("nodeId", nodeId).
|
|
|
|
|
Attr("time", time).
|
|
|
|
|
Attr("itemId", itemId).
|
|
|
|
|
Attr("version", version).
|
|
|
|
|
Result("SUM(count) AS `count`, SUM(total) AS total").
|
|
|
|
|
Find()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
if one == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return int64(one.(*MetricSumStat).Count), float32(one.(*MetricSumStat).Total), nil
|
|
|
|
|
}
|
2021-12-03 10:57:40 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|