diff --git a/analyzers/analyzers.go b/analyzers/analyzers.go index 89255a0e0..19714f9f1 100755 --- a/analyzers/analyzers.go +++ b/analyzers/analyzers.go @@ -57,7 +57,7 @@ type AnalyzerService struct { } func (aS *AnalyzerService) initDB() (err error) { - dbPath := path.Join(aS.cfg.AnalyzerSCfg().DBPath, "db") + dbPath := path.Join(aS.cfg.AnalyzerSCfg().DBPath, utils.AnzDBDir) if _, err = os.Stat(dbPath); err == nil { aS.db, err = bleve.Open(dbPath) } else if os.IsNotExist(err) { @@ -166,20 +166,12 @@ func (aS *AnalyzerService) V1StringQuery(args *QueryArgs, reply *[]map[string]in obj.Fields[utils.ReplyError] = nil } if lCntFltrs != 0 { - repDP, err := unmarshalJSON(rep) - if err != nil { - return err - } - reqDP, err := unmarshalJSON(req) + dp, err := getDPFromSearchresult(req, rep, obj.Fields) if err != nil { return err } if pass, err := aS.filterS.Pass(aS.cfg.GeneralCfg().DefaultTenant, - args.ContentFilters, utils.MapStorage{ - utils.MetaReq: reqDP, - utils.MetaRep: repDP, - utils.MetaHdr: utils.MapStorage(obj.Fields), - }); err != nil { + args.ContentFilters, dp); err != nil { return err } else if !pass { continue diff --git a/analyzers/analyzers_test.go b/analyzers/analyzers_test.go index 8f9736fa2..70c30cdb8 100644 --- a/analyzers/analyzers_test.go +++ b/analyzers/analyzers_test.go @@ -318,6 +318,7 @@ func TestAnalyzersV1Search(t *testing.T) { } else if !reflect.DeepEqual(expRply, reply) { t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply)) } + reply = []map[string]interface{}{} if err = anz.V1StringQuery(&QueryArgs{ HeaderFilters: "RequestEncoding:*gob", ContentFilters: []string{"*gt:~*hdr.RequestDuration:1m"}, @@ -326,6 +327,15 @@ func TestAnalyzersV1Search(t *testing.T) { } else if !reflect.DeepEqual(expRply, reply) { t.Errorf("Expected %s received: %s", utils.ToJSON(expRply), utils.ToJSON(reply)) } + reply = []map[string]interface{}{} + if err = anz.V1StringQuery(&QueryArgs{ + HeaderFilters: "RequestEncoding:*gob", + ContentFilters: []string{"*string:~*opts.EventSource:*attributes"}, + }, &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{}{} diff --git a/analyzers/libanalyzers.go b/analyzers/libanalyzers.go index fd0d558e4..143476936 100644 --- a/analyzers/libanalyzers.go +++ b/analyzers/libanalyzers.go @@ -102,13 +102,12 @@ func getIndex(indx string) (indxType, storeType string) { // unmarshalJSON will transform the message in a map[string]interface{} of []interface{} // depending of the first character -// used for filters purposes so the nil is replaced with empty map func unmarshalJSON(jsn json.RawMessage) (interface{}, error) { switch { case string(jsn) == "null" || len(jsn) == 0: // nil or empty response // by default consider nil as an empty map for filtering purposes - return map[string]interface{}{}, nil + return nil, nil case string(jsn) == "true": // booleans return true, nil case string(jsn) == "false": @@ -129,3 +128,26 @@ func unmarshalJSON(jsn json.RawMessage) (interface{}, error) { return nil, new(json.SyntaxError) } } + +// getDPFromSearchresult will unmarshal the request and reply and populate a DataProvider +// if the req is a map[string]interface{} we will try to put in *opts prefix the Opts field from req +func getDPFromSearchresult(req, rep json.RawMessage, hdr utils.MapStorage) (utils.MapStorage, error) { + repDP, err := unmarshalJSON(rep) + if err != nil { + return nil, err + } + reqDP, err := unmarshalJSON(req) + if err != nil { + return nil, err + } + var opts interface{} + if reqMp, canCast := reqDP.(map[string]interface{}); canCast { + opts = reqMp[utils.Opts] + } + return utils.MapStorage{ + utils.MetaReq: reqDP, + utils.MetaOpts: opts, + utils.MetaRep: repDP, + utils.MetaHdr: hdr, + }, nil +} diff --git a/analyzers/libanalyzers_test.go b/analyzers/libanalyzers_test.go index 7d6cac4ee..d1c4f6392 100644 --- a/analyzers/libanalyzers_test.go +++ b/analyzers/libanalyzers_test.go @@ -144,7 +144,7 @@ func TestUnmarshalJSON(t *testing.T) { t.Errorf("Expected: %s,received %s", utils.ToJSON(exp), utils.ToJSON(val)) } - exp = map[string]interface{}{} + exp = nil if val, err := unmarshalJSON(json.RawMessage(`null`)); err != nil { t.Error(err) } else if !reflect.DeepEqual(val, exp) { diff --git a/utils/consts.go b/utils/consts.go index 23d16a02b..d64371fb6 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -2516,6 +2516,8 @@ const ( RequestParams = "RequestParams" Reply = "Reply" ReplyError = "ReplyError" + AnzDBDir = "db" + Opts = "Opts" ) var (