Updated GetSupplierProfilesForEvent

This commit is contained in:
Tripon Alexandru-Ionut
2019-04-18 15:04:10 +03:00
parent 0a7529f114
commit 0f9ad87d50
11 changed files with 78 additions and 65 deletions

View File

@@ -338,9 +338,9 @@ func (dSup *DispatcherSupplierSv1) GetSuppliers(args *engine.ArgsGetSuppliers,
}
// GetSuppliersProfiles returns a list of suppliers profiles that match for Event
func (dSup *DispatcherSupplierSv1) GetSupplierForEvent(args *utils.CGREventWithArgDispatcher,
reply *engine.SupplierProfile) error {
return dSup.dSup.SupplierSv1GetSupplierForEvent(args, reply)
func (dSup *DispatcherSupplierSv1) GetSupplierProfilesForEvent(args *utils.CGREventWithArgDispatcher,
reply *[]*engine.SupplierProfile) error {
return dSup.dSup.SupplierSv1GetSupplierProfilesForEvent(args, reply)
}
func NewDispatcherAttributeSv1(dps *dispatchers.DispatcherService) *DispatcherAttributeSv1 {

View File

@@ -55,7 +55,7 @@ type ResourceSv1Interface interface {
type SupplierSv1Interface interface {
GetSuppliers(args *engine.ArgsGetSuppliers, reply *engine.SortedSuppliers) error
GetSupplierForEvent(args *utils.CGREventWithArgDispatcher, reply *engine.SupplierProfile) error
GetSupplierProfilesForEvent(args *utils.CGREventWithArgDispatcher, reply *[]*engine.SupplierProfile) error
Ping(ign *utils.CGREventWithArgDispatcher, reply *string) error
}

View File

@@ -134,9 +134,9 @@ func (splv1 *SupplierSv1) GetSuppliers(args *engine.ArgsGetSuppliers,
}
// GetSuppliersProfiles returns a list of suppliers profiles that match for Event
func (splv1 *SupplierSv1) GetSupplierForEvent(args *utils.CGREventWithArgDispatcher,
reply *engine.SupplierProfile) error {
return splv1.splS.V1GetSupplierForEvent(args, reply)
func (splv1 *SupplierSv1) GetSupplierProfilesForEvent(args *utils.CGREventWithArgDispatcher,
reply *[]*engine.SupplierProfile) error {
return splv1.splS.V1GetSupplierProfilesForEvent(args, reply)
}
func (splv1 *SupplierSv1) Ping(ign *utils.CGREventWithArgDispatcher, reply *string) error {

View File

@@ -928,18 +928,18 @@ func testV1SplSGetSupplierForEvent(t *testing.T) {
},
Weight: 50,
}
var supProf engine.SupplierProfile
if err := splSv1Rpc.Call(utils.SupplierSv1GetSupplierForEvent,
var supProf []*engine.SupplierProfile
if err := splSv1Rpc.Call(utils.SupplierSv1GetSupplierProfilesForEvent,
ev, &supProf); err != nil {
t.Fatal(err)
}
sort.Slice(expected.Suppliers, func(i, j int) bool {
return supProf.Suppliers[i].Weight < supProf.Suppliers[j].Weight
return expected.Suppliers[i].Weight < expected.Suppliers[j].Weight
})
sort.Slice(supProf.Suppliers, func(i, j int) bool {
return supProf.Suppliers[i].Weight < supProf.Suppliers[j].Weight
sort.Slice(supProf[0].Suppliers, func(i, j int) bool {
return supProf[0].Suppliers[i].Weight < supProf[0].Suppliers[j].Weight
})
if !reflect.DeepEqual(expected, supProf) {
if !reflect.DeepEqual(expected, *supProf[0]) {
t.Errorf("Expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(supProf))
}
}

View File

@@ -25,8 +25,8 @@ import (
func init() {
c := &CmdGetSupplierForEvent{
name: "supplier_for_event",
rpcMethod: utils.SupplierSv1GetSupplierForEvent,
name: "supplier_profiles_for_event",
rpcMethod: utils.SupplierSv1GetSupplierProfilesForEvent,
rpcParams: &utils.CGREventWithArgDispatcher{},
}
commands[c.Name()] = c
@@ -60,6 +60,6 @@ func (self *CmdGetSupplierForEvent) PostprocessRpcParams() error {
}
func (self *CmdGetSupplierForEvent) RpcResult() interface{} {
atr := engine.SupplierProfile{}
atr := []*engine.SupplierProfile{}
return &atr
}

View File

@@ -6,7 +6,7 @@ cgrates.org,ATTR_API_ATTR_FAKE_AUTH,*auth,*string:~APIKey:12345,,,APIMethods,*co
cgrates.org,ATTR_API_ATTR_AUTH,*auth,*string:~APIKey:attr12345,,,APIMethods,*constant,AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent,false,20
cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:~APIKey:chrg12345,,,APIMethods,*constant,ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent,false,20
cgrates.org,ATTR_API_THR_AUTH,*auth,*string:~APIKey:thr12345,,,APIMethods,*constant,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent&ThresholdSv1.GetThreshold&ThresholdSv1.GetThresholdIDs,false,20
cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~APIKey:sup12345,,,APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierForEvent,false,20
cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~APIKey:sup12345,,,APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent,false,20
cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:~APIKey:stat12345,,,APIMethods,*constant,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics,false,20
cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~APIKey:res12345,,,APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources,false,20
cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~APIKey:ses12345,,,APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessEvent&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession,false,20
1 #Tenant ID Contexts FilterIDs ActivationInterval AttributeFilterIDs FieldName Type Value Blocker Weight
6 cgrates.org ATTR_API_ATTR_AUTH *auth *string:~APIKey:attr12345 APIMethods *constant AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent false 20
7 cgrates.org ATTR_API_CHRG_AUTH *auth *string:~APIKey:chrg12345 APIMethods *constant ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent false 20
8 cgrates.org ATTR_API_THR_AUTH *auth *string:~APIKey:thr12345 APIMethods *constant ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent&ThresholdSv1.GetThreshold&ThresholdSv1.GetThresholdIDs false 20
9 cgrates.org ATTR_API_SUP_AUTH *auth *string:~APIKey:sup12345 APIMethods *constant SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierForEvent SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent false 20
10 cgrates.org ATTR_API_STAT_AUTH *auth *string:~APIKey:stat12345 APIMethods *constant StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics false 20
11 cgrates.org ATTR_API_RES_AUTH *auth *string:~APIKey:res12345 APIMethods *constant ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources false 20
12 cgrates.org ATTR_API_SES_AUTH *auth *string:~APIKey:ses12345 APIMethods *constant SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessEvent&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession false 20

View File

@@ -54,18 +54,18 @@ func (dS *DispatcherService) SupplierSv1GetSuppliers(args *engine.ArgsGetSupplie
utils.SupplierSv1GetSuppliers, args, reply)
}
func (dS *DispatcherService) SupplierSv1GetSupplierForEvent(args *utils.CGREventWithArgDispatcher,
reply *engine.SupplierProfile) (err error) {
func (dS *DispatcherService) SupplierSv1GetSupplierProfilesForEvent(args *utils.CGREventWithArgDispatcher,
reply *[]*engine.SupplierProfile) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
}
if dS.attrS != nil {
if err = dS.authorize(utils.SupplierSv1GetSupplierForEvent,
if err = dS.authorize(utils.SupplierSv1GetSupplierProfilesForEvent,
args.CGREvent.Tenant,
args.APIKey, args.CGREvent.Time); err != nil {
return
}
}
return dS.Dispatch(args.CGREvent, utils.MetaSuppliers, args.RouteID,
utils.SupplierSv1GetSupplierForEvent, args, reply)
utils.SupplierSv1GetSupplierProfilesForEvent, args, reply)
}

View File

@@ -377,18 +377,18 @@ func testDspSupGetSupplierForEvent(t *testing.T) {
},
Weight: 10,
}
var supProf engine.SupplierProfile
if err := dispEngine.RCP.Call(utils.SupplierSv1GetSupplierForEvent,
var supProf []*engine.SupplierProfile
if err := dispEngine.RCP.Call(utils.SupplierSv1GetSupplierProfilesForEvent,
ev, &supProf); err != nil {
t.Fatal(err)
}
sort.Slice(expected.Suppliers, func(i, j int) bool {
return supProf.Suppliers[i].Weight < supProf.Suppliers[j].Weight
return expected.Suppliers[i].Weight < expected.Suppliers[j].Weight
})
sort.Slice(supProf.Suppliers, func(i, j int) bool {
return supProf.Suppliers[i].Weight < supProf.Suppliers[j].Weight
sort.Slice(supProf[0].Suppliers, func(i, j int) bool {
return supProf[0].Suppliers[i].Weight < supProf[0].Suppliers[j].Weight
})
if !reflect.DeepEqual(expected, supProf) {
if !reflect.DeepEqual(expected, *supProf[0]) {
t.Errorf("Expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(supProf))
}
}

View File

@@ -125,12 +125,15 @@ func (spS *SupplierService) Shutdown() error {
}
// matchingSupplierProfilesForEvent returns ordered list of matching resources which are active by the time of the call
func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent) (matchingLP *SupplierProfile, err error) {
func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent, singleResult bool) (matchingSLP []*SupplierProfile, err error) {
sPrflIDs, err := MatchingItemIDsForEvent(ev.Event, spS.stringIndexedFields, spS.prefixIndexedFields,
spS.dm, utils.CacheSupplierFilterIndexes, ev.Tenant, spS.filterS.cfg.SupplierSCfg().IndexedSelects)
if err != nil {
return nil, err
}
if singleResult {
matchingSLP = make([]*SupplierProfile, 1)
}
for lpID := range sPrflIDs {
splPrfl, err := spS.dm.GetSupplierProfile(ev.Tenant, lpID, true, true, utils.NonTransactional)
if err != nil {
@@ -149,12 +152,23 @@ func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent)
} else if !pass {
continue
}
if matchingLP == nil || matchingLP.Weight < splPrfl.Weight {
matchingLP = splPrfl
if singleResult {
if matchingSLP[0] == nil || matchingSLP[0].Weight < splPrfl.Weight {
matchingSLP[0] = splPrfl
}
} else {
matchingSLP = append(matchingSLP, splPrfl)
}
}
if matchingLP == nil {
return nil, utils.ErrNotFound
if singleResult {
if matchingSLP[0] == nil {
return nil, utils.ErrNotFound
}
} else {
if len(matchingSLP) == 0 {
return nil, utils.ErrNotFound
}
sort.Slice(matchingSLP, func(i, j int) bool { return matchingSLP[i].Weight < matchingSLP[j].Weight })
}
return
}
@@ -402,10 +416,11 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor
if _, has := args.CGREvent.Event[utils.Usage]; !has {
args.CGREvent.Event[utils.Usage] = time.Duration(time.Minute) // make sure we have default set for Usage
}
var splPrfl *SupplierProfile
if splPrfl, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent); err != nil {
var splPrfls []*SupplierProfile
if splPrfls, err = spS.matchingSupplierProfilesForEvent(&args.CGREvent, true); err != nil {
return
}
splPrfl := splPrfls[0]
extraOpts, err := args.asOptsGetSuppliers() // convert suppliers arguments into internal options used to limit data
if err != nil {
return nil, err
@@ -469,7 +484,7 @@ type optsGetSuppliers struct {
sortingParameters []string //used for QOS strategy
}
// V1GetSupplierForEvent returns the list of valid supplier IDs
// V1GetSupplierProfilesForEvent 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 {
return utils.NewErrMandatoryIeMissing(missing...)
@@ -502,21 +517,19 @@ func (spS *SupplierService) V1GetSuppliers(args *ArgsGetSuppliers, reply *Sorted
}
// V1GetSupplierProfiles returns the list of valid supplier profiles
func (spS *SupplierService) V1GetSupplierForEvent(args *utils.CGREventWithArgDispatcher, reply *SupplierProfile) (err error) {
func (spS *SupplierService) V1GetSupplierProfilesForEvent(args *utils.CGREventWithArgDispatcher, reply *[]*SupplierProfile) (err error) {
if missing := utils.MissingStructFields(args.CGREvent, []string{"Tenant", "ID"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
} else if args.CGREvent.Event == nil {
return utils.NewErrMandatoryIeMissing("Event")
}
sPs, err := spS.matchingSupplierProfilesForEvent(args.CGREvent)
sPs, err := spS.matchingSupplierProfilesForEvent(args.CGREvent, false)
if err != nil {
if err != utils.ErrNotFound {
err = utils.NewErrServerError(err)
}
return err
}
if sPs != nil {
*reply = *sPs
}
*reply = sPs
return
}

View File

@@ -386,28 +386,28 @@ func TestSuppliersCache(t *testing.T) {
}
func TestSuppliersmatchingSupplierProfilesForEvent(t *testing.T) {
sprf, err := splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[0].CGREvent)
sprf, err := splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[0].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[0], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf)
if !reflect.DeepEqual(sppTest[0], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0])
}
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent)
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[1], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf)
if !reflect.DeepEqual(sppTest[1], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0])
}
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent)
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[2], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf)
if !reflect.DeepEqual(sppTest[2], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0])
}
}
@@ -630,27 +630,27 @@ func TestSuppliersAsOptsGetSuppliersMaxCost(t *testing.T) {
func TestSuppliersMatchWithIndexFalse(t *testing.T) {
splService.filterS.cfg.SupplierSCfg().IndexedSelects = false
sprf, err := splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[0].CGREvent)
sprf, err := splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[0].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[0], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf)
if !reflect.DeepEqual(sppTest[0], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0])
}
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent)
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[1].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[1], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf)
if !reflect.DeepEqual(sppTest[1], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0])
}
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent)
sprf, err = splService.matchingSupplierProfilesForEvent(&argsGetSuppliers[2].CGREvent, true)
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(sppTest[2], sprf) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf)
if !reflect.DeepEqual(sppTest[2], sprf[0]) {
t.Errorf("Expecting: %+v, received: %+v", sppTest[2], sprf[0])
}
}

