From b24f7f1cfdd9b6722e70e7a098904c65ef326e7a Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 30 Mar 2018 04:21:22 -0400 Subject: [PATCH] MaxCost now per request --- engine/libsuppliers.go | 8 ++++---- engine/spls_leastcost.go | 16 ++++++++++++++-- engine/suppliers.go | 5 ++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/engine/libsuppliers.go b/engine/libsuppliers.go index c60ba68f9..5a82df6b8 100644 --- a/engine/libsuppliers.go +++ b/engine/libsuppliers.go @@ -92,7 +92,7 @@ type SupplierWithParams struct { // SuppliersSorter is the interface which needs to be implemented by supplier sorters type SuppliersSorter interface { - SortSuppliers(string, []*Supplier, *utils.CGREvent) (*SortedSuppliers, error) + SortSuppliers(string, []*Supplier, *utils.CGREvent, *extraOptions) (*SortedSuppliers, error) } // NewSupplierSortDispatcher constructs SupplierSortDispatcher @@ -108,12 +108,12 @@ func NewSupplierSortDispatcher(lcrS *SupplierService) (ssd SupplierSortDispatche type SupplierSortDispatcher map[string]SuppliersSorter func (ssd SupplierSortDispatcher) SortSuppliers(prflID, strategy string, - suppls []*Supplier, suplEv *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraFields *extraOptions) (sortedSuppls *SortedSuppliers, err error) { sd, has := ssd[strategy] if !has { return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy) } - return sd.SortSuppliers(prflID, suppls, suplEv) + return sd.SortSuppliers(prflID, suppls, suplEv, extraFields) } func NewWeightSorter() *WeightSorter { @@ -126,7 +126,7 @@ type WeightSorter struct { } func (ws *WeightSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraFields *extraOptions) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, len(suppls))} diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index 17ca6f75e..31918bf58 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -35,14 +35,26 @@ type LeastCostSorter struct { spS *SupplierService } +// LeastCostSorter sorts suppliers based on their cost +type extraOptions struct { + maxCost *float64 + ignoreErrors bool +} + func (lcs *LeastCostSorter) SortSuppliers(prflID string, - suppls []*Supplier, ev *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, ev *utils.CGREvent, extraFilters *extraOptions) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: lcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} for _, s := range suppls { costData, err := lcs.spS.costForEvent(ev, s.AccountIDs, s.RatingPlanIDs) if err != nil { + if extraFilters.ignoreErrors { + utils.Logger.Warning( + fmt.Sprintf("<%s> profile: %s ignoring supplier with ID: %s, err: %s", + utils.SupplierS, prflID, s.ID, err.Error())) + continue + } return nil, err } else if len(costData) == 0 { utils.Logger.Warning( @@ -50,7 +62,7 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, utils.SupplierS, prflID, s.ID)) continue } - if costData[utils.Cost].(float64) > *lcs.spS.maxCost && *lcs.spS.maxCost != 0 { + if costData[utils.Cost].(float64) > *extraFilters.maxCost && *extraFilters.maxCost != 0 { continue } srtData := map[string]interface{}{ diff --git a/engine/suppliers.go b/engine/suppliers.go index 3d549eeb6..a533c7946 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -92,7 +92,6 @@ type SupplierService struct { filterS *FilterS stringIndexedFields *[]string prefixIndexedFields *[]string - maxCost *float64 resourceS, statS rpcclient.RpcClientConnection sorter SupplierSortDispatcher @@ -266,7 +265,6 @@ 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 - spS.maxCost = utils.Float64Pointer(args.MaxCost) if suppPrfls, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent); err != nil { return } else if len(suppPrfls) == 0 { @@ -285,7 +283,8 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor } spls = append(spls, s) } - sortedSuppliers, err := spS.sorter.SortSuppliers(splPrfl.ID, splPrfl.Sorting, spls, &args.CGREvent) + sortedSuppliers, err := spS.sorter.SortSuppliers(splPrfl.ID, splPrfl.Sorting, spls, &args.CGREvent, + &extraOptions{maxCost: utils.Float64Pointer(args.MaxCost), ignoreErrors: args.IgnoreError}) if err != nil { return nil, err }