Merge pull request #50 from criteo-forks/add_results

Add support for raw results
This commit is contained in:
Russell Cardullo 2019-07-18 08:17:02 -07:00 коммит произвёл GitHub
Родитель 7fc01671cb c95c31ab88
Коммит bc586efbde
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 120 добавлений и 0 удалений

Просмотреть файл

@ -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"`

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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)
}