grafana/pkg/tsdb/azuremonitor/time-grain.go
Arve Knudsen b79e61656a
Introduce TSDB service (#31520)
* Introduce TSDB service

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

Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
Co-authored-by: Will Browne <will.browne@grafana.com>
Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
2021-03-08 07:02:49 +01:00

53 lines
1.4 KiB
Go

package azuremonitor
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/grafana/grafana/pkg/tsdb/interval"
)
// TimeGrain handles conversions 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) createISO8601DurationFromIntervalMS(it int64) (string, error) {
formatted := interval.FormatDuration(time.Duration(it) * time.Millisecond)
if strings.Contains(formatted, "ms") {
return "PT1M", nil
}
timeValueString := formatted[0 : len(formatted)-1]
timeValue, err := strconv.Atoi(timeValueString)
if err != nil {
return "", fmt.Errorf("could not parse interval %q to an ISO 8061 duration: %w", it, err)
}
unit := formatted[len(formatted)-1:]
if unit == "s" && timeValue < 60 {
// minimum interval is 1m for Azure Monitor
return "PT1M", 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]))
}