diff --git a/expr/expr.go b/expr/expr.go index 51a7ea5b6..07274ff69 100644 --- a/expr/expr.go +++ b/expr/expr.go @@ -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 @@ -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 @@ -140,31 +140,7 @@ 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) { @@ -172,6 +148,10 @@ func Init(limiter limiter.SimpleLimiter, zipper zipper.CarbonZipper) { _evaluator.zipper = zipper } +func Evaluator() *evaluator { + return _evaluator +} + func init() { // helper.SetEvaluator(_evaluator) metadata.SetEvaluator(_evaluator) @@ -179,7 +159,21 @@ func init() { // 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) { diff --git a/expr/functions/moving/moving_refetch/function_test.go b/expr/functions/moving/moving_refetch/function_test.go index a1cc3759c..3e7d6ddbe 100644 --- a/expr/functions/moving/moving_refetch/function_test.go +++ b/expr/functions/moving/moving_refetch/function_test.go @@ -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)