From d2889de9bceb04452d3f583c781b36a4c0664023 Mon Sep 17 00:00:00 2001 From: Jmnote Date: Wed, 31 May 2023 16:39:26 +0900 Subject: [PATCH] misspell & gocyclo (#82) * misspell action * misspell tool & correction ok * add misspell to checks.sh * gocyclo --- .github/workflows/pull-request.yml | 12 ++++ Makefile | 6 ++ hack/checks.sh | 1 + hack/gocyclo.sh | 11 +++ hack/misspell.sh | 11 +++ letheql/engine.go | 104 +---------------------------- storage/logservice/match/label.go | 2 +- 7 files changed, 43 insertions(+), 104 deletions(-) create mode 100755 hack/gocyclo.sh create mode 100755 hack/misspell.sh diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 13a5793..42b4a43 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -17,6 +17,18 @@ jobs: gofmt-path: '.' gofmt-flags: '-l -d' + misspell: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: make misspell + + gocyclo: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + - run: make gocyclo goimports: runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index a0fa469..20f6815 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,12 @@ test-win: cover: hack/test-cover.sh +misspell: + hack/misspell.sh + +gocyclo: + hack/gocyclo.sh + checks: hack/checks.sh diff --git a/hack/checks.sh b/hack/checks.sh index 82b5b06..b22fdb1 100755 --- a/hack/checks.sh +++ b/hack/checks.sh @@ -4,6 +4,7 @@ cd $(dirname $0)/.. set -xeuo pipefail go mod tidy go fmt ./... +./hack/misspell.sh go vet ./... which goimports || go install golang.org/x/tools/cmd/goimports@latest goimports -local -v -w . diff --git a/hack/gocyclo.sh b/hack/gocyclo.sh new file mode 100755 index 0000000..64fb191 --- /dev/null +++ b/hack/gocyclo.sh @@ -0,0 +1,11 @@ +#!/bin/bash +cd $(dirname $0)/../ + +which gocyclo || go install github.com/fzipp/gocyclo/cmd/gocyclo@latest + +gocyclo -over 15 -ignore letheql/parser . +if [[ $? != 0 ]]; then + echo "❌ FAIL" + exit 1 +fi +echo "✔️ OK" diff --git a/hack/misspell.sh b/hack/misspell.sh new file mode 100755 index 0000000..a506f4f --- /dev/null +++ b/hack/misspell.sh @@ -0,0 +1,11 @@ +#!/bin/bash +cd $(dirname $0)/.. + +[ -f ./bin/misspell ] || curl -L https://git.io/misspell | bash + +find . -type f -name '*.*' | xargs ./bin/misspell -error +if [[ $? != 0 ]]; then + echo "❌ FAIL - misspell found" + exit 1 +fi +echo "✔️ OK - misspell not found" diff --git a/letheql/engine.go b/letheql/engine.go index e7cff4e..920be4c 100644 --- a/letheql/engine.go +++ b/letheql/engine.go @@ -54,7 +54,7 @@ func (ng *Engine) NewRangeQuery(_ context.Context, q storage.Queryable, qs strin func (ng *Engine) newQuery(q storage.Queryable, expr parser.Expr, start, end time.Time, interval time.Duration) (*query, error) { es := &parser.EvalStmt{ - Expr: PreprocessExpr(expr, start, end), + Expr: expr, Start: start, End: end, Interval: interval, @@ -196,108 +196,6 @@ func contextErr(err error, env string) error { } } -func PreprocessExpr(expr parser.Expr, start, end time.Time) parser.Expr { - isStepInvariant := preprocessExprHelper(expr, start, end) - if isStepInvariant { - return newStepInvariantExpr(expr) - } - return expr -} - -func preprocessExprHelper(expr parser.Expr, start, end time.Time) bool { - switch n := expr.(type) { - case *parser.VectorSelector: - switch n.StartOrEnd { - case parser.START: - n.Timestamp = makeInt64Pointer(timestamp.FromTime(start)) - case parser.END: - n.Timestamp = makeInt64Pointer(timestamp.FromTime(end)) - } - return n.Timestamp != nil - - case *parser.AggregateExpr: - return preprocessExprHelper(n.Expr, start, end) - - case *parser.BinaryExpr: - isInvariant1, isInvariant2 := preprocessExprHelper(n.LHS, start, end), preprocessExprHelper(n.RHS, start, end) - if isInvariant1 && isInvariant2 { - return true - } - - if isInvariant1 { - n.LHS = newStepInvariantExpr(n.LHS) - } - if isInvariant2 { - n.RHS = newStepInvariantExpr(n.RHS) - } - - return false - - case *parser.Call: - _, ok := AtModifierUnsafeFunctions[n.Func.Name] - isStepInvariant := !ok - isStepInvariantSlice := make([]bool, len(n.Args)) - for i := range n.Args { - isStepInvariantSlice[i] = preprocessExprHelper(n.Args[i], start, end) - isStepInvariant = isStepInvariant && isStepInvariantSlice[i] - } - - if isStepInvariant { - // The function and all arguments are step invariant. - return true - } - - for i, isi := range isStepInvariantSlice { - if isi { - n.Args[i] = newStepInvariantExpr(n.Args[i]) - } - } - return false - - case *parser.MatrixSelector: - return preprocessExprHelper(n.VectorSelector, start, end) - - case *parser.SubqueryExpr: - // Since we adjust offset for the @ modifier evaluation, - // it gets tricky to adjust it for every subquery step. - // Hence we wrap the inside of subquery irrespective of - // @ on subquery (given it is also step invariant) so that - // it is evaluated only once w.r.t. the start time of subquery. - isInvariant := preprocessExprHelper(n.Expr, start, end) - if isInvariant { - n.Expr = newStepInvariantExpr(n.Expr) - } - switch n.StartOrEnd { - case parser.START: - n.Timestamp = makeInt64Pointer(timestamp.FromTime(start)) - case parser.END: - n.Timestamp = makeInt64Pointer(timestamp.FromTime(end)) - } - return n.Timestamp != nil - - case *parser.ParenExpr: - return preprocessExprHelper(n.Expr, start, end) - - case *parser.UnaryExpr: - return preprocessExprHelper(n.Expr, start, end) - - case *parser.StringLiteral, *parser.NumberLiteral: - return true - } - - panic(fmt.Sprintf("found unexpected node %#v", expr)) -} - -func makeInt64Pointer(val int64) *int64 { - valp := new(int64) - *valp = val - return valp -} - -func newStepInvariantExpr(expr parser.Expr) parser.Expr { - return &parser.StepInvariantExpr{Expr: expr} -} - func subqueryTimes(path []parser.Node) (time.Duration, time.Duration, *int64) { var ( subqOffset, subqRange time.Duration diff --git a/storage/logservice/match/label.go b/storage/logservice/match/label.go index c05248e..0a81d97 100644 --- a/storage/logservice/match/label.go +++ b/storage/logservice/match/label.go @@ -15,7 +15,7 @@ func getLabelMatchFuncs(sel *model.LogSelector) ([]MatchFunc, error) { case "pod": return getLabelMatchFuncsDetail(sel, "pod", "container") } - return nil, fmt.Errorf("unknwon logType: %s", sel.Name) + return nil, fmt.Errorf("unknown logType: %s", sel.Name) } func getLabelMatchFuncsDetail(sel *model.LogSelector, names ...string) ([]MatchFunc, error) {