mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	指标图表可以设置忽略空值和其他对象值
This commit is contained in:
		@@ -74,7 +74,7 @@ func (this *MetricChartDAO) FindMetricChartName(tx *dbs.Tx, chartId int64) (stri
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateChart 创建图表
 | 
			
		||||
func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map) (int64, error) {
 | 
			
		||||
func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, ignoreEmptyKeys bool, ignoredKeys []string) (int64, error) {
 | 
			
		||||
	op := NewMetricChartOperator()
 | 
			
		||||
	op.ItemId = itemId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
@@ -90,13 +90,25 @@ func (this *MetricChartDAO) CreateChart(tx *dbs.Tx, itemId int64, name string, c
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	op.Params = paramsJSON
 | 
			
		||||
	op.IgnoreEmptyKeys = ignoreEmptyKeys
 | 
			
		||||
 | 
			
		||||
	if len(ignoredKeys) == 0 {
 | 
			
		||||
		op.IgnoredKeys = "[]"
 | 
			
		||||
	} else {
 | 
			
		||||
		ignoredKeysJSON, err := json.Marshal(ignoredKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return 0, err
 | 
			
		||||
		}
 | 
			
		||||
		op.IgnoredKeys = ignoredKeysJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = MetricChartStateEnabled
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateChart 修改图表
 | 
			
		||||
func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, isOn bool) error {
 | 
			
		||||
func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string, chartType string, widthDiv int32, maxItems int32, params maps.Map, ignoreEmptyKeys bool, ignoredKeys []string, isOn bool) error {
 | 
			
		||||
	if chartId <= 0 {
 | 
			
		||||
		return errors.New("invalid chartId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -115,6 +127,17 @@ func (this *MetricChartDAO) UpdateChart(tx *dbs.Tx, chartId int64, name string,
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	op.Params = paramsJSON
 | 
			
		||||
	op.IgnoreEmptyKeys = ignoreEmptyKeys
 | 
			
		||||
 | 
			
		||||
	if len(ignoredKeys) == 0 {
 | 
			
		||||
		op.IgnoredKeys = "[]"
 | 
			
		||||
	} else {
 | 
			
		||||
		ignoredKeysJSON, err := json.Marshal(ignoredKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		op.IgnoredKeys = ignoredKeysJSON
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,8 @@ type MetricChart struct {
 | 
			
		||||
	IsOn            uint8  `field:"isOn"`            // 是否启用
 | 
			
		||||
	State           uint8  `field:"state"`           // 状态
 | 
			
		||||
	MaxItems        uint32 `field:"maxItems"`        // 最多条目
 | 
			
		||||
	IgnoreEmptyKeys uint8  `field:"ignoreEmptyKeys"` // 忽略空的键值
 | 
			
		||||
	IgnoredKeys     string `field:"ignoredKeys"`     // 忽略键值
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type MetricChartOperator struct {
 | 
			
		||||
@@ -27,6 +29,8 @@ type MetricChartOperator struct {
 | 
			
		||||
	IsOn            interface{} // 是否启用
 | 
			
		||||
	State           interface{} // 状态
 | 
			
		||||
	MaxItems        interface{} // 最多条目
 | 
			
		||||
	IgnoreEmptyKeys interface{} // 忽略空的键值
 | 
			
		||||
	IgnoredKeys     interface{} // 忽略键值
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewMetricChartOperator() *MetricChartOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,17 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import "encoding/json"
 | 
			
		||||
 | 
			
		||||
func (this *MetricChart) DecodeIgnoredKeys() []string {
 | 
			
		||||
	if len(this.IgnoredKeys) == 0 {
 | 
			
		||||
		return []string{}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var result = []string{}
 | 
			
		||||
	err := json.Unmarshal([]byte(this.IgnoredKeys), &result)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// 这里忽略错误
 | 
			
		||||
		return result
 | 
			
		||||
	}
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -86,6 +86,7 @@ func (this *MetricStatDAO) DeleteOldItemStats(tx *dbs.Tx, itemId int64, version
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Where("version<:version").
 | 
			
		||||
		Param("version", version).
 | 
			
		||||
		Delete()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@@ -123,7 +124,7 @@ func (this *MetricStatDAO) ListItemStats(tx *dbs.Tx, itemId int64, version int32
 | 
			
		||||
 | 
			
		||||
// FindItemStatsAtLastTime 取得所有集群最近一次计时前 N 个数据
 | 
			
		||||
// 适合每条数据中包含不同的Key的场景
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	// 最近一次时间
 | 
			
		||||
	statOne, err := this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
@@ -138,8 +139,7 @@ func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ver
 | 
			
		||||
	}
 | 
			
		||||
	var lastStat = statOne.(*MetricStat)
 | 
			
		||||
	var lastTime = lastStat.Time
 | 
			
		||||
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
		Attr("time", lastTime).
 | 
			
		||||
@@ -149,14 +149,26 @@ func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ver
 | 
			
		||||
		Desc("value").
 | 
			
		||||
		Group("keys").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindItemStatsWithClusterIdAndLastTime 取得集群最近一次计时前 N 个数据
 | 
			
		||||
// 适合每条数据中包含不同的Key的场景
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clusterId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clusterId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	// 最近一次时间
 | 
			
		||||
	statOne, err := this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
@@ -172,7 +184,7 @@ func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clu
 | 
			
		||||
	var lastStat = statOne.(*MetricStat)
 | 
			
		||||
	var lastTime = lastStat.Time
 | 
			
		||||
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("clusterId", clusterId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -183,14 +195,27 @@ func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clu
 | 
			
		||||
		Desc("value").
 | 
			
		||||
		Group("keys").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindItemStatsWithNodeIdAndLastTime 取得节点最近一次计时前 N 个数据
 | 
			
		||||
// 适合每条数据中包含不同的Key的场景
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	// 最近一次时间
 | 
			
		||||
	statOne, err := this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
@@ -205,8 +230,7 @@ func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId
 | 
			
		||||
	}
 | 
			
		||||
	var lastStat = statOne.(*MetricStat)
 | 
			
		||||
	var lastTime = lastStat.Time
 | 
			
		||||
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("nodeId", nodeId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -217,14 +241,27 @@ func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId
 | 
			
		||||
		Desc("value").
 | 
			
		||||
		Group("keys").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindItemStatsWithServerIdAndLastTime 取得节点最近一次计时前 N 个数据
 | 
			
		||||
// 适合每条数据中包含不同的Key的场景
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serverId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serverId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	// 最近一次时间
 | 
			
		||||
	statOne, err := this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
@@ -240,7 +277,7 @@ func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serv
 | 
			
		||||
	var lastStat = statOne.(*MetricStat)
 | 
			
		||||
	var lastTime = lastStat.Time
 | 
			
		||||
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("serverId", serverId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -251,15 +288,28 @@ func (this *MetricStatDAO) FindItemStatsWithServerIdAndLastTime(tx *dbs.Tx, serv
 | 
			
		||||
		Desc("value").
 | 
			
		||||
		Group("keys").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindLatestItemStats 取得所有集群上最近 N 个时间的数据
 | 
			
		||||
// 适合同个Key在不同时间段的变化场景
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
		// TODO 增加更多聚合算法,比如 AVG、MEDIAN、MIN、MAX 等
 | 
			
		||||
@@ -268,7 +318,20 @@ func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version
 | 
			
		||||
		Desc("time").
 | 
			
		||||
		Group("time").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -279,8 +342,8 @@ func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, version
 | 
			
		||||
 | 
			
		||||
// FindLatestItemStatsWithClusterId 取得集群最近 N 个时间的数据
 | 
			
		||||
// 适合同个Key在不同时间段的变化场景
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("clusterId", clusterId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -290,7 +353,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterI
 | 
			
		||||
		Desc("time").
 | 
			
		||||
		Group("time").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -301,8 +377,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterI
 | 
			
		||||
 | 
			
		||||
// FindLatestItemStatsWithNodeId 取得节点最近 N 个时间的数据
 | 
			
		||||
// 适合同个Key在不同时间段的变化场景
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("nodeId", nodeId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -312,7 +388,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int6
 | 
			
		||||
		Desc("time").
 | 
			
		||||
		Group("time").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -323,8 +412,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int6
 | 
			
		||||
 | 
			
		||||
// FindLatestItemStatsWithServerId 取得服务最近 N 个时间的数据
 | 
			
		||||
// 适合同个Key在不同时间段的变化场景
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId int64, itemId int64, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId int64, itemId int64, ignoreEmptyKeys bool, ignoreKeys []string, version int32, size int64) (result []*MetricStat, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		Attr("serverId", serverId).
 | 
			
		||||
		Attr("itemId", itemId).
 | 
			
		||||
		Attr("version", version).
 | 
			
		||||
@@ -334,7 +423,20 @@ func (this *MetricStatDAO) FindLatestItemStatsWithServerId(tx *dbs.Tx, serverId
 | 
			
		||||
		Desc("time").
 | 
			
		||||
		Group("time").
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		Slice(&result)
 | 
			
		||||
	if ignoreEmptyKeys {
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(`keys`, '\"\"')")
 | 
			
		||||
	}
 | 
			
		||||
	if len(ignoreKeys) > 0 {
 | 
			
		||||
		ignoreKeysJSON, err := json.Marshal(ignoreKeys)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		query.Where("NOT JSON_CONTAINS(:ignoredKeys, JSON_EXTRACT(`keys`, '$[0]'))") // TODO $[0] 需要换成keys中的primary key位置
 | 
			
		||||
		query.Param("ignoredKeys", string(ignoreKeysJSON))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
@@ -751,7 +751,7 @@ func (this *AdminService) findMetricDataCharts(tx *dbs.Tx) (result []*pb.MetricD
 | 
			
		||||
			var pbStats = []*pb.MetricStat{}
 | 
			
		||||
			switch chart.Type {
 | 
			
		||||
			case serverconfigs.MetricChartTypeTimeLine:
 | 
			
		||||
				itemStats, err := models.SharedMetricStatDAO.FindLatestItemStats(tx, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
				itemStats, err := models.SharedMetricStatDAO.FindLatestItemStats(tx, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
				}
 | 
			
		||||
@@ -780,7 +780,7 @@ func (this *AdminService) findMetricDataCharts(tx *dbs.Tx) (result []*pb.MetricD
 | 
			
		||||
					})
 | 
			
		||||
				}
 | 
			
		||||
			default:
 | 
			
		||||
				itemStats, err := models.SharedMetricStatDAO.FindItemStatsAtLastTime(tx, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
				itemStats, err := models.SharedMetricStatDAO.FindItemStatsAtLastTime(tx, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
				}
 | 
			
		||||
 
 | 
			
		||||
@@ -31,7 +31,7 @@ func (this *MetricChartService) CreateMetricChart(ctx context.Context, req *pb.C
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	chartId, err := models.SharedMetricChartDAO.CreateChart(tx, req.MetricItemId, req.Name, req.Type, req.WidthDiv, req.MaxItems, params)
 | 
			
		||||
	chartId, err := models.SharedMetricChartDAO.CreateChart(tx, req.MetricItemId, req.Name, req.Type, req.WidthDiv, req.MaxItems, params, req.IgnoreEmptyKeys, req.IgnoredKeys)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -53,7 +53,7 @@ func (this *MetricChartService) UpdateMetricChart(ctx context.Context, req *pb.U
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	err = models.SharedMetricChartDAO.UpdateChart(tx, req.MetricChartId, req.Name, req.Type, req.WidthDiv, req.MaxItems, params, req.IsOn)
 | 
			
		||||
	err = models.SharedMetricChartDAO.UpdateChart(tx, req.MetricChartId, req.Name, req.Type, req.WidthDiv, req.MaxItems, params, req.IgnoreEmptyKeys, req.IgnoredKeys, req.IsOn)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -83,6 +83,8 @@ func (this *MetricChartService) FindEnabledMetricChart(ctx context.Context, req
 | 
			
		||||
			WidthDiv:        types.Int32(chart.WidthDiv),
 | 
			
		||||
			MaxItems:        types.Int32(chart.MaxItems),
 | 
			
		||||
			ParamsJSON:      []byte(chart.Params),
 | 
			
		||||
			IgnoreEmptyKeys: chart.IgnoreEmptyKeys == 1,
 | 
			
		||||
			IgnoredKeys:     chart.DecodeIgnoredKeys(),
 | 
			
		||||
			IsOn:            chart.IsOn == 1,
 | 
			
		||||
			MetricItem:      &pb.MetricItem{Id: int64(chart.ItemId)},
 | 
			
		||||
		},
 | 
			
		||||
@@ -125,6 +127,8 @@ func (this *MetricChartService) ListEnabledMetricCharts(ctx context.Context, req
 | 
			
		||||
			WidthDiv:        types.Int32(chart.WidthDiv),
 | 
			
		||||
			MaxItems:        types.Int32(chart.MaxItems),
 | 
			
		||||
			ParamsJSON:      []byte(chart.Params),
 | 
			
		||||
			IgnoreEmptyKeys: chart.IgnoreEmptyKeys == 1,
 | 
			
		||||
			IgnoredKeys:     chart.DecodeIgnoredKeys(),
 | 
			
		||||
			IsOn:            chart.IsOn == 1,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -605,11 +605,11 @@ func (this *ServerStatBoardService) findNodeClusterMetricDataCharts(tx *dbs.Tx,
 | 
			
		||||
			case serverconfigs.MetricChartTypeTimeLine:
 | 
			
		||||
				var itemStats []*models.MetricStat
 | 
			
		||||
				if serverId > 0 {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithServerId(tx, serverId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithServerId(tx, serverId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				} else if nodeId > 0 {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithNodeId(tx, nodeId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithNodeId(tx, nodeId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				} else {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithClusterId(tx, clusterId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindLatestItemStatsWithClusterId(tx, clusterId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				}
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
@@ -649,11 +649,11 @@ func (this *ServerStatBoardService) findNodeClusterMetricDataCharts(tx *dbs.Tx,
 | 
			
		||||
			default:
 | 
			
		||||
				var itemStats []*models.MetricStat
 | 
			
		||||
				if serverId > 0 {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithServerIdAndLastTime(tx, serverId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithServerIdAndLastTime(tx, serverId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				} else if nodeId > 0 {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithNodeIdAndLastTime(tx, nodeId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithNodeIdAndLastTime(tx, nodeId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				} else {
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithClusterIdAndLastTime(tx, clusterId, itemId, types.Int32(item.Version), 10)
 | 
			
		||||
					itemStats, err = models.SharedMetricStatDAO.FindItemStatsWithClusterIdAndLastTime(tx, clusterId, itemId, chart.IgnoreEmptyKeys == 1, chart.DecodeIgnoredKeys(), types.Int32(item.Version), 10)
 | 
			
		||||
				}
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user