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-04-30 14:23:12 -04:00
|
|
|
RuleOrgID int64 `xorm:"def_org_id"`
|
|
|
|
|
RuleUID string `xorm:"def_uid"`
|
2021-01-18 20:57:17 +02:00
|
|
|
Labels InstanceLabels
|
|
|
|
|
LabelsHash string
|
|
|
|
|
CurrentState InstanceStateType
|
|
|
|
|
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
|
|
|
|
|
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 {
|
2021-05-03 07:19:15 -04:00
|
|
|
RuleOrgID int64 `json:"-"`
|
|
|
|
|
RuleUID string
|
|
|
|
|
State InstanceStateType
|
2021-01-18 20:57:17 +02:00
|
|
|
|
2021-03-08 22:19:21 +02:00
|
|
|
Result []*ListAlertInstancesQueryResult
|
2021-01-18 20:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 08:11:33 -07:00
|
|
|
type FetchUniqueOrgIdsQuery struct {
|
|
|
|
|
Result []*FetchUniqueOrgIdsQueryResult
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 22:19:21 +02:00
|
|
|
// ListAlertInstancesQueryResult represents the result of listAlertInstancesQuery.
|
|
|
|
|
type ListAlertInstancesQueryResult struct {
|
2021-04-30 14:23:12 -04:00
|
|
|
RuleOrgID int64 `xorm:"def_org_id" json:"definitionOrgId"`
|
|
|
|
|
RuleDefinitionUID string `xorm:"def_uid" json:"definitionUid"`
|
2021-01-19 19:11:11 +02:00
|
|
|
Labels InstanceLabels `json:"labels"`
|
|
|
|
|
LabelsHash string `json:"labeHash"`
|
|
|
|
|
CurrentState InstanceStateType `json:"currentState"`
|
|
|
|
|
CurrentStateSince time.Time `json:"currentStateSince"`
|
2021-04-02 08:11:33 -07:00
|
|
|
CurrentStateEnd time.Time `json:"currentStateEnd"`
|
2021-01-19 19:11:11 +02:00
|
|
|
LastEvalTime time.Time `json:"lastEvalTime"`
|
2021-01-18 20:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-02 08:11:33 -07:00
|
|
|
type FetchUniqueOrgIdsQueryResult struct {
|
|
|
|
|
DefinitionOrgID int64 `xorm:"def_org_id" json:"definitionOrgId"`
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 22:19:21 +02:00
|
|
|
// ValidateAlertInstance validates that the alert instance contains an alert definition 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-01-18 20:57:17 +02:00
|
|
|
return fmt.Errorf("alert instance is invalid due to missing alert definition organisation")
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 14:23:12 -04:00
|
|
|
if alertInstance.RuleUID == "" {
|
2021-01-18 20:57:17 +02:00
|
|
|
return fmt.Errorf("alert instance is invalid due to missing alert definition uid")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !alertInstance.CurrentState.IsValid() {
|
|
|
|
|
return fmt.Errorf("alert instance is invalid because the state '%v' is invalid", alertInstance.CurrentState)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|