1// Copyright 2013 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package model
15
16import (
17	"fmt"
18	"time"
19)
20
21type AlertStatus string
22
23const (
24	AlertFiring   AlertStatus = "firing"
25	AlertResolved AlertStatus = "resolved"
26)
27
28// Alert is a generic representation of an alert in the Prometheus eco-system.
29type Alert struct {
30	// Label value pairs for purpose of aggregation, matching, and disposition
31	// dispatching. This must minimally include an "alertname" label.
32	Labels LabelSet `json:"labels"`
33
34	// Extra key/value information which does not define alert identity.
35	Annotations LabelSet `json:"annotations"`
36
37	// The known time range for this alert. Both ends are optional.
38	StartsAt time.Time `json:"startsAt,omitempty"`
39	EndsAt   time.Time `json:"endsAt,omitempty"`
40}
41
42// Name returns the name of the alert. It is equivalent to the "alertname" label.
43func (a *Alert) Name() string {
44	return string(a.Labels[AlertNameLabel])
45}
46
47// Fingerprint returns a unique hash for the alert. It is equivalent to
48// the fingerprint of the alert's label set.
49func (a *Alert) Fingerprint() Fingerprint {
50	return a.Labels.Fingerprint()
51}
52
53func (a *Alert) String() string {
54	s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7])
55	if a.Resolved() {
56		return s + "[resolved]"
57	}
58	return s + "[active]"
59}
60
61// Resolved returns true iff the activity interval ended in the past.
62func (a *Alert) Resolved() bool {
63	if a.EndsAt.IsZero() {
64		return false
65	}
66	return !a.EndsAt.After(time.Now())
67}
68
69// Status returns the status of the alert.
70func (a *Alert) Status() AlertStatus {
71	if a.Resolved() {
72		return AlertResolved
73	}
74	return AlertFiring
75}
76
77// Alert is a list of alerts that can be sorted in chronological order.
78type Alerts []*Alert
79
80func (as Alerts) Len() int      { return len(as) }
81func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
82
83func (as Alerts) Less(i, j int) bool {
84	if as[i].StartsAt.Before(as[j].StartsAt) {
85		return true
86	}
87	if as[i].EndsAt.Before(as[j].EndsAt) {
88		return true
89	}
90	return as[i].Fingerprint() < as[j].Fingerprint()
91}
92
93// HasFiring returns true iff one of the alerts is not resolved.
94func (as Alerts) HasFiring() bool {
95	for _, a := range as {
96		if !a.Resolved() {
97			return true
98		}
99	}
100	return false
101}
102
103// Status returns StatusFiring iff at least one of the alerts is firing.
104func (as Alerts) Status() AlertStatus {
105	if as.HasFiring() {
106		return AlertFiring
107	}
108	return AlertResolved
109}
110