Dashboard: Add week start option to global and dashboard preferences (#40010)
* Add global week start option to shared preferences * Add default_week_start to configuration docs * Add week start option to dashboards * Add week start argument to tsdb time range parser * Fix strict check issues * Add tests for week start * Change wording on default_week_start documentation Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Update week_start column to be a nullable field Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Update configuration to include browser option * Update WeekStartPicker container selector Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com> * Add menuShouldPortal to WeekStartPicker to remove deprecation warning Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com> * Add inputId to WeekStartPicker * Use e2e selector on WeekStartPicker aria-label * Simplify WeekStartPicker onChange condition * Specify value type on WeekStartPicker weekStarts * Remove setWeekStart side effect from reducer * Fix updateLocale failing to reset week start * Store week start as string to handle empty values Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com> Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
This commit is contained in:
co-authored by
achatterjee-grafana
Emil Tullstedt
Hugo Häggmark
Alex Khomenko
parent
db62ce477d
commit
a9faab6b09
@@ -38,6 +38,7 @@ type CurrentUser struct {
|
||||
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
|
||||
GravatarUrl string `json:"gravatarUrl"`
|
||||
Timezone string `json:"timezone"`
|
||||
WeekStart string `json:"weekStart"`
|
||||
Locale string `json:"locale"`
|
||||
HelpFlags1 models.HelpFlags1 `json:"helpFlags1"`
|
||||
HasEditPermissionInFolders bool `json:"hasEditPermissionInFolders"`
|
||||
|
||||
@@ -4,10 +4,12 @@ type Prefs struct {
|
||||
Theme string `json:"theme"`
|
||||
HomeDashboardID int64 `json:"homeDashboardId"`
|
||||
Timezone string `json:"timezone"`
|
||||
WeekStart string `json:"weekStart"`
|
||||
}
|
||||
|
||||
type UpdatePrefsCmd struct {
|
||||
Theme string `json:"theme"`
|
||||
HomeDashboardID int64 `json:"homeDashboardId"`
|
||||
Timezone string `json:"timezone"`
|
||||
WeekStart string `json:"weekStart"`
|
||||
}
|
||||
|
||||
@@ -484,6 +484,7 @@ func (hs *HTTPServer) setIndexViewData(c *models.ReqContext) (*dtos.IndexViewDat
|
||||
IsGrafanaAdmin: c.IsGrafanaAdmin,
|
||||
LightTheme: prefs.Theme == lightName,
|
||||
Timezone: prefs.Timezone,
|
||||
WeekStart: prefs.WeekStart,
|
||||
Locale: locale,
|
||||
HelpFlags1: c.HelpFlags1,
|
||||
HasEditPermissionInFolders: hasEditPerm,
|
||||
|
||||
@@ -41,6 +41,7 @@ func getPreferencesFor(orgID, userID, teamID int64) response.Response {
|
||||
Theme: prefsQuery.Result.Theme,
|
||||
HomeDashboardID: prefsQuery.Result.HomeDashboardId,
|
||||
Timezone: prefsQuery.Result.Timezone,
|
||||
WeekStart: prefsQuery.Result.WeekStart,
|
||||
}
|
||||
|
||||
return response.JSON(200, &dto)
|
||||
@@ -61,6 +62,7 @@ func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsC
|
||||
TeamId: teamId,
|
||||
Theme: dtoCmd.Theme,
|
||||
Timezone: dtoCmd.Timezone,
|
||||
WeekStart: dtoCmd.WeekStart,
|
||||
HomeDashboardId: dtoCmd.HomeDashboardID,
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ type Preferences struct {
|
||||
Version int
|
||||
HomeDashboardId int64
|
||||
Timezone string
|
||||
WeekStart string
|
||||
Theme string
|
||||
Created time.Time
|
||||
Updated time.Time
|
||||
@@ -44,5 +45,6 @@ type SavePreferencesCommand struct {
|
||||
|
||||
HomeDashboardId int64 `json:"homeDashboardId"`
|
||||
Timezone string `json:"timezone"`
|
||||
WeekStart string `json:"weekStart"`
|
||||
Theme string `json:"theme"`
|
||||
}
|
||||
|
||||
@@ -42,4 +42,8 @@ func addPreferencesMigrations(mg *Migrator) {
|
||||
SQLite("UPDATE preferences SET team_id=0 WHERE team_id IS NULL;").
|
||||
Postgres("UPDATE preferences SET team_id=0 WHERE team_id IS NULL;").
|
||||
Mysql("UPDATE preferences SET team_id=0 WHERE team_id IS NULL;"))
|
||||
|
||||
mg.AddMigration("Add column week_start in preferences", NewAddColumnMigration(preferencesV2, &Column{
|
||||
Name: "week_start", Type: DB_NVarchar, Length: 10, Nullable: true,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ func (ss *SQLStore) GetPreferencesWithDefaults(ctx context.Context, query *model
|
||||
res := &models.Preferences{
|
||||
Theme: ss.Cfg.DefaultTheme,
|
||||
Timezone: ss.Cfg.DateFormats.DefaultTimezone,
|
||||
WeekStart: ss.Cfg.DateFormats.DefaultWeekStart,
|
||||
HomeDashboardId: 0,
|
||||
}
|
||||
|
||||
@@ -54,6 +55,9 @@ func (ss *SQLStore) GetPreferencesWithDefaults(ctx context.Context, query *model
|
||||
if p.Timezone != "" {
|
||||
res.Timezone = p.Timezone
|
||||
}
|
||||
if p.WeekStart != "" {
|
||||
res.WeekStart = p.WeekStart
|
||||
}
|
||||
if p.HomeDashboardId != 0 {
|
||||
res.HomeDashboardId = p.HomeDashboardId
|
||||
}
|
||||
@@ -96,6 +100,7 @@ func SavePreferences(cmd *models.SavePreferencesCommand) error {
|
||||
TeamId: cmd.TeamId,
|
||||
HomeDashboardId: cmd.HomeDashboardId,
|
||||
Timezone: cmd.Timezone,
|
||||
WeekStart: cmd.WeekStart,
|
||||
Theme: cmd.Theme,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
@@ -104,7 +109,7 @@ func SavePreferences(cmd *models.SavePreferencesCommand) error {
|
||||
return err
|
||||
}
|
||||
prefs.HomeDashboardId = cmd.HomeDashboardId
|
||||
prefs.Timezone = cmd.Timezone
|
||||
prefs.WeekStart = cmd.WeekStart
|
||||
prefs.Theme = cmd.Theme
|
||||
prefs.Updated = time.Now()
|
||||
prefs.Version += 1
|
||||
|
||||
@@ -11,6 +11,7 @@ type DateFormats struct {
|
||||
UseBrowserLocale bool `json:"useBrowserLocale"`
|
||||
Interval DateFormatIntervals `json:"interval"`
|
||||
DefaultTimezone string `json:"defaultTimezone"`
|
||||
DefaultWeekStart string `json:"defaultWeekStart"`
|
||||
}
|
||||
|
||||
type DateFormatIntervals struct {
|
||||
@@ -22,17 +23,17 @@ type DateFormatIntervals struct {
|
||||
Year string `json:"year"`
|
||||
}
|
||||
|
||||
const localBrowserTimezone = "browser"
|
||||
const localBrowser = "browser"
|
||||
|
||||
func valueAsTimezone(section *ini.Section, keyName string) (string, error) {
|
||||
timezone := section.Key(keyName).MustString(localBrowserTimezone)
|
||||
if timezone == localBrowserTimezone {
|
||||
return localBrowserTimezone, nil
|
||||
timezone := section.Key(keyName).MustString(localBrowser)
|
||||
if timezone == localBrowser {
|
||||
return localBrowser, nil
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return localBrowserTimezone, err
|
||||
return localBrowser, err
|
||||
}
|
||||
|
||||
return location.String(), nil
|
||||
@@ -54,4 +55,5 @@ func (cfg *Cfg) readDateFormats() {
|
||||
cfg.Logger.Warn("Unknown timezone as default_timezone", "err", err)
|
||||
}
|
||||
cfg.DateFormats.DefaultTimezone = timezone
|
||||
cfg.DateFormats.DefaultWeekStart = valueAsString(dateFormats, "default_week_start", "browser")
|
||||
}
|
||||
|
||||
+19
-5
@@ -79,22 +79,30 @@ func tryParseUnixMsEpoch(val string) (time.Time, bool) {
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseFrom() (time.Time, error) {
|
||||
return parse(tr.From, tr.now, false, nil)
|
||||
return parse(tr.From, tr.now, false, nil, -1)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseTo() (time.Time, error) {
|
||||
return parse(tr.To, tr.now, true, nil)
|
||||
return parse(tr.To, tr.now, true, nil, -1)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseFromWithLocation(location *time.Location) (time.Time, error) {
|
||||
return parse(tr.From, tr.now, false, location)
|
||||
return parse(tr.From, tr.now, false, location, -1)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseToWithLocation(location *time.Location) (time.Time, error) {
|
||||
return parse(tr.To, tr.now, true, location)
|
||||
return parse(tr.To, tr.now, true, location, -1)
|
||||
}
|
||||
|
||||
func parse(s string, now time.Time, withRoundUp bool, location *time.Location) (time.Time, error) {
|
||||
func (tr *TimeRange) ParseFromWithWeekStart(location *time.Location, weekstart time.Weekday) (time.Time, error) {
|
||||
return parse(tr.From, tr.now, false, location, weekstart)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseToWithWeekStart(location *time.Location, weekstart time.Weekday) (time.Time, error) {
|
||||
return parse(tr.To, tr.now, true, location, weekstart)
|
||||
}
|
||||
|
||||
func parse(s string, now time.Time, withRoundUp bool, location *time.Location, weekstart time.Weekday) (time.Time, error) {
|
||||
if res, ok := tryParseUnixMsEpoch(s); ok {
|
||||
return res, nil
|
||||
}
|
||||
@@ -108,6 +116,12 @@ func parse(s string, now time.Time, withRoundUp bool, location *time.Location) (
|
||||
if location != nil {
|
||||
options = append(options, datemath.WithLocation(location))
|
||||
}
|
||||
if weekstart != -1 {
|
||||
if weekstart > now.Weekday() {
|
||||
weekstart = weekstart - 7
|
||||
}
|
||||
options = append(options, datemath.WithStartOfWeek(weekstart))
|
||||
}
|
||||
|
||||
return datemath.ParseAndEvaluate(s, options...)
|
||||
}
|
||||
|
||||
@@ -248,5 +248,120 @@ func TestTimeRange(t *testing.T) {
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can parse now-1w/w, now-1w/w without timezone and week start on Monday", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-1w/w",
|
||||
To: "now-1w/w",
|
||||
now: now,
|
||||
}
|
||||
weekstart := time.Monday
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("from now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-13T00:00:00.000Z")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithWeekStart(nil, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("to now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-19T23:59:59.999Z")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithWeekStart(nil, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can parse now-1w/w, now-1w/w with America/Chicago timezone and week start on Monday", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-1w/w",
|
||||
To: "now-1w/w",
|
||||
now: now,
|
||||
}
|
||||
weekstart := time.Monday
|
||||
location, err := time.LoadLocation("America/Chicago")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("from now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-13T00:00:00.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("to now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-19T23:59:59.999-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can parse now-1w/w, now-1w/w with America/Chicago timezone and week start on Sunday", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-1w/w",
|
||||
To: "now-1w/w",
|
||||
now: now,
|
||||
}
|
||||
weekstart := time.Sunday
|
||||
location, err := time.LoadLocation("America/Chicago")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("from now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-19T00:00:00.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("to now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-25T23:59:59.999-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can parse now-1w/w, now-1w/w with America/Chicago timezone and week start on Saturday", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-1w/w",
|
||||
To: "now-1w/w",
|
||||
now: now,
|
||||
}
|
||||
weekstart := time.Saturday
|
||||
location, err := time.LoadLocation("America/Chicago")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("from now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-18T00:00:00.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("to now-1w/w ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-24T23:59:59.999-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithWeekStart(location, weekstart)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user