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:
Guilherme Caulada
2021-10-18 10:27:14 -03:00
committed by GitHub
co-authored by achatterjee-grafana Emil Tullstedt Hugo Häggmark Alex Khomenko
parent db62ce477d
commit a9faab6b09
27 changed files with 362 additions and 21 deletions
+19 -5
View File
@@ -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...)
}
+115
View File
@@ -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)
})
})
})
}