diff --git a/pingdom/api_responses.go b/pingdom/api_responses.go index 31949b9..3b90226 100644 --- a/pingdom/api_responses.go +++ b/pingdom/api_responses.go @@ -146,6 +146,22 @@ type SummaryPerformanceSummary struct { Uptime int `json:"uptime"` } +// ResultsResponse represents the JSON response for detailed check results from the Pingdom API. +type ResultsResponse struct { + ActiveProbes []int `json:"activeprobes"` + Results []Result `json:"results"` +} + +// Result reprensents the JSON response for a detailed check result. +type Result struct { + ProbeID int `json:"probeid"` + Time int `json:"time"` + Status string `json:"status"` + ResponseTime int `json:"responsetime"` + StatusDesc string `json:"statusdesc"` + StatusDescLong string `json:"statusdesclong"` +} + // UserSmsResponse represents the JSON response for a user SMS contact. type UserSmsResponse struct { Id int `json:"id"` diff --git a/pingdom/check.go b/pingdom/check.go index 5c94544..814d1da 100644 --- a/pingdom/check.go +++ b/pingdom/check.go @@ -149,3 +149,32 @@ func (cs *CheckService) SummaryPerformance(request SummaryPerformanceRequest) (* return m, nil } + +// Results returns raw check results and the list of associated probe IDs used from Pingdom. +func (cs *CheckService) Results(id int, params ...map[string]string) (*ResultsResponse, error) { + param := map[string]string{} + if len(params) == 1 { + param = params[0] + } + req, err := cs.client.NewRequest("GET", "/results/"+strconv.Itoa(id), param) + if err != nil { + return nil, err + } + + resp, err := cs.client.client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if err := validateResponse(resp); err != nil { + return nil, err + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyString := string(bodyBytes) + m := &ResultsResponse{} + err = json.Unmarshal([]byte(bodyString), &m) + + return m, err +} diff --git a/pingdom/check_test.go b/pingdom/check_test.go index 0b746f2..48954e0 100644 --- a/pingdom/check_test.go +++ b/pingdom/check_test.go @@ -383,3 +383,78 @@ func TestCheckServiceSummaryPerformance(t *testing.T) { assert.Equal(t, expectedResponse, *resp) }) } + +func TestCheckServiceResults(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/results/12345", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{ + "activeprobes": [ + 259, + 255, + 93, + 94, + 87 + ], + "results": [ + { + "probeid": 259, + "time": 1563370611, + "status": "up", + "responsetime": 145, + "statusdesc": "OK", + "statusdesclong": "OK" + }, + { + "probeid": 87, + "time": 1563370551, + "status": "up", + "responsetime": 56, + "statusdesc": "OK", + "statusdesclong": "OK" + }, + { + "probeid": 93, + "time": 1563370491, + "status": "up", + "responsetime": 962, + "statusdesc": "OK", + "statusdesclong": "OK" + }, + { + "probeid": 255, + "time": 1563370431, + "status": "up", + "responsetime": 395, + "statusdesc": "OK", + "statusdesclong": "OK" + }, + { + "probeid": 94, + "time": 1563370371, + "status": "up", + "responsetime": 1084, + "statusdesc": "OK", + "statusdesclong": "OK" + } + ] +}`) + }) + + want := &ResultsResponse{ + ActiveProbes: []int{259, 255, 93, 94, 87}, + Results: []Result{ + {ProbeID: 259, Time: 1563370611, Status: "up", ResponseTime: 145, StatusDesc: "OK", StatusDescLong: "OK"}, + {ProbeID: 87, Time: 1563370551, Status: "up", ResponseTime: 56, StatusDesc: "OK", StatusDescLong: "OK"}, + {ProbeID: 93, Time: 1563370491, Status: "up", ResponseTime: 962, StatusDesc: "OK", StatusDescLong: "OK"}, + {ProbeID: 255, Time: 1563370431, Status: "up", ResponseTime: 395, StatusDesc: "OK", StatusDescLong: "OK"}, + {ProbeID: 94, Time: 1563370371, Status: "up", ResponseTime: 1084, StatusDesc: "OK", StatusDescLong: "OK"}, + }, + } + + results, err := client.Checks.Results(12345) + assert.NoError(t, err) + assert.Equal(t, want, results) +}