diff --git a/apier/v1/suppliers_it_test.go b/apier/v1/suppliers_it_test.go index f42b6843d..7edb70fff 100644 --- a/apier/v1/suppliers_it_test.go +++ b/apier/v1/suppliers_it_test.go @@ -165,6 +165,7 @@ func testV1SplSGetWeightSuppliers(t *testing.T) { func testV1SplSGetLeastCostSuppliers(t *testing.T) { ev := &engine.ArgsGetSuppliers{ + //MaxCost: 0.020, CGREvent: utils.CGREvent{ Tenant: "cgrates.org", ID: "testV1SplSGetLeastCostSuppliers", diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index c62d9a37d..caad60a84 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -41,6 +41,7 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, Sorting: lcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} for _, s := range suppls { + utils.Logger.Debug(fmt.Sprintf("s : %+v \n", s)) costData, err := lcs.spS.costForEvent(ev, s.AccountIDs, s.RatingPlanIDs) if err != nil { return nil, err @@ -50,6 +51,14 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, utils.SupplierS, prflID, s.ID)) continue } + cost, err := utils.IfaceAsFloat64(costData[utils.Cost]) + if err != nil { + return nil, err + } + utils.Logger.Debug(fmt.Sprintf("maxCost : %+v and cost costData[utils.Cost] %+v", float64(*lcs.spS.maxCost), cost)) + if cost > float64(*lcs.spS.maxCost) { + continue + } srtData := map[string]interface{}{ utils.Weight: s.Weight, } @@ -61,6 +70,10 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, SortingData: srtData, SupplierParameters: s.SupplierParameters}) } + utils.Logger.Debug(fmt.Sprintf("sortedSuppls : %+v \n", sortedSuppls)) + if len(sortedSuppls.SortedSuppliers) == 0 { + return nil, utils.ErrNotFound + } sortedSuppls.SortCost() return } diff --git a/engine/suppliers.go b/engine/suppliers.go index 41c58d3e1..a5275b4dc 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -92,6 +92,7 @@ type SupplierService struct { filterS *FilterS stringIndexedFields *[]string prefixIndexedFields *[]string + maxCost *float64 resourceS, statS rpcclient.RpcClientConnection sorter SupplierSortDispatcher @@ -265,6 +266,8 @@ func (spS *SupplierService) resourceUsage(resIDs []string) (tUsage float64, err // for event based on filters and sorting algorithms func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { var suppPrfls SupplierProfiles + utils.Logger.Debug(fmt.Sprintf("args.maxcost %+v", args.MaxCost)) + spS.maxCost = utils.Float64Pointer(args.MaxCost) if suppPrfls, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent); err != nil { return } else if len(suppPrfls) == 0 { @@ -301,6 +304,8 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor } type ArgsGetSuppliers struct { + IgnoreError bool + MaxCost float64 utils.CGREvent utils.Paginator }