forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_usage_events.go
80 lines (73 loc) · 2.91 KB
/
app_usage_events.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cfclient
import (
"encoding/json"
"fmt"
"net/url"
"github.com/pkg/errors"
)
type AppUsageEvent struct {
GUID string `json:"guid"`
CreatedAt string `json:"created_at"`
State string `json:"state"`
PreviousState string `json:"previous_state"`
MemoryInMbPerInstance int `json:"memory_in_mb_per_instance"`
PreviousMemoryInMbPerInstance int `json:"previous_memory_in_mb_per_instance"`
InstanceCount int `json:"instance_count"`
PreviousInstanceCount int `json:"previous_instance_count"`
AppGUID string `json:"app_guid"`
SpaceGUID string `json:"space_guid"`
SpaceName string `json:"space_name"`
OrgGUID string `json:"org_guid"`
BuildpackGUID string `json:"buildpack_guid"`
BuildpackName string `json:"buildpack_name"`
PackageState string `json:"package_state"`
PreviousPackageState string `json:"previous_package_state"`
ParentAppGUID string `json:"parent_app_guid"`
ParentAppName string `json:"parent_app_name"`
ProcessType string `json:"process_type"`
TaskName string `json:"task_name"`
TaskGUID string `json:"task_guid"`
c *Client
}
type AppUsageEventsResponse struct {
TotalResults int `json:"total_results"`
Pages int `json:"total_pages"`
NextURL string `json:"next_url"`
Resources []AppUsageEventResource `json:"resources"`
}
type AppUsageEventResource struct {
Meta Meta `json:"metadata"`
Entity AppUsageEvent `json:"entity"`
}
// ListAppUsageEventsByQuery lists all events matching the provided query.
func (c *Client) ListAppUsageEventsByQuery(query url.Values) ([]AppUsageEvent, error) {
var appUsageEvents []AppUsageEvent
requestURL := fmt.Sprintf("/v2/app_usage_events?%s", query.Encode())
for {
var appUsageEventsResponse AppUsageEventsResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "error requesting events")
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&appUsageEventsResponse); err != nil {
return nil, errors.Wrap(err, "error unmarshaling events")
}
for _, e := range appUsageEventsResponse.Resources {
e.Entity.GUID = e.Meta.Guid
e.Entity.CreatedAt = e.Meta.CreatedAt
e.Entity.c = c
appUsageEvents = append(appUsageEvents, e.Entity)
}
requestURL = appUsageEventsResponse.NextURL
if requestURL == "" {
break
}
}
return appUsageEvents, nil
}
// ListAppUsageEvents lists all unfiltered events.
func (c *Client) ListAppUsageEvents() ([]AppUsageEvent, error) {
return c.ListAppUsageEventsByQuery(nil)
}