refactor(tsdb): remove toUnix from timerange

pull/6089/head
bergquist 9 years ago
parent 9534a04d3d
commit af551b8825
  1. 28
      pkg/tsdb/time_range.go
  2. 24
      pkg/tsdb/time_range_test.go

@ -20,17 +20,6 @@ type TimeRange struct {
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) {
fromRaw := strings.Replace(tr.From, "now-", "", 1)
@ -42,23 +31,6 @@ func (tr TimeRange) FromTime() (time.Time, error) {
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) {
if tr.To == "now" {
return tr.Now, nil

@ -23,15 +23,15 @@ func TestTimeRange(t *testing.T) {
fiveMinAgo, _ := time.ParseDuration("-5m")
expected := now.Add(fiveMinAgo)
res, err := tr.FromUnix()
res, err := tr.FromTime()
So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, expected.Unix())
So(res.Unix(), ShouldEqual, expected.Unix())
})
Convey("now ", func() {
res, err := tr.ToUnix()
res, err := tr.ToTime()
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() {
fiveMinAgo, _ := time.ParseDuration("-5h")
expected := now.Add(fiveMinAgo)
fiveHourAgo, _ := time.ParseDuration("-5h")
expected := now.Add(fiveHourAgo)
res, err := tr.FromUnix()
res, err := tr.FromTime()
So(err, ShouldBeNil)
So(res, ShouldAlmostEqual, expected.Unix())
So(res.Unix(), ShouldEqual, expected.Unix())
})
Convey("now-10m ", func() {
fiveMinAgo, _ := time.ParseDuration("-10m")
expected := now.Add(fiveMinAgo)
res, err := tr.ToUnix()
res, err := tr.ToTime()
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,
}
_, err = tr.FromUnix()
_, err = tr.FromTime()
So(err, ShouldNotBeNil)
_, err = tr.ToUnix()
_, err = tr.ToTime()
So(err, ShouldNotBeNil)
})
})

Loading…
Cancel
Save