MaxCost now per request

This commit is contained in:
TeoV
2018-03-30 04:21:22 -04:00
committed by Dan Christian Bogos
parent 5eebc6c71e
commit b24f7f1cfd
3 changed files with 20 additions and 9 deletions

View File

@@ -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))}

View File

@@ -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{}{

View File

@@ -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
}