Revenue protection through Min-/MaxCost filters to GetLCR APIs, fixes #411

This commit is contained in:
DanB
2016-06-05 19:07:12 +02:00
committed by Razvan Crainea
parent 814531997b
commit 979203c657
5 changed files with 21 additions and 6 deletions

View File

@@ -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() {

View File

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

View File

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

View File

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

View File

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