mirror of https://github.com/grafana/grafana
parent
452c4f5b9b
commit
60327953a2
@ -0,0 +1,53 @@ |
||||
package azuremonitor |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strconv" |
||||
"strings" |
||||
) |
||||
|
||||
// TimeGrain handles convertions between
|
||||
// the ISO 8601 Duration format (PT1H), Kbn units (1h) and Time Grains (1 hour)
|
||||
// Also handles using the automatic Grafana interval to calculate a ISO 8601 Duration.
|
||||
type TimeGrain struct{} |
||||
|
||||
var ( |
||||
smallTimeUnits = []string{"hour", "minute", "h", "m"} |
||||
) |
||||
|
||||
func (tg *TimeGrain) createISO8601DurationFromInterval(interval string) (string, error) { |
||||
if strings.Contains(interval, "ms") { |
||||
return "PT1M", nil |
||||
} |
||||
|
||||
timeValueString := interval[0 : len(interval)-1] |
||||
timeValue, err := strconv.Atoi(timeValueString) |
||||
if err != nil { |
||||
return "", fmt.Errorf("Could not parse interval %v to an ISO 8061 duration", interval) |
||||
} |
||||
|
||||
unit := interval[len(interval)-1:] |
||||
|
||||
if unit == "s" { |
||||
toMinutes := (timeValue * 60) % 60 |
||||
|
||||
// mimumum interval is 1m for Azure Monitor
|
||||
if toMinutes < 1 { |
||||
toMinutes = 1 |
||||
} |
||||
|
||||
return tg.createISO8601Duration(toMinutes, "m"), nil |
||||
} |
||||
|
||||
return tg.createISO8601Duration(timeValue, unit), nil |
||||
} |
||||
|
||||
func (tg *TimeGrain) createISO8601Duration(timeValue int, timeUnit string) string { |
||||
for _, smallTimeUnit := range smallTimeUnits { |
||||
if timeUnit == smallTimeUnit { |
||||
return fmt.Sprintf("PT%v%v", timeValue, strings.ToUpper(timeUnit[0:1])) |
||||
} |
||||
} |
||||
|
||||
return fmt.Sprintf("P%v%v", timeValue, strings.ToUpper(timeUnit[0:1])) |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
package azuremonitor |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestTimeGrain(t *testing.T) { |
||||
Convey("TimeGrain", t, func() { |
||||
tgc := &TimeGrain{} |
||||
|
||||
Convey("create ISO 8601 Duration", func() { |
||||
Convey("when given a time unit smaller than a day", func() { |
||||
minuteKbnDuration := tgc.createISO8601Duration(1, "m") |
||||
hourKbnDuration := tgc.createISO8601Duration(2, "h") |
||||
minuteDuration := tgc.createISO8601Duration(1, "minute") |
||||
hourDuration := tgc.createISO8601Duration(2, "hour") |
||||
|
||||
Convey("should convert it to a time duration", func() { |
||||
So(minuteKbnDuration, ShouldEqual, "PT1M") |
||||
So(hourKbnDuration, ShouldEqual, "PT2H") |
||||
|
||||
So(minuteDuration, ShouldEqual, "PT1M") |
||||
So(hourDuration, ShouldEqual, "PT2H") |
||||
}) |
||||
}) |
||||
|
||||
Convey("when given the day time unit", func() { |
||||
kbnDuration := tgc.createISO8601Duration(1, "d") |
||||
duration := tgc.createISO8601Duration(2, "day") |
||||
|
||||
Convey("should convert it to a date duration", func() { |
||||
So(kbnDuration, ShouldEqual, "P1D") |
||||
So(duration, ShouldEqual, "P2D") |
||||
}) |
||||
}) |
||||
}) |
||||
|
||||
Convey("create ISO 8601 Duration from Grafana interval", func() { |
||||
Convey("and interval is less than a minute", func() { |
||||
durationMS, _ := tgc.createISO8601DurationFromInterval("100ms") |
||||
durationS, _ := tgc.createISO8601DurationFromInterval("59s") |
||||
Convey("should be rounded up to a minute as is the minimum interval for Azure Monitor", func() { |
||||
So(durationMS, ShouldEqual, "PT1M") |
||||
So(durationS, ShouldEqual, "PT1M") |
||||
}) |
||||
}) |
||||
|
||||
Convey("and interval is more than a minute", func() { |
||||
durationM, _ := tgc.createISO8601DurationFromInterval("10m") |
||||
durationD, _ := tgc.createISO8601DurationFromInterval("2d") |
||||
Convey("should be rounded up to a minute as is the minimum interval for Azure Monitor", func() { |
||||
So(durationM, ShouldEqual, "PT10M") |
||||
So(durationD, ShouldEqual, "P2D") |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue