From 61ff360dab94eb04012acde9da7b608a9b4a70df Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 30 Mar 2018 09:37:50 -0400 Subject: [PATCH] Update MaxCost For GetSuppliers --- apier/v1/suppliers_it_test.go | 6 ++--- engine/libsuppliers.go | 6 ++--- engine/spls_leastcost.go | 8 +------ engine/suppliers.go | 41 +++++++++++++++++++++++++++++++---- sessions/sessions.go | 20 ++++++++++------- utils/consts.go | 1 + 6 files changed, 57 insertions(+), 25 deletions(-) diff --git a/apier/v1/suppliers_it_test.go b/apier/v1/suppliers_it_test.go index 655ba2321..faa68da93 100644 --- a/apier/v1/suppliers_it_test.go +++ b/apier/v1/suppliers_it_test.go @@ -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", diff --git a/engine/libsuppliers.go b/engine/libsuppliers.go index d951a8c16..22c0d878c 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, *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))} diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index 4c129bd23..2f8b6d689 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -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)} diff --git a/engine/suppliers.go b/engine/suppliers.go index 20feba6d1..4fbf2b463 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -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 { diff --git a/sessions/sessions.go b/sessions/sessions.go index b29e5ba12..fb417a3f4 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -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 { diff --git a/utils/consts.go b/utils/consts.go index 10c70beac..338ab70a1 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -532,6 +532,7 @@ const ( MetaRound = "*round" LoaderS = "LoaderS" Pong = "Pong" + MetaEventCost = "*event_cost" ) //MetaMetrics