From 2e91886056422e73859f4632fe1523f198898048 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Thu, 25 Jun 2020 23:28:04 +0300 Subject: [PATCH] issue #28: simplify filter.Filter --- internal/cmd/coverage.go | 4 ++-- internal/cmd/metrics.go | 4 ++-- internal/cnf/config.go | 10 ++++++++++ internal/filter/metric.go | 8 +------- internal/repl/decorator.go | 13 +++++++++++++ internal/repl/executor.go | 6 ++---- 6 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 internal/repl/decorator.go diff --git a/internal/cmd/coverage.go b/internal/cmd/coverage.go index 3037b78..6a378d9 100644 --- a/internal/cmd/coverage.go +++ b/internal/cmd/coverage.go @@ -135,7 +135,7 @@ func NewCoverageCommand( printer.SetPrefix(config.Graphite.Prefix) if !replMode { - metrics, err := filter.Filter(metrics, config.Graphite.Filter, config.Graphite.Prefix) + metrics, err := filter.Filter(metrics, config.Pattern()) if err != nil { return err } @@ -144,7 +144,7 @@ func NewCoverageCommand( return printer.PrintCoverage(reporter.Report(metrics)) } prompt.New( - repl.NewCoverageExecutor(config.Graphite.Prefix, metrics, reporter, printer, logger), + repl.Prefix(config.Graphite.Prefix, repl.NewCoverageExecutor(metrics, reporter, printer, logger)), repl.NewMetricsCompleter(metrics), ).Run() return nil diff --git a/internal/cmd/metrics.go b/internal/cmd/metrics.go index 8e89b10..a9d5dd1 100644 --- a/internal/cmd/metrics.go +++ b/internal/cmd/metrics.go @@ -83,7 +83,7 @@ func NewMetricsCommand( printer.SetPrefix(config.Graphite.Prefix) if !replMode { - metrics, err = filter.Filter(metrics, config.Graphite.Filter, config.Graphite.Prefix) + metrics, err = filter.Filter(metrics, config.Pattern()) if err != nil { return err } @@ -92,7 +92,7 @@ func NewMetricsCommand( return printer.PrintMetrics(metrics) } prompt.New( - repl.NewMetricsExecutor(config.Graphite.Prefix, metrics, printer, logger), + repl.Prefix(config.Graphite.Prefix, repl.NewMetricsExecutor(metrics, printer, logger)), repl.NewMetricsCompleter(metrics), ).Run() return nil diff --git a/internal/cnf/config.go b/internal/cnf/config.go index 34fb693..7ef14bd 100644 --- a/internal/cnf/config.go +++ b/internal/cnf/config.go @@ -1,5 +1,7 @@ package cnf +import "strings" + // Config contains all necessary tool configuration. type Config struct { Grafana struct { @@ -12,3 +14,11 @@ type Config struct { Prefix string `mapstructure:"metrics"` } `mapstructure:",squash"` } + +func (config Config) Pattern() string { + pattern, prefix := config.Graphite.Filter, config.Graphite.Prefix + if !strings.HasPrefix(pattern, prefix) { + pattern = prefix + "." + pattern + } + return pattern +} diff --git a/internal/filter/metric.go b/internal/filter/metric.go index 6015edd..ad8b01d 100644 --- a/internal/filter/metric.go +++ b/internal/filter/metric.go @@ -1,8 +1,6 @@ package filter import ( - "strings" - "github.com/gobwas/glob" "github.com/pkg/errors" @@ -28,15 +26,11 @@ func Exclude(metrics provider.Metrics, pattern string) (provider.Metrics, error) return filtered, nil } -func Filter(metrics provider.Metrics, pattern, prefix string) (provider.Metrics, error) { +func Filter(metrics provider.Metrics, pattern string) (provider.Metrics, error) { if len(metrics) == 0 || pattern == "" { return metrics, nil } - if !strings.HasPrefix(pattern, prefix) { - pattern = prefix + "." + pattern - } - matcher, err := glob.Compile(pattern) if err != nil { return metrics, errors.Wrapf(err, "filter: compile pattern %q", pattern) diff --git a/internal/repl/decorator.go b/internal/repl/decorator.go new file mode 100644 index 0000000..7e58d8f --- /dev/null +++ b/internal/repl/decorator.go @@ -0,0 +1,13 @@ +package repl + +import "strings" + +func Prefix(prefix string, fn func(string)) func(string) { + return func(pattern string) { + pattern = strings.TrimSpace(pattern) + if pattern != "" { + pattern = prefix + "." + pattern + } + fn(pattern) + } +} diff --git a/internal/repl/executor.go b/internal/repl/executor.go index aea6014..018fae4 100644 --- a/internal/repl/executor.go +++ b/internal/repl/executor.go @@ -11,7 +11,6 @@ import ( ) func NewCoverageExecutor( - prefix string, metrics provider.Metrics, reporter interface { Report(provider.Metrics) model.Report @@ -20,7 +19,7 @@ func NewCoverageExecutor( logger *logrus.Logger, ) func(string) { return func(pattern string) { - metrics, err := filter.Filter(metrics, pattern, prefix) + metrics, err := filter.Filter(metrics, pattern) if err != nil { logger.WithError(err).WithField("pattern", pattern).Error("repl: filter metrics") return @@ -35,13 +34,12 @@ func NewCoverageExecutor( } func NewMetricsExecutor( - prefix string, metrics provider.Metrics, printer interface{ PrintMetrics(provider.Metrics) error }, logger *logrus.Logger, ) func(string) { return func(pattern string) { - metrics, err := filter.Filter(metrics, pattern, prefix) + metrics, err := filter.Filter(metrics, pattern) if err != nil { logger.WithError(err).WithField("pattern", pattern).Error("repl: filter metrics") return