2021-03-08 14:19:21 -06:00
|
|
|
package models
|
2021-01-18 12:57:17 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AlertInstance represents a single alert instance.
|
|
|
|
type AlertInstance struct {
|
2021-05-12 06:17:43 -05:00
|
|
|
RuleOrgID int64 `xorm:"rule_org_id"`
|
|
|
|
RuleUID string `xorm:"rule_uid"`
|
2021-01-18 12:57:17 -06:00
|
|
|
Labels InstanceLabels
|
|
|
|
LabelsHash string
|
|
|
|
CurrentState InstanceStateType
|
|
|
|
CurrentStateSince time.Time
|
2021-04-02 10:11:33 -05:00
|
|
|
CurrentStateEnd time.Time
|
2021-01-18 12:57:17 -06:00
|
|
|
LastEvalTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstanceStateType is an enum for instance states.
|
|
|
|
type InstanceStateType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// InstanceStateFiring is for a firing alert.
|
|
|
|
InstanceStateFiring InstanceStateType = "Alerting"
|
|
|
|
// InstanceStateNormal is for a normal alert.
|
|
|
|
InstanceStateNormal InstanceStateType = "Normal"
|
2021-04-13 16:38:09 -05:00
|
|
|
// InstanceStatePending is for an alert that is firing but has not met the duration
|
|
|
|
InstanceStatePending InstanceStateType = "Pending"
|
2021-04-03 12:13:29 -05:00
|
|
|
// InstanceStateNoData is for an alert with no data.
|
|
|
|
InstanceStateNoData InstanceStateType = "NoData"
|
|
|
|
// InstanceStateError is for a erroring alert.
|
|
|
|
InstanceStateError InstanceStateType = "Error"
|
2021-01-18 12:57:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsValid checks that the value of InstanceStateType is a valid
|
|
|
|
// string.
|
|
|
|
func (i InstanceStateType) IsValid() bool {
|
|
|
|
return i == InstanceStateFiring ||
|
2021-04-03 12:13:29 -05:00
|
|
|
i == InstanceStateNormal ||
|
|
|
|
i == InstanceStateNoData ||
|
2021-05-04 11:24:20 -05:00
|
|
|
i == InstanceStatePending ||
|
2021-04-03 12:13:29 -05:00
|
|
|
i == InstanceStateError
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// SaveAlertInstanceCommand is the query for saving a new alert instance.
|
|
|
|
type SaveAlertInstanceCommand struct {
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID int64
|
|
|
|
RuleUID string
|
2021-04-02 10:11:33 -05:00
|
|
|
Labels InstanceLabels
|
|
|
|
State InstanceStateType
|
|
|
|
LastEvalTime time.Time
|
|
|
|
CurrentStateSince time.Time
|
|
|
|
CurrentStateEnd time.Time
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// GetAlertInstanceQuery is the query for retrieving/deleting an alert definition by ID.
|
2021-01-18 12:57:17 -06:00
|
|
|
// nolint:unused
|
2021-03-08 14:19:21 -06:00
|
|
|
type GetAlertInstanceQuery struct {
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID int64
|
|
|
|
RuleUID string
|
|
|
|
Labels InstanceLabels
|
2021-01-18 12:57:17 -06:00
|
|
|
|
|
|
|
Result *AlertInstance
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// ListAlertInstancesQuery is the query list alert Instances.
|
|
|
|
type ListAlertInstancesQuery struct {
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID int64 `json:"-"`
|
|
|
|
RuleUID string
|
|
|
|
State InstanceStateType
|
2021-01-18 12:57:17 -06:00
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
Result []*ListAlertInstancesQueryResult
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// ListAlertInstancesQueryResult represents the result of listAlertInstancesQuery.
|
|
|
|
type ListAlertInstancesQueryResult struct {
|
2021-05-12 06:17:43 -05:00
|
|
|
RuleOrgID int64 `xorm:"rule_org_id" json:"ruleOrgId"`
|
|
|
|
RuleUID string `xorm:"rule_uid" json:"ruleUid"`
|
2021-01-19 11:11:11 -06:00
|
|
|
Labels InstanceLabels `json:"labels"`
|
|
|
|
LabelsHash string `json:"labeHash"`
|
|
|
|
CurrentState InstanceStateType `json:"currentState"`
|
|
|
|
CurrentStateSince time.Time `json:"currentStateSince"`
|
2021-04-02 10:11:33 -05:00
|
|
|
CurrentStateEnd time.Time `json:"currentStateEnd"`
|
2021-01-19 11:11:11 -06:00
|
|
|
LastEvalTime time.Time `json:"lastEvalTime"`
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:17:43 -05:00
|
|
|
// ValidateAlertInstance validates that the alert instance contains an alert rule id,
|
2021-01-18 12:57:17 -06:00
|
|
|
// and state.
|
2021-03-08 14:19:21 -06:00
|
|
|
func ValidateAlertInstance(alertInstance *AlertInstance) error {
|
2021-01-18 12:57:17 -06:00
|
|
|
if alertInstance == nil {
|
|
|
|
return fmt.Errorf("alert instance is invalid because it is nil")
|
|
|
|
}
|
|
|
|
|
2021-04-30 13:23:12 -05:00
|
|
|
if alertInstance.RuleOrgID == 0 {
|
2021-05-12 06:17:43 -05:00
|
|
|
return fmt.Errorf("alert instance is invalid due to missing alert rule organisation")
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
2021-04-30 13:23:12 -05:00
|
|
|
if alertInstance.RuleUID == "" {
|
2021-05-12 06:17:43 -05:00
|
|
|
return fmt.Errorf("alert instance is invalid due to missing alert rule uid")
|
2021-01-18 12:57:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !alertInstance.CurrentState.IsValid() {
|
|
|
|
return fmt.Errorf("alert instance is invalid because the state '%v' is invalid", alertInstance.CurrentState)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|