Core: use go-datemath in timerange utility (#23120)

* Core: use go-datemath in time_range

* Core: update timerange for negative epoch

* Core: update gtime component

* Core: clean up time_range tests

* Update pkg/components/gtime/gtime.go

Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>

* Core: clean gtime tests

* components/gtime: Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Agnès Toulet
2020-04-06 09:00:05 +02:00
committed by GitHub
parent 69bdcccd10
commit 56687a08f9
4 changed files with 148 additions and 52 deletions

View File

@@ -1,7 +1,9 @@
package tsdb
import (
"strconv"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
@@ -20,7 +22,8 @@ func TestTimeRange(t *testing.T) {
}
Convey("5m ago ", func() {
fiveMinAgo, _ := time.ParseDuration("-5m")
fiveMinAgo, err := time.ParseDuration("-5m")
So(err, ShouldBeNil)
expected := now.Add(fiveMinAgo)
res, err := tr.ParseFrom()
@@ -43,7 +46,8 @@ func TestTimeRange(t *testing.T) {
}
Convey("5h ago ", func() {
fiveHourAgo, _ := time.ParseDuration("-5h")
fiveHourAgo, err := time.ParseDuration("-5h")
So(err, ShouldBeNil)
expected := now.Add(fiveHourAgo)
res, err := tr.ParseFrom()
@@ -52,7 +56,8 @@ func TestTimeRange(t *testing.T) {
})
Convey("now-10m ", func() {
tenMinAgo, _ := time.ParseDuration("-10m")
tenMinAgo, err := time.ParseDuration("-10m")
So(err, ShouldBeNil)
expected := now.Add(tenMinAgo)
res, err := tr.ParseTo()
So(err, ShouldBeNil)
@@ -60,7 +65,101 @@ func TestTimeRange(t *testing.T) {
})
})
Convey("can parse unix epocs", func() {
now, err := time.Parse(time.RFC3339Nano, "2020-03-26T15:12:56.000Z")
So(err, ShouldBeNil)
Convey("Can parse now-1M/M, now-1M/M", func() {
tr := TimeRange{
From: "now-1M/M",
To: "now-1M/M",
now: now,
}
Convey("from now-1M/M ", func() {
expected, err := time.Parse(time.RFC3339Nano, "2020-02-01T00:00:00.000Z")
So(err, ShouldBeNil)
res, err := tr.ParseFrom()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
Convey("to now-1M/M ", func() {
expected, err := time.Parse(time.RFC3339Nano, "2020-02-29T23:59:59.999Z")
So(err, ShouldBeNil)
res, err := tr.ParseTo()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
})
Convey("Can parse now-3d, now+3w", func() {
tr := TimeRange{
From: "now-3d",
To: "now+3w",
now: now,
}
Convey("now-3d ", func() {
expected, err := time.Parse(time.RFC3339Nano, "2020-03-23T15:12:56.000Z")
So(err, ShouldBeNil)
res, err := tr.ParseFrom()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
Convey("now+3w ", func() {
expected, err := time.Parse(time.RFC3339Nano, "2020-04-16T15:12:56.000Z")
So(err, ShouldBeNil)
res, err := tr.ParseTo()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
})
Convey("Can parse 1960-02-01T07:00:00.000Z, 1965-02-03T08:00:00.000Z", func() {
tr := TimeRange{
From: "1960-02-01T07:00:00.000Z",
To: "1965-02-03T08:00:00.000Z",
now: now,
}
Convey("1960-02-01T07:00:00.000Z ", func() {
expected, err := time.Parse(time.RFC3339Nano, "1960-02-01T07:00:00.000Z")
So(err, ShouldBeNil)
res, err := tr.ParseFrom()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
Convey("1965-02-03T08:00:00.000Z ", func() {
expected, err := time.Parse(time.RFC3339Nano, "1965-02-03T08:00:00.000Z")
So(err, ShouldBeNil)
res, err := tr.ParseTo()
So(err, ShouldBeNil)
So(res, ShouldEqual, expected)
})
})
Convey("Can parse negative unix epochs", func() {
from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
tr := NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
res, err := tr.ParseFrom()
So(err, ShouldBeNil)
So(res, ShouldEqual, from)
res, err = tr.ParseTo()
So(err, ShouldBeNil)
So(res, ShouldEqual, to)
})
Convey("can parse unix epochs", func() {
var err error
tr := TimeRange{
From: "1474973725473",