|
| 1 | +package zendesk |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// TimeDuration represents a time in business or calendar days |
| 11 | +type TimeDuration struct { |
| 12 | + Business int `json:"business"` |
| 13 | + Calendar int `json:"calendar"` |
| 14 | +} |
| 15 | + |
| 16 | +type TicketMetric struct { |
| 17 | + AgentWaitTimeInMinutes TimeDuration `json:"agent_wait_time_in_minutes"` |
| 18 | + AssignedAt time.Time `json:"assigned_at"` |
| 19 | + AssigneeStations int `json:"assignee_stations"` |
| 20 | + AssigneeUpdatedAt time.Time `json:"assignee_updated_at"` |
| 21 | + CreatedAt time.Time `json:"created_at"` |
| 22 | + CustomStatusUpdatedAt time.Time `json:"custom_status_updated_at"` |
| 23 | + FirstResolutionTimeInMinutes TimeDuration `json:"first_resolution_time_in_minutes"` |
| 24 | + FullResolutionTimeInMinutes TimeDuration `json:"full_resolution_time_in_minutes"` |
| 25 | + GroupStations int `json:"group_stations"` |
| 26 | + ID int `json:"id"` |
| 27 | + InitiallyAssignedAt time.Time `json:"initially_assigned_at"` |
| 28 | + LatestCommentAddedAt time.Time `json:"latest_comment_added_at"` |
| 29 | + OnHoldTimeInMinutes TimeDuration `json:"on_hold_time_in_minutes"` |
| 30 | + Reopens int `json:"reopens"` |
| 31 | + Replies int `json:"replies"` |
| 32 | + ReplyTimeInMinutes TimeDuration `json:"reply_time_in_minutes"` |
| 33 | + ReplyTimeInSeconds struct { |
| 34 | + Calendar int `json:"calendar"` |
| 35 | + } `json:"reply_time_in_seconds"` |
| 36 | + RequesterUpdatedAt time.Time `json:"requester_updated_at"` |
| 37 | + RequesterWaitTimeInMinutes TimeDuration `json:"requester_wait_time_in_minutes"` |
| 38 | + SolvedAt time.Time `json:"solved_at"` |
| 39 | + StatusUpdatedAt time.Time `json:"status_updated_at"` |
| 40 | + TicketID int `json:"ticket_id"` |
| 41 | + UpdatedAt time.Time `json:"updated_at"` |
| 42 | +} |
| 43 | + |
| 44 | +type TicketMetricListOptions struct { |
| 45 | + PageOptions |
| 46 | + |
| 47 | + SortBy string `url:"sort_by,omitempty"` |
| 48 | + |
| 49 | + SortOrder string `url:"sort_order,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +// TicketMetricsAPI is an interface containing all methods for the ticket |
| 53 | +// metrics API |
| 54 | +type TicketMetricsAPI interface { |
| 55 | + GetTicketMetrics(ctx context.Context, opts ...TicketMetricListOptions) ([]TicketMetric, Page, error) |
| 56 | + GetTicketMetric(ctx context.Context, ticketMetricsID int64) (TicketMetric, error) |
| 57 | + GetTicketMetricByTicket(ctx context.Context, ticketID int64) (TicketMetric, error) |
| 58 | +} |
| 59 | + |
| 60 | +// GetTicketMetrics get ticket metrics list with offset based pagination |
| 61 | +// |
| 62 | +// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_metrics/#list-ticket-metrics |
| 63 | +func (z *Client) GetTicketMetrics(ctx context.Context, opts *TicketMetricListOptions) ([]TicketMetric, Page, error) { |
| 64 | + var data struct { |
| 65 | + TicketMetrics []TicketMetric `json:"ticket_metrics"` |
| 66 | + Page |
| 67 | + } |
| 68 | + |
| 69 | + tmp := opts |
| 70 | + if tmp == nil { |
| 71 | + tmp = &TicketMetricListOptions{} |
| 72 | + } |
| 73 | + |
| 74 | + u, err := addOptions("/ticket_metrics.json", tmp) |
| 75 | + if err != nil { |
| 76 | + return nil, Page{}, err |
| 77 | + } |
| 78 | + |
| 79 | + body, err := z.get(ctx, u) |
| 80 | + if err != nil { |
| 81 | + return nil, Page{}, err |
| 82 | + } |
| 83 | + |
| 84 | + err = json.Unmarshal(body, &data) |
| 85 | + if err != nil { |
| 86 | + return nil, Page{}, err |
| 87 | + } |
| 88 | + return data.TicketMetrics, data.Page, nil |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_metrics/#show-ticket-metrics |
| 93 | +func (z *Client) GetTicketMetric(ctx context.Context, ticketMetricsID int64) (TicketMetric, error) { |
| 94 | + var result struct { |
| 95 | + TicketMetric TicketMetric `json:"ticket_metric"` |
| 96 | + } |
| 97 | + |
| 98 | + body, err := z.get(ctx, fmt.Sprintf("/ticket_metrics/%d.json", ticketMetricsID)) |
| 99 | + if err != nil { |
| 100 | + return TicketMetric{}, err |
| 101 | + } |
| 102 | + |
| 103 | + err = json.Unmarshal(body, &result) |
| 104 | + if err != nil { |
| 105 | + return TicketMetric{}, err |
| 106 | + } |
| 107 | + |
| 108 | + return result.TicketMetric, err |
| 109 | +} |
| 110 | + |
| 111 | +// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_metrics/#show-ticket-metrics |
| 112 | +func (z *Client) GetTicketMetricByTicket(ctx context.Context, ticketID int64) (TicketMetric, error) { |
| 113 | + var result struct { |
| 114 | + TicketMetric TicketMetric `json:"ticket_metric"` |
| 115 | + } |
| 116 | + |
| 117 | + body, err := z.get(ctx, fmt.Sprintf("/tickets/%d/metrics.json", ticketID)) |
| 118 | + if err != nil { |
| 119 | + return TicketMetric{}, err |
| 120 | + } |
| 121 | + |
| 122 | + err = json.Unmarshal(body, &result) |
| 123 | + if err != nil { |
| 124 | + return TicketMetric{}, err |
| 125 | + } |
| 126 | + |
| 127 | + return result.TicketMetric, err |
| 128 | +} |
0 commit comments