app/vmselect/prometheus: support d, w and y suffixes for durations passed to step in /api/v1/query_range like Prometheus does

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/641
This commit is contained in:
Aliaksandr Valialkin 2020-07-22 16:26:16 +03:00
parent bd4299fafe
commit 20d0c41ac5

View file

@ -977,14 +977,14 @@ func getTime(r *http.Request, argKey string, defaultValue int64) (int64, error)
return maxTimeMsecs, nil return maxTimeMsecs, nil
} }
// Try parsing duration relative to the current time // Try parsing duration relative to the current time
d, err1 := time.ParseDuration(argValue) d, err1 := metricsql.DurationValue(argValue, 0)
if err1 != nil { if err1 != nil {
return 0, fmt.Errorf("cannot parse %q=%q: %w", argKey, argValue, err) return 0, fmt.Errorf("cannot parse %q=%q: %w", argKey, argValue, err)
} }
if d > 0 { if d > 0 {
d = -d d = -d
} }
t = time.Now().Add(d) t = time.Now().Add(time.Duration(d) * time.Millisecond)
} }
secs = float64(t.UnixNano()) / 1e9 secs = float64(t.UnixNano()) / 1e9
} }
@ -1019,11 +1019,11 @@ func getDuration(r *http.Request, argKey string, defaultValue int64) (int64, err
secs, err := strconv.ParseFloat(argValue, 64) secs, err := strconv.ParseFloat(argValue, 64)
if err != nil { if err != nil {
// Try parsing string format // Try parsing string format
d, err := time.ParseDuration(argValue) d, err := metricsql.DurationValue(argValue, 0)
if err != nil { if err != nil {
return 0, fmt.Errorf("cannot parse %q=%q: %w", argKey, argValue, err) return 0, fmt.Errorf("cannot parse %q=%q: %w", argKey, argValue, err)
} }
secs = d.Seconds() secs = float64(d) / 1000
} }
msecs := int64(secs * 1e3) msecs := int64(secs * 1e3)
if msecs <= 0 || msecs > maxDurationMsecs { if msecs <= 0 || msecs > maxDurationMsecs {