Refactor based on goreportcard.com

This commit is contained in:
Matej Kramny
2015-03-20 20:40:16 +01:00
parent 8e17ebceef
commit 3d0b0c88e1
7 changed files with 55 additions and 38 deletions

View File

@@ -4,15 +4,16 @@ import (
"time"
)
// Cachet component model
type Component struct {
Id int `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Status int `json:"status"`
Link *string `json:"link"`
Order *int `json:"order"`
Group_id *int `json:"group_id"`
Created_at *time.Time `json:"created_at"`
Updated_at *time.Time `json:"updated_at"`
Deleted_at *time.Time `json:"deleted_at"`
GroupId *int `json:"group_id"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
}

View File

@@ -10,11 +10,13 @@ import (
"encoding/json"
)
// Static config
var Config CachetConfig
// Monitoring tool configuration
type CachetConfig struct {
API_Url string `json:"api_url"`
API_Token string `json:"api_token"`
APIUrl string `json:"api_url"`
APIToken string `json:"api_token"`
Monitors []*Monitor `json:"monitors"`
}
@@ -56,13 +58,13 @@ func init() {
}
if len(os.Getenv("CACHET_API")) > 0 {
Config.API_Url = os.Getenv("CACHET_API")
Config.APIUrl = os.Getenv("CACHET_API")
}
if len(os.Getenv("CACHET_TOKEN")) > 0 {
Config.API_Token = os.Getenv("CACHET_TOKEN")
Config.APIToken = os.Getenv("CACHET_TOKEN")
}
if len(Config.API_Token) == 0 || len(Config.API_Url) == 0 {
if len(Config.APIToken) == 0 || len(Config.APIUrl) == 0 {
fmt.Printf("API URL or API Token not set. cachet-monitor won't be able to report incidents.\n\nPlease set:\n CACHET_API and CACHET_TOKEN environment variable to override settings.\n\nGet help at https://github.com/CastawayLabs/cachet-monitor\n")
os.Exit(1)
}

View File

@@ -6,26 +6,30 @@ import (
"encoding/json"
)
// Cachet Incident data model
type Incident struct {
Id int `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
Message string `json:"message"`
Status int `json:"status"`// 4?
Human_status string `json:"human_status"`
HumanStatus string `json:"human_status"`
Component *Component `json:"component"`
Component_id *int `json:"component_id"`
Created_at int `json:"created_at"`
Updated_at int `json:"updated_at"`
ComponentId *int `json:"component_id"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
}
// Response when creating/updating an incident
type IncidentData struct {
Incident Incident `json:"data"`
}
// from API /incidents
type IncidentList struct {
Incidents []Incident `json:"data"`
}
// Get list of incidents
func GetIncidents() []Incident {
_, body, err := makeRequest("GET", "/incidents", nil)
if err != nil {
@@ -36,12 +40,12 @@ func GetIncidents() []Incident {
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("Cannot parse incidents.")
panic(err)
}
return data.Incidents
}
// Create or Update incident
func (incident *Incident) Send() {
jsonBytes, err := json.Marshal(incident)
if err != nil {
@@ -49,13 +53,13 @@ func (incident *Incident) Send() {
}
requestType := "POST"
requestUrl := "/incidents"
if incident.Id > 0 {
requestURL := "/incidents"
if incident.ID > 0 {
requestType = "PUT"
requestUrl = "/incidents/" + strconv.Itoa(incident.Id)
requestURL = "/incidents/" + strconv.Itoa(incident.ID)
}
resp, body, err := makeRequest(requestType, requestUrl, jsonBytes)
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
if err != nil {
panic(err)
}
@@ -68,44 +72,50 @@ func (incident *Incident) Send() {
fmt.Println("Cannot parse incident body.")
panic(err)
} else {
incident.Id = data.Incident.Id
incident.ID = data.Incident.ID
}
fmt.Println("ID:"+strconv.Itoa(incident.Id))
fmt.Println("ID:"+strconv.Itoa(incident.ID))
if resp.StatusCode != 200 {
fmt.Println("Could not create/update incident!")
}
}
// Get the same incident.
// Updates incident.ID
func (incident *Incident) GetSimilarIncidentId() {
incidents := GetIncidents()
for _, inc := range incidents {
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.Human_status == inc.Human_status {
incident.Id = inc.Id
fmt.Printf("Updated incident id to %v\n", inc.Id)
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
incident.ID = inc.ID
fmt.Printf("Updated incident id to %v\n", inc.ID)
break
}
}
}
// Set status to Investigating
func (incident *Incident) SetInvestigating() {
incident.Status = 1
incident.Human_status = "Investigating"
incident.HumanStatus = "Investigating"
}
// Set status to Identified
func (incident *Incident) SetIdentified() {
incident.Status = 2
incident.Human_status = "Identified"
incident.HumanStatus = "Identified"
}
// Set status to Watching
func (incident *Incident) SetWatching() {
incident.Status = 3
incident.Human_status = "Watching"
incident.HumanStatus = "Watching"
}
// Set status to Fixed
func (incident *Incident) SetFixed() {
incident.Status = 4
incident.Human_status = "Fixed"
incident.HumanStatus = "Fixed"
}

View File

@@ -6,8 +6,9 @@ import (
"encoding/json"
)
func SendMetric(metricId int, delay int64) {
if metricId <= 0 {
// Send lag metric point
func SendMetric(metricID int, delay int64) {
if metricID <= 0 {
return
}
@@ -15,7 +16,7 @@ func SendMetric(metricId int, delay int64) {
"value": delay,
})
resp, _, err := makeRequest("POST", "/metrics/" + strconv.Itoa(metricId) + "/points", jsonBytes)
resp, _, err := makeRequest("POST", "/metrics/" + strconv.Itoa(metricID) + "/points", jsonBytes)
if err != nil || resp.StatusCode != 200 {
fmt.Printf("Could not log data point!\n%v\n", err)
return

View File

@@ -8,12 +8,13 @@ import (
const timeout = time.Duration(time.Second)
// Monitor data model
type Monitor struct {
Name string `json:"name"`
Url string `json:"url"`
MetricId int `json:"metric_id"`
URL string `json:"url"`
MetricID int `json:"metric_id"`
Threshold float32 `json:"threshold"`
ComponentId *int `json:"component_id"`
ComponentID *int `json:"component_id"`
ExpectedStatusCode int `json:"expected_status_code"`
History []bool `json:"-"`
@@ -21,6 +22,7 @@ type Monitor struct {
Incident *Incident `json:"-"`
}
// Run loop
func (monitor *Monitor) Run() {
reqStart := getMs()
isUp := monitor.doRequest()
@@ -53,6 +55,7 @@ func (monitor *Monitor) doRequest() bool {
return resp.StatusCode == monitor.ExpectedStatusCode
}
// Decides if the monitor is statistically up or down and creates / resolves an incident
func (monitor *Monitor) AnalyseData() {
// look at the past few incidents
numDown := 0

View File

@@ -7,10 +7,10 @@ import (
)
func makeRequest(requestType string, url string, reqBody []byte) (*http.Response, []byte, error) {
req, err := http.NewRequest(requestType, Config.API_Url + url, bytes.NewBuffer(reqBody))
req, err := http.NewRequest(requestType, Config.APIUrl + url, bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Cachet-Token", Config.API_Token)
req.Header.Set("X-Cachet-Token", Config.APIToken)
client := &http.Client{}
res, err := client.Do(req)