Skip to content

Commit

Permalink
expr: cleanup refactor expr
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf1980 committed Jan 19, 2024
1 parent 8afcc0c commit 220c62b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
52 changes: 23 additions & 29 deletions expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
zipper "github.com/go-graphite/carbonapi/zipper/interfaces"
)

type Evaluator struct {
type evaluator struct {
limiter limiter.SimpleLimiter
zipper zipper.CarbonZipper
}

func (eval Evaluator) Fetch(ctx context.Context, exprs []parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (map[parser.MetricRequest][]*types.MetricData, error) {
func (eval evaluator) Fetch(ctx context.Context, exprs []parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (map[parser.MetricRequest][]*types.MetricData, error) {
if eval.zipper == nil {
// TODO: may be return error or not check. But for use expr in external applications without CarbonZipper implementation
return values, nil
Expand Down Expand Up @@ -114,7 +114,7 @@ func (eval Evaluator) Fetch(ctx context.Context, exprs []parser.Expr, from, unti
}

// Eval evaluates expressions.
func (eval Evaluator) Eval(ctx context.Context, exp parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (results []*types.MetricData, err error) {
func (eval evaluator) Eval(ctx context.Context, exp parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (results []*types.MetricData, err error) {
rewritten, targets, err := RewriteExpr(ctx, exp, from, until, values)
if err != nil {
return nil, err
Expand All @@ -140,46 +140,40 @@ func (eval Evaluator) Eval(ctx context.Context, exp parser.Expr, from, until int
return EvalExpr(ctx, exp, from, until, values)
}

// FetchAndEvalExp fetch data and evaluates expressions
func (eval Evaluator) FetchAndEvalExp(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, merry.Error) {
targetValues, err := eval.Fetch(ctx, []parser.Expr{e}, from, until, values)
if err != nil {
return nil, merry.Wrap(err)
}

res, err := eval.Eval(ctx, e, from, until, targetValues)
if err != nil {
return nil, merry.Wrap(err)
}

for mReq := range values {
SortMetrics(values[mReq], mReq)
}

return res, nil
}

// New init evaluator
func New(limiter limiter.SimpleLimiter, zipper zipper.CarbonZipper) *Evaluator {
return &Evaluator{limiter: limiter, zipper: zipper}
}

var _evaluator = &Evaluator{}
var _evaluator = &evaluator{}

// Init call on configure phase, if need limiter or/and custom zipper (for refetch/etc)
func Init(limiter limiter.SimpleLimiter, zipper zipper.CarbonZipper) {
_evaluator.limiter = limiter
_evaluator.zipper = zipper
}

func Evaluator() *evaluator {
return _evaluator
}

func init() {
// helper.SetEvaluator(_evaluator)
metadata.SetEvaluator(_evaluator)
}

// FetchAndEvalExp fetch data and evaluates expressions
func FetchAndEvalExp(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, merry.Error) {
return _evaluator.FetchAndEvalExp(ctx, e, from, until, values)
targetValues, err := _evaluator.Fetch(ctx, []parser.Expr{e}, from, until, values)
if err != nil {
return nil, merry.Wrap(err)
}

res, err := _evaluator.Eval(ctx, e, from, until, targetValues)
if err != nil {
return nil, merry.Wrap(err)
}

for mReq := range values {
SortMetrics(values[mReq], mReq)
}

return res, nil
}

func FetchAndEvalExprs(ctx context.Context, exprs []parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, map[string]merry.Error) {
Expand Down
3 changes: 2 additions & 1 deletion expr/functions/moving/moving_refetch/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var M = map[parser.MetricRequest][]*types.MetricData{

func init() {
md := moving.New("")
evaluator := expr.New(nil, th.NewTestZipper(M))
expr.Init(nil, th.NewTestZipper(M))
evaluator := expr.Evaluator()
metadata.SetEvaluator(evaluator)
for _, m := range md {
metadata.RegisterFunction(m.Name, m.F)
Expand Down

0 comments on commit 220c62b

Please sign in to comment.