2016-09-21 00:01:53 -05:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2020-04-06 02:00:05 -05:00
|
|
|
"strconv"
|
2016-09-21 00:01:53 -05:00
|
|
|
"testing"
|
2020-04-06 02:00:05 -05:00
|
|
|
|
2016-09-21 00:01:53 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeRange(t *testing.T) {
|
|
|
|
Convey("Time range", t, func() {
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
Convey("Can parse 5m, now", func() {
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "5m",
|
|
|
|
To: "now",
|
2018-01-11 08:01:49 -06:00
|
|
|
now: now,
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Convey("5m ago ", func() {
|
2020-04-06 02:00:05 -05:00
|
|
|
fiveMinAgo, err := time.ParseDuration("-5m")
|
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 00:01:53 -05:00
|
|
|
expected := now.Add(fiveMinAgo)
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err := tr.ParseFrom()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("now ", func() {
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err := tr.ParseTo()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, now.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can parse 5h, now-10m", func() {
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "5h",
|
|
|
|
To: "now-10m",
|
2018-01-11 08:01:49 -06:00
|
|
|
now: now,
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Convey("5h ago ", func() {
|
2020-04-06 02:00:05 -05:00
|
|
|
fiveHourAgo, err := time.ParseDuration("-5h")
|
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
expected := now.Add(fiveHourAgo)
|
2016-09-21 00:01:53 -05:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err := tr.ParseFrom()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("now-10m ", func() {
|
2020-04-06 02:00:05 -05:00
|
|
|
tenMinAgo, err := time.ParseDuration("-10m")
|
|
|
|
So(err, ShouldBeNil)
|
2017-05-24 01:38:13 -05:00
|
|
|
expected := now.Add(tenMinAgo)
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err := tr.ParseTo()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-06 02:00:05 -05:00
|
|
|
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() {
|
2016-09-27 07:39:51 -05:00
|
|
|
var err error
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "1474973725473",
|
|
|
|
To: "1474975757930",
|
2018-01-11 08:01:49 -06:00
|
|
|
now: now,
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err := tr.ParseFrom()
|
2016-09-27 07:39:51 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-05-06 02:27:45 -05:00
|
|
|
So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474973725473))
|
2016-09-27 07:39:51 -05:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
res, err = tr.ParseTo()
|
2016-09-27 07:39:51 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-05-06 02:27:45 -05:00
|
|
|
So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474975757930))
|
2016-09-27 07:39:51 -05:00
|
|
|
})
|
|
|
|
|
2016-09-21 00:01:53 -05:00
|
|
|
Convey("Cannot parse asdf", func() {
|
|
|
|
var err error
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "asdf",
|
|
|
|
To: "asdf",
|
2018-01-11 08:01:49 -06:00
|
|
|
now: now,
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
_, err = tr.ParseFrom()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
_, err = tr.ParseTo()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|