Updated analyzers V1StringQuery

This commit is contained in:
Trial97
2021-07-16 08:54:01 +03:00
committed by Dan Christian Bogos
parent 4555789d53
commit 9bf6b0218d
2 changed files with 49 additions and 3 deletions

View File

@@ -29,6 +29,7 @@ import (
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/query"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
@@ -140,7 +141,13 @@ type QueryArgs struct {
// V1StringQuery returns a list of API that match the query
func (aS *AnalyzerService) V1StringQuery(args *QueryArgs, reply *[]map[string]interface{}) error {
s := bleve.NewSearchRequest(bleve.NewQueryStringQuery(args.HeaderFilters))
var q query.Query
if args.HeaderFilters == utils.EmptyString {
q = bleve.NewMatchAllQuery()
} else {
q = bleve.NewQueryStringQuery(args.HeaderFilters)
}
s := bleve.NewSearchRequest(q)
s.Fields = []string{utils.Meta} // return all fields
searchResults, err := aS.db.Search(s)
if err != nil {

View File

@@ -32,6 +32,7 @@ import (
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
)
func TestNewAnalyzerService(t *testing.T) {
@@ -242,14 +243,25 @@ func TestAnalyzersV1Search(t *testing.T) {
t1.Add(-24*time.Hour), t1.Add(-23*time.Hour)); err != nil {
t.Fatal(err)
}
if err = anz.logTrafic(3, utils.CoreSv1Status,
&utils.CGREvent{
APIOpts: map[string]interface{}{
utils.EventSource: utils.MetaEEs,
},
}, utils.Pong, nil,
rpcclient.BiRPCJSON, "127.0.0.1:5566", "127.0.0.1:2013",
t1.Add(-11*time.Hour), t1.Add(-10*time.Hour-30*time.Minute)); err != nil {
t.Fatal(err)
}
reply := []map[string]interface{}{}
if err = anz.V1StringQuery(&QueryArgs{HeaderFilters: utils.CoreSv1Ping}, &reply); err != nil {
if err = anz.V1StringQuery(&QueryArgs{HeaderFilters: `"` + utils.CoreSv1Ping + `"`}, &reply); err != nil {
t.Fatal(err)
} else if len(reply) != 4 {
t.Errorf("Expected 4 hits received: %v", len(reply))
}
reply = []map[string]interface{}{}
if err = anz.V1StringQuery(&QueryArgs{HeaderFilters: "RequestMethod:" + utils.CoreSv1Ping}, &reply); err != nil {
if err = anz.V1StringQuery(&QueryArgs{HeaderFilters: "RequestMethod:" + `"` + utils.CoreSv1Ping + `"`}, &reply); err != nil {
t.Fatal(err)
} else if len(reply) != 4 {
t.Errorf("Expected 4 hits received: %v", len(reply))
@@ -323,6 +335,26 @@ func TestAnalyzersV1Search(t *testing.T) {
} else if !reflect.DeepEqual(expRply, reply) {
t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply))
}
expRply = []map[string]interface{}{{
"RequestDestination": "127.0.0.1:2013",
"RequestDuration": "30m0s",
"RequestEncoding": "*birpc_json",
"RequestID": 3.,
"RequestMethod": "CoreSv1.Status",
"RequestParams": json.RawMessage(`{"Tenant":"","ID":"","Time":null,"Event":null,"APIOpts":{"EventSource":"*ees"}}`),
"Reply": json.RawMessage(`"Pong"`),
"RequestSource": "127.0.0.1:5566",
"RequestStartTime": t1.Add(-11 * time.Hour).UTC().Format(time.RFC3339),
"ReplyError": nil,
}}
reply = []map[string]interface{}{}
if err = anz.V1StringQuery(&QueryArgs{
ContentFilters: []string{"*string:~*req.APIOpts.EventSource:*ees"},
}, &reply); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(expRply, reply) {
t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply))
}
expRply = []map[string]interface{}{}
reply = []map[string]interface{}{}
@@ -342,6 +374,13 @@ func TestAnalyzersV1Search(t *testing.T) {
} else if !reflect.DeepEqual(expRply, reply) {
t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply))
}
if err = anz.V1StringQuery(&QueryArgs{
ContentFilters: []string{"*string:~*req.APIOpts.EventSource:*sessions"},
}, &reply); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(expRply, reply) {
t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply))
}
expErr := utils.ErrPrefixNotErrNotImplemented("*type")
if err = anz.V1StringQuery(&QueryArgs{