From e79b2a3f66b1a296dc8894099b712280b1ccc8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agn=C3=A8s=20Toulet?= <35176601+AgnesToulet@users.noreply.github.com> Date: Tue, 19 May 2020 08:52:43 +0200 Subject: [PATCH] Core: add location option to parse timerange (#24796) * Core: add location option to parse timerange * Extensions: add go-datemath to not break Enterprise --- pkg/tsdb/time_range.go | 28 +++++++++++++----- pkg/tsdb/time_range_test.go | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/pkg/tsdb/time_range.go b/pkg/tsdb/time_range.go index 085604a44f5..eb855c0976b 100644 --- a/pkg/tsdb/time_range.go +++ b/pkg/tsdb/time_range.go @@ -80,23 +80,37 @@ func tryParseUnixMsEpoch(val string) (time.Time, bool) { } func (tr *TimeRange) ParseFrom() (time.Time, error) { - return parse(tr.From, tr.now, false) + return parse(tr.From, tr.now, false, nil) } func (tr *TimeRange) ParseTo() (time.Time, error) { - return parse(tr.To, tr.now, true) + return parse(tr.To, tr.now, true, nil) } -func parse(s string, now time.Time, withRoundUp bool) (time.Time, error) { +func (tr *TimeRange) ParseFromWithLocation(location *time.Location) (time.Time, error) { + return parse(tr.From, tr.now, false, location) +} + +func (tr *TimeRange) ParseToWithLocation(location *time.Location) (time.Time, error) { + return parse(tr.To, tr.now, true, location) +} + +func parse(s string, now time.Time, withRoundUp bool, location *time.Location) (time.Time, error) { if res, ok := tryParseUnixMsEpoch(s); ok { return res, nil } - withoutNow := strings.Replace(s, "now-", "", 1) - - diff, err := time.ParseDuration("-" + withoutNow) + diff, err := time.ParseDuration("-" + s) if err != nil { - return datemath.ParseAndEvaluate(s, datemath.WithNow(now), datemath.WithRoundUp(withRoundUp)) + options := []func(*datemath.Options){ + datemath.WithNow(now), + datemath.WithRoundUp(withRoundUp), + } + if location != nil { + options = append(options, datemath.WithLocation(location)) + } + + return datemath.ParseAndEvaluate(s, options...) } return now.Add(diff), nil diff --git a/pkg/tsdb/time_range_test.go b/pkg/tsdb/time_range_test.go index 89de8e81463..4beb5914083 100644 --- a/pkg/tsdb/time_range_test.go +++ b/pkg/tsdb/time_range_test.go @@ -190,5 +190,64 @@ func TestTimeRange(t *testing.T) { _, err = tr.ParseTo() So(err, ShouldNotBeNil) }) + + now, err = time.Parse(time.RFC3339Nano, "2020-07-26T15:12:56.000Z") + So(err, ShouldBeNil) + + Convey("Can parse now-1M/M, now-1M/M with America/Chicago timezone", func() { + tr := TimeRange{ + From: "now-1M/M", + To: "now-1M/M", + now: now, + } + location, err := time.LoadLocation("America/Chicago") + So(err, ShouldBeNil) + + Convey("from now-1M/M ", func() { + expected, err := time.Parse(time.RFC3339Nano, "2020-06-01T00:00:00.000-05:00") + So(err, ShouldBeNil) + + res, err := tr.ParseFromWithLocation(location) + So(err, ShouldBeNil) + So(res, ShouldEqual, expected) + }) + + Convey("to now-1M/M ", func() { + expected, err := time.Parse(time.RFC3339Nano, "2020-06-30T23:59:59.999-05:00") + So(err, ShouldBeNil) + + res, err := tr.ParseToWithLocation(location) + So(err, ShouldBeNil) + So(res, ShouldEqual, expected) + }) + }) + + Convey("Can parse now-3h, now+2h with America/Chicago timezone", func() { + tr := TimeRange{ + From: "now-3h", + To: "now+2h", + now: now, + } + location, err := time.LoadLocation("America/Chicago") + So(err, ShouldBeNil) + + Convey("now-3h ", func() { + expected, err := time.Parse(time.RFC3339Nano, "2020-07-26T07:12:56.000-05:00") + So(err, ShouldBeNil) + + res, err := tr.ParseFromWithLocation(location) + So(err, ShouldBeNil) + So(res, ShouldEqual, expected) + }) + + Convey("now+2h ", func() { + expected, err := time.Parse(time.RFC3339Nano, "2020-07-26T12:12:56.000-05:00") + So(err, ShouldBeNil) + + res, err := tr.ParseToWithLocation(location) + So(err, ShouldBeNil) + So(res, ShouldEqual, expected) + }) + }) }) }