-
Notifications
You must be signed in to change notification settings - Fork 2
/
proto.go
100 lines (85 loc) · 1.81 KB
/
proto.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gobearmon
import "encoding/json"
import "errors"
import "strconv"
import "time"
type CheckId int
type CheckStatus string
type CheckResults map[CheckId]*CheckResult
const (
StatusOffline CheckStatus = "offline"
StatusOnline = "online"
StatusFail = "fail"
)
type CheckResult struct {
Status CheckStatus `json:"status"`
Message string `json:"message"`
}
type ControllerRequest struct {
Results CheckResults `json:"results"`
Count int `json:"count"`
}
func MakeControllerRequest() *ControllerRequest {
return &ControllerRequest{
Results: make(CheckResults),
}
}
type ControllerResponse struct {
Checks []CheckId `json:"checks"`
}
type Check struct {
Id CheckId
Name string
Type string
Data string
Interval int
Delay int
Status CheckStatus
Lock string
LockTime time.Time
LastWorker string
LastTime time.Time
TurnSet map[string]bool
TurnCount int
LastStatusChange time.Time
}
func (check *Check) SetStatusFromString(status string) {
if status == string(StatusOffline) {
check.Status = StatusOffline
} else if status == string(StatusOnline) {
check.Status = StatusOnline
} else {
panic(errors.New("invalid check status to set: " + status))
}
}
func MakeCheck() *Check {
return &Check{
TurnSet: make(map[string]bool),
}
}
type Alert struct {
Type string
Data string
}
func (results CheckResults) MarshalJSON() ([]byte, error) {
stringMap := make(map[string]*CheckResult)
for k, v := range results {
stringMap[strconv.Itoa(int(k))] = v
}
return json.Marshal(stringMap)
}
func (results CheckResults) UnmarshalJSON(bytes []byte) error {
stringMap := make(map[string]*CheckResult)
err := json.Unmarshal(bytes, &stringMap)
if err != nil {
return err
}
for k, v := range stringMap {
i, err := strconv.Atoi(k)
if err != nil {
return err
}
results[CheckId(i)] = v
}
return nil
}