Datasource: Use intervalMs field of query model in GetIntervalFrom if interval field is not set (#34270)

The /api/ds/query and /api/tsdb/query endpoints extract the intervalMs field from
each query in the request, but it currently seems to be ignored, at
least for queries to a Prometheus datasource. This changes
GetIntervalFrom function to check for the presence of intervalMs in the
query model if interval is missing.
This commit is contained in:
Ben Sully 2021-05-26 10:45:11 +01:00 committed by GitHub
parent ad3b0b2272
commit b7ea66b00d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -69,6 +69,15 @@ func (ic *intervalCalculator) Calculate(timerange plugins.DataTimeRange, minInte
func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error) {
interval := queryModel.Get("interval").MustString("")
// intervalMs field appears in the v2 plugins API and should be preferred
// if 'interval' isn't present.
if interval == "" {
intervalMS := queryModel.Get("intervalMs").MustInt(0)
if intervalMS != 0 {
return time.Duration(intervalMS) * time.Millisecond, nil
}
}
if interval == "" && dsInfo != nil && dsInfo.JsonData != nil {
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
if dsInterval != "" {

View File

@ -8,6 +8,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntervalCalculator_Calculate(t *testing.T) {
@ -70,6 +71,8 @@ func TestFormatDuration(t *testing.T) {
}
func TestGetIntervalFrom(t *testing.T) {
dsJSON, err := simplejson.NewJson([]byte(`{"timeInterval": "60s"}`))
require.NoError(t, err)
testCases := []struct {
name string
dsInfo *models.DataSource
@ -80,6 +83,13 @@ func TestGetIntervalFrom(t *testing.T) {
{"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},
{"intervalMs", nil, `{"intervalMs": 45000}`, time.Second * 15, time.Second * 45},
{"intervalMs sub-seconds", nil, `{"intervalMs": 45200}`, time.Second * 15, time.Millisecond * 45200},
{"dsInfo timeInterval", &models.DataSource{
JsonData: dsJSON,
}, `{}`, time.Second * 15, time.Second * 60},
{"defaultInterval when interval empty", nil, `{"interval": ""}`, time.Second * 15, time.Second * 15},
{"defaultInterval when intervalMs 0", nil, `{"intervalMs": 0}`, time.Second * 15, time.Second * 15},
}
for _, tc := range testCases {