统计看板中合并相同Key的指标数据

This commit is contained in:
刘祥超
2022-04-24 15:24:40 +08:00
parent ee2781fe65
commit b212e124c2

View File

@@ -202,6 +202,8 @@ func (this *MetricStatDAO) ListItemStats(tx *dbs.Tx, itemId int64, version int32
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
if result[i].Time > result[j].Time {
return true
@@ -268,6 +270,8 @@ func (this *MetricStatDAO) FindItemStatsAtLastTime(tx *dbs.Tx, itemId int64, ign
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Value > result[j].Value
})
@@ -331,6 +335,8 @@ func (this *MetricStatDAO) FindItemStatsWithClusterIdAndLastTime(tx *dbs.Tx, clu
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Value > result[j].Value
})
@@ -396,6 +402,8 @@ func (this *MetricStatDAO) FindItemStatsWithNodeIdAndLastTime(tx *dbs.Tx, nodeId
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Value > result[j].Value
})
@@ -492,6 +500,8 @@ func (this *MetricStatDAO) FindLatestItemStats(tx *dbs.Tx, itemId int64, ignoreE
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Time > result[j].Time
})
@@ -551,6 +561,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithClusterId(tx *dbs.Tx, clusterI
return nil, err
}
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Time > result[j].Time
})
@@ -605,6 +617,8 @@ func (this *MetricStatDAO) FindLatestItemStatsWithNodeId(tx *dbs.Tx, nodeId int6
return nil
})
result = this.mergeStats(result)
sort.Slice(result, func(i, j int) bool {
return result[i].Time > result[j].Time
})
@@ -721,3 +735,19 @@ func (this *MetricStatDAO) runBatch(f func(table string, locker *sync.Mutex) err
wg.Wait()
return resultErr
}
// 合并统计数据
func (this *MetricStatDAO) mergeStats(stats []*MetricStat) (result []*MetricStat) {
var m = map[string]*MetricStat{} // key+time => *MetricStat
for _, stat := range stats {
var uniqueKey = stat.Time + "@" + string(stat.Keys)
oldStat, ok := m[uniqueKey]
if !ok {
result = append(result, stat)
m[uniqueKey] = stat
} else {
oldStat.Value += stat.Value
}
}
return result
}