From 07da68b0bb9a2e43bb0b10a31d3387d17eae064f Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 9 May 2025 19:03:22 +0300 Subject: [PATCH] prevent redundant cost calculations in V1CostForEvent Previously the function continued looping through all verbosity iterations (default 1000) even after finding a valid cost, causing unnecessary processing. --- rates/apis.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rates/apis.go b/rates/apis.go index 15f8fb98a..35e97be91 100644 --- a/rates/apis.go +++ b/rates/apis.go @@ -128,9 +128,10 @@ func (rS *RateS) V1RateProfileRatesForEvent(ctx *context.Context, args *utils.CG return } -// V1CostForEvent will be called to calculate the cost for an event -// -// it will loop through all matching profiles for the event in the eventuality the ones with higher priority are erroring +// V1CostForEvent calculates the cost for an event using matching rate +// profiles. If a higher priority profile fails, it tries the next matching +// profile. This continues until a valid cost is found or all profiles are +// exhausted. func (rS *RateS) V1CostForEvent(ctx *context.Context, args *utils.CGREvent, rpCost *utils.RateProfileCost) (err error) { var rPfIDs []string if rPfIDs, err = engine.GetStringSliceOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.RateSCfg().Opts.ProfileIDs, @@ -144,7 +145,7 @@ func (rS *RateS) V1CostForEvent(ctx *context.Context, args *utils.CGREvent, rpCo } ignoredRPfIDs := utils.NewStringSet([]string{}) var firstError error - for i := 0; i < rS.cfg.RateSCfg().Verbosity; i++ { + for i := range rS.cfg.RateSCfg().Verbosity { var rtPrl *utils.RateProfile if rtPrl, err = rS.matchingRateProfileForEvent(ctx, args.Tenant, rPfIDs, args, ignFilters, ignoredRPfIDs); err != nil { if err != utils.ErrNotFound { @@ -167,6 +168,7 @@ func (rS *RateS) V1CostForEvent(ctx *context.Context, args *utils.CGREvent, rpCo return } *rpCost = *rcvCost + return } return }