RequestFilter with passString, started ResourceLimiterService

This commit is contained in:
DanB
2016-08-01 11:15:34 +02:00
parent 146b4fff6e
commit 0532732aee
3 changed files with 39 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ import (
)
const (
MetaString = "*string"
MetaStringPrefix = "*string_prefix"
MetaTimings = "*timings"
MetaRSRFields = "*rsr_fields"
@@ -101,6 +102,8 @@ type RequestFilter struct {
// Pass is the method which should be used from outside.
func (fltr *RequestFilter) Pass(req interface{}, extraFieldsLabel string) (bool, error) {
switch fltr.Type {
case MetaString:
return fltr.passString(req, extraFieldsLabel)
case MetaStringPrefix:
return fltr.passStringPrefix(req, extraFieldsLabel)
case MetaTimings:
@@ -116,6 +119,19 @@ func (fltr *RequestFilter) Pass(req interface{}, extraFieldsLabel string) (bool,
}
}
func (fltr *RequestFilter) passString(req interface{}, extraFieldsLabel string) (bool, error) {
strVal, err := utils.ReflectFieldAsString(req, fltr.FieldName, extraFieldsLabel)
if err != nil {
return false, err
}
for _, val := range fltr.Values {
if strVal == val {
return true, nil
}
}
return false, nil
}
func (fltr *RequestFilter) passStringPrefix(req interface{}, extraFieldsLabel string) (bool, error) {
strVal, err := utils.ReflectFieldAsString(req, fltr.FieldName, extraFieldsLabel)
if err != nil {

View File

@@ -25,6 +25,24 @@ import (
"github.com/cgrates/cgrates/utils"
)
func TestPassString(t *testing.T) {
cd := &CallDescriptor{Direction: "*out", Category: "call", Tenant: "cgrates.org", Subject: "dan", Destination: "+4986517174963",
TimeStart: time.Date(2013, time.October, 7, 14, 50, 0, 0, time.UTC), TimeEnd: time.Date(2013, time.October, 7, 14, 52, 12, 0, time.UTC),
DurationIndex: 132 * time.Second, ExtraFields: map[string]string{"navigation": "off"}}
rf := &RequestFilter{Type: MetaString, FieldName: "Category", Values: []string{"call"}}
if passes, err := rf.passString(cd, ""); err != nil {
t.Error(err)
} else if !passes {
t.Error("Not passes filter")
}
rf = &RequestFilter{Type: MetaString, FieldName: "Category", Values: []string{"cal"}}
if passes, err := rf.passString(cd, ""); err != nil {
t.Error(err)
} else if passes {
t.Error("Filter passes")
}
}
func TestPassStringPrefix(t *testing.T) {
cd := &CallDescriptor{Direction: "*out", Category: "call", Tenant: "cgrates.org", Subject: "dan", Destination: "+4986517174963",
TimeStart: time.Date(2013, time.October, 7, 14, 50, 0, 0, time.UTC), TimeEnd: time.Date(2013, time.October, 7, 14, 52, 12, 0, time.UTC),

View File

@@ -31,3 +31,8 @@ type ResourceLimit struct {
Limit float64 // Limit value
ActionTriggers ActionTriggers // Thresholds to check after changing Limit
}
// ResourcesLimiter is the service handling channel limits
type ResourceLimiterService struct {
stringIndexes map[string]map[string]string // map[fieldName]map[fieldValue]resourceID
}