Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
issue #28: simplify filter.Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jun 25, 2020
1 parent 978ae9c commit 2e91886
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions internal/cnf/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cnf

import "strings"

// Config contains all necessary tool configuration.
type Config struct {
Grafana struct {
Expand All @@ -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
}
8 changes: 1 addition & 7 deletions internal/filter/metric.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package filter

import (
"strings"

"github.com/gobwas/glob"
"github.com/pkg/errors"

Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/repl/decorator.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 2 additions & 4 deletions internal/repl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func NewCoverageExecutor(
prefix string,
metrics provider.Metrics,
reporter interface {
Report(provider.Metrics) model.Report
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2e91886

Please sign in to comment.