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