Skip to content

Commit

Permalink
parser updated to include or as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandagohain committed Aug 1, 2022
1 parent 5b28fe1 commit 6eb9389
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 18 additions & 9 deletions pkg/query-service/app/logs/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
IDEND = "idEnd"
)

var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?)?(([\w.-]+ (in|nin) \([\S ]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) (')?[^']+(')?)`)
var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+ (in|nin) \([\S ]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) (')?[^']+(')?)`)
var operatorRegex, _ = regexp.Compile(`(?i)(?: )(in|nin|gt|lt|gte|lte|contains|ncontains)(?: )`)

func ParseLogFilterParams(r *http.Request) (*model.LogsFilterParams, error) {
Expand Down Expand Up @@ -259,33 +259,42 @@ func GenerateSQLWhere(allFields *model.GetFieldsResponse, params *model.LogsFilt
return sqlWhere, err
}

filterTokens := []string{}
if params.TimestampStart != 0 {
filter := fmt.Sprintf("timestamp >= '%d' ", params.TimestampStart)
if len(tokens) > 0 {
if len(filterTokens) > 0 {
filter = "and " + filter
}
tokens = append(tokens, filter)
filterTokens = append(filterTokens, filter)
}
if params.TimestampEnd != 0 {
filter := fmt.Sprintf("timestamp <= '%d' ", params.TimestampEnd)
if len(tokens) > 0 {
if len(filterTokens) > 0 {
filter = "and " + filter
}
tokens = append(tokens, filter)
filterTokens = append(filterTokens, filter)
}
if params.IdStart != "" {
filter := fmt.Sprintf("id > '%v' ", params.IdStart)
if len(tokens) > 0 {
if len(filterTokens) > 0 {
filter = "and " + filter
}
tokens = append(tokens, filter)
filterTokens = append(filterTokens, filter)
}
if params.IdEnd != "" {
filter := fmt.Sprintf("id < '%v' ", params.IdEnd)
if len(tokens) > 0 {
if len(filterTokens) > 0 {
filter = "and " + filter
}
tokens = append(tokens, filter)
filterTokens = append(filterTokens, filter)
}

if len(filterTokens) > 0 {
if len(tokens) > 0 {
tokens[0] = fmt.Sprintf("and %s", tokens[0])
}
filterTokens = append(filterTokens, tokens...)
tokens = filterTokens
}

sqlWhere = strings.Join(tokens, "")
Expand Down
9 changes: 7 additions & 2 deletions pkg/query-service/app/logs/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ var correctQueriesTest = []struct {
`id lt 100 and id gt 50 and code lte 500 and code gte 400`,
[]string{`id < 100 `, `and id > 50 `, `and code <= 500 `, `and code >= 400 `},
},
{
`filters with lt,gt,lte,gte operators seprated by OR`,
`id lt 100 or id gt 50 or code lte 500 or code gte 400`,
[]string{`id < 100 `, `or id > 50 `, `or code <= 500 `, `or code >= 400 `},
},
{
`filter with number`,
`status gte 200 AND FULLTEXT ncontains '"key"'`,
Expand Down Expand Up @@ -216,8 +221,8 @@ func TestGenerateSQLQuery(t *testing.T) {
tsEnd := uint64(1657689294000)
idStart := "2BsKLKv8cZrLCn6rkOcRGkdjBdM"
idEnd := "2BsKG6tRpFWjYMcWsAGKfSxoQdU"
sqlWhere := "id < 100 and id > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 and timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' "
Convey("testInterestingFields", t, func() {
sqlWhere := "timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' and id < 100 and id > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 "
Convey("testGenerateSQL", t, func() {
res, _ := GenerateSQLWhere(&allFields, &model.LogsFilterParams{Query: query, TimestampStart: tsStart, TimestampEnd: tsEnd, IdStart: idStart, IdEnd: idEnd})
So(res, ShouldEqual, sqlWhere)
})
Expand Down

0 comments on commit 6eb9389

Please sign in to comment.