Update MaxCost For GetSuppliers

This commit is contained in:
TeoV
2018-03-30 09:37:50 -04:00
committed by Dan Christian Bogos
parent 9002c8c964
commit 61ff360dab
6 changed files with 57 additions and 25 deletions

View File

@@ -45,7 +45,7 @@ var sTestsSupplierSV1 = []func(t *testing.T){
testV1SplSLoadConfig,
testV1SplSInitDataDb,
testV1SplSResetStorDb,
testV1SplSStartEngine,
//testV1SplSStartEngine,
testV1SplSRpcConn,
testV1SplSFromFolder,
testV1SplSGetWeightSuppliers,
@@ -221,7 +221,7 @@ func testV1SplSGetLeastCostSuppliers(t *testing.T) {
func testV1SplSGetLeastCostSuppliersWithMaxCost(t *testing.T) {
ev := &engine.ArgsGetSuppliers{
MaxCost: 0.30,
MaxCost: "0.30",
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
ID: "testV1SplSGetLeastCostSuppliers",
@@ -268,7 +268,7 @@ func testV1SplSGetLeastCostSuppliersWithMaxCost(t *testing.T) {
func testV1SplSGetLeastCostSuppliersWithMaxCostNotFound(t *testing.T) {
ev := &engine.ArgsGetSuppliers{
MaxCost: 0.001,
MaxCost: "0.001",
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
ID: "testV1SplSGetLeastCostSuppliers",

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, *extraOpts) (*SortedSuppliers, error)
SortSuppliers(string, []*Supplier, *utils.CGREvent, *optsGetSuppliers) (*SortedSuppliers, error)
}
// NewSupplierSortDispatcher constructs SupplierSortDispatcher
@@ -108,7 +108,7 @@ func NewSupplierSortDispatcher(lcrS *SupplierService) (ssd SupplierSortDispatche
type SupplierSortDispatcher map[string]SuppliersSorter
func (ssd SupplierSortDispatcher) SortSuppliers(prflID, strategy string,
suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *extraOpts) (sortedSuppls *SortedSuppliers, err error) {
suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) {
sd, has := ssd[strategy]
if !has {
return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy)
@@ -126,7 +126,7 @@ type WeightSorter struct {
}
func (ws *WeightSorter) SortSuppliers(prflID string,
suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *extraOpts) (sortedSuppls *SortedSuppliers, err error) {
suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) {
sortedSuppls = &SortedSuppliers{ProfileID: prflID,
Sorting: ws.sorting,
SortedSuppliers: make([]*SortedSupplier, len(suppls))}

View File

@@ -35,14 +35,8 @@ type LeastCostSorter struct {
spS *SupplierService
}
// LeastCostSorter sorts suppliers based on their cost
type extraOpts struct {
maxCost float64
ignoreErrors bool
}
func (lcs *LeastCostSorter) SortSuppliers(prflID string, suppls []*Supplier,
ev *utils.CGREvent, extraOpts *extraOpts) (sortedSuppls *SortedSuppliers, err error) {
ev *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) {
sortedSuppls = &SortedSuppliers{ProfileID: prflID,
Sorting: lcs.sorting,
SortedSuppliers: make([]*SortedSupplier, 0)}

View File

@@ -21,6 +21,7 @@ package engine
import (
"fmt"
"sort"
"strconv"
"time"
"github.com/cgrates/cgrates/config"
@@ -283,8 +284,11 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor
}
spls = append(spls, s)
}
sortedSuppliers, err := spS.sorter.SortSuppliers(splPrfl.ID, splPrfl.Sorting, spls, &args.CGREvent,
&extraOpts{maxCost: args.MaxCost, ignoreErrors: args.IgnoreError})
extraOpts, err := args.asOptsGetSuppliers()
if err != nil {
return nil, err
}
sortedSuppliers, err := spS.sorter.SortSuppliers(splPrfl.ID, splPrfl.Sorting, spls, &args.CGREvent, extraOpts)
if err != nil {
return nil, err
}
@@ -302,12 +306,41 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor
}
type ArgsGetSuppliers struct {
IgnoreError bool
MaxCost float64
IgnoreErrors bool
MaxCost string
utils.CGREvent
utils.Paginator
}
//de convertit din ArgsGetSuppliers in argsGetSuppliers
func (args *ArgsGetSuppliers) asOptsGetSuppliers() (out *optsGetSuppliers, err error) {
out = new(optsGetSuppliers)
if args.MaxCost != "" {
if args.MaxCost == utils.MetaEventCost {
val, err := strconv.ParseFloat(args.MaxCost, 64)
if err != nil {
return nil, err
}
out.maxCost = val
//implement
} else {
val, err := strconv.ParseFloat(args.MaxCost, 64)
if err != nil {
return nil, err
}
out.maxCost = val
}
}
out.ignoreErrors = args.IgnoreErrors
return
}
type optsGetSuppliers struct {
ignoreErrors bool
maxCost float64
}
// V1GetSuppliersForEvent returns the list of valid supplier IDs
func (spS *SupplierService) V1GetSuppliers(args *ArgsGetSuppliers, reply *SortedSuppliers) (err error) {
if missing := utils.MissingStructFields(&args.CGREvent, []string{"Tenant", "ID"}); len(missing) != 0 {

View File

@@ -1301,12 +1301,14 @@ func (smg *SMGeneric) BiRPCV1ReplicatePassiveSessions(clnt rpcclient.RpcClientCo
}
type V1AuthorizeArgs struct {
GetAttributes bool
AuthorizeResources bool
GetMaxUsage bool
GetSuppliers bool
ProcessThresholds *bool
ProcessStatQueues *bool
GetAttributes bool
AuthorizeResources bool
GetMaxUsage bool
GetSuppliers bool
SuppliersMaxCost string
SuppliersIgnoreErrors bool
ProcessThresholds *bool
ProcessStatQueues *bool
utils.CGREvent
utils.Paginator
}
@@ -1410,8 +1412,10 @@ func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection,
}
var splsReply engine.SortedSuppliers
sArgs := &engine.ArgsGetSuppliers{
CGREvent: *cgrEv,
Paginator: args.Paginator,
IgnoreErrors: args.SuppliersIgnoreErrors,
MaxCost: args.SuppliersMaxCost,
CGREvent: *cgrEv,
Paginator: args.Paginator,
}
if err = smg.splS.Call(utils.SupplierSv1GetSuppliers,
sArgs, &splsReply); err != nil {

View File

@@ -532,6 +532,7 @@ const (
MetaRound = "*round"
LoaderS = "LoaderS"
Pong = "Pong"
MetaEventCost = "*event_cost"
)
//MetaMetrics