mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 09:08:45 +05:00
RequestFilter with passString, started ResourceLimiterService
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user