Skip to content

Commit

Permalink
Merge pull request #14 from panperla/RE-12
Browse files Browse the repository at this point in the history
adding GetReportsChannelSummary
  • Loading branch information
panperla authored Jan 15, 2024
2 parents 67b39c0 + 02b9fab commit e9962a1
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Conversations // DONE
Messages // DONE
Reports // INPROGRESS
Response Templates // TODO
Staff // INPROGRESS
Staff // DONE
Status Page // TODO
102 changes: 102 additions & 0 deletions reamaze/reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package reamaze

import (
"encoding/json"
"net/http"
)

// GetReportsVolume returns a daily volume count
// The start and end dates of the report will default to the last 30 days. Time frames can be no smaller than 1 day and no larger than 1 year.
// https://www.reamaze.com/api/get_reports_volume
func (c *Client) GetReportsVolume(o ...ReportsOption) (*GetReportsVolumeResponse, error) {
var response *GetReportsVolumeResponse
settings, _ := newReportsSettings(o)
urlEndpoint := reportsEndpoint + "/volume" + settings.GetQuery()

resp, err := c.reamazeRequest(http.MethodGet, urlEndpoint, []byte{})
if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
return response, nil
}

// GetReportsResponseTime Returns a daily response time metric and a response times summary object. Response times are reported in seconds.
// https://www.reamaze.com/api/get_reports_response_time
func (c *Client) GetReportsResponseTime(o ...ReportsOption) (*GetReportsResponseTimeRespone, error) {
var response *GetReportsResponseTimeRespone
settings, _ := newReportsSettings(o)
urlEndpoint := reportsEndpoint + "/response_time" + settings.GetQuery()
resp, err := c.reamazeRequest(http.MethodGet, urlEndpoint, []byte{})
if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
return response, nil
}

// GetReportsStaffResponse returns a staff report summarizing staff metrics
// The start and end dates of the report will default to the last 30 days. Time frames can be no smaller than 1 day and no larger than 1 year.
// https://www.reamaze.com/api/get_reports_staff
func (c *Client) GetReportsStaff(o ...ReportsOption) (*GetReportsStaffResponse, error) {
var response *GetReportsStaffResponse
settings, _ := newReportsSettings(o)
urlEndpoint := reportsEndpoint + "/staff" + settings.GetQuery()
resp, err := c.reamazeRequest(http.MethodGet, urlEndpoint, []byte{})
if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
return response, nil
}

// GetReportsTags returns a tag report summarizing tag usage
// The start and end dates of the report will default to the last 30 days. Time frames can be no smaller than 1 day and no larger than 1 year.
// https://www.reamaze.com/api/get_reports_tags
func (c *Client) GetReportsTags(o ...ReportsOption) (*GetReportsTagsResponse, error) {
var response *GetReportsTagsResponse
settings, _ := newReportsSettings(o)
urlEndpoint := reportsEndpoint + "/tags" + settings.GetQuery()
resp, err := c.reamazeRequest(http.MethodGet, urlEndpoint, []byte{})
if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
return response, nil
}

// GetReportsChannelSummary returns a channel report summarizing channel metrics
// The start and end dates of the report will default to the last 30 days. Time frames can be no smaller than 1 day and no larger than 1 year.
// https://www.reamaze.com/api/get_reports_channel_summary
func (c *Client) GetReportsChannelSummary(o ...ReportsOption) (*GetReportsChannelSummaryResponse, error) {
var response *GetReportsChannelSummaryResponse
settings, _ := newReportsSettings(o)

urlEndpoint := reportsEndpoint + "/channel_summary" + settings.GetQuery()
resp, err := c.reamazeRequest(http.MethodGet, urlEndpoint, []byte{})
if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
return response, nil
}
160 changes: 160 additions & 0 deletions reamaze/reports_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package reamaze

import (
"fmt"
"strings"
"time"
)

const reportsEndpoint string = "/api/v1/reports"

type ReamazeReportsStartDate time.Time
type ReamazeReportsEndDate time.Time

type ReportsOption interface {
Apply(*ReamazeReportOptions)
}

func (w ReamazeReportsEndDate) Apply(o *ReamazeReportOptions) {
date := time.Time(w)
if date.Year() > 1 && date.Month() > 0 && date.Day() > 0 {
o.ReamazeEndDate = "end_date=" + fmt.Sprintf("%04d", date.Year()) + "-" + fmt.Sprintf("%02d", date.Month()) + "-" + fmt.Sprintf("%02d", date.Day())
}
}

func (w ReamazeReportsStartDate) Apply(o *ReamazeReportOptions) {
date := time.Time(w)
if date.Year() > 1 && date.Month() > 0 && date.Day() > 0 {
o.ReamazeStartDate = "start_date=" + fmt.Sprintf("%04d", date.Year()) + "-" + fmt.Sprintf("%02d", date.Month()) + "-" + fmt.Sprintf("%02d", date.Day())
}
}

func WithReportsEndDate(year int, month int, day int) ReamazeReportsEndDate {
endDate := ReamazeReportsEndDate{}
if year > 0 && month > 0 && day > 0 {
endDate = ReamazeReportsEndDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC))
}
return endDate
}

func WithReportsStartDate(year, month, day int) ReamazeReportsStartDate {
startDate := ReamazeReportsStartDate{}
if year > 0 && month > 0 && day > 0 {

startDate = ReamazeReportsStartDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC))
}
return startDate
}
func (r ReamazeReportOptions) GetQuery() string {
output := ""
var queryParams []string
// checking if start_date is set
if len(r.ReamazeStartDate) > 0 {
queryParams = append(queryParams, r.ReamazeStartDate)
}
// checking if end_date is set
if len(r.ReamazeStartDate) > 0 {
queryParams = append(queryParams, r.ReamazeEndDate)
}

output = strings.Join(queryParams, "&")
if len(output) > 0 {
output = "?" + output
}
return output
}
func newReportsSettings(opts []ReportsOption) (*ReamazeReportOptions, error) {
var o ReamazeReportOptions
for _, opt := range opts {
opt.Apply(&o)
}
return &o, nil
}

type ReamazeReportOptions struct {
ReamazeStartDate string
ReamazeEndDate string
}

type GetReportsVolumeResponse struct {
ConversationCounts map[string]int `json:"conversation_counts,omitempty"`
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}

type GetReportsResponseTimeRespone struct {
ResponseTimes map[string]float64 `json:"response_times,omitempty"`
Summary struct {
Averages struct {
InRange float64 `json:"in_range,omitempty"`
ThisMonth float64 `json:"this_month,omitempty"`
ThisWeek float64 `json:"this_week,omitempty"`
} `json:"averages,omitempty"`
Trends struct {
Last30Days struct {
Average float64 `json:"average,omitempty"`
ChangeRate any `json:"change_rate,omitempty"`
} `json:"last_30_days,omitempty"`
Last7Days struct {
Average float64 `json:"average,omitempty"`
ChangeRate string `json:"change_rate,omitempty"`
} `json:"last_7_days,omitempty"`
} `json:"trends,omitempty"`
Ratio struct {
Under1Hour float64 `json:"under_1_hour,omitempty"`
Under1Day float64 `json:"under_1_day,omitempty"`
Under1Week float64 `json:"under_1_week,omitempty"`
} `json:"ratio,omitempty"`
} `json:"summary,omitempty"`
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}

type GetReportsStaffResponse struct {
Report struct {
RelacjeHiLIVE struct {
ResponseCount int `json:"response_count,omitempty"`
ArchivedCount int `json:"archived_count,omitempty"`
ResolvedCount int `json:"resolved_count,omitempty"`
SatisfactionAverage any `json:"satisfaction_average,omitempty"`
AppreciationsCount any `json:"appreciations_count,omitempty"`
ResponsesTrend map[string]int `json:"responses_trend,omitempty"`
ResponseTimeSeconds float64 `json:"response_time_seconds,omitempty"`
} `json:"Relacje HiLIVE,omitempty"`
} `json:"report,omitempty"`
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}

type GetReportsTagsResponse struct {
Tags map[string]int `json:"tags,omitempty"`
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}

type GetReportsChannelSummaryResponse struct {
Channels map[string]struct {
Category struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ChannelType int `json:"channel_type,omitempty"`
ChannelTypeName string `json:"channel_type_name,omitempty"`
} `json:"category,omitempty"`
Brand struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
} `json:"brand,omitempty"`
StaffResponses int `json:"staff_responses,omitempty"`
CustomerResponses int `json:"customer_responses,omitempty"`
AverageResponseTimeSeconds any `json:"average_response_time_seconds,omitempty"`
Appreciations int `json:"appreciations,omitempty"`
ActiveConversations int `json:"active_conversations,omitempty"`
ResolvedConversations int `json:"resolved_conversations,omitempty"`
ArchivedConversations int `json:"archived_conversations,omitempty"`
AverageSatisfactionRating float64 `json:"average_satisfaction_rating,omitempty"`
AverageThreadSize float64 `json:"average_thread_size,omitempty"`
} `json:"channels,omitempty"`
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}

0 comments on commit e9962a1

Please sign in to comment.