Added *opts to StringQuery API when filtering over content

This commit is contained in:
Trial97
2020-11-17 11:38:07 +02:00
committed by Dan Christian Bogos
parent 326c86f583
commit b90ff99846
5 changed files with 40 additions and 14 deletions

View File

@@ -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

View File

@@ -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{}{}

View File

@@ -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
}

View File

@@ -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) {