View File

@@ -754,13 +754,13 @@ const (
// SupplierS APIs
const (
SupplierSv1GetSuppliers = "SupplierSv1.GetSuppliers"
SupplierSv1GetSupplierForEvent = "SupplierSv1.GetSupplierForEvent"
SupplierSv1Ping = "SupplierSv1.Ping"
ApierV1GetSupplierProfile = "ApierV1.GetSupplierProfile"
ApierV1GetSupplierProfileIDs = "ApierV1.GetSupplierProfileIDs"
ApierV1RemoveSupplierProfile = "ApierV1.RemoveSupplierProfile"
ApierV1SetSupplierProfile = "ApierV1.SetSupplierProfile"
SupplierSv1GetSuppliers = "SupplierSv1.GetSuppliers"
SupplierSv1GetSupplierProfilesForEvent = "SupplierSv1.GetSupplierProfilesForEvent"
SupplierSv1Ping = "SupplierSv1.Ping"
ApierV1GetSupplierProfile = "ApierV1.GetSupplierProfile"
ApierV1GetSupplierProfileIDs = "ApierV1.GetSupplierProfileIDs"
ApierV1RemoveSupplierProfile = "ApierV1.RemoveSupplierProfile"
ApierV1SetSupplierProfile = "ApierV1.SetSupplierProfile"
)
// AttributeS APIs