-
Notifications
You must be signed in to change notification settings - Fork 3
/
state.go
86 lines (74 loc) · 1.65 KB
/
state.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package libhealth
import "strings"
// Status is a representation of the health of the component.
type Status int
const (
OUTAGE Status = iota
MAJOR
MINOR
OK
)
// ParseStatus parses the given string into a Status.
// If the string is malformed, OUTAGE is returned.
func ParseStatus(state string) Status {
switch strings.ToUpper(state) {
case "OUTAGE":
return OUTAGE
case "MAJOR":
return MAJOR
case "MINOR":
return MINOR
case "OK":
return OK
default:
return OUTAGE
}
}
// String provides a regular string representation of a Status.
func (s Status) String() string {
switch s {
case OUTAGE:
return "OUTAGE"
case MAJOR:
return "MAJOR"
case MINOR:
return "MINOR"
case OK:
return "OK"
}
return "INVALID"
}
// WorseThan compares a Status to another Status.
func (s Status) WorseThan(level Status) bool {
return s < level
}
// SameOrWorseThan compares a Status to another Status.
func (s Status) SameOrWorseThan(level Status) bool {
return s <= level
}
// SameOrBetterThan compares a Status to another Status.
func (s Status) SameOrBetterThan(level Status) bool {
return s >= level
}
// BetterThan compares a Status to another Status.
func (s Status) BetterThan(level Status) bool {
return s > level
}
// SameAs compares a Status to another Status.
func (s Status) SameAs(level Status) bool {
return s == level
}
// BestState returns the more cheerful of two states.
func BestState(left, right Status) Status {
if left.SameOrBetterThan(right) {
return left
}
return right
}
// WorstState returns the less positive of two states.
func WorstState(left, right Status) Status {
if left.SameOrWorseThan(right) {
return left
}
return right
}