2016-09-21 00:01:53 -05:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-27 07:39:51 -05:00
|
|
|
"strconv"
|
2016-09-21 00:01:53 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
func NewTimeRange(from, to string) *TimeRange {
|
|
|
|
return &TimeRange{
|
2016-09-21 00:01:53 -05:00
|
|
|
From: from,
|
|
|
|
To: to,
|
2018-01-11 08:01:49 -06:00
|
|
|
now: time.Now(),
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 11:53:12 -05:00
|
|
|
func NewFakeTimeRange(from, to string, now time.Time) *TimeRange {
|
|
|
|
return &TimeRange{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
now: now,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 00:01:53 -05:00
|
|
|
type TimeRange struct {
|
|
|
|
From string
|
|
|
|
To string
|
2018-01-11 08:01:49 -06:00
|
|
|
now time.Time
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-28 03:37:30 -05:00
|
|
|
func (tr *TimeRange) GetFromAsMsEpoch() int64 {
|
|
|
|
return tr.MustGetFrom().UnixNano() / int64(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-04-12 11:53:12 -05:00
|
|
|
func (tr *TimeRange) GetFromAsSecondsEpoch() int64 {
|
|
|
|
return tr.GetFromAsMsEpoch() / 1000
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:37:30 -05:00
|
|
|
func (tr *TimeRange) GetToAsMsEpoch() int64 {
|
|
|
|
return tr.MustGetTo().UnixNano() / int64(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-04-12 11:53:12 -05:00
|
|
|
func (tr *TimeRange) GetToAsSecondsEpoch() int64 {
|
|
|
|
return tr.GetToAsMsEpoch() / 1000
|
|
|
|
}
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
func (tr *TimeRange) MustGetFrom() time.Time {
|
|
|
|
if res, err := tr.ParseFrom(); err != nil {
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
} else {
|
|
|
|
return res
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
2016-09-27 11:17:39 -05:00
|
|
|
}
|
2016-09-27 07:39:51 -05:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
func (tr *TimeRange) MustGetTo() time.Time {
|
|
|
|
if res, err := tr.ParseTo(); err != nil {
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
} else {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryParseUnixMsEpoch(val string) (time.Time, bool) {
|
|
|
|
if val, err := strconv.ParseInt(val, 10, 64); err == nil {
|
|
|
|
seconds := val / 1000
|
|
|
|
nano := (val - seconds*1000) * 1000000
|
|
|
|
return time.Unix(seconds, nano), true
|
|
|
|
}
|
|
|
|
return time.Time{}, false
|
|
|
|
}
|
2016-09-21 00:01:53 -05:00
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
func (tr *TimeRange) ParseFrom() (time.Time, error) {
|
|
|
|
if res, ok := tryParseUnixMsEpoch(tr.From); ok {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fromRaw := strings.Replace(tr.From, "now-", "", 1)
|
2016-09-21 00:01:53 -05:00
|
|
|
diff, err := time.ParseDuration("-" + fromRaw)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:01:49 -06:00
|
|
|
return tr.now.Add(diff), nil
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
func (tr *TimeRange) ParseTo() (time.Time, error) {
|
2016-09-21 00:01:53 -05:00
|
|
|
if tr.To == "now" {
|
2018-01-11 08:01:49 -06:00
|
|
|
return tr.now, nil
|
2016-09-21 00:01:53 -05:00
|
|
|
} else if strings.HasPrefix(tr.To, "now-") {
|
|
|
|
withoutNow := strings.Replace(tr.To, "now-", "", 1)
|
|
|
|
|
|
|
|
diff, err := time.ParseDuration("-" + withoutNow)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:01:49 -06:00
|
|
|
return tr.now.Add(diff), nil
|
2016-09-21 00:01:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-27 11:17:39 -05:00
|
|
|
if res, ok := tryParseUnixMsEpoch(tr.To); ok {
|
|
|
|
return res, nil
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 00:01:53 -05:00
|
|
|
return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To)
|
|
|
|
}
|
2018-03-20 13:40:10 -05:00
|
|
|
|
|
|
|
// EpochPrecisionToMs converts epoch precision to millisecond, if needed.
|
|
|
|
// Only seconds to milliseconds supported right now
|
|
|
|
func EpochPrecisionToMs(value float64) float64 {
|
2018-04-10 09:36:00 -05:00
|
|
|
s := strconv.FormatFloat(value, 'e', -1, 64)
|
|
|
|
if strings.HasSuffix(s, "e+09") {
|
|
|
|
return value * float64(1e3)
|
2018-03-20 13:40:10 -05:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:36:00 -05:00
|
|
|
if strings.HasSuffix(s, "e+18") {
|
|
|
|
return value / float64(time.Millisecond)
|
2018-04-10 03:32:30 -05:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:36:00 -05:00
|
|
|
return value
|
2018-03-20 13:40:10 -05:00
|
|
|
}
|