Skip to content

Commit

Permalink
Merge pull request #397 from t-junjie/master
Browse files Browse the repository at this point in the history
refactor: Reduce code duplication in analytics.go
  • Loading branch information
theckman committed Nov 17, 2021
2 parents 12152a7 + 8fe27bf commit 9190a3b
Showing 1 changed file with 11 additions and 30 deletions.
41 changes: 11 additions & 30 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ 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 {
Expand Down Expand Up @@ -60,48 +63,26 @@ type AnalyticsData struct {

// GetAggregatedIncidentData gets the aggregated incident analytics for the requested data.
func (c *Client) GetAggregatedIncidentData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}
resp, err := c.post(ctx, "/analytics/metrics/incidents/all", 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
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) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}

resp, err := c.post(ctx, "/analytics/metrics/incidents/services", 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
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",
}

resp, err := c.post(ctx, "/analytics/metrics/incidents/teams", analytics, h)
u := fmt.Sprintf("%s/%s", analyticsBaseURL, endpoint)
resp, err := c.post(ctx, u, analytics, h)
if err != nil {
return AnalyticsResponse{}, err
}
Expand Down

0 comments on commit 9190a3b

Please sign in to comment.