feat(testdata): lots of work on new test data data source and scenarios
This commit is contained in:
+33
-9
@@ -7,8 +7,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewTimeRange(from, to string) TimeRange {
|
||||
return TimeRange{
|
||||
func NewTimeRange(from, to string) *TimeRange {
|
||||
return &TimeRange{
|
||||
From: from,
|
||||
To: to,
|
||||
Now: time.Now(),
|
||||
@@ -21,13 +21,37 @@ type TimeRange struct {
|
||||
Now time.Time
|
||||
}
|
||||
|
||||
func (tr TimeRange) FromTime() (time.Time, error) {
|
||||
if val, err := strconv.ParseInt(tr.From, 10, 64); err == nil {
|
||||
return time.Unix(val, 0), nil
|
||||
func (tr *TimeRange) MustGetFrom() time.Time {
|
||||
if res, err := tr.ParseFrom(); err != nil {
|
||||
return time.Unix(0, 0)
|
||||
} else {
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseFrom() (time.Time, error) {
|
||||
if res, ok := tryParseUnixMsEpoch(tr.From); ok {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
fromRaw := strings.Replace(tr.From, "now-", "", 1)
|
||||
|
||||
diff, err := time.ParseDuration("-" + fromRaw)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
@@ -36,7 +60,7 @@ func (tr TimeRange) FromTime() (time.Time, error) {
|
||||
return tr.Now.Add(diff), nil
|
||||
}
|
||||
|
||||
func (tr TimeRange) ToTime() (time.Time, error) {
|
||||
func (tr *TimeRange) ParseTo() (time.Time, error) {
|
||||
if tr.To == "now" {
|
||||
return tr.Now, nil
|
||||
} else if strings.HasPrefix(tr.To, "now-") {
|
||||
@@ -50,8 +74,8 @@ func (tr TimeRange) ToTime() (time.Time, error) {
|
||||
return tr.Now.Add(diff), nil
|
||||
}
|
||||
|
||||
if val, err := strconv.ParseInt(tr.To, 10, 64); err == nil {
|
||||
return time.Unix(val, 0), nil
|
||||
if res, ok := tryParseUnixMsEpoch(tr.To); ok {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To)
|
||||
|
||||
Reference in New Issue
Block a user