From b212e124c284c53865962364a4fd7ef6cf4681cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 24 Apr 2022 15:24:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E7=9C=8B=E6=9D=BF=E4=B8=AD?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E7=9B=B8=E5=90=8CKey=E7=9A=84=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/metric_stat_dao.go | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/db/models/metric_stat_dao.go b/internal/db/models/metric_stat_dao.go index b023bcdf..4f86fecb 100644 --- a/internal/db/models/metric_stat_dao.go +++ b/internal/db/models/metric_stat_dao.go @@ -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 +}