forked from CastawayLabs/cachet-monitor
-
Notifications
You must be signed in to change notification settings - Fork 6
/
component.go
48 lines (37 loc) · 1.03 KB
/
component.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
package cachet
import (
"encoding/json"
"strconv"
"github.com/Sirupsen/logrus"
)
// Component Cachet data model
type Component struct {
ID int `json:"id"`
Name string `json:"name"`
Status int `json:"status"`
Enabled bool `json:"enabled"`
}
// LoadCurrentIncident - Returns current incident
func (comp *Component) LoadCurrentIncident(cfg *CachetMonitor) (*Incident, error) {
if comp.ID == 0 {
return nil, nil
}
jsonBytes, _ := json.Marshal(map[string]interface{}{
"component_id": strconv.Itoa(comp.ID),
"status": 1,
"per_page": 1,
})
resp, body, err := cfg.API.NewRequest("GET", "/incidents", jsonBytes)
if err != nil || resp.StatusCode != 200 {
logrus.Warnf("Could not get data from component (id: %d, status: %d, err: %v)", comp.ID, resp.StatusCode, err)
return nil, err
}
incidentInfoA := []Incident{}
if e := json.Unmarshal(body.Data, &incidentInfoA); e != nil {
logrus.Warnf("Error decoding JSON: %v\n", e)
}
if len(incidentInfoA) == 0 {
return nil, err
}
return &incidentInfoA[0], err
}