mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
20faef8de5
* Convert Azure Application Insights datasource to Go Allows for alerting of Application Insights data source Closes: #15153 * Fix timeGrainReset * Default time interval for querys for alerts * Fix a few rename related bugs * Update readme to indicate App Insights alerting * Fix typo and add tests to ensure migration is happening * Address code review feedback (mostly typos and unintended changes)
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package azuremonitor
|
|
|
|
import "encoding/json"
|
|
|
|
// setAutoTimeGrain tries to find the closest interval to the query's intervalMs value
|
|
// if the metric has a limited set of possible intervals/time grains then use those
|
|
// instead of the default list of intervals
|
|
func setAutoTimeGrain(intervalMs int64, timeGrains interface{}) (string, error) {
|
|
// parses array of numbers from the timeGrains json field
|
|
allowedTimeGrains := []int64{}
|
|
tgs, ok := timeGrains.([]interface{})
|
|
if ok {
|
|
for _, v := range tgs {
|
|
jsonNumber, ok := v.(json.Number)
|
|
if ok {
|
|
tg, err := jsonNumber.Int64()
|
|
if err == nil {
|
|
allowedTimeGrains = append(allowedTimeGrains, tg)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
autoInterval := findClosestAllowedIntervalMS(intervalMs, allowedTimeGrains)
|
|
tg := &TimeGrain{}
|
|
autoTimeGrain, err := tg.createISO8601DurationFromIntervalMS(autoInterval)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return autoTimeGrain, nil
|
|
}
|
|
|
|
// findClosestAllowedIntervalMs is used for the auto time grain setting.
|
|
// It finds the closest time grain from the list of allowed time grains for Azure Monitor
|
|
// using the Grafana interval in milliseconds
|
|
// Some metrics only allow a limited list of time grains. The allowedTimeGrains parameter
|
|
// allows overriding the default list of allowed time grains.
|
|
func findClosestAllowedIntervalMS(intervalMs int64, allowedTimeGrains []int64) int64 {
|
|
allowedIntervals := defaultAllowedIntervalsMS
|
|
|
|
if len(allowedTimeGrains) > 0 {
|
|
allowedIntervals = allowedTimeGrains
|
|
}
|
|
|
|
closest := allowedIntervals[0]
|
|
|
|
for i, allowed := range allowedIntervals {
|
|
if intervalMs > allowed {
|
|
if i+1 < len(allowedIntervals) {
|
|
closest = allowedIntervals[i+1]
|
|
} else {
|
|
closest = allowed
|
|
}
|
|
}
|
|
}
|
|
return closest
|
|
}
|