Skip to content

Commit

Permalink
misspell & gocyclo (#82)
Browse files Browse the repository at this point in the history
* misspell action
* misspell tool & correction ok
* add misspell to checks.sh
* gocyclo
  • Loading branch information
jmnote authored May 31, 2023
1 parent 01aa33c commit d2889de
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 104 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ test-win:
cover:
hack/test-cover.sh

misspell:
hack/misspell.sh

gocyclo:
hack/gocyclo.sh

checks:
hack/checks.sh

Expand Down
1 change: 1 addition & 0 deletions hack/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
11 changes: 11 additions & 0 deletions hack/gocyclo.sh
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions hack/misspell.sh
Original file line number Diff line number Diff line change
@@ -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"
104 changes: 1 addition & 103 deletions letheql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion storage/logservice/match/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d2889de

Please sign in to comment.