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

Commit

Permalink
fix #36: simplify unpack env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Aug 1, 2020
1 parent 5af0c8a commit 99871db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 13 additions & 16 deletions internal/model/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/provider/grafana/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

func TestProvider(t *testing.T) {
ctx := context.Background()
_ = ctx

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
Expand Down

0 comments on commit 99871db

Please sign in to comment.