github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_task_queues_statistics.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Taskrouter
     8   * This is the public Twilio REST API.
     9   *
    10   * NOTE: This class is auto generated by OpenAPI Generator.
    11   * https://openapi-generator.tech
    12   * Do not edit the class manually.
    13   */
    14  
    15  package openapi
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'FetchTaskQueueStatistics'
    28  type FetchTaskQueueStatisticsParams struct {
    29  	// Only calculate statistics from this date and time and earlier, specified in GMT as an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time.
    30  	EndDate *time.Time `json:"EndDate,omitempty"`
    31  	// Only calculate statistics since this many minutes in the past. The default is 15 minutes.
    32  	Minutes *int `json:"Minutes,omitempty"`
    33  	// Only calculate statistics from this date and time and later, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
    34  	StartDate *time.Time `json:"StartDate,omitempty"`
    35  	// Only calculate real-time and cumulative statistics for the specified TaskChannel. Can be the TaskChannel's SID or its `unique_name`, such as `voice`, `sms`, or `default`.
    36  	TaskChannel *string `json:"TaskChannel,omitempty"`
    37  	// A comma separated list of values that describes the thresholds, in seconds, to calculate statistics on. For each threshold specified, the number of Tasks canceled and reservations accepted above and below the specified thresholds in seconds are computed.
    38  	SplitByWaitTime *string `json:"SplitByWaitTime,omitempty"`
    39  }
    40  
    41  func (params *FetchTaskQueueStatisticsParams) SetEndDate(EndDate time.Time) *FetchTaskQueueStatisticsParams {
    42  	params.EndDate = &EndDate
    43  	return params
    44  }
    45  func (params *FetchTaskQueueStatisticsParams) SetMinutes(Minutes int) *FetchTaskQueueStatisticsParams {
    46  	params.Minutes = &Minutes
    47  	return params
    48  }
    49  func (params *FetchTaskQueueStatisticsParams) SetStartDate(StartDate time.Time) *FetchTaskQueueStatisticsParams {
    50  	params.StartDate = &StartDate
    51  	return params
    52  }
    53  func (params *FetchTaskQueueStatisticsParams) SetTaskChannel(TaskChannel string) *FetchTaskQueueStatisticsParams {
    54  	params.TaskChannel = &TaskChannel
    55  	return params
    56  }
    57  func (params *FetchTaskQueueStatisticsParams) SetSplitByWaitTime(SplitByWaitTime string) *FetchTaskQueueStatisticsParams {
    58  	params.SplitByWaitTime = &SplitByWaitTime
    59  	return params
    60  }
    61  
    62  //
    63  func (c *ApiService) FetchTaskQueueStatistics(WorkspaceSid string, TaskQueueSid string, params *FetchTaskQueueStatisticsParams) (*TaskrouterV1TaskQueueStatistics, error) {
    64  	path := "/v1/Workspaces/{WorkspaceSid}/TaskQueues/{TaskQueueSid}/Statistics"
    65  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    66  	path = strings.Replace(path, "{"+"TaskQueueSid"+"}", TaskQueueSid, -1)
    67  
    68  	data := url.Values{}
    69  	headers := make(map[string]interface{})
    70  
    71  	if params != nil && params.EndDate != nil {
    72  		data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
    73  	}
    74  	if params != nil && params.Minutes != nil {
    75  		data.Set("Minutes", fmt.Sprint(*params.Minutes))
    76  	}
    77  	if params != nil && params.StartDate != nil {
    78  		data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
    79  	}
    80  	if params != nil && params.TaskChannel != nil {
    81  		data.Set("TaskChannel", *params.TaskChannel)
    82  	}
    83  	if params != nil && params.SplitByWaitTime != nil {
    84  		data.Set("SplitByWaitTime", *params.SplitByWaitTime)
    85  	}
    86  
    87  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	defer resp.Body.Close()
    93  
    94  	ps := &TaskrouterV1TaskQueueStatistics{}
    95  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return ps, err
   100  }
   101  
   102  // Optional parameters for the method 'ListTaskQueuesStatistics'
   103  type ListTaskQueuesStatisticsParams struct {
   104  	// Only calculate statistics from this date and time and earlier, specified in GMT as an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date-time.
   105  	EndDate *time.Time `json:"EndDate,omitempty"`
   106  	// The `friendly_name` of the TaskQueue statistics to read.
   107  	FriendlyName *string `json:"FriendlyName,omitempty"`
   108  	// Only calculate statistics since this many minutes in the past. The default is 15 minutes.
   109  	Minutes *int `json:"Minutes,omitempty"`
   110  	// Only calculate statistics from this date and time and later, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
   111  	StartDate *time.Time `json:"StartDate,omitempty"`
   112  	// Only calculate statistics on this TaskChannel. Can be the TaskChannel's SID or its `unique_name`, such as `voice`, `sms`, or `default`.
   113  	TaskChannel *string `json:"TaskChannel,omitempty"`
   114  	// A comma separated list of values that describes the thresholds, in seconds, to calculate statistics on. For each threshold specified, the number of Tasks canceled and reservations accepted above and below the specified thresholds in seconds are computed.
   115  	SplitByWaitTime *string `json:"SplitByWaitTime,omitempty"`
   116  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   117  	PageSize *int `json:"PageSize,omitempty"`
   118  	// Max number of records to return.
   119  	Limit *int `json:"limit,omitempty"`
   120  }
   121  
   122  func (params *ListTaskQueuesStatisticsParams) SetEndDate(EndDate time.Time) *ListTaskQueuesStatisticsParams {
   123  	params.EndDate = &EndDate
   124  	return params
   125  }
   126  func (params *ListTaskQueuesStatisticsParams) SetFriendlyName(FriendlyName string) *ListTaskQueuesStatisticsParams {
   127  	params.FriendlyName = &FriendlyName
   128  	return params
   129  }
   130  func (params *ListTaskQueuesStatisticsParams) SetMinutes(Minutes int) *ListTaskQueuesStatisticsParams {
   131  	params.Minutes = &Minutes
   132  	return params
   133  }
   134  func (params *ListTaskQueuesStatisticsParams) SetStartDate(StartDate time.Time) *ListTaskQueuesStatisticsParams {
   135  	params.StartDate = &StartDate
   136  	return params
   137  }
   138  func (params *ListTaskQueuesStatisticsParams) SetTaskChannel(TaskChannel string) *ListTaskQueuesStatisticsParams {
   139  	params.TaskChannel = &TaskChannel
   140  	return params
   141  }
   142  func (params *ListTaskQueuesStatisticsParams) SetSplitByWaitTime(SplitByWaitTime string) *ListTaskQueuesStatisticsParams {
   143  	params.SplitByWaitTime = &SplitByWaitTime
   144  	return params
   145  }
   146  func (params *ListTaskQueuesStatisticsParams) SetPageSize(PageSize int) *ListTaskQueuesStatisticsParams {
   147  	params.PageSize = &PageSize
   148  	return params
   149  }
   150  func (params *ListTaskQueuesStatisticsParams) SetLimit(Limit int) *ListTaskQueuesStatisticsParams {
   151  	params.Limit = &Limit
   152  	return params
   153  }
   154  
   155  // Retrieve a single page of TaskQueuesStatistics records from the API. Request is executed immediately.
   156  func (c *ApiService) PageTaskQueuesStatistics(WorkspaceSid string, params *ListTaskQueuesStatisticsParams, pageToken, pageNumber string) (*ListTaskQueuesStatisticsResponse, error) {
   157  	path := "/v1/Workspaces/{WorkspaceSid}/TaskQueues/Statistics"
   158  
   159  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   160  
   161  	data := url.Values{}
   162  	headers := make(map[string]interface{})
   163  
   164  	if params != nil && params.EndDate != nil {
   165  		data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
   166  	}
   167  	if params != nil && params.FriendlyName != nil {
   168  		data.Set("FriendlyName", *params.FriendlyName)
   169  	}
   170  	if params != nil && params.Minutes != nil {
   171  		data.Set("Minutes", fmt.Sprint(*params.Minutes))
   172  	}
   173  	if params != nil && params.StartDate != nil {
   174  		data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
   175  	}
   176  	if params != nil && params.TaskChannel != nil {
   177  		data.Set("TaskChannel", *params.TaskChannel)
   178  	}
   179  	if params != nil && params.SplitByWaitTime != nil {
   180  		data.Set("SplitByWaitTime", *params.SplitByWaitTime)
   181  	}
   182  	if params != nil && params.PageSize != nil {
   183  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   184  	}
   185  
   186  	if pageToken != "" {
   187  		data.Set("PageToken", pageToken)
   188  	}
   189  	if pageNumber != "" {
   190  		data.Set("Page", pageNumber)
   191  	}
   192  
   193  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	defer resp.Body.Close()
   199  
   200  	ps := &ListTaskQueuesStatisticsResponse{}
   201  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return ps, err
   206  }
   207  
   208  // Lists TaskQueuesStatistics records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   209  func (c *ApiService) ListTaskQueuesStatistics(WorkspaceSid string, params *ListTaskQueuesStatisticsParams) ([]TaskrouterV1TaskQueuesStatistics, error) {
   210  	response, errors := c.StreamTaskQueuesStatistics(WorkspaceSid, params)
   211  
   212  	records := make([]TaskrouterV1TaskQueuesStatistics, 0)
   213  	for record := range response {
   214  		records = append(records, record)
   215  	}
   216  
   217  	if err := <-errors; err != nil {
   218  		return nil, err
   219  	}
   220  
   221  	return records, nil
   222  }
   223  
   224  // Streams TaskQueuesStatistics records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   225  func (c *ApiService) StreamTaskQueuesStatistics(WorkspaceSid string, params *ListTaskQueuesStatisticsParams) (chan TaskrouterV1TaskQueuesStatistics, chan error) {
   226  	if params == nil {
   227  		params = &ListTaskQueuesStatisticsParams{}
   228  	}
   229  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   230  
   231  	recordChannel := make(chan TaskrouterV1TaskQueuesStatistics, 1)
   232  	errorChannel := make(chan error, 1)
   233  
   234  	response, err := c.PageTaskQueuesStatistics(WorkspaceSid, params, "", "")
   235  	if err != nil {
   236  		errorChannel <- err
   237  		close(recordChannel)
   238  		close(errorChannel)
   239  	} else {
   240  		go c.streamTaskQueuesStatistics(response, params, recordChannel, errorChannel)
   241  	}
   242  
   243  	return recordChannel, errorChannel
   244  }
   245  
   246  func (c *ApiService) streamTaskQueuesStatistics(response *ListTaskQueuesStatisticsResponse, params *ListTaskQueuesStatisticsParams, recordChannel chan TaskrouterV1TaskQueuesStatistics, errorChannel chan error) {
   247  	curRecord := 1
   248  
   249  	for response != nil {
   250  		responseRecords := response.TaskQueuesStatistics
   251  		for item := range responseRecords {
   252  			recordChannel <- responseRecords[item]
   253  			curRecord += 1
   254  			if params.Limit != nil && *params.Limit < curRecord {
   255  				close(recordChannel)
   256  				close(errorChannel)
   257  				return
   258  			}
   259  		}
   260  
   261  		record, err := client.GetNext(c.baseURL, response, c.getNextListTaskQueuesStatisticsResponse)
   262  		if err != nil {
   263  			errorChannel <- err
   264  			break
   265  		} else if record == nil {
   266  			break
   267  		}
   268  
   269  		response = record.(*ListTaskQueuesStatisticsResponse)
   270  	}
   271  
   272  	close(recordChannel)
   273  	close(errorChannel)
   274  }
   275  
   276  func (c *ApiService) getNextListTaskQueuesStatisticsResponse(nextPageUrl string) (interface{}, error) {
   277  	if nextPageUrl == "" {
   278  		return nil, nil
   279  	}
   280  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   281  	if err != nil {
   282  		return nil, err
   283  	}
   284  
   285  	defer resp.Body.Close()
   286  
   287  	ps := &ListTaskQueuesStatisticsResponse{}
   288  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   289  		return nil, err
   290  	}
   291  	return ps, nil
   292  }