diff --git a/config/routescfg.go b/config/routescfg.go index f754764a7..6a8940fda 100644 --- a/config/routescfg.go +++ b/config/routescfg.go @@ -23,15 +23,18 @@ import ( "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/utils" + "github.com/ericlagergren/decimal" ) -var RoutesProfileCountDftOpt = utils.IntPointer(1) +var ( + RoutesProfileCountDftOpt = utils.IntPointer(1) + RoutesUsageDftOpt = decimal.New(int64(time.Minute), 0) +) const ( RoutesContextDftOpt = "*routes" RoutesIgnoreErrorsDftOpt = false RoutesMaxCostDftOpt = utils.EmptyString - RoutesUsageDftOpt = time.Minute ) type RoutesOpts struct { diff --git a/engine/route_cost_sort.go b/engine/route_cost_sort.go index 661e9d523..45c85f3b2 100644 --- a/engine/route_cost_sort.go +++ b/engine/route_cost_sort.go @@ -22,17 +22,24 @@ import ( "fmt" "github.com/cgrates/birpc/context" + "github.com/ericlagergren/decimal" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) -func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig, - connMgr *ConnManager, routes map[string]*RouteWithWeight, - ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes []*SortedRoute, err error) { +func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig, connMgr *ConnManager, + fltrS *FilterS, routes map[string]*RouteWithWeight, ev *utils.CGREvent, + extraOpts *optsGetRoutes) (sortedRoutes []*SortedRoute, err error) { if len(cfg.RouteSCfg().RateSConns) == 0 { return nil, utils.NewErrMandatoryIeMissing("connIDs") } + var usage *decimal.Big + if usage, err = GetDecimalBigOpts(ctx, ev.Tenant, ev, fltrS, cfg.RouteSCfg().Opts.Usage, + config.RoutesUsageDftOpt, utils.OptsRoutesUsage, utils.MetaUsage); err != nil { + return + } + ev.APIOpts[utils.MetaUsage] = usage sortedRoutes = make([]*SortedRoute, 0, len(routes)) for _, route := range routes { if len(route.RateProfileIDs) == 0 && len(route.AccountIDs) == 0 { @@ -121,20 +128,21 @@ func populateCostForRoutes(ctx *context.Context, cfg *config.CGRConfig, return } -func NewHighestCostSorter(cfg *config.CGRConfig, connMgr *ConnManager) *HightCostSorter { - return &HightCostSorter{cfg: cfg, connMgr: connMgr} +func NewHighestCostSorter(cfg *config.CGRConfig, connMgr *ConnManager, fltrS *FilterS) *HightCostSorter { + return &HightCostSorter{cfg: cfg, connMgr: connMgr, fltrS: fltrS} } // HightCostSorter sorts routes based on their cost type HightCostSorter struct { cfg *config.CGRConfig connMgr *ConnManager + fltrS *FilterS } func (hcs *HightCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*RouteWithWeight, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { var sRoutes []*SortedRoute - if sRoutes, err = populateCostForRoutes(ctx, hcs.cfg, hcs.connMgr, routes, ev, extraOpts); err != nil { + if sRoutes, err = populateCostForRoutes(ctx, hcs.cfg, hcs.connMgr, hcs.fltrS, routes, ev, extraOpts); err != nil { return } @@ -147,20 +155,21 @@ func (hcs *HightCostSorter) SortRoutes(ctx *context.Context, prflID string, rout return } -func NewLeastCostSorter(cfg *config.CGRConfig, connMgr *ConnManager) *LeastCostSorter { - return &LeastCostSorter{cfg: cfg, connMgr: connMgr} +func NewLeastCostSorter(cfg *config.CGRConfig, connMgr *ConnManager, fltrS *FilterS) *LeastCostSorter { + return &LeastCostSorter{cfg: cfg, connMgr: connMgr, fltrS: fltrS} } // LeastCostSorter sorts routes based on their cost type LeastCostSorter struct { cfg *config.CGRConfig connMgr *ConnManager + fltrS *FilterS } func (lcs *LeastCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*RouteWithWeight, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { var sRoutes []*SortedRoute - if sRoutes, err = populateCostForRoutes(ctx, lcs.cfg, lcs.connMgr, routes, ev, extraOpts); err != nil { + if sRoutes, err = populateCostForRoutes(ctx, lcs.cfg, lcs.connMgr, lcs.fltrS, routes, ev, extraOpts); err != nil { return } sortedRoutes = &SortedRoutes{ diff --git a/engine/route_cost_sort_test.go b/engine/route_cost_sort_test.go index 52d898312..a88fb8cd2 100644 --- a/engine/route_cost_sort_test.go +++ b/engine/route_cost_sort_test.go @@ -29,6 +29,7 @@ import ( func TestPopulateCostForRoutesConnRefused(t *testing.T) { cfg := config.NewDefaultCGRConfig() connMgr := NewConnManager(cfg) + fltrS := NewFilterS(cfg, connMgr, nil) routes := map[string]*RouteWithWeight{ "RW": { Route: &Route{ @@ -49,7 +50,7 @@ func TestPopulateCostForRoutesConnRefused(t *testing.T) { } extraOpts := &optsGetRoutes{} cfg.RouteSCfg().RateSConns = []string{"*localhost"} - _, err := populateCostForRoutes(context.Background(), cfg, connMgr, routes, ev, extraOpts) + _, err := populateCostForRoutes(context.Background(), cfg, connMgr, fltrS, routes, ev, extraOpts) errExpect := "RATES_ERROR:dial tcp 127.0.0.1:2012: connect: connection refused" if err.Error() != errExpect { t.Errorf("Expected %v\n but received %v", errExpect, err) diff --git a/engine/routes.go b/engine/routes.go index 688dfc210..f3bf9c4ac 100644 --- a/engine/routes.go +++ b/engine/routes.go @@ -111,8 +111,8 @@ func NewRouteService(dm *DataManager, connMgr: connMgr, sorter: RouteSortDispatcher{ utils.MetaWeight: NewWeightSorter(cfg), - utils.MetaLC: NewLeastCostSorter(cfg, connMgr), - utils.MetaHC: NewHighestCostSorter(cfg, connMgr), + utils.MetaLC: NewLeastCostSorter(cfg, connMgr, filterS), + utils.MetaHC: NewHighestCostSorter(cfg, connMgr, filterS), utils.MetaQOS: NewQOSRouteSorter(cfg, connMgr), utils.MetaReas: NewResourceAscendetSorter(cfg, connMgr), utils.MetaReds: NewResourceDescendentSorter(cfg, connMgr),