From a3a383a709095fb1d9671bf8ca831fddd4636df7 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Mon, 3 Jul 2017 16:24:24 +0200 Subject: [PATCH] nilpointer bugfix: in[0] may not exist. fix #674 while we're at it, small refactor for DRY --- expr/func_avgseries.go | 12 +++--------- expr/func_persecond.go | 10 +++------- expr/func_sumseries.go | 12 +++--------- expr/funcs.go | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/expr/func_avgseries.go b/expr/func_avgseries.go index 78e57d6ebb..10e5b16087 100644 --- a/expr/func_avgseries.go +++ b/expr/func_avgseries.go @@ -28,15 +28,9 @@ func (s *FuncAvgSeries) Context(context Context) Context { } func (s *FuncAvgSeries) Exec(cache map[Req][]models.Series) ([]models.Series, error) { - var series []models.Series - var queryPatts []string - for i := range s.in { - in, err := s.in[i].Exec(cache) - if err != nil { - return nil, err - } - series = append(series, in...) - queryPatts = append(queryPatts, in[0].QueryPatt) + series, queryPatts, err := consumeFuncs(cache, s.in) + if err != nil { + return nil, err } if len(series) == 0 { diff --git a/expr/func_persecond.go b/expr/func_persecond.go index 1ac6cbc746..d18eb76c95 100644 --- a/expr/func_persecond.go +++ b/expr/func_persecond.go @@ -32,13 +32,9 @@ func (s *FuncPerSecond) Context(context Context) Context { } func (s *FuncPerSecond) Exec(cache map[Req][]models.Series) ([]models.Series, error) { - var series []models.Series - for i := range s.in { - serie, err := s.in[i].Exec(cache) - if err != nil { - return nil, err - } - series = append(series, serie...) + series, _, err := consumeFuncs(cache, s.in) + if err != nil { + return nil, err } maxValue := math.NaN() if s.maxValue > 0 { diff --git a/expr/func_sumseries.go b/expr/func_sumseries.go index a2fbabdc7c..1064731ad4 100644 --- a/expr/func_sumseries.go +++ b/expr/func_sumseries.go @@ -28,15 +28,9 @@ func (s *FuncSumSeries) Context(context Context) Context { } func (s *FuncSumSeries) Exec(cache map[Req][]models.Series) ([]models.Series, error) { - var series []models.Series - var queryPatts []string - for i := range s.in { - in, err := s.in[i].Exec(cache) - if err != nil { - return nil, err - } - series = append(series, in...) - queryPatts = append(queryPatts, in[0].QueryPatt) + series, queryPatts, err := consumeFuncs(cache, s.in) + if err != nil { + return nil, err } if len(series) == 0 { diff --git a/expr/funcs.go b/expr/funcs.go index 33dd99cb40..156512dc19 100644 --- a/expr/funcs.go +++ b/expr/funcs.go @@ -73,3 +73,19 @@ func summarizeCons(series []models.Series) (consolidation.Consolidator, consolid } return series[0].Consolidator, series[0].QueryCons } + +func consumeFuncs(cache map[Req][]models.Series, fns []GraphiteFunc) ([]models.Series, []string, error) { + var series []models.Series + var queryPatts []string + for i := range fns { + in, err := fns[i].Exec(cache) + if err != nil { + return nil, nil, err + } + if len(in) != 0 { + series = append(series, in...) + queryPatts = append(queryPatts, in[0].QueryPatt) + } + } + return series, queryPatts, nil +}