|
|
@@ -1,8 +1,9 @@
|
|
|
-package util
|
|
|
+package timeutil
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
)
|
|
|
@@ -64,80 +65,6 @@ func DurationOffsetStrings(duration, offset time.Duration) (string, string) {
|
|
|
return DurationString(duration), DurationString(offset)
|
|
|
}
|
|
|
|
|
|
-// ParseDuration converts a Prometheus-style duration string into a Duration
|
|
|
-func ParseDuration(duration string) (*time.Duration, error) {
|
|
|
- unitStr := duration[len(duration)-1:]
|
|
|
- var unit time.Duration
|
|
|
- switch unitStr {
|
|
|
- case "s":
|
|
|
- unit = time.Second
|
|
|
- case "m":
|
|
|
- unit = time.Minute
|
|
|
- case "h":
|
|
|
- unit = time.Hour
|
|
|
- case "d":
|
|
|
- unit = 24.0 * time.Hour
|
|
|
- default:
|
|
|
- return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
|
|
|
- }
|
|
|
-
|
|
|
- amountStr := duration[:len(duration)-1]
|
|
|
- amount, err := strconv.ParseInt(amountStr, 10, 64)
|
|
|
- if err != nil {
|
|
|
- return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
|
|
|
- }
|
|
|
-
|
|
|
- dur := time.Duration(amount) * unit
|
|
|
- return &dur, nil
|
|
|
-}
|
|
|
-
|
|
|
-// ParseTimeRange returns a start and end time, respectively, which are converted from
|
|
|
-// a duration and offset, defined as strings with Prometheus-style syntax.
|
|
|
-func ParseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
|
|
|
- // endTime defaults to the current time, unless an offset is explicity declared,
|
|
|
- // in which case it shifts endTime back by given duration
|
|
|
- endTime := time.Now()
|
|
|
- if offset != "" {
|
|
|
- o, err := ParseDuration(offset)
|
|
|
- if err != nil {
|
|
|
- return nil, nil, fmt.Errorf("error parsing offset (%s): %s", offset, err)
|
|
|
- }
|
|
|
- endTime = endTime.Add(-1 * *o)
|
|
|
- }
|
|
|
-
|
|
|
- // if duration is defined in terms of days, convert to hours
|
|
|
- // e.g. convert "2d" to "48h"
|
|
|
- durationNorm, err := normalizeTimeParam(duration)
|
|
|
- if err != nil {
|
|
|
- return nil, nil, fmt.Errorf("error parsing duration (%s): %s", duration, err)
|
|
|
- }
|
|
|
-
|
|
|
- // convert time duration into start and end times, formatted
|
|
|
- // as ISO datetime strings
|
|
|
- dur, err := time.ParseDuration(durationNorm)
|
|
|
- if err != nil {
|
|
|
- return nil, nil, fmt.Errorf("errorf parsing duration (%s): %s", durationNorm, err)
|
|
|
- }
|
|
|
- startTime := endTime.Add(-1 * dur)
|
|
|
-
|
|
|
- return &startTime, &endTime, nil
|
|
|
-}
|
|
|
-
|
|
|
-func normalizeTimeParam(param string) (string, error) {
|
|
|
- // convert days to hours
|
|
|
- if param[len(param)-1:] == "d" {
|
|
|
- count := param[:len(param)-1]
|
|
|
- val, err := strconv.ParseInt(count, 10, 64)
|
|
|
- if err != nil {
|
|
|
- return "", err
|
|
|
- }
|
|
|
- val = val * 24
|
|
|
- param = fmt.Sprintf("%dh", val)
|
|
|
- }
|
|
|
-
|
|
|
- return param, nil
|
|
|
-}
|
|
|
-
|
|
|
// FormatStoreResolution provides a clean notation for ETL store resolutions.
|
|
|
// e.g. daily => 1d; hourly => 1h
|
|
|
func FormatStoreResolution(dur time.Duration) string {
|
|
|
@@ -223,3 +150,92 @@ func (jt *JobTicker) TickIn(d time.Duration) {
|
|
|
}
|
|
|
}(d)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// ParseDuration converts a Prometheus-style duration string into a Duration
|
|
|
+func ParseDuration(duration string) (*time.Duration, error) {
|
|
|
+ // Trim prefix of Prometheus format duration
|
|
|
+ duration = CleanDurationString(duration)
|
|
|
+ if len(duration) < 2 {
|
|
|
+ return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
|
|
|
+ }
|
|
|
+ unitStr := duration[len(duration)-1:]
|
|
|
+ var unit time.Duration
|
|
|
+ switch unitStr {
|
|
|
+ case "s":
|
|
|
+ unit = time.Second
|
|
|
+ case "m":
|
|
|
+ unit = time.Minute
|
|
|
+ case "h":
|
|
|
+ unit = time.Hour
|
|
|
+ case "d":
|
|
|
+ unit = 24.0 * time.Hour
|
|
|
+ default:
|
|
|
+ return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
|
|
|
+ }
|
|
|
+
|
|
|
+ amountStr := duration[:len(duration)-1]
|
|
|
+ amount, err := strconv.ParseInt(amountStr, 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("error parsing duration: %s did not match expected format [0-9+](s|m|d|h)", duration)
|
|
|
+ }
|
|
|
+
|
|
|
+ dur := time.Duration(amount) * unit
|
|
|
+ return &dur, nil
|
|
|
+}
|
|
|
+
|
|
|
+// CleanDurationString removes prometheus formatted prefix "offset " allong with leading a trailing whitespace
|
|
|
+// from duration string, leaving behind a string with format [0-9+](s|m|d|h)
|
|
|
+func CleanDurationString(duration string) string {
|
|
|
+ duration = strings.TrimSpace(duration)
|
|
|
+ duration = strings.TrimPrefix(duration, "offset ")
|
|
|
+ return duration
|
|
|
+}
|
|
|
+
|
|
|
+// ParseTimeRange returns a start and end time, respectively, which are converted from
|
|
|
+// a duration and offset, defined as strings with Prometheus-style syntax.
|
|
|
+func ParseTimeRange(duration, offset string) (*time.Time, *time.Time, error) {
|
|
|
+ // endTime defaults to the current time, unless an offset is explicity declared,
|
|
|
+ // in which case it shifts endTime back by given duration
|
|
|
+ endTime := time.Now()
|
|
|
+ if offset != "" {
|
|
|
+ o, err := ParseDuration(offset)
|
|
|
+ if err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("error parsing offset (%s): %s", offset, err)
|
|
|
+ }
|
|
|
+ endTime = endTime.Add(-1 * *o)
|
|
|
+ }
|
|
|
+
|
|
|
+ // if duration is defined in terms of days, convert to hours
|
|
|
+ // e.g. convert "2d" to "48h"
|
|
|
+ durationNorm, err := normalizeTimeParam(duration)
|
|
|
+ if err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("error parsing duration (%s): %s", duration, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // convert time duration into start and end times, formatted
|
|
|
+ // as ISO datetime strings
|
|
|
+ dur, err := time.ParseDuration(durationNorm)
|
|
|
+ if err != nil {
|
|
|
+ return nil, nil, fmt.Errorf("errorf parsing duration (%s): %s", durationNorm, err)
|
|
|
+ }
|
|
|
+ startTime := endTime.Add(-1 * dur)
|
|
|
+
|
|
|
+ return &startTime, &endTime, nil
|
|
|
+}
|
|
|
+
|
|
|
+func normalizeTimeParam(param string) (string, error) {
|
|
|
+ // convert days to hours
|
|
|
+ if param[len(param)-1:] == "d" {
|
|
|
+ count := param[:len(param)-1]
|
|
|
+ val, err := strconv.ParseInt(count, 10, 64)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ val = val * 24
|
|
|
+ param = fmt.Sprintf("%dh", val)
|
|
|
+ }
|
|
|
+
|
|
|
+ return param, nil
|
|
|
+}
|