refactor(tsdb): remove toUnix from timerange

This commit is contained in:
bergquist
2016-09-21 13:41:25 +02:00
parent 9534a04d3d
commit af551b8825
2 changed files with 12 additions and 40 deletions

View File

@@ -20,17 +20,6 @@ type TimeRange struct {
Now time.Time Now time.Time
} }
func (tr TimeRange) FromUnix() (int64, error) {
fromRaw := strings.Replace(tr.From, "now-", "", 1)
diff, err := time.ParseDuration("-" + fromRaw)
if err != nil {
return 0, err
}
return tr.Now.Add(diff).Unix(), nil
}
func (tr TimeRange) FromTime() (time.Time, error) { func (tr TimeRange) FromTime() (time.Time, error) {
fromRaw := strings.Replace(tr.From, "now-", "", 1) fromRaw := strings.Replace(tr.From, "now-", "", 1)
@@ -42,23 +31,6 @@ func (tr TimeRange) FromTime() (time.Time, error) {
return tr.Now.Add(diff), nil return tr.Now.Add(diff), nil
} }
func (tr TimeRange) ToUnix() (int64, error) {
if tr.To == "now" {
return tr.Now.Unix(), nil
} else if strings.HasPrefix(tr.To, "now-") {
withoutNow := strings.Replace(tr.To, "now-", "", 1)
diff, err := time.ParseDuration("-" + withoutNow)
if err != nil {
return 0, nil
}
return tr.Now.Add(diff).Unix(), nil
}
return 0, fmt.Errorf("cannot parse to value %s", tr.To)
}
func (tr TimeRange) ToTime() (time.Time, error) { func (tr TimeRange) ToTime() (time.Time, error) {
if tr.To == "now" { if tr.To == "now" {
return tr.Now, nil return tr.Now, nil

View File

@@ -23,15 +23,15 @@ func TestTimeRange(t *testing.T) {
fiveMinAgo, _ := time.ParseDuration("-5m") fiveMinAgo, _ := time.ParseDuration("-5m")
expected := now.Add(fiveMinAgo) expected := now.Add(fiveMinAgo)
res, err := tr.FromUnix() res, err := tr.FromTime()
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, expected.Unix()) So(res.Unix(), ShouldEqual, expected.Unix())
}) })
Convey("now ", func() { Convey("now ", func() {
res, err := tr.ToUnix() res, err := tr.ToTime()
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, now.Unix()) So(res.Unix(), ShouldEqual, now.Unix())
}) })
}) })
@@ -43,20 +43,20 @@ func TestTimeRange(t *testing.T) {
} }
Convey("5h ago ", func() { Convey("5h ago ", func() {
fiveMinAgo, _ := time.ParseDuration("-5h") fiveHourAgo, _ := time.ParseDuration("-5h")
expected := now.Add(fiveMinAgo) expected := now.Add(fiveHourAgo)
res, err := tr.FromUnix() res, err := tr.FromTime()
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, expected.Unix()) So(res.Unix(), ShouldEqual, expected.Unix())
}) })
Convey("now-10m ", func() { Convey("now-10m ", func() {
fiveMinAgo, _ := time.ParseDuration("-10m") fiveMinAgo, _ := time.ParseDuration("-10m")
expected := now.Add(fiveMinAgo) expected := now.Add(fiveMinAgo)
res, err := tr.ToUnix() res, err := tr.ToTime()
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, expected.Unix()) So(res.Unix(), ShouldEqual, expected.Unix())
}) })
}) })
@@ -68,10 +68,10 @@ func TestTimeRange(t *testing.T) {
Now: now, Now: now,
} }
_, err = tr.FromUnix() _, err = tr.FromTime()
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
_, err = tr.ToUnix() _, err = tr.ToTime()
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
}) })
}) })