Use sdk pkg for gtime (#39354)

This commit is contained in:
Andres Martinez Gotor 2021-09-21 13:08:52 +02:00 committed by GitHub
parent 875be5ef7c
commit 64c8d32fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 13 additions and 202 deletions

2
go.mod
View File

@ -50,7 +50,7 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/gosimple/slug v1.9.0
github.com/grafana/grafana-aws-sdk v0.7.0
github.com/grafana/grafana-plugin-sdk-go v0.113.0
github.com/grafana/grafana-plugin-sdk-go v0.114.0
github.com/grafana/loki v1.6.2-0.20210520072447-15d417efe103
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hashicorp/go-hclog v0.16.1

4
go.sum
View File

@ -1025,8 +1025,8 @@ github.com/grafana/go-mssqldb v0.0.0-20210326084033-d0ce3c521036/go.mod h1:xbL0r
github.com/grafana/grafana-aws-sdk v0.7.0 h1:D+Lhxi3P/7vpyDHUK/fdX9bL2mRz8hLG04ucNf1E02o=
github.com/grafana/grafana-aws-sdk v0.7.0/go.mod h1:+pPo5U+pX0zWimR7YBc7ASeSQfbRkcTyQYqMiAj7G5U=
github.com/grafana/grafana-plugin-sdk-go v0.79.0/go.mod h1:NvxLzGkVhnoBKwzkst6CFfpMFKwAdIUZ1q8ssuLeF60=
github.com/grafana/grafana-plugin-sdk-go v0.113.0 h1:X46np4UNgM0YLhxC0oLa2q7WOHdU5T/oppZ+XlYusMk=
github.com/grafana/grafana-plugin-sdk-go v0.113.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk=
github.com/grafana/grafana-plugin-sdk-go v0.114.0 h1:9I55IXw7mOT71tZ/pdqCaWGz8vxfz31CXjaDtBV9ZBo=
github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk=
github.com/grafana/loki v1.6.2-0.20210520072447-15d417efe103 h1:qCmofFVwQR9QnsinstVqI1NPLMVl33jNCnOCXEAVn6E=
github.com/grafana/loki v1.6.2-0.20210520072447-15d417efe103/go.mod h1:GHIsn+EohCChsdu5YouNZewqLeV9L2FNw4DEJU3P9qE=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=

View File

@ -1,87 +0,0 @@
package gtime
import (
"fmt"
"regexp"
"strconv"
"time"
)
var dateUnitPattern = regexp.MustCompile(`^(\d+)([dwMy])$`)
// ParseInterval parses an interval with support for all units that Grafana uses.
// An interval is relative to the current wall time.
func ParseInterval(inp string) (time.Duration, error) {
dur, period, err := parse(inp)
if err != nil {
return 0, err
}
if period == "" {
return dur, nil
}
num := int(dur)
// Use UTC to ensure that the interval is deterministic, and daylight saving
// doesn't cause surprises
now := time.Now().UTC()
switch period {
case "d":
return now.AddDate(0, 0, num).Sub(now), nil
case "w":
return now.AddDate(0, 0, num*7).Sub(now), nil
case "M":
return now.AddDate(0, num, 0).Sub(now), nil
case "y":
return now.AddDate(num, 0, 0).Sub(now), nil
}
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
}

View File

@ -1,102 +0,0 @@
package gtime
import (
"fmt"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestParseInterval(t *testing.T) {
daysInMonth, daysInYear := calculateDays()
tcs := []struct {
inp string
duration time.Duration
err *regexp.Regexp
}{
{inp: "1d", duration: 24 * time.Hour},
{inp: "1w", duration: 168 * time.Hour},
{inp: "2w", duration: 2 * 168 * time.Hour},
{inp: "1M", duration: time.Duration(daysInMonth * 24 * int(time.Hour))},
{inp: "1y", duration: time.Duration(daysInYear * 24 * int(time.Hour))},
{inp: "5y", duration: time.Duration(calculateDays5y() * 24 * int(time.Hour))},
{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 {
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
res, err := ParseDuration(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 calculateDays() (int, int) {
now := time.Now().UTC()
currentYear, currentMonth, _ := now.Date()
firstDayOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, time.UTC)
daysInMonth := firstDayOfMonth.AddDate(0, 1, -1).Day()
t1 := time.Date(currentYear, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := time.Date(currentYear+1, 1, 1, 0, 0, 0, 0, time.UTC)
daysInYear := int(t2.Sub(t1).Hours() / 24)
return daysInMonth, daysInYear
}
func calculateDays5y() int {
now := time.Now().UTC()
currentYear, _, _ := now.Date()
var daysInYear int
for i := 0; i < 5; i++ {
t1 := time.Date(currentYear+i, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := time.Date(currentYear+i+1, 1, 1, 0, 0, 0, 0, time.UTC)
daysInYear = daysInYear + int(t2.Sub(t1).Hours()/24)
}
return daysInYear
}

View File

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/expr/mathexp"
)

View File

@ -15,9 +15,9 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana/pkg/infra/fs"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/remotecache"

View File

@ -7,7 +7,7 @@ import (
"net/http/httptest"
"testing"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/auth"

View File

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/dashboards"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/setting"

View File

@ -19,7 +19,7 @@ import (
"time"
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/util"

View File

@ -4,7 +4,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/prometheus/alertmanager/cluster"
"gopkg.in/ini.v1"

View File

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"

View File

@ -7,7 +7,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)

View File

@ -7,7 +7,7 @@ import (
"strings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)

View File

@ -7,7 +7,7 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana/pkg/tsdb/sqleng"
)