PublicDashboards: support time range selection on the backend (#60203)

This commit is contained in:
Ezequiel Victorero
2022-12-15 10:44:33 -03:00
committed by GitHub
parent 6928ad2949
commit 8d5b19bc61
6 changed files with 176 additions and 22 deletions

View File

@@ -1,7 +1,9 @@
package models
import (
"strconv"
"testing"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
@@ -15,36 +17,58 @@ func TestPublicDashboardTableName(t *testing.T) {
func TestBuildTimeSettings(t *testing.T) {
var dashboardData = simplejson.NewFromAny(map[string]interface{}{"time": map[string]interface{}{"from": "2022-09-01T00:00:00.000Z", "to": "2022-09-01T12:00:00.000Z"}})
fromMs, toMs := internal.GetTimeRangeFromDashboard(t, dashboardData)
defaultFromMs, defaultToMs := internal.GetTimeRangeFromDashboard(t, dashboardData)
selectionFromMs := strconv.FormatInt(time.Now().UnixMilli(), 10)
selectionToMs := strconv.FormatInt(time.Now().Add(time.Hour).UnixMilli(), 10)
testCases := []struct {
name string
dashboard *models.Dashboard
pubdash *PublicDashboard
timeResult TimeSettings
reqDTO PublicDashboardQueryDTO
}{
{
name: "should use dashboard time if pubdash time empty",
dashboard: &models.Dashboard{Data: dashboardData},
pubdash: &PublicDashboard{},
pubdash: &PublicDashboard{TimeSelectionEnabled: false},
timeResult: TimeSettings{
From: fromMs,
To: toMs,
From: defaultFromMs,
To: defaultToMs,
},
reqDTO: PublicDashboardQueryDTO{},
},
{
name: "should use dashboard time even if pubdash time exists",
name: "should use dashboard time when time selection is disabled",
dashboard: &models.Dashboard{Data: dashboardData},
pubdash: &PublicDashboard{TimeSettings: &TimeSettings{From: "now-12", To: "now"}},
pubdash: &PublicDashboard{TimeSelectionEnabled: false, TimeSettings: &TimeSettings{From: "now-12", To: "now"}},
timeResult: TimeSettings{
From: fromMs,
To: toMs,
From: defaultFromMs,
To: defaultToMs,
},
reqDTO: PublicDashboardQueryDTO{},
},
{
name: "should use selected values if time selection is enabled",
dashboard: &models.Dashboard{Data: dashboardData},
pubdash: &PublicDashboard{TimeSelectionEnabled: true, TimeSettings: &TimeSettings{From: "now-12", To: "now"}},
reqDTO: PublicDashboardQueryDTO{
TimeRange: TimeSettings{
From: selectionFromMs,
To: selectionToMs,
},
},
timeResult: TimeSettings{
From: selectionFromMs,
To: selectionToMs,
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.timeResult, test.pubdash.BuildTimeSettings(test.dashboard))
assert.Equal(t, test.timeResult, test.pubdash.BuildTimeSettings(test.dashboard, test.reqDTO))
})
}
}