Skip to content

Commit

Permalink
fix(finder): fix seriesByTag arg t=~^q (go-graphite#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf1980 authored Nov 9, 2023
1 parent d38ebe6 commit 2935529
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 4 additions & 0 deletions finder/tagged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func TestTaggedWhere(t *testing.T) {
{"seriesByTag('name=~^cpu|mem')", 0, "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem')", false},
{"seriesByTag('name=~^cpu|mem$')", 0, "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem$')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem$')", false},
{"seriesByTag('name=rps', 'key=~value')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=%' AND match(x, '^key=.*value'), Tags))", "", false},
// test for issue #244
{"seriesByTag('name=rps', 'key=~^value')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=value%' AND match(x, '^key=value'), Tags))", "", false},
{"seriesByTag('name=rps', 'key=~^value.*')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=value%' AND match(x, '^key=value.*'), Tags))", "", false},
{"seriesByTag('name=rps', 'key=~^valu[a-e]')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=valu%' AND match(x, '^key=valu[a-e]'), Tags))", "", false},
{"seriesByTag('name=rps', 'key=~^value$')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x='key=value', Tags))", "", false},
{"seriesByTag('name=rps', 'key=~hello.world')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=%' AND match(x, '^key=.*hello.world'), Tags))", "", false},
{`seriesByTag('cpu=cpu-total','host=~Vladimirs-MacBook-Pro\.local')`, 0, `(Tag1='cpu=cpu-total') AND (arrayExists((x) -> x LIKE 'host=%' AND match(x, '^host=.*Vladimirs-MacBook-Pro\\.local'), Tags))`, "", false},
Expand Down
6 changes: 3 additions & 3 deletions pkg/where/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ func TreeGlob(field string, query string) string {
func ConcatMatchKV(key, value string) string {
startLine := value[0] == '^'
endLine := value[len(value)-1] == '$'
if startLine {
if startLine && endLine {
return key + opEq + value[1:]
} else if endLine {
return key + opEq + value + "\\\\%"
} else if startLine {
return key + opEq + value[1:] + "\\\\%"
}
return key + opEq + "\\\\%" + value
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/where/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,8 @@ func quote(value interface{}) string {

func quoteRegex(key, value string) string {
startLine := value[0] == '^'
endLine := value[len(value)-1] == '$'
if startLine && endLine {
if startLine {
return fmt.Sprintf("'^%s%s%s'", key, opEq, escape(value[1:]))
} else if startLine {
return fmt.Sprintf("'^%s%s%s'", key, opEq, escape(value[1:]))
} else if endLine {
return fmt.Sprintf("'^%s%s.*%s'", key, opEq, escape(value[0:]))
}
return fmt.Sprintf("'^%s%s.*%s'", key, opEq, escape(value))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/where/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestNonRegexpPrefix(t *testing.T) {
{`__name__=cpu.load`, `__name__=cpu`},
{`__name__=~(cpu|mem)`, `__name__=~`},
{`__name__=~cpu|mem`, `__name__=~`},
{`__name__=~^host`, `__name__=~`},
}

for _, test := range table {
Expand Down

0 comments on commit 2935529

Please sign in to comment.