-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsnapshot.go
43 lines (35 loc) · 1.01 KB
/
snapshot.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
package megos
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
// GetMetricsSnapshot will return the snapshot of pid
func (c *Client) GetMetricsSnapshot(pid *Pid) (*MetricsSnapshot, error) {
u := c.GetURLForMetricsSnapshotPid(*pid)
resp, err := c.GetHTTPResponse(&u)
return c.parseMetricsSnapshotResponse(resp, err)
}
// GetURLForMetricsSnapshotPid will return the URL for the snapshot
// based on a PID
func (c *Client) GetURLForMetricsSnapshotPid(pid Pid) url.URL {
return c.getBaseURLForFilePid(pid, "metrics/snapshot")
}
// parseMetricsSnapshotResponse will transform a http.Response to snapshot map
func (c *Client) parseMetricsSnapshotResponse(resp *http.Response, err error) (*MetricsSnapshot, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var snapshot MetricsSnapshot
err = json.Unmarshal(body, &snapshot)
if err != nil {
return nil, err
}
c.Lock()
c.MetricsSnapshot = &snapshot
c.Unlock()
return c.MetricsSnapshot, nil
}