Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/filter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Filter 调试器

在编写各种[过滤规则](https://docs.guance.com/datakit/datakit-filter/)时,
普通用户难以确定规则是否编写正确,这给调试具体的数据策略造成一些麻烦。

通过 filter 调试器,可以得知具体的 filter 规则是否编写正确,对于语法错误
的规则,会提示具体的错误位置。

编译:

```bash
$ ./build.sh
```

```bash
# 正确的 filter 规则示例
$ ./fdbg -condition-path filter-sample.txt
Parse 3 conditions ok


# 错误的 filter 规则示例:有错误位置提示
$ ./fdbg -condition-path filter-bad-sample.txt
2023/10/28 10:26:33 GetConds: 1:46 parse error: unterminated quoted string
```
1 change: 1 addition & 0 deletions cmd/filter/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go build -o fdbg main.go
1 change: 1 addition & 0 deletions cmd/filter/filter-bad-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ `service` = prd-oa-main-service-sql-server' }
3 changes: 3 additions & 0 deletions cmd/filter/filter-sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ `source` = 'manager' and ( `llllll` in [ 'lllllll' ] )};
{ `source` = 'worker-0' and ( `jinei` in [ 'ss' ] )};
{ `source` = re(`.*`) and ( `host` in [ 'cn-hangzhou.172.16.211.112' ] )};
38 changes: 38 additions & 0 deletions cmd/filter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"

"github.com/GuanceCloud/cliutils/filter"
)

var (
conditionFile string
)

// nolint: gochecknoinits
func init() {
flag.StringVar(&conditionFile, "condition-path", "", "condition file")
}

func main() {
flag.Parse()

condData, err := os.ReadFile(filepath.Clean(conditionFile))
if err != nil {
log.Printf("ReadFile: %s", err)
os.Exit(-1)
}

where, err := filter.GetConds(string(condData))
if err != nil {
log.Printf("GetConds: %s", err)
os.Exit(-1)
}

fmt.Printf("Parse %d conditions ok\n", len(where))
}
2 changes: 1 addition & 1 deletion filter/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func (x WhereConditions) String() string {

arr = append(arr, c.String())
}
return strings.Join(arr, "; ")
return strings.Join(arr, ";\n")
}

func (x WhereConditions) Eval(data KVs) int {
Expand Down
2 changes: 0 additions & 2 deletions filter/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ type parser struct {
}

func GetConds(input string) (WhereConditions, error) {
log.Debugf("parse %s", input)

var err error
p := newParser(input)
defer parserPool.Put(p)
Expand Down