diff --git a/apier/v1/lcr.go b/apier/v1/lcr.go index 2b3850eef..638f24234 100644 --- a/apier/v1/lcr.go +++ b/apier/v1/lcr.go @@ -32,7 +32,7 @@ func (self *ApierV1) GetLcr(lcrReq engine.LcrRequest, lcrReply *engine.LcrReply) return err } var lcrQried engine.LCRCost - if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, Paginator: lcrReq.Paginator}, &lcrQried); err != nil { + if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, LCRFilter: lcrReq.LCRFilter, Paginator: lcrReq.Paginator}, &lcrQried); err != nil { return utils.NewErrServerError(err) } if lcrQried.Entry == nil { @@ -65,7 +65,7 @@ func (self *ApierV1) GetLcrSuppliers(lcrReq engine.LcrRequest, suppliers *string return err } var lcrQried engine.LCRCost - if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, Paginator: lcrReq.Paginator}, &lcrQried); err != nil { + if err := self.Responder.GetLCR(&engine.AttrGetLcr{CallDescriptor: cd, LCRFilter: lcrReq.LCRFilter, Paginator: lcrReq.Paginator}, &lcrQried); err != nil { return utils.NewErrServerError(err) } if lcrQried.HasErrors() { diff --git a/engine/calldesc.go b/engine/calldesc.go index d62a0005e..0f4097277 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -966,7 +966,7 @@ func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) { return nil, utils.ErrNotFound } -func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, p *utils.Paginator) (*LCRCost, error) { +func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, lcrFltr *LCRFilter, p *utils.Paginator) (*LCRCost, error) { cd.account = nil // make sure it's not cached lcr, err := cd.GetLCRFromStorage() if err != nil { @@ -1259,6 +1259,14 @@ func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, p *utils.P }) continue } else { + if lcrFltr != nil { + if lcrFltr.MinCost != nil && cc.Cost < *lcrFltr.MinCost { + continue // MinCost not reached, ignore the supplier + } + if lcrFltr.MaxCost != nil && cc.Cost >= *lcrFltr.MaxCost { + continue // Equal or higher than MaxCost allowed, ignore the supplier + } + } supplCost := &LCRSupplierCost{ Supplier: fullSupplier, Cost: cc.Cost, diff --git a/engine/lcr.go b/engine/lcr.go index 2a50c6003..f3de6183f 100644 --- a/engine/lcr.go +++ b/engine/lcr.go @@ -58,9 +58,15 @@ type LcrRequest struct { Duration string IgnoreErrors bool ExtraFields map[string]string + *LCRFilter *utils.Paginator } +type LCRFilter struct { + MinCost *float64 + MaxCost *float64 +} + func (self *LcrRequest) AsCallDescriptor(timezone string) (*CallDescriptor, error) { if len(self.Account) == 0 || len(self.Destination) == 0 { return nil, utils.ErrMandatoryIeMissing diff --git a/engine/lcr_test.go b/engine/lcr_test.go index 49f201cff..e5a2e5377 100644 --- a/engine/lcr_test.go +++ b/engine/lcr_test.go @@ -204,7 +204,7 @@ func TestLcrGet(t *testing.T) { Account: "rif", Subject: "rif", } - lcr, err := cd.GetLCR(nil, nil) + lcr, err := cd.GetLCR(nil, nil, nil) if err != nil || lcr == nil { t.Errorf("Bad lcr: %+v, %v", lcr, err) } @@ -222,7 +222,7 @@ func TestLcrGetPrefix(t *testing.T) { Account: "rif", Subject: "rifus", } - lcr, err := cd.GetLCR(nil, nil) + lcr, err := cd.GetLCR(nil, nil, nil) if err != nil || lcr == nil { t.Errorf("Bad lcr: %+v, %v", lcr, err) } diff --git a/engine/responder.go b/engine/responder.go index f85daa733..6de7c4d53 100644 --- a/engine/responder.go +++ b/engine/responder.go @@ -43,6 +43,7 @@ type SessionRun struct { type AttrGetLcr struct { *CallDescriptor + *LCRFilter *utils.Paginator } @@ -543,7 +544,7 @@ func (rs *Responder) GetLCR(attrs *AttrGetLcr, reply *LCRCost) error { rs.getCache().Cache(cacheKey, &cache2go.CacheItem{Err: err}) return err } - lcrCost, err := attrs.CallDescriptor.GetLCR(rs.Stats, attrs.Paginator) + lcrCost, err := attrs.CallDescriptor.GetLCR(rs.Stats, attrs.LCRFilter, attrs.Paginator) if err != nil { rs.getCache().Cache(cacheKey, &cache2go.CacheItem{Err: err}) return err