mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Updated filterSessions
This commit is contained in:
@@ -287,8 +287,8 @@ func testRAitAcctStart(t *testing.T) {
|
||||
if err := raRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "e4921177ab0e3586c37f6a185864b71a@0:0:0:0:0:0:0:0-51585361-75c2f57b"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "e4921177ab0e3586c37f6a185864b71a@0:0:0:0:0:0:0:0-51585361-75c2f57b"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -358,8 +358,8 @@ func testRAitAcctStop(t *testing.T) {
|
||||
if err := raRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "e4921177ab0e3586c37f6a185864b71a@0:0:0:0:0:0:0:0-51585361-75c2f57b"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "e4921177ab0e3586c37f6a185864b71a@0:0:0:0:0:0:0:0-51585361-75c2f57b"),
|
||||
},
|
||||
}, &aSessions); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
|
||||
@@ -274,3 +274,8 @@ func (me MapEvent) AsCDR(cfg *config.CGRConfig, tnt, tmz string) (cdr *CDR, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Data returns the MapEvent as a map[string]interface{}
|
||||
func (me MapEvent) Data() map[string]interface{} {
|
||||
return me
|
||||
}
|
||||
|
||||
@@ -155,14 +155,14 @@ func testSes2ItInitSession(t *testing.T) {
|
||||
func testSes2ItAsActiveSessions(t *testing.T) {
|
||||
var count int
|
||||
if err := ses2RPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{
|
||||
Filters: []string{"*string:~Account:1001"},
|
||||
Filters: []string{"*string:~*req.Account:1001"},
|
||||
}, &count); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if count != 2 { // 2 chargers
|
||||
t.Errorf("Expeced 2 session received %v session(s)", count)
|
||||
}
|
||||
if err := ses2RPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{
|
||||
Filters: []string{"*string:~Account:1002"},
|
||||
Filters: []string{"*string:~*req.Account:1002"},
|
||||
}, &count); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if count != 0 {
|
||||
|
||||
@@ -196,7 +196,7 @@ func testSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcMstrRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -213,7 +213,7 @@ func testSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcSlvRPC.Call(utils.SessionSv1GetPassiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &pSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -230,7 +230,7 @@ func testSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcMstrRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -245,7 +245,7 @@ func testSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcSlvRPC.Call(utils.SessionSv1GetPassiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &pSessions); err != nil {
|
||||
t.Error(err)
|
||||
|
||||
@@ -811,13 +811,13 @@ func (sS *SessionS) getIndexedFilters(tenant string, fltrs []string) (
|
||||
continue
|
||||
}
|
||||
for _, fltr := range f.Rules {
|
||||
fldName := strings.TrimPrefix(fltr.FieldName, utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep) // remove ~req. prefix
|
||||
if fltr.Type != utils.MetaString ||
|
||||
!sS.cgrCfg.SessionSCfg().SessionIndexes.HasKey(
|
||||
strings.TrimPrefix(fltr.FieldName, utils.DynamicDataPrefix)) {
|
||||
!sS.cgrCfg.SessionSCfg().SessionIndexes.HasKey(fldName) {
|
||||
unindexedFltr = append(unindexedFltr, fltr)
|
||||
continue
|
||||
}
|
||||
indexedFltr[fltr.FieldName] = fltr.Values
|
||||
indexedFltr[fldName] = fltr.Values
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -838,7 +838,7 @@ func (sS *SessionS) getSessionIDsMatchingIndexes(fltrs map[string][]string,
|
||||
checkNr := 0
|
||||
getMatchingIndexes := func(fltrName string, values []string) (matchingSessionsbyValue map[string]utils.StringMap) {
|
||||
matchingSessionsbyValue = make(map[string]utils.StringMap)
|
||||
fltrName = strings.TrimPrefix(fltrName, utils.DynamicDataPrefix)
|
||||
// fltrName = strings.TrimPrefix(fltrName, utils.DynamicDataPrefix+utils.MetaReq+utils.NestingSep) //
|
||||
if _, hasFldName := ssIndx[fltrName]; !hasFldName {
|
||||
return
|
||||
}
|
||||
@@ -907,6 +907,9 @@ func (sS *SessionS) filterSessions(sf *utils.SessionFilter, psv bool) (aSs []*Ex
|
||||
tenant := utils.FirstNonEmpty(sf.Tenant, sS.cgrCfg.GeneralCfg().DefaultTenant)
|
||||
indx, unindx := sS.getIndexedFilters(tenant, sf.Filters)
|
||||
cgrIDs, matchingSRuns := sS.getSessionIDsMatchingIndexes(indx, psv)
|
||||
if len(indx) != 0 && len(cgrIDs) == 0 { // no sessions matched the indexed filters
|
||||
return
|
||||
}
|
||||
ss := sS.getSessionsFromCGRIDs(psv, cgrIDs...)
|
||||
pass := func(filterRules []*engine.FilterRule,
|
||||
me engine.MapEvent) (pass bool) {
|
||||
@@ -915,7 +918,7 @@ func (sS *SessionS) filterSessions(sf *utils.SessionFilter, psv bool) (aSs []*Ex
|
||||
return
|
||||
}
|
||||
var err error
|
||||
ev := config.NewNavigableMap(me)
|
||||
ev := config.NewNavigableMap(map[string]interface{}{utils.MetaReq: me.Data()})
|
||||
for _, fltr := range filterRules {
|
||||
// we don't know how many values we have so we need to build the fieldValues DataProvider
|
||||
fieldValuesDP := make([]config.DataProvider, len(fltr.Values))
|
||||
@@ -964,6 +967,9 @@ func (sS *SessionS) filterSessionsCount(sf *utils.SessionFilter, psv bool) (coun
|
||||
tenant := utils.FirstNonEmpty(sf.Tenant, sS.cgrCfg.GeneralCfg().DefaultTenant)
|
||||
indx, unindx := sS.getIndexedFilters(tenant, sf.Filters)
|
||||
cgrIDs, matchingSRuns := sS.getSessionIDsMatchingIndexes(indx, psv)
|
||||
if len(indx) != 0 && len(cgrIDs) == 0 { // no sessions matched the indexed filters
|
||||
return
|
||||
}
|
||||
ss := sS.getSessionsFromCGRIDs(psv, cgrIDs...)
|
||||
pass := func(filterRules []*engine.FilterRule,
|
||||
me engine.MapEvent) (pass bool) {
|
||||
@@ -972,7 +978,7 @@ func (sS *SessionS) filterSessionsCount(sf *utils.SessionFilter, psv bool) (coun
|
||||
return
|
||||
}
|
||||
var err error
|
||||
ev := config.NewNavigableMap(me)
|
||||
ev := config.NewNavigableMap(map[string]interface{}{utils.MetaReq: me.Data()})
|
||||
for _, fltr := range filterRules {
|
||||
// we don't know how many values we have so we need to build the fieldValues DataProvider
|
||||
fieldValuesDP := make([]config.DataProvider, len(fltr.Values))
|
||||
|
||||
@@ -164,7 +164,7 @@ func sendInit() {
|
||||
func getCount() int {
|
||||
var count int
|
||||
if err := sBenchRPC.Call(utils.SessionSv1GetActiveSessionsCount, utils.SessionFilter{
|
||||
Filters: []string{"*string:~ToR:*voice"},
|
||||
Filters: []string{"*string:~*req.ToR:*voice"},
|
||||
}, &count); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -284,7 +284,7 @@ func TestSessionsItTerminatePassive(t *testing.T) {
|
||||
if err := sItRPC.Call(utils.SessionSv1GetPassiveSessions,
|
||||
&utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123789"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123789"),
|
||||
},
|
||||
}, &pSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -326,7 +326,7 @@ func TestSessionsItTerminatePassive(t *testing.T) {
|
||||
if err := sItRPC.Call(utils.SessionSv1GetPassiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123789"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123789"),
|
||||
},
|
||||
}, &pSessions); err == nil ||
|
||||
err.Error() != utils.ErrNotFound.Error() {
|
||||
|
||||
@@ -144,7 +144,7 @@ func TestSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcMstrRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -159,7 +159,7 @@ func TestSessionSRplInitiate(t *testing.T) {
|
||||
if err := smgRplcSlvRPC.Call(utils.SessionSv1GetPassiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &pSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -208,7 +208,7 @@ func TestSessionSRplUpdate(t *testing.T) {
|
||||
if err := smgRplcSlvRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -229,7 +229,7 @@ func TestSessionSRplUpdate(t *testing.T) {
|
||||
if err := smgRplcMstrRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err == nil ||
|
||||
err.Error() != utils.ErrNotFound.Error() {
|
||||
@@ -282,7 +282,7 @@ func TestSessionSRplTerminate(t *testing.T) {
|
||||
if err := smgRplcMstrRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "123451"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "123451"),
|
||||
},
|
||||
}, &aSessions); err == nil ||
|
||||
err.Error() != utils.ErrNotFound.Error() {
|
||||
|
||||
@@ -19,7 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -1572,11 +1575,11 @@ func TestSessionSGetIndexedFilters(t *testing.T) {
|
||||
expUindx := []*engine.FilterRule{
|
||||
&engine.FilterRule{
|
||||
Type: utils.MetaString,
|
||||
FieldName: utils.DynamicDataPrefix + utils.ToR,
|
||||
FieldName: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.ToR,
|
||||
Values: []string{utils.VOICE},
|
||||
},
|
||||
}
|
||||
fltrs := []string{"*string:~ToR:*voice"}
|
||||
fltrs := []string{"*string:~*req.ToR:*voice"}
|
||||
if rplyindx, rplyUnindx := sS.getIndexedFilters("", fltrs); !reflect.DeepEqual(expIndx, rplyindx) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expIndx), utils.ToJSON(rplyindx))
|
||||
} else if !reflect.DeepEqual(expUindx, rplyUnindx) {
|
||||
@@ -1586,7 +1589,7 @@ func TestSessionSGetIndexedFilters(t *testing.T) {
|
||||
"ToR": true,
|
||||
}
|
||||
sS = NewSessionS(sSCfg, engine.NewDataManager(mpStr, config.CgrConfig().CacheCfg(), nil), nil)
|
||||
expIndx = map[string][]string{(utils.DynamicDataPrefix + utils.ToR): []string{utils.VOICE}}
|
||||
expIndx = map[string][]string{(utils.ToR): []string{utils.VOICE}}
|
||||
expUindx = nil
|
||||
if rplyindx, rplyUnindx := sS.getIndexedFilters("", fltrs); !reflect.DeepEqual(expIndx, rplyindx) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expIndx), utils.ToJSON(rplyindx))
|
||||
@@ -1658,7 +1661,7 @@ func TestSessionSgetSessionIDsMatchingIndexes(t *testing.T) {
|
||||
}
|
||||
cgrID := GetSetCGRID(sEv)
|
||||
sS.indexSession(session, false)
|
||||
indx := map[string][]string{"~ToR": []string{utils.VOICE, utils.DATA}}
|
||||
indx := map[string][]string{"ToR": []string{utils.VOICE, utils.DATA}}
|
||||
expCGRIDs := []string{cgrID}
|
||||
expmatchingSRuns := map[string]utils.StringMap{cgrID: utils.StringMap{
|
||||
"RunID": true,
|
||||
@@ -1675,8 +1678,8 @@ func TestSessionSgetSessionIDsMatchingIndexes(t *testing.T) {
|
||||
sS = NewSessionS(sSCfg, nil, nil)
|
||||
sS.indexSession(session, false)
|
||||
indx = map[string][]string{
|
||||
"~ToR": []string{utils.VOICE, utils.DATA},
|
||||
"~Extra2": []string{"55"},
|
||||
"ToR": []string{utils.VOICE, utils.DATA},
|
||||
"Extra2": []string{"55"},
|
||||
}
|
||||
expCGRIDs = []string{}
|
||||
expmatchingSRuns = map[string]utils.StringMap{}
|
||||
@@ -1709,8 +1712,8 @@ func TestSessionSgetSessionIDsMatchingIndexes(t *testing.T) {
|
||||
sS = NewSessionS(sSCfg, nil, nil)
|
||||
sS.indexSession(session, true)
|
||||
indx = map[string][]string{
|
||||
"~ToR": []string{utils.VOICE, utils.DATA},
|
||||
"~Extra2": []string{"5"},
|
||||
"ToR": []string{utils.VOICE, utils.DATA},
|
||||
"Extra2": []string{"5"},
|
||||
}
|
||||
|
||||
expCGRIDs = []string{cgrID}
|
||||
@@ -1722,7 +1725,6 @@ func TestSessionSgetSessionIDsMatchingIndexes(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(expmatchingSRuns, matchingSRuns) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expmatchingSRuns), utils.ToJSON(matchingSRuns))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type testRPCClientConnection struct{}
|
||||
@@ -1947,3 +1949,281 @@ func TestSessionSgetSession(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSessionSfilterSessions(t *testing.T) {
|
||||
sSCfg, _ := config.NewDefaultCGRConfig()
|
||||
sSCfg.SessionSCfg().SessionIndexes = utils.StringMap{
|
||||
"ToR": true,
|
||||
}
|
||||
sS := NewSessionS(sSCfg, nil, nil)
|
||||
sEv := engine.NewMapEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "12345",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.DISCONNECT_CAUSE: "NORMAL_DISCONNECT",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
"Extra1": "Value1",
|
||||
"Extra2": 5,
|
||||
"Extra3": "",
|
||||
})
|
||||
sr2 := sEv.Clone()
|
||||
// Index first session
|
||||
session := &Session{
|
||||
CGRID: GetSetCGRID(sEv),
|
||||
EventStart: sEv,
|
||||
SRuns: []*SRun{
|
||||
&SRun{
|
||||
Event: sEv,
|
||||
CD: &engine.CallDescriptor{
|
||||
RunID: "RunID",
|
||||
},
|
||||
},
|
||||
&SRun{
|
||||
Event: sr2,
|
||||
CD: &engine.CallDescriptor{
|
||||
RunID: "RunID2",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sr2[utils.ToR] = utils.SMS
|
||||
sr2[utils.Subject] = "subject2"
|
||||
sr2[utils.CGRID] = GetSetCGRID(sEv)
|
||||
sS.registerSession(session, false)
|
||||
st, err := utils.IfaceAsTime("2015-11-09T14:21:24+02:00", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
at, err := utils.IfaceAsTime("2015-11-09T14:22:02+02:00", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eses1 := &ExternalSession{
|
||||
CGRID: "cade401f46f046311ed7f62df3dfbb84adb98aad",
|
||||
ToR: "*voice",
|
||||
OriginID: "12345",
|
||||
OriginHost: "127.0.0.1",
|
||||
Source: "SessionS_TEST_EVENT",
|
||||
RequestType: "*prepaid",
|
||||
Category: "call",
|
||||
Account: "account1",
|
||||
Subject: "subject1",
|
||||
Destination: "+4986517174963",
|
||||
SetupTime: st,
|
||||
AnswerTime: at,
|
||||
ExtraFields: map[string]string{
|
||||
"Direction": "*out",
|
||||
"DisconnectCause": "NORMAL_DISCONNECT",
|
||||
"EventName": "TEST_EVENT",
|
||||
"Extra1": "Value1",
|
||||
"Extra2": "5",
|
||||
"Extra3": "",
|
||||
"LastUsed": "21s",
|
||||
"PDD": "300ms",
|
||||
"Supplier": "supplier1",
|
||||
},
|
||||
NodeID: sSCfg.GeneralCfg().NodeID,
|
||||
}
|
||||
eses2 := &ExternalSession{
|
||||
CGRID: "cade401f46f046311ed7f62df3dfbb84adb98aad",
|
||||
ToR: utils.SMS,
|
||||
OriginID: "12345",
|
||||
OriginHost: "127.0.0.1",
|
||||
Source: "SessionS_TEST_EVENT",
|
||||
RequestType: "*prepaid",
|
||||
Category: "call",
|
||||
Account: "account1",
|
||||
Subject: "subject2",
|
||||
Destination: "+4986517174963",
|
||||
SetupTime: st,
|
||||
AnswerTime: at,
|
||||
ExtraFields: map[string]string{
|
||||
"Direction": "*out",
|
||||
"DisconnectCause": "NORMAL_DISCONNECT",
|
||||
"EventName": "TEST_EVENT",
|
||||
"Extra1": "Value1",
|
||||
"Extra2": "5",
|
||||
"Extra3": "",
|
||||
"LastUsed": "21s",
|
||||
"PDD": "300ms",
|
||||
"Supplier": "supplier1",
|
||||
},
|
||||
NodeID: sSCfg.GeneralCfg().NodeID,
|
||||
}
|
||||
expSess := []*ExternalSession{
|
||||
eses1,
|
||||
}
|
||||
fltrs := &utils.SessionFilter{Filters: []string{fmt.Sprintf("*string:~*req.ToR:%s;%s", utils.VOICE, utils.DATA)}}
|
||||
if sess := sS.filterSessions(fltrs, true); len(sess) != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(sess))
|
||||
}
|
||||
if sess := sS.filterSessions(fltrs, false); !reflect.DeepEqual(expSess, sess) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expSess), utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:NoToR", "*string:~*req.Subject:subject1"}}
|
||||
if sess := sS.filterSessions(fltrs, false); len(sess) != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:NoToR"}}
|
||||
if sess := sS.filterSessions(fltrs, false); len(sess) != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:*voice", "*string:~*req.Subject:subject1"}}
|
||||
if sess := sS.filterSessions(fltrs, false); !reflect.DeepEqual(expSess, sess) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expSess), utils.ToJSON(sess))
|
||||
}
|
||||
sSCfg.SessionSCfg().SessionIndexes = utils.StringMap{
|
||||
"ToR": true,
|
||||
"Extra3": true,
|
||||
}
|
||||
sS = NewSessionS(sSCfg, nil, nil)
|
||||
sS.registerSession(session, false)
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:*voice", "*string:~*req.Subject:subject1"}}
|
||||
if sess := sS.filterSessions(fltrs, false); !reflect.DeepEqual(expSess, sess) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expSess), utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.Subject:subject1"}}
|
||||
if sess := sS.filterSessions(fltrs, false); !reflect.DeepEqual(expSess, sess) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expSess), utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.Subject:subject3"}}
|
||||
if sess := sS.filterSessions(fltrs, false); len(sess) != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(sess))
|
||||
}
|
||||
expSess = append(expSess, eses2)
|
||||
sort.Slice(expSess, func(i, j int) bool {
|
||||
return strings.Compare(expSess[i].ToR, expSess[j].ToR) == -1
|
||||
})
|
||||
fltrs = &utils.SessionFilter{Filters: []string{}}
|
||||
sess := sS.filterSessions(fltrs, false)
|
||||
sort.Slice(sess, func(i, j int) bool {
|
||||
return strings.Compare(sess[i].ToR, sess[j].ToR) == -1
|
||||
})
|
||||
if !reflect.DeepEqual(expSess, sess) {
|
||||
t.Errorf("Expected %s , received: %s", utils.ToJSON(expSess), utils.ToJSON(sess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{}, Limit: utils.IntPointer(1)}
|
||||
if sess := sS.filterSessions(fltrs, false); len(sess) != 1 {
|
||||
t.Errorf("Expected one session, received: %s", utils.ToJSON(sess))
|
||||
} else if !reflect.DeepEqual(expSess[0], eses1) && !reflect.DeepEqual(expSess[0], eses2) {
|
||||
t.Errorf("Expected %s or %s, received: %s", utils.ToJSON(eses1), utils.ToJSON(eses2), utils.ToJSON(sess[0]))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{fmt.Sprintf("*string:~*req.ToR:%s;%s", utils.VOICE, utils.SMS)}, Limit: utils.IntPointer(1)}
|
||||
if sess := sS.filterSessions(fltrs, false); len(sess) != 1 {
|
||||
t.Errorf("Expected one session, received: %s", utils.ToJSON(sess))
|
||||
} else if !reflect.DeepEqual(expSess[0], eses1) && !reflect.DeepEqual(expSess[0], eses2) {
|
||||
t.Errorf("Expected %s or %s, received: %s", utils.ToJSON(eses1), utils.ToJSON(eses2), utils.ToJSON(sess[0]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionSfilterSessionsCount(t *testing.T) {
|
||||
sSCfg, _ := config.NewDefaultCGRConfig()
|
||||
sSCfg.SessionSCfg().SessionIndexes = utils.StringMap{
|
||||
"ToR": true,
|
||||
}
|
||||
sS := NewSessionS(sSCfg, nil, nil)
|
||||
sEv := engine.NewMapEvent(map[string]interface{}{
|
||||
utils.EVENT_NAME: "TEST_EVENT",
|
||||
utils.ToR: "*voice",
|
||||
utils.OriginID: "12345",
|
||||
utils.Direction: "*out",
|
||||
utils.Account: "account1",
|
||||
utils.Subject: "subject1",
|
||||
utils.Destination: "+4986517174963",
|
||||
utils.Category: "call",
|
||||
utils.Tenant: "cgrates.org",
|
||||
utils.RequestType: "*prepaid",
|
||||
utils.SetupTime: "2015-11-09 14:21:24",
|
||||
utils.AnswerTime: "2015-11-09 14:22:02",
|
||||
utils.Usage: "1m23s",
|
||||
utils.LastUsed: "21s",
|
||||
utils.PDD: "300ms",
|
||||
utils.SUPPLIER: "supplier1",
|
||||
utils.DISCONNECT_CAUSE: "NORMAL_DISCONNECT",
|
||||
utils.OriginHost: "127.0.0.1",
|
||||
"Extra1": "Value1",
|
||||
"Extra2": 5,
|
||||
"Extra3": "",
|
||||
})
|
||||
sr2 := sEv.Clone()
|
||||
// Index first session
|
||||
session := &Session{
|
||||
CGRID: GetSetCGRID(sEv),
|
||||
EventStart: sEv,
|
||||
SRuns: []*SRun{
|
||||
&SRun{
|
||||
Event: sEv,
|
||||
CD: &engine.CallDescriptor{
|
||||
RunID: "RunID",
|
||||
},
|
||||
},
|
||||
&SRun{
|
||||
Event: sr2,
|
||||
CD: &engine.CallDescriptor{
|
||||
RunID: "RunID2",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sEv[utils.ToR] = utils.DATA
|
||||
sr2[utils.CGRID] = GetSetCGRID(sEv)
|
||||
sS.registerSession(session, false)
|
||||
fltrs := &utils.SessionFilter{Filters: []string{fmt.Sprintf("*string:~*req.ToR:%s;%s", utils.VOICE, utils.DATA)}}
|
||||
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 2 {
|
||||
t.Errorf("Expected %v , received: %s", 2, utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:NoToR", "*string:~*req.Subject:subject1"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:NoToR"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:*voice", "*string:~*req.Subject:subject1"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 1 {
|
||||
t.Errorf("Expected %v , received: %s", 1, utils.ToJSON(noSess))
|
||||
}
|
||||
sSCfg.SessionSCfg().SessionIndexes = utils.StringMap{
|
||||
"ToR": true,
|
||||
"Extra3": true,
|
||||
}
|
||||
sS = NewSessionS(sSCfg, nil, nil)
|
||||
sS.registerSession(session, false)
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.ToR:*voice", "*string:~*req.Subject:subject1"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 1 {
|
||||
t.Errorf("Expected %v , received: %s", 1, utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.Subject:subject1"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 2 {
|
||||
t.Errorf("Expected %v , received: %s", 2, utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{"*string:~*req.Subject:subject2"}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 0 {
|
||||
t.Errorf("Expected no session, received: %s", utils.ToJSON(noSess))
|
||||
}
|
||||
fltrs = &utils.SessionFilter{Filters: []string{}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, false); noSess != 2 {
|
||||
t.Errorf("Expected %v , received: %s", 2, utils.ToJSON(noSess))
|
||||
}
|
||||
sS = NewSessionS(sSCfg, nil, nil)
|
||||
sS.registerSession(session, true)
|
||||
fltrs = &utils.SessionFilter{Filters: []string{fmt.Sprintf("*string:~*req.ToR:%s;%s", utils.VOICE, utils.DATA)}}
|
||||
if noSess := sS.filterSessionsCount(fltrs, true); noSess != 2 {
|
||||
t.Errorf("Expected %v , received: %s", 2, utils.ToJSON(noSess))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -814,8 +814,8 @@ func TestSessionsVoiceSessionTTL(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12372-1"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12372-1"),
|
||||
"*string:~no_field:10",
|
||||
},
|
||||
}, &aSessions); err == nil ||
|
||||
@@ -825,8 +825,8 @@ func TestSessionsVoiceSessionTTL(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12360"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12360"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -878,8 +878,8 @@ func TestSessionsVoiceSessionTTL(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12360"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12360"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -986,8 +986,8 @@ func TestSessionsVoiceSessionTTLWithRelocate(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12361"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12361"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -1043,8 +1043,8 @@ func TestSessionsVoiceSessionTTLWithRelocate(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12362"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12362"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -1072,8 +1072,8 @@ func TestSessionsVoiceSessionTTLWithRelocate(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12362"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12362"),
|
||||
},
|
||||
}, &aSessions); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err, utils.ToJSON(aSessions))
|
||||
@@ -1159,8 +1159,8 @@ func TestSessionsVoiceRelocateWithOriginIDPrefix(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12371"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12371"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -1216,8 +1216,8 @@ func TestSessionsVoiceRelocateWithOriginIDPrefix(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12372-1"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12372-1"),
|
||||
},
|
||||
}, &aSessions); err != nil {
|
||||
t.Error(err)
|
||||
@@ -1266,8 +1266,8 @@ func TestSessionsVoiceRelocateWithOriginIDPrefix(t *testing.T) {
|
||||
if err := sessionsRPC.Call(utils.SessionSv1GetActiveSessions,
|
||||
utils.SessionFilter{
|
||||
Filters: []string{
|
||||
fmt.Sprintf("*string:~%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~%s:%s", utils.OriginID, "12372-1"),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.RunID, utils.MetaDefault),
|
||||
fmt.Sprintf("*string:~*req.%s:%s", utils.OriginID, "12372-1"),
|
||||
},
|
||||
}, &aSessions); err == nil ||
|
||||
err.Error() != utils.ErrNotFound.Error() {
|
||||
|
||||
Reference in New Issue
Block a user