mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-12 03:29:02 +08:00
parser updated to include or as well
This commit is contained in:
parent
5b28fe1c9d
commit
6eb9389e81
@ -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) {
|
||||
@ -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, "")
|
||||
|
@ -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"'`,
|
||||
@ -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)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user