mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 03:11:01 -06:00
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
This commit is contained in:
parent
d05e10f792
commit
45b9ea7b4e
@ -2,6 +2,7 @@ package tsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -81,6 +82,13 @@ func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, def
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return time.Duration(0), err
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user