File size: 1,425 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package heartbeat

import (
	"time"

	"github.com/target/goalert/alert"
	"github.com/target/goalert/validation/validate"
)

// A Monitor will generate an alert if it does not receive a heartbeat within the configured TimeoutMinutes.
type Monitor struct {
	ID        string        `json:"id,omitempty"`
	Name      string        `json:"name,omitempty"`
	ServiceID string        `json:"service_id,omitempty"`
	Timeout   time.Duration `json:"timeout,omitempty"`

	AdditionalDetails string

	// Muted indicates the reason the monitor is muted.
	//
	// If non-empty, the monitor will not generate alerts.
	Muted string

	lastState     State
	lastHeartbeat time.Time
}

// LastState returns the last known state.
func (m Monitor) LastState() State { return m.lastState }

// LastHeartbeat returns the timestamp of the last successful heartbeat.
func (m Monitor) LastHeartbeat() time.Time { return m.lastHeartbeat }

// Normalize performs validation and returns a new copy.
func (m Monitor) Normalize() (*Monitor, error) {
	err := validate.Many(
		validate.UUID("ServiceID", m.ServiceID),
		validate.IDName("Name", m.Name),
		validate.Duration("Timeout", m.Timeout, 5*time.Minute, 9000*time.Hour),
		validate.Text("AdditionalDetails", m.AdditionalDetails, 0, alert.MaxDetailsLength),
		validate.Text("Muted", m.Muted, 0, 255),
	)
	if err != nil {
		return nil, err
	}

	m.Timeout = m.Timeout.Truncate(time.Minute)

	return &m, nil
}