-
Notifications
You must be signed in to change notification settings - Fork 39
/
stat.go
85 lines (70 loc) · 2.59 KB
/
stat.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
81
82
83
84
85
package sentry
import (
"fmt"
"net/url"
"strconv"
)
const (
// StatReceived is set to received for sending to /stats/ endpoints
StatReceived StatQuery = "received"
// StatRejected is set to rejected for sending to /stats/ endpoints
StatRejected StatQuery = "rejected"
// StatBlacklisted is set to blacklisted for sending to /stats/ endpoints
StatBlacklisted StatQuery = "blacklisted"
)
type statRequest struct {
Stat StatQuery `json:"stat"`
Since int64 `json:"since"`
Until int64 `json:"until"`
Resolution *string `json:"resolution,omitempty"`
}
func (o *statRequest) ToQueryString() string {
query := url.Values{}
query.Add("stat", string(o.Stat))
query.Add("since", strconv.FormatInt(o.Since, 10))
query.Add("until", strconv.FormatInt(o.Until, 10))
if o.Resolution != nil {
query.Add("resolution", string(*o.Resolution))
}
return query.Encode()
}
// Stat is used for tetting a time in seconds and the metric in a float
type Stat [2]float64
// StatQuery is sample type for sending to /stats/ endpoints
type StatQuery string
// GetOrganizationStats fetches stats from the org. Needs a Organization, a StatQuery, a timestamp in seconds since epoch and a optional resolution
func (c *Client) GetOrganizationStats(org Organization, stat StatQuery, since, until int64, resolution *string) ([]Stat, error) {
var orgstats []Stat
orgstatrequest := &statRequest{
Stat: stat,
Since: since,
Until: until,
Resolution: resolution,
}
err := c.doWithQuery("GET", fmt.Sprintf("%s/%s/stats", OrgEndpointName, *org.Slug), &orgstats, nil, orgstatrequest)
return orgstats, err
}
// GetTeamStats will fetch all stats for a specific team. Similar to GetOrganizationStats
func (c *Client) GetTeamStats(o Organization, t Team, stat StatQuery, since, until int64, resolution *string) ([]Stat, error) {
var teamstats []Stat
teamstatrequest := &statRequest{
Stat: stat,
Since: since,
Until: until,
Resolution: resolution,
}
err := c.doWithQuery("GET", fmt.Sprintf("teams/%s/%s/stats", *o.Slug, *t.Slug), &teamstats, nil, teamstatrequest)
return teamstats, err
}
// GetProjectStats will fetch all stats for a specific project. Similar to GetOrganizationStats
func (c *Client) GetProjectStats(o Organization, p Project, stat StatQuery, since, until int64, resolution *string) ([]Stat, error) {
var stats []Stat
statrequest := &statRequest{
Stat: stat,
Since: since,
Until: until,
Resolution: resolution,
}
err := c.doWithQuery("GET", fmt.Sprintf("projects/%s/%s/stats", *o.Slug, *p.Slug), &stats, nil, statrequest)
return stats, err
}