Commit 45b9ea7b by Nutmos Committed by GitHub

Prometheus: Min step defaults to seconds when no unit is set to prevent errors…

Prometheus: Min step defaults to seconds when no unit is set to prevent errors when running alerts. (#30966)

* update interval to fix no unit

* fix bug

* update with new method

* format go

* add test

* change test method to add test error nil

* add simplejson and model package

* change json format

* add parentheses

* add simplejson to function

* change mode to model

* move assert import package

* add one enter to avoid goimport error

* change to test case
parent d05e10f7
...@@ -2,6 +2,7 @@ package tsdb ...@@ -2,6 +2,7 @@ package tsdb
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"time" "time"
...@@ -81,6 +82,13 @@ func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, def ...@@ -81,6 +82,13 @@ func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, def
} }
interval = strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1) interval = strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1)
isPureNum, err := regexp.MatchString(`^\d+$`, interval)
if err != nil {
return time.Duration(0), err
}
if isPureNum {
interval += "s"
}
parsedInterval, err := time.ParseDuration(interval) parsedInterval, err := time.ParseDuration(interval)
if err != nil { if err != nil {
return time.Duration(0), err return time.Duration(0), err
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
...@@ -65,3 +67,26 @@ func TestFormatDuration(t *testing.T) { ...@@ -65,3 +67,26 @@ func TestFormatDuration(t *testing.T) {
}) })
} }
} }
func TestGetIntervalFrom(t *testing.T) {
testCases := []struct {
name string
dsInfo *models.DataSource
queryModel string
defaultInterval time.Duration
expected time.Duration
}{
{"45s", nil, `{"interval": "45s"}`, time.Second * 15, time.Second * 45},
{"45", nil, `{"interval": "45"}`, time.Second * 15, time.Second * 45},
{"2m", nil, `{"interval": "2m"}`, time.Second * 15, time.Minute * 2},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
js, _ := simplejson.NewJson([]byte(tc.queryModel))
actual, err := GetIntervalFrom(tc.dsInfo, js, tc.defaultInterval)
assert.Nil(t, err)
assert.Equal(t, tc.expected, actual)
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment