2021-03-08 14:19:21 -06:00
|
|
|
package models
|
2020-11-12 07:11:30 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-03-14 05:39:20 -05:00
|
|
|
"errors"
|
2020-11-12 07:11:30 -06:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-01-04 10:47:13 -06:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
|
2020-11-19 06:17:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/expr"
|
2020-11-12 07:11:30 -06:00
|
|
|
)
|
|
|
|
|
2021-05-17 11:46:52 -05:00
|
|
|
const defaultMaxDataPoints float64 = 43200 // 12 hours at 1sec interval
|
2020-11-12 07:11:30 -06:00
|
|
|
const defaultIntervalMS float64 = 1000
|
|
|
|
|
2022-03-14 05:39:20 -05:00
|
|
|
var ErrNoQuery = errors.New("no `expr` property in the query model")
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
// Duration is a type used for marshalling durations.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
func (d Duration) String() string {
|
|
|
|
return time.Duration(d).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(time.Duration(d).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2023-08-30 10:46:47 -05:00
|
|
|
var v any
|
2020-11-12 07:11:30 -06:00
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
*d = Duration(time.Duration(value) * time.Second)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid duration %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
func (d Duration) MarshalYAML() (any, error) {
|
2022-07-04 05:03:34 -05:00
|
|
|
return time.Duration(d).Seconds(), nil
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
func (d *Duration) UnmarshalYAML(unmarshal func(any) error) error {
|
|
|
|
var v any
|
2022-07-04 05:03:34 -05:00
|
|
|
if err := unmarshal(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case int:
|
|
|
|
*d = Duration(time.Duration(value) * time.Second)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid duration %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
// RelativeTimeRange is the per query start and end time
|
|
|
|
// for requests.
|
|
|
|
type RelativeTimeRange struct {
|
2022-07-04 05:03:34 -05:00
|
|
|
From Duration `json:"from" yaml:"from"`
|
|
|
|
To Duration `json:"to" yaml:"to"`
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// isValid checks that From duration is greater than To duration.
|
|
|
|
func (rtr *RelativeTimeRange) isValid() bool {
|
|
|
|
return rtr.From > rtr.To
|
|
|
|
}
|
|
|
|
|
2022-10-26 15:13:58 -05:00
|
|
|
func (rtr *RelativeTimeRange) ToTimeRange() expr.TimeRange {
|
|
|
|
return expr.RelativeTimeRange{
|
|
|
|
From: -time.Duration(rtr.From),
|
|
|
|
To: -time.Duration(rtr.To),
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlertQuery represents a single query associated with an alert definition.
|
|
|
|
type AlertQuery struct {
|
|
|
|
// RefID is the unique identifier of the query, set by the frontend call.
|
|
|
|
RefID string `json:"refId"`
|
|
|
|
|
|
|
|
// QueryType is an optional identifier for the type of query.
|
|
|
|
// It can be used to distinguish different types of queries.
|
|
|
|
QueryType string `json:"queryType"`
|
|
|
|
|
|
|
|
// RelativeTimeRange is the relative Start and End of the query as sent by the frontend.
|
|
|
|
RelativeTimeRange RelativeTimeRange `json:"relativeTimeRange"`
|
|
|
|
|
2023-01-31 11:50:10 -06:00
|
|
|
// Grafana data source unique identifier; it should be '__expr__' for a Server Side Expression operation.
|
2021-04-23 09:52:32 -05:00
|
|
|
DatasourceUID string `json:"datasourceUid"`
|
2020-11-12 07:11:30 -06:00
|
|
|
|
|
|
|
// JSON is the raw JSON query and includes the above properties as well as custom properties.
|
|
|
|
Model json.RawMessage `json:"model"`
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
modelProps map[string]any
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
|
2023-04-06 10:02:28 -05:00
|
|
|
func (aq *AlertQuery) String() string {
|
|
|
|
return fmt.Sprintf("refID: %s, queryType: %s, datasourceUID: %s", aq.RefID, aq.QueryType, aq.DatasourceUID)
|
|
|
|
}
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
func (aq *AlertQuery) setModelProps() error {
|
2023-08-30 10:46:47 -05:00
|
|
|
aq.modelProps = make(map[string]any)
|
2020-11-12 07:11:30 -06:00
|
|
|
err := json.Unmarshal(aq.Model, &aq.modelProps)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal query model: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsExpression returns true if the alert query is an expression.
|
|
|
|
func (aq *AlertQuery) IsExpression() (bool, error) {
|
2023-06-16 12:05:06 -05:00
|
|
|
return expr.NodeTypeFromDatasourceUID(aq.DatasourceUID) == expr.TypeCMDNode, nil
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
|
2024-01-04 10:47:13 -06:00
|
|
|
// IsHysteresisExpression returns true if the model describes a hysteresis command expression. Returns error if the Model is not a valid JSON
|
|
|
|
func (aq *AlertQuery) IsHysteresisExpression() (bool, error) {
|
|
|
|
if aq.modelProps == nil {
|
|
|
|
err := aq.setModelProps()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expr.IsHysteresisExpression(aq.modelProps), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PatchHysteresisExpression updates the AlertQuery to include loaded metrics into hysteresis
|
|
|
|
func (aq *AlertQuery) PatchHysteresisExpression(loadedMetrics map[data.Fingerprint]struct{}) error {
|
|
|
|
if aq.modelProps == nil {
|
|
|
|
err := aq.setModelProps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expr.SetLoadedDimensionsToHysteresisCommand(aq.modelProps, loadedMetrics)
|
|
|
|
}
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
// setMaxDatapoints sets the model maxDataPoints if it's missing or invalid
|
|
|
|
func (aq *AlertQuery) setMaxDatapoints() error {
|
|
|
|
if aq.modelProps == nil {
|
|
|
|
err := aq.setModelProps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i, ok := aq.modelProps["maxDataPoints"] // GEL requires maxDataPoints inside the query JSON
|
|
|
|
if !ok {
|
|
|
|
aq.modelProps["maxDataPoints"] = defaultMaxDataPoints
|
|
|
|
}
|
|
|
|
maxDataPoints, ok := i.(float64)
|
|
|
|
if !ok || maxDataPoints == 0 {
|
|
|
|
aq.modelProps["maxDataPoints"] = defaultMaxDataPoints
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
func (aq *AlertQuery) GetMaxDatapoints() (int64, error) {
|
2020-11-12 07:11:30 -06:00
|
|
|
err := aq.setMaxDatapoints()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxDataPoints, ok := aq.modelProps["maxDataPoints"].(float64)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("failed to cast maxDataPoints to float64: %v", aq.modelProps["maxDataPoints"])
|
|
|
|
}
|
|
|
|
return int64(maxDataPoints), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setIntervalMS sets the model IntervalMs if it's missing or invalid
|
|
|
|
func (aq *AlertQuery) setIntervalMS() error {
|
|
|
|
if aq.modelProps == nil {
|
|
|
|
err := aq.setModelProps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i, ok := aq.modelProps["intervalMs"] // GEL requires intervalMs inside the query JSON
|
|
|
|
if !ok {
|
|
|
|
aq.modelProps["intervalMs"] = defaultIntervalMS
|
|
|
|
}
|
|
|
|
intervalMs, ok := i.(float64)
|
|
|
|
if !ok || intervalMs == 0 {
|
|
|
|
aq.modelProps["intervalMs"] = defaultIntervalMS
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aq *AlertQuery) getIntervalMS() (int64, error) {
|
|
|
|
err := aq.setIntervalMS()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
intervalMs, ok := aq.modelProps["intervalMs"].(float64)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("failed to cast intervalMs to float64: %v", aq.modelProps["intervalMs"])
|
|
|
|
}
|
|
|
|
return int64(intervalMs), nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
func (aq *AlertQuery) GetIntervalDuration() (time.Duration, error) {
|
2020-11-19 06:17:00 -06:00
|
|
|
err := aq.setIntervalMS()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
intervalMs, ok := aq.modelProps["intervalMs"].(float64)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("failed to cast intervalMs to float64: %v", aq.modelProps["intervalMs"])
|
|
|
|
}
|
|
|
|
return time.Duration(intervalMs) * time.Millisecond, nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
// GetDatasource returns the query datasource identifier.
|
2021-01-15 10:33:50 -06:00
|
|
|
func (aq *AlertQuery) GetDatasource() (string, error) {
|
|
|
|
return aq.DatasourceUID, nil
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
|
2022-03-14 05:39:20 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
func (aq *AlertQuery) GetModel() ([]byte, error) {
|
2021-04-23 09:52:32 -05:00
|
|
|
err := aq.setMaxDatapoints()
|
2020-11-12 07:11:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = aq.setIntervalMS()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
model, err := json.Marshal(aq.modelProps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to marshal query model: %w", err)
|
|
|
|
}
|
|
|
|
return model, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aq *AlertQuery) setQueryType() error {
|
|
|
|
if aq.modelProps == nil {
|
|
|
|
err := aq.setModelProps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i, ok := aq.modelProps["queryType"]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
queryType, ok := i.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to get queryType from query model: %v", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
aq.QueryType = queryType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreSave sets query's properties.
|
|
|
|
// It should be called before being saved.
|
2020-12-07 09:30:38 -06:00
|
|
|
func (aq *AlertQuery) PreSave() error {
|
2020-11-12 07:11:30 -06:00
|
|
|
if err := aq.setQueryType(); err != nil {
|
|
|
|
return fmt.Errorf("failed to set query type to query model: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// override model
|
2021-03-08 14:19:21 -06:00
|
|
|
model, err := aq.GetModel()
|
2020-11-12 07:11:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aq.Model = model
|
|
|
|
|
|
|
|
isExpression, err := aq.IsExpression()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := isExpression || aq.RelativeTimeRange.isValid(); !ok {
|
2024-04-05 09:20:21 -05:00
|
|
|
return ErrInvalidRelativeTimeRange(aq.RefID, aq.RelativeTimeRange)
|
2020-11-12 07:11:30 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|