Skip to content

Commit

Permalink
Merge pull request #310 from PagerDuty/analytics_style_fixes
Browse files Browse the repository at this point in the history
Add documentation to analytics.go; style updates
  • Loading branch information
Scott McAllister committed Mar 17, 2021
2 parents 1428ccf + 2d68c46 commit 0c26903
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"context"
)

// AnalyticsRequest represents the request to be sent to PagerDuty when you want
// aggregated analytics.
type AnalyticsRequest struct {
AnalyticsFilter *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"`
AnalyticsFilter *AnalyticsFilter `json:"filters,omitempty"`
AggregateUnit string `json:"aggregate_unit,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
}

// AnalyticsFilter is the filter is part of the request to PagerDuty when
// requesting incident analytics.
type AnalyticsFilter struct {
CreatedAtStart string `json:"created_at_start,omitempty"`
CreatedAtEnd string `json:"created_at_end,omitempty"`
Expand All @@ -27,6 +33,7 @@ type AnalyticsFilter struct {
PriorityName []string `json:"priority_name,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"`
Expand All @@ -51,15 +58,21 @@ type AnalyticsData struct {
RangeStart string `json:"range_start,omitempty"`
}

// GetAggregatedIncidentData gets the aggregated analytics for the requested data.
func (c *Client) GetAggregatedIncidentData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
var analyticsResponse AnalyticsResponse
headers := make(map[string]string)
headers["X-EARLY-ACCESS"] = "analytics-v2"
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}

resp, err := c.post(ctx, "/analytics/metrics/incidents/all", analytics, headers)
resp, err := c.post(ctx, "/analytics/metrics/incidents/all", analytics, h)
if err != nil {
return analyticsResponse, err
return AnalyticsResponse{}, err
}
err = c.decodeJSON(resp, &analyticsResponse)
return analyticsResponse, err

var analyticsResponse AnalyticsResponse
if err = c.decodeJSON(resp, &analyticsResponse); err != nil {
return AnalyticsResponse{}, err
}

return analyticsResponse, nil
}

0 comments on commit 0c26903

Please sign in to comment.