Commit 494c20db by Arve Knudsen Committed by GitHub

gtime: Add ParseDuration function (#28525)

* gtime: Make calculations independent of current time

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Introduce gtime.ParseDuration function

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* gtime: Fix ParseDuration

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent f71f03bf
...@@ -10,17 +10,19 @@ import ( ...@@ -10,17 +10,19 @@ import (
var dateUnitPattern = regexp.MustCompile(`^(\d+)([dwMy])$`) var dateUnitPattern = regexp.MustCompile(`^(\d+)([dwMy])$`)
// ParseInterval parses an interval with support for all units that Grafana uses. // ParseInterval parses an interval with support for all units that Grafana uses.
func ParseInterval(interval string) (time.Duration, error) { // An interval is relative the current wall time.
result := dateUnitPattern.FindSubmatch([]byte(interval)) func ParseInterval(inp string) (time.Duration, error) {
dur, period, err := parse(inp)
if len(result) != 3 { if err != nil {
return time.ParseDuration(interval) return 0, err
}
if period == "" {
return dur, nil
} }
num, _ := strconv.Atoi(string(result[1])) num := int(dur)
period := string(result[2])
now := time.Now()
now := time.Now()
switch period { switch period {
case "d": case "d":
return now.Sub(now.AddDate(0, 0, -num)), nil return now.Sub(now.AddDate(0, 0, -num)), nil
...@@ -32,5 +34,52 @@ func ParseInterval(interval string) (time.Duration, error) { ...@@ -32,5 +34,52 @@ func ParseInterval(interval string) (time.Duration, error) {
return now.Sub(now.AddDate(-num, 0, 0)), nil return now.Sub(now.AddDate(-num, 0, 0)), nil
} }
return 0, fmt.Errorf("ParseInterval: invalid duration %q", interval) return 0, fmt.Errorf("invalid interval %q", inp)
}
// ParseDuration parses a duration with support for all units that Grafana uses.
// Durations are independent of wall time.
func ParseDuration(inp string) (time.Duration, error) {
dur, period, err := parse(inp)
if err != nil {
return 0, err
}
if period == "" {
return dur, nil
}
// The average number of days in a year, using the Julian calendar
const daysInAYear = 365.25
const day = 24 * time.Hour
const week = 7 * day
const year = time.Duration(float64(day) * daysInAYear)
const month = time.Duration(float64(year) / 12)
switch period {
case "d":
return dur * day, nil
case "w":
return dur * week, nil
case "M":
return dur * month, nil
case "y":
return dur * year, nil
}
return 0, fmt.Errorf("invalid duration %q", inp)
}
func parse(inp string) (time.Duration, string, error) {
result := dateUnitPattern.FindSubmatch([]byte(inp))
if len(result) != 3 {
dur, err := time.ParseDuration(inp)
return dur, "", err
}
num, err := strconv.Atoi(string(result[1]))
if err != nil {
return 0, "", err
}
return time.Duration(num), string(result[2]), nil
} }
...@@ -13,27 +13,57 @@ func TestParseInterval(t *testing.T) { ...@@ -13,27 +13,57 @@ func TestParseInterval(t *testing.T) {
now := time.Now() now := time.Now()
tcs := []struct { tcs := []struct {
interval string inp string
duration time.Duration duration time.Duration
err *regexp.Regexp err *regexp.Regexp
}{ }{
{interval: "1d", duration: now.Sub(now.AddDate(0, 0, -1))}, {inp: "1d", duration: now.Sub(now.AddDate(0, 0, -1))},
{interval: "1w", duration: now.Sub(now.AddDate(0, 0, -7))}, {inp: "1w", duration: now.Sub(now.AddDate(0, 0, -7))},
{interval: "2w", duration: now.Sub(now.AddDate(0, 0, -14))}, {inp: "2w", duration: now.Sub(now.AddDate(0, 0, -14))},
{interval: "1M", duration: now.Sub(now.AddDate(0, -1, 0))}, {inp: "1M", duration: now.Sub(now.AddDate(0, -1, 0))},
{interval: "1y", duration: now.Sub(now.AddDate(-1, 0, 0))}, {inp: "1y", duration: now.Sub(now.AddDate(-1, 0, 0))},
{interval: "5y", duration: now.Sub(now.AddDate(-5, 0, 0))}, {inp: "5y", duration: now.Sub(now.AddDate(-5, 0, 0))},
{interval: "invalid-duration", err: regexp.MustCompile(`^time: invalid duration "?invalid-duration"?$`)}, {inp: "invalid-duration", err: regexp.MustCompile(`^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.inp)
if tc.err == nil {
require.NoError(t, err, "input %q", tc.inp)
require.Equal(t, tc.duration, res, "input %q", tc.inp)
} else {
require.Error(t, err, "input %q", tc.inp)
require.Regexp(t, tc.err, err.Error())
}
})
}
}
func TestParseDuration(t *testing.T) {
tcs := []struct {
inp string
duration time.Duration
err *regexp.Regexp
}{
{inp: "1s", duration: time.Second},
{inp: "1m", duration: time.Minute},
{inp: "1h", duration: time.Hour},
{inp: "1d", duration: 24 * time.Hour},
{inp: "1w", duration: 7 * 24 * time.Hour},
{inp: "2w", duration: 2 * 7 * 24 * time.Hour},
{inp: "1M", duration: time.Duration(730.5 * float64(time.Hour))},
{inp: "1y", duration: 365.25 * 24 * time.Hour},
{inp: "5y", duration: 5 * 365.25 * 24 * time.Hour},
{inp: "invalid-duration", err: regexp.MustCompile(`^time: invalid duration "?invalid-duration"?$`)},
}
for i, tc := range tcs { for i, tc := range tcs {
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) { t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
res, err := ParseInterval(tc.interval) res, err := ParseDuration(tc.inp)
if tc.err == nil { if tc.err == nil {
require.NoError(t, err, "interval %q", tc.interval) require.NoError(t, err, "input %q", tc.inp)
require.Equal(t, tc.duration, res, "interval %q", tc.interval) require.Equal(t, tc.duration, res, "input %q", tc.inp)
} else { } else {
require.Error(t, err, "interval %q", tc.interval) require.Error(t, err, "input %q", tc.inp)
require.Regexp(t, tc.err, err.Error()) require.Regexp(t, tc.err, err.Error())
} }
}) })
......
...@@ -546,7 +546,9 @@ func middlewareScenario(t *testing.T, desc string, fn scenarioFunc) { ...@@ -546,7 +546,9 @@ func middlewareScenario(t *testing.T, desc string, fn scenarioFunc) {
defer bus.ClearBusHandlers() defer bus.ClearBusHandlers()
setting.LoginCookieName = "grafana_session" setting.LoginCookieName = "grafana_session"
setting.LoginMaxLifetime, _ = gtime.ParseInterval("30d") var err error
setting.LoginMaxLifetime, err = gtime.ParseDuration("30d")
require.NoError(t, err)
sc := &scenarioContext{} sc := &scenarioContext{}
...@@ -637,7 +639,11 @@ func TestTokenRotationAtEndOfRequest(t *testing.T) { ...@@ -637,7 +639,11 @@ func TestTokenRotationAtEndOfRequest(t *testing.T) {
func initTokenRotationTest(ctx context.Context) (*models.ReqContext, *httptest.ResponseRecorder, error) { func initTokenRotationTest(ctx context.Context) (*models.ReqContext, *httptest.ResponseRecorder, error) {
setting.LoginCookieName = "login_token" setting.LoginCookieName = "login_token"
setting.LoginMaxLifetime, _ = gtime.ParseInterval("7d") var err error
setting.LoginMaxLifetime, err = gtime.ParseDuration("7d")
if err != nil {
return nil, nil, err
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "", "", nil) req, err := http.NewRequestWithContext(ctx, "", "", nil)
......
...@@ -190,11 +190,11 @@ func validateDashboardRefreshInterval(dash *models.Dashboard) error { ...@@ -190,11 +190,11 @@ func validateDashboardRefreshInterval(dash *models.Dashboard) error {
return nil return nil
} }
minRefreshInterval, err := gtime.ParseInterval(setting.MinRefreshInterval) minRefreshInterval, err := gtime.ParseDuration(setting.MinRefreshInterval)
if err != nil { if err != nil {
return err return err
} }
d, err := gtime.ParseInterval(refresh) d, err := gtime.ParseDuration(refresh)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -452,7 +452,7 @@ func (cfg *Cfg) readAnnotationSettings() { ...@@ -452,7 +452,7 @@ func (cfg *Cfg) readAnnotationSettings() {
alertingSection := cfg.Raw.Section("alerting") alertingSection := cfg.Raw.Section("alerting")
var newAnnotationCleanupSettings = func(section *ini.Section, maxAgeField string) AnnotationCleanupSettings { var newAnnotationCleanupSettings = func(section *ini.Section, maxAgeField string) AnnotationCleanupSettings {
maxAge, err := gtime.ParseInterval(section.Key(maxAgeField).MustString("")) maxAge, err := gtime.ParseDuration(section.Key(maxAgeField).MustString(""))
if err != nil { if err != nil {
maxAge = 0 maxAge = 0
} }
...@@ -1018,7 +1018,7 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) (err error) { ...@@ -1018,7 +1018,7 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) (err error) {
maxInactiveDaysVal = "7d" maxInactiveDaysVal = "7d"
} }
maxInactiveDurationVal := valueAsString(auth, "login_maximum_inactive_lifetime_duration", maxInactiveDaysVal) maxInactiveDurationVal := valueAsString(auth, "login_maximum_inactive_lifetime_duration", maxInactiveDaysVal)
cfg.LoginMaxInactiveLifetime, err = gtime.ParseInterval(maxInactiveDurationVal) cfg.LoginMaxInactiveLifetime, err = gtime.ParseDuration(maxInactiveDurationVal)
if err != nil { if err != nil {
return err return err
} }
...@@ -1031,7 +1031,7 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) (err error) { ...@@ -1031,7 +1031,7 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) (err error) {
maxLifetimeDaysVal = "7d" maxLifetimeDaysVal = "7d"
} }
maxLifetimeDurationVal := valueAsString(auth, "login_maximum_lifetime_duration", maxLifetimeDaysVal) maxLifetimeDurationVal := valueAsString(auth, "login_maximum_lifetime_duration", maxLifetimeDaysVal)
cfg.LoginMaxLifetime, err = gtime.ParseInterval(maxLifetimeDurationVal) cfg.LoginMaxLifetime, err = gtime.ParseDuration(maxLifetimeDurationVal)
if err != nil { if err != nil {
return err return err
} }
...@@ -1121,7 +1121,7 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1121,7 +1121,7 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error {
cfg.EditorsCanAdmin = users.Key("editors_can_admin").MustBool(false) cfg.EditorsCanAdmin = users.Key("editors_can_admin").MustBool(false)
userInviteMaxLifetimeVal := valueAsString(users, "user_invite_max_lifetime_duration", "24h") userInviteMaxLifetimeVal := valueAsString(users, "user_invite_max_lifetime_duration", "24h")
userInviteMaxLifetimeDuration, err := gtime.ParseInterval(userInviteMaxLifetimeVal) userInviteMaxLifetimeDuration, err := gtime.ParseDuration(userInviteMaxLifetimeVal)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -215,7 +215,8 @@ func TestLoadingSettings(t *testing.T) { ...@@ -215,7 +215,8 @@ func TestLoadingSettings(t *testing.T) {
}) })
So(err, ShouldBeNil) So(err, ShouldBeNil)
hostname, _ := os.Hostname() hostname, err := os.Hostname()
So(err, ShouldBeNil)
So(InstanceName, ShouldEqual, hostname) So(InstanceName, ShouldEqual, hostname)
}) })
...@@ -291,7 +292,7 @@ func TestLoadingSettings(t *testing.T) { ...@@ -291,7 +292,7 @@ func TestLoadingSettings(t *testing.T) {
}) })
} }
func TestParseAppUrlAndSubUrl(t *testing.T) { func TestParseAppURLAndSubURL(t *testing.T) {
testCases := []struct { testCases := []struct {
rootURL string rootURL string
expectedAppURL string expectedAppURL string
...@@ -315,7 +316,10 @@ func TestParseAppUrlAndSubUrl(t *testing.T) { ...@@ -315,7 +316,10 @@ func TestParseAppUrlAndSubUrl(t *testing.T) {
require.Equal(t, tc.expectedAppSubURL, appSubURL) require.Equal(t, tc.expectedAppSubURL, appSubURL)
} }
} }
func TestAuthDurationSettings(t *testing.T) { func TestAuthDurationSettings(t *testing.T) {
const maxInactiveDaysTest = 240 * time.Hour
f := ini.Empty() f := ini.Empty()
cfg := NewCfg() cfg := NewCfg()
sec, err := f.NewSection("auth") sec, err := f.NewSection("auth")
...@@ -324,7 +328,6 @@ func TestAuthDurationSettings(t *testing.T) { ...@@ -324,7 +328,6 @@ func TestAuthDurationSettings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
_, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "") _, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "")
require.NoError(t, err) require.NoError(t, err)
maxInactiveDaysTest, _ := time.ParseDuration("240h")
err = readAuthSettings(f, cfg) err = readAuthSettings(f, cfg)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, maxInactiveDaysTest, cfg.LoginMaxInactiveLifetime) require.Equal(t, maxInactiveDaysTest, cfg.LoginMaxInactiveLifetime)
...@@ -334,7 +337,8 @@ func TestAuthDurationSettings(t *testing.T) { ...@@ -334,7 +337,8 @@ func TestAuthDurationSettings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
_, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "824h") _, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "824h")
require.NoError(t, err) require.NoError(t, err)
maxInactiveDurationTest, _ := time.ParseDuration("824h") maxInactiveDurationTest, err := time.ParseDuration("824h")
require.NoError(t, err)
err = readAuthSettings(f, cfg) err = readAuthSettings(f, cfg)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, maxInactiveDurationTest, cfg.LoginMaxInactiveLifetime) require.Equal(t, maxInactiveDurationTest, cfg.LoginMaxInactiveLifetime)
...@@ -346,7 +350,8 @@ func TestAuthDurationSettings(t *testing.T) { ...@@ -346,7 +350,8 @@ func TestAuthDurationSettings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
_, err = sec.NewKey("login_maximum_lifetime_duration", "") _, err = sec.NewKey("login_maximum_lifetime_duration", "")
require.NoError(t, err) require.NoError(t, err)
maxLifetimeDaysTest, _ := time.ParseDuration("576h") maxLifetimeDaysTest, err := time.ParseDuration("576h")
require.NoError(t, err)
err = readAuthSettings(f, cfg) err = readAuthSettings(f, cfg)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, maxLifetimeDaysTest, cfg.LoginMaxLifetime) require.Equal(t, maxLifetimeDaysTest, cfg.LoginMaxLifetime)
...@@ -356,7 +361,8 @@ func TestAuthDurationSettings(t *testing.T) { ...@@ -356,7 +361,8 @@ func TestAuthDurationSettings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
_, err = sec.NewKey("login_maximum_lifetime_duration", "824h") _, err = sec.NewKey("login_maximum_lifetime_duration", "824h")
require.NoError(t, err) require.NoError(t, err)
maxLifetimeDurationTest, _ := time.ParseDuration("824h") maxLifetimeDurationTest, err := time.ParseDuration("824h")
require.NoError(t, err)
err = readAuthSettings(f, cfg) err = readAuthSettings(f, cfg)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, maxLifetimeDurationTest, cfg.LoginMaxLifetime) require.Equal(t, maxLifetimeDurationTest, cfg.LoginMaxLifetime)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment