mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
PublicDashboards: support time range selection on the backend (#60203)
This commit is contained in:
committed by
GitHub
parent
6928ad2949
commit
8d5b19bc61
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user