From bb42cbac42e852421d9d685ac6d83aef9fce4690 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 17 Oct 2024 16:12:34 +0200 Subject: [PATCH] TrendSummary implementation --- engine/libtrends.go | 29 +++++++++++++++++++++++++++++ engine/trends.go | 26 ++++++-------------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/engine/libtrends.go b/engine/libtrends.go index e0693bfbd..ecb98cddf 100644 --- a/engine/libtrends.go +++ b/engine/libtrends.go @@ -122,6 +122,27 @@ func (t *Trend) Clone() (tC *Trend) { return } +// AsTrendSummary transforms the trend into TrendSummary +func (t *Trend) AsTrendSummary() (ts *TrendSummary) { + ts = &TrendSummary{ + Tenant: t.Tenant, + ID: t.ID, + Metrics: make(map[string]*MetricWithTrend), + } + if len(t.RunTimes) != 0 { + ts.Time = t.RunTimes[len(t.RunTimes)-1] + for mID, mWt := range t.Metrics[ts.Time] { + ts.Metrics[mID] = &MetricWithTrend{ + ID: mWt.ID, + Value: mWt.Value, + TrendGrowth: mWt.TrendGrowth, + TrendLabel: mWt.TrendLabel, + } + } + } + return +} + func (t *Trend) compress(ms Marshaler) (err error) { if config.CgrConfig().TrendSCfg().StoreUncompressedLimit > len(t.RunTimes) { return @@ -278,3 +299,11 @@ type MetricWithTrend struct { func (tr *Trend) TenantID() string { return utils.ConcatenatedKey(tr.Tenant, tr.ID) } + +// TrendSummary represents the last trend computed +type TrendSummary struct { + Tenant string + ID string + Time time.Time + Metrics map[string]*MetricWithTrend +} diff --git a/engine/trends.go b/engine/trends.go index ca536ca73..92de9c51b 100644 --- a/engine/trends.go +++ b/engine/trends.go @@ -180,22 +180,15 @@ func (tS *TrendS) processThresholds(trnd *Trend) (err error) { copy(thIDs, trnd.tPrfl.ThresholdIDs) } opts[utils.OptsThresholdsProfileIDs] = thIDs - mtrx := make(map[string]*MetricWithTrend) - for mtID, mtWT := range trnd.Metrics[trnd.RunTimes[len(trnd.RunTimes)-1]] { - mtrx[mtID] = &MetricWithTrend{ - ID: mtWT.ID, - Value: mtWT.Value, - TrendGrowth: mtWT.TrendGrowth, - TrendLabel: mtWT.TrendLabel, - } - } + ts := trnd.AsTrendSummary() trndEv := &utils.CGREvent{ Tenant: trnd.Tenant, ID: utils.GenUUID(), APIOpts: opts, Event: map[string]any{ utils.TrendID: trnd.ID, - utils.Metrics: mtrx, + utils.Time: ts.Time, + utils.Metrics: ts.Metrics, }, } var withErrs bool @@ -225,15 +218,7 @@ func (tS *TrendS) processEEs(trnd *Trend) (err error) { opts := map[string]any{ utils.MetaEventType: utils.TrendUpdate, } - mtrx := make(map[string]*MetricWithTrend) - for mtID, mtWT := range trnd.Metrics[trnd.RunTimes[len(trnd.RunTimes)-1]] { - mtrx[mtID] = &MetricWithTrend{ - ID: mtWT.ID, - Value: mtWT.Value, - TrendGrowth: mtWT.TrendGrowth, - TrendLabel: mtWT.TrendLabel, - } - } + ts := trnd.AsTrendSummary() trndEv := &CGREventWithEeIDs{ CGREvent: &utils.CGREvent{ Tenant: trnd.Tenant, @@ -241,7 +226,8 @@ func (tS *TrendS) processEEs(trnd *Trend) (err error) { APIOpts: opts, Event: map[string]any{ utils.TrendID: trnd.ID, - utils.Metrics: mtrx, + utils.Time: ts.Time, + utils.Metrics: ts.Metrics, }, }, EeIDs: tS.cgrcfg.TrendSCfg().EEsExporterIDs,