From 99871db5f99f07ab9e165f11a8c4aaaa911d9831 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Sat, 1 Aug 2020 14:54:51 +0300 Subject: [PATCH] fix #36: simplify unpack env variables --- internal/cmd/coverage.go | 2 +- internal/cmd/queries.go | 2 +- internal/model/dashboard.go | 29 ++++++++++------------ internal/provider/grafana/provider_test.go | 1 - 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/internal/cmd/coverage.go b/internal/cmd/coverage.go index 786f231..c383486 100644 --- a/internal/cmd/coverage.go +++ b/internal/cmd/coverage.go @@ -108,7 +108,7 @@ func NewCoverageCommand( return err } - queries, err := dashboard.Queries(model.Transform{ + queries, err := dashboard.Queries(model.Config{ SkipRaw: false, SkipDuplicates: false, NeedSorting: true, diff --git a/internal/cmd/queries.go b/internal/cmd/queries.go index 174720f..781cc95 100644 --- a/internal/cmd/queries.go +++ b/internal/cmd/queries.go @@ -66,7 +66,7 @@ func NewQueriesCommand( } dashboard.Prefix = config.Graphite.Prefix - queries, err := dashboard.Queries(model.Transform{ + queries, err := dashboard.Queries(model.Config{ SkipRaw: raw, SkipDuplicates: duplicates, TrimPrefixes: trim, diff --git a/internal/model/dashboard.go b/internal/model/dashboard.go index b9f209e..1fdc752 100644 --- a/internal/model/dashboard.go +++ b/internal/model/dashboard.go @@ -7,13 +7,21 @@ import ( "github.com/pkg/errors" ) +type Config struct { + SkipRaw bool + SkipDuplicates bool + NeedSorting bool + Unpack bool + TrimPrefixes []string +} + type Dashboard struct { Prefix string RawData []Query Variables []Variable } -func (dashboard *Dashboard) Queries(cfg Transform) (Queries, error) { +func (dashboard *Dashboard) Queries(cfg Config) (Queries, error) { transformed := make(Queries, 0, len(dashboard.RawData)) for _, raw := range dashboard.RawData { @@ -66,29 +74,18 @@ func (dashboard *Dashboard) Queries(cfg Transform) (Queries, error) { } func unpack(metric string, variables []Variable) []string { - for i, variable := range variables { + for _, variable := range variables { env := "$" + variable.Name if !strings.Contains(metric, env) { continue } - result := make([]string, 0, len(variable.Options)) - for _, option := range variable.Options { - // TODO:research some variable has $__all option - result = append(result, unpack(strings.ReplaceAll(metric, env, option.Value), variables[i+1:])...) - } - return result + // simplify logic: replace a variable by wildcard + // motivation: variable can use dynamic source and that fact increase the complexity of the algorithm + metric = strings.ReplaceAll(metric, env, "*") } return []string{metric} } -type Transform struct { - SkipRaw bool - SkipDuplicates bool - NeedSorting bool - Unpack bool - TrimPrefixes []string -} - type Variable struct { Name string Options []Option diff --git a/internal/provider/grafana/provider_test.go b/internal/provider/grafana/provider_test.go index fb87c7f..4c9b7ad 100644 --- a/internal/provider/grafana/provider_test.go +++ b/internal/provider/grafana/provider_test.go @@ -22,7 +22,6 @@ import ( func TestProvider(t *testing.T) { ctx := context.Background() - _ = ctx logger := logrus.New() logger.SetOutput(ioutil.Discard)