Alerting: Display query from grafana-managed alert rules on /api/v1/rules (#45969)
* Aleting: Extract query from alerting rule model for api/v1/rules * more changes and fixtures * appease the linter
This commit is contained in:
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
const defaultMaxDataPoints float64 = 43200 // 12 hours at 1sec interval
|
||||
const defaultIntervalMS float64 = 1000
|
||||
|
||||
var ErrNoQuery = errors.New("no `expr` property in the query model")
|
||||
|
||||
// Duration is a type used for marshalling durations.
|
||||
type Duration time.Duration
|
||||
|
||||
@@ -174,6 +177,28 @@ func (aq *AlertQuery) GetDatasource() (string, error) {
|
||||
return aq.DatasourceUID, nil
|
||||
}
|
||||
|
||||
// GetQuery returns the query defined by `expr` within the model.
|
||||
// Returns an ErrNoQuery if it is unable to find the query.
|
||||
// Returns an error if it is not able to cast the query to a string.
|
||||
func (aq *AlertQuery) GetQuery() (string, error) {
|
||||
if aq.modelProps == nil {
|
||||
err := aq.setModelProps()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
query, ok := aq.modelProps["expr"]
|
||||
if !ok {
|
||||
return "", ErrNoQuery
|
||||
}
|
||||
|
||||
q, ok := query.(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("failed to cast query to string: %v", aq.modelProps["expr"])
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (aq *AlertQuery) GetModel() ([]byte, error) {
|
||||
err := aq.setMaxDatapoints()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user