Skip to content

Commit

Permalink
Merge pull request #109 from K-Phoen/sort-interval-by-duration
Browse files Browse the repository at this point in the history
fix: sort interval vars by duration
  • Loading branch information
K-Phoen authored Apr 17, 2021
2 parents 2fd066b + eff91da commit c60c8c4
Show file tree
Hide file tree
Showing 164 changed files with 33,003 additions and 11,543 deletions.
10 changes: 5 additions & 5 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,16 @@ variables:
"allValue": "",
"multi": false,
"multiFormat": "",
"query": "1m,30s",
"query": "30s,1m",
"options": [
{
"text": "1m",
"value": "1m",
"text": "30s",
"value": "30s",
"selected": false
},
{
"text": "30s",
"value": "30s",
"text": "1m",
"value": "1m",
"selected": false
}
],
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/gosimple/slug v1.9.0 // indirect
github.com/grafana-tools/sdk v0.0.0-20210402150123-f7c763c3738c
github.com/prometheus/common v0.20.0
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.4.0
Expand Down
220 changes: 211 additions & 9 deletions go.sum

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion variable/interval/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/grafana-tools/sdk"
"github.com/prometheus/common/model"
)

// Option represents an option that can be used to configure an interval.
Expand Down Expand Up @@ -36,7 +37,11 @@ func New(name string, options ...Option) *Interval {
// Values sets the possible values for the variable.
func Values(values ValuesList) Option {
return func(interval *Interval) {
sort.Strings(values)
sort.SliceStable(values, func(i, j int) bool {
iDuration, _ := model.ParseDuration(values[i])
jDuration, _ := model.ParseDuration(values[j])
return iDuration < jDuration
})

for _, value := range values {
interval.Builder.Options = append(interval.Builder.Options, sdk.Option{
Expand Down
11 changes: 10 additions & 1 deletion variable/interval/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ func TestValuesCanBeSet(t *testing.T) {
varValues = append(varValues, opt.Value)
}

req.Equal("10m,12h,1h,1m,30m,30s,5m,6h", panel.Builder.Query)
req.Equal("30s,1m,5m,10m,30m,1h,6h,12h", panel.Builder.Query)
req.ElementsMatch(values, varLabels)
req.ElementsMatch(values, varValues)
}

func TestValuesAreSortedByDuration(t *testing.T) {
req := require.New(t)
values := []string{"12h", "30m", "6h", "30d", "30s"}

panel := New("", Values(values))

req.Equal("30s,30m,6h,12h,30d", panel.Builder.Query)
}

func TestDefaultValueCanBeSet(t *testing.T) {
req := require.New(t)

Expand Down
324 changes: 324 additions & 0 deletions vendor/github.com/golang/protobuf/proto/buffer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c60c8c4

Please sign in to comment.