forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics.go
96 lines (83 loc) · 4.37 KB
/
analytics.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
86
87
88
89
90
91
92
93
94
95
96
package pagerduty
import (
"context"
"fmt"
)
const analyticsBaseURL = "/analytics/metrics/incidents"
// AnalyticsRequest represents the request to be sent to PagerDuty when you want
// aggregated analytics.
type AnalyticsRequest struct {
Filters *AnalyticsFilter `json:"filters,omitempty"`
AggregateUnit string `json:"aggregate_unit,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
}
// AnalyticsResponse represents the response from the PagerDuty API.
type AnalyticsResponse struct {
Data []AnalyticsData `json:"data,omitempty"`
Filters *AnalyticsFilter `json:"filters,omitempty"`
AggregateUnit string `json:"aggregate_unit,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
}
// AnalyticsFilter represents the set of filters as part of the request to PagerDuty when
// requesting analytics.
type AnalyticsFilter struct {
CreatedAtStart string `json:"created_at_start,omitempty"`
CreatedAtEnd string `json:"created_at_end,omitempty"`
Urgency string `json:"urgency,omitempty"`
Major bool `json:"major,omitempty"`
ServiceIDs []string `json:"service_ids,omitempty"`
TeamIDs []string `json:"team_ids,omitempty"`
PriorityIDs []string `json:"priority_ids,omitempty"`
PriorityNames []string `json:"priority_names,omitempty"`
}
// AnalyticsData represents the structure of the analytics we have available.
type AnalyticsData struct {
ServiceID string `json:"service_id,omitempty"`
ServiceName string `json:"service_name,omitempty"`
TeamID string `json:"team_id,omitempty"`
TeamName string `json:"team_name,omitempty"`
MeanSecondsToResolve int `json:"mean_seconds_to_resolve,omitempty"`
MeanSecondsToFirstAck int `json:"mean_seconds_to_first_ack,omitempty"`
MeanSecondsToEngage int `json:"mean_seconds_to_engage,omitempty"`
MeanSecondsToMobilize int `json:"mean_seconds_to_mobilize,omitempty"`
MeanEngagedSeconds int `json:"mean_engaged_seconds,omitempty"`
MeanEngagedUserCount int `json:"mean_engaged_user_count,omitempty"`
TotalEscalationCount int `json:"total_escalation_count,omitempty"`
MeanAssignmentCount int `json:"mean_assignment_count,omitempty"`
TotalBusinessHourInterruptions int `json:"total_business_hour_interruptions,omitempty"`
TotalSleepHourInterruptions int `json:"total_sleep_hour_interruptions,omitempty"`
TotalOffHourInterruptions int `json:"total_off_hour_interruptions,omitempty"`
TotalSnoozedSeconds int `json:"total_snoozed_seconds,omitempty"`
TotalEngagedSeconds int `json:"total_engaged_seconds,omitempty"`
TotalIncidentCount int `json:"total_incident_count,omitempty"`
UpTimePct float64 `json:"up_time_pct,omitempty"`
UserDefinedEffortSeconds int `json:"user_defined_effort_seconds,omitempty"`
RangeStart string `json:"range_start,omitempty"`
}
// GetAggregatedIncidentData gets the aggregated incident analytics for the requested data.
func (c *Client) GetAggregatedIncidentData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
return c.getAggregatedData(ctx, analytics, "all")
}
// GetAggregatedServiceData gets the aggregated service analytics for the requested data.
func (c *Client) GetAggregatedServiceData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
return c.getAggregatedData(ctx, analytics, "services")
}
// GetAggregatedTeamData gets the aggregated team analytics for the requested data.
func (c *Client) GetAggregatedTeamData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
return c.getAggregatedData(ctx, analytics, "teams")
}
func (c *Client) getAggregatedData(ctx context.Context, analytics AnalyticsRequest, endpoint string) (AnalyticsResponse, error) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}
u := fmt.Sprintf("%s/%s", analyticsBaseURL, endpoint)
resp, err := c.post(ctx, u, analytics, h)
if err != nil {
return AnalyticsResponse{}, err
}
var analyticsResponse AnalyticsResponse
if err = c.decodeJSON(resp, &analyticsResponse); err != nil {
return AnalyticsResponse{}, err
}
return analyticsResponse, nil
}