forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'grafana/master' into backend-service-in…
…-grafanaui * grafana/master: Annotations: Improve annotation option tooltips (grafana#17384) InfluxDB: Fixes single quotes are not escaped (grafana#17398) Chore: Bump axios to 0.19.0 (grafana#17403) Alerting: golint fixes for alerting (grafana#17246) Batch disable users (grafana#17254) Chore: Remove unused properties in explore (grafana#17359) MySQL/Postgres/MSSQL: Add parsing for day, weeks and year intervals in macros (grafana#13086) Security: Prevent csv formula injection attack (grafana#17363)
- Loading branch information
Showing
70 changed files
with
480 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package gtime | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// ParseInterval parses and interval with support for all units that Grafana uses. | ||
func ParseInterval(interval string) (time.Duration, error) { | ||
re := regexp.MustCompile(`(\d+)([wdy])`) | ||
result := re.FindSubmatch([]byte(interval)) | ||
|
||
if len(result) == 3 { | ||
num, _ := strconv.Atoi(string(result[1])) | ||
period := string(result[2]) | ||
|
||
if period == `d` { | ||
return time.Hour * 24 * time.Duration(num), nil | ||
} else if period == `w` { | ||
return time.Hour * 24 * 7 * time.Duration(num), nil | ||
} else { | ||
return time.Hour * 24 * 7 * 365 * time.Duration(num), nil | ||
} | ||
} else { | ||
return time.ParseDuration(interval) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gtime | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseInterval(t *testing.T) { | ||
tcs := []struct { | ||
interval string | ||
duration time.Duration | ||
err error | ||
}{ | ||
{interval: "1d", duration: time.Hour * 24}, | ||
{interval: "1w", duration: time.Hour * 24 * 7}, | ||
{interval: "1y", duration: time.Hour * 24 * 7 * 365}, | ||
{interval: "1M", err: errors.New("time: unknown unit M in duration 1M")}, | ||
{interval: "invalid-duration", err: errors.New("time: invalid duration invalid-duration")}, | ||
} | ||
|
||
for i, tc := range tcs { | ||
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) { | ||
res, err := ParseInterval(tc.interval) | ||
if err != nil && err.Error() != tc.err.Error() { | ||
t.Fatalf("expected '%v' got '%v'", tc.err, err) | ||
} | ||
if res != tc.duration { | ||
t.Errorf("expected %v got %v", tc.duration, res) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.