Skip to content

Commit

Permalink
[query] Added functionality to time parsing logic (#2751)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywahle authored Oct 20, 2020
1 parent 2ca175b commit d05741f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
29 changes: 18 additions & 11 deletions src/query/graphite/common/basic_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
"seconds": time.Second,
"m": time.Minute,
"min": time.Minute,
"mins": time.Minute,
"minute": time.Minute,
"minutes": time.Minute,
"h": time.Hour,
Expand Down Expand Up @@ -181,19 +182,26 @@ func Count(ctx *Context, seriesList ts.SeriesList, renamer SeriesListRenamer) (t
}

// ParseInterval parses an interval string and returns the corresponding duration.
func ParseInterval(s string) (time.Duration, error) {
if m := reInterval.FindStringSubmatch(strings.TrimSpace(s)); len(m) != 0 {
amount, err := strconv.ParseInt(m[1], 10, 32)
func ParseInterval(fullInterval string) (time.Duration, error) {
allIntervals := reInterval.FindAllString(fullInterval, -1)
output := time.Duration(0)
if allIntervals == nil {
return 0, errors.NewInvalidParamsError(fmt.Errorf("Unrecognized interval string: %s", fullInterval))
}

for _, interval := range allIntervals {
if m := reInterval.FindStringSubmatch(strings.TrimSpace(interval)); len(m) != 0 {
amount, err := strconv.ParseInt(m[1], 10, 32)
if err != nil {
return 0, errors.NewInvalidParamsError(err)
}

if err != nil {
return 0, errors.NewInvalidParamsError(err)
interval := intervals[strings.ToLower(m[2])]
output += (interval * time.Duration(amount))
}

interval := intervals[strings.ToLower(m[2])]
return interval * time.Duration(amount), nil
}

return 0, ErrInvalidIntervalFormat
return output, nil
}

// ConstantLine draws a horizontal line at a specified value
Expand Down Expand Up @@ -281,6 +289,5 @@ func init() {
intervalNames = append(intervalNames, name)
}

reInterval = regexp.MustCompile(fmt.Sprintf("(?i)^([+-]?[0-9]+)(%s)$",
strings.Join(intervalNames, "|")))
reInterval = regexp.MustCompile("(?i)([+-]?[0-9]+)(s|min|h|d|w|mon|y)([A-Z]*)")
}
1 change: 1 addition & 0 deletions src/query/graphite/common/basic_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestParseInterval(t *testing.T) {
"6months": time.Hour * 24 * 30 * 6,
"2y": time.Hour * 24 * 365 * 2,
"10years": time.Hour * 24 * 365 * 10,
"1w5min": (time.Hour * 24 * 7) + (time.Minute * 5),
}

for s, d := range tests {
Expand Down
35 changes: 26 additions & 9 deletions src/query/graphite/graphite/timespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/m3db/m3/src/query/graphite/errors"
)

var reRelativeTime = regexp.MustCompile(`(?i)^\-([0-9]+)(s|min|h|d|w|mon|y)$`) // allows -3min, -4d, etc.
var reTimeOffset = regexp.MustCompile(`(?i)^(\-|\+)([0-9]+)(s|min|h|d|w|mon|y)$`) // -3min, +3min, -4d, +4d
var reRelativeTime = regexp.MustCompile(`(?i)^\-([0-9]+)(s|min|h|d|w|mon|y)(.*)$`) // allows -3min, -4d, etc.
var reTimeOffset = regexp.MustCompile(`(?i)^(\-|\+)([0-9]+)(s|min|h|d|w|mon|y)(.*)$`) // -3min, +3min, -4d, +4d
var reMonthAndDay = regexp.MustCompile(`(?i)^(january|february|march|april|may|june|july|august|september|october|november|december)([0-9]{1,2}?)$`)
var reDayOfWeek = regexp.MustCompile(`(?i)^(sunday|monday|tuesday|wednesday|thursday|friday|saturday)$`)
var reDayOfWeekOffset = regexp.MustCompile(`(?i)^(\-|\+)(sunday|monday|tuesday|wednesday|thursday|friday|saturday)$`) // +monday, +thursday, etc
Expand All @@ -40,13 +40,30 @@ var reAM = regexp.MustCompile(`(?i)^(([0-1]?)([0-9])am)([[:ascii:]])*$`)
var reTimeOfDayWithColon = regexp.MustCompile(`(?i)^(([0-1]?)([0-9]):([0-5])([0-9])((am|pm)?))([[:ascii:]])*$`) // 8:12pm, 11:20am, 2:00am

var periods = map[string]time.Duration{
"s": time.Second,
"min": time.Minute,
"h": time.Hour,
"d": time.Hour * 24,
"w": time.Hour * 24 * 7,
"mon": time.Hour * 24 * 30,
"y": time.Hour * 24 * 365,
"s": time.Second,
"sec": time.Second,
"seconds": time.Second,
"m": time.Minute,
"min": time.Minute,
"mins": time.Minute,
"minute": time.Minute,
"minutes": time.Minute,
"h": time.Hour,
"hr": time.Hour,
"hour": time.Hour,
"hours": time.Hour,
"d": time.Hour * 24,
"day": time.Hour * 24,
"days": time.Hour * 24,
"w": time.Hour * 24 * 7,
"week": time.Hour * 24 * 7,
"weeks": time.Hour * 24 * 7,
"mon": time.Hour * 24 * 30,
"month": time.Hour * 24 * 30,
"months": time.Hour * 24 * 30,
"y": time.Hour * 24 * 365,
"year": time.Hour * 24 * 365,
"years": time.Hour * 24 * 365,
}

var weekdays = map[string]int{
Expand Down
3 changes: 3 additions & 0 deletions src/query/graphite/graphite/timespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ func TestParseTime(t *testing.T) {
{"midnight", time.Date(2013, time.April, 3, 0, 0, 0, 0, time.UTC)},
{"midnight+1h", time.Date(2013, time.April, 3, 1, 0, 0, 0, time.UTC)},
{"april08+1d", time.Date(2013, time.April, 9, 4, 5, 0, 0, time.UTC)},
{"april08+1day", time.Date(2013, time.April, 9, 4, 5, 0, 0, time.UTC)},
{"monday", time.Date(2013, time.April, 1, 4, 5, 0, 0, time.UTC)},
{"9am monday", time.Date(2013, time.April, 1, 9, 0, 0, 0, time.UTC)},
{"9am monday +5min", time.Date(2013, time.April, 1, 9, 5, 0, 0, time.UTC)},
{"9am monday +5mins", time.Date(2013, time.April, 1, 9, 5, 0, 0, time.UTC)},
{"9:00am monday +5min", time.Date(2013, time.April, 1, 9, 5, 0, 0, time.UTC)},
}

Expand Down Expand Up @@ -93,6 +95,7 @@ func TestParseOffset(t *testing.T) {
{"+4h", 4 * time.Hour},
{"+35MIN", 35 * time.Minute},
{"+10s", 10 * time.Second},
{"+1day", time.Hour * 24},
}

for _, test := range tests {
Expand Down

0 comments on commit d05741f

Please sign in to comment.