github.com/mailgun/mailgun-go/v3@v3.6.4/stats.go (about)

     1  package mailgun
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"time"
     7  )
     8  
     9  // Stats on accepted messages
    10  type Accepted struct {
    11  	Incoming int `json:"incoming"`
    12  	Outgoing int `json:"outgoing"`
    13  	Total    int `json:"total"`
    14  }
    15  
    16  // Stats on delivered messages
    17  type Delivered struct {
    18  	Smtp  int `json:"smtp"`
    19  	Http  int `json:"http"`
    20  	Total int `json:"total"`
    21  }
    22  
    23  // Stats on temporary failures
    24  type Temporary struct {
    25  	Espblock int `json:"espblock"`
    26  }
    27  
    28  // Stats on permanent failures
    29  type Permanent struct {
    30  	SuppressBounce      int `json:"suppress-bounce"`
    31  	SuppressUnsubscribe int `json:"suppress-unsubscribe"`
    32  	SuppressComplaint   int `json:"suppress-complaint"`
    33  	Bounce              int `json:"bounce"`
    34  	DelayedBounce       int `json:"delayed-bounce"`
    35  	Total               int `json:"total"`
    36  }
    37  
    38  // Stats on failed messages
    39  type Failed struct {
    40  	Temporary Temporary `json:"temporary"`
    41  	Permanent Permanent `json:"permanent"`
    42  }
    43  
    44  // Total stats for messages
    45  type Total struct {
    46  	Total int `json:"total"`
    47  }
    48  
    49  // Stats as returned by `GetStats()`
    50  type Stats struct {
    51  	Time         string    `json:"time"`
    52  	Accepted     Accepted  `json:"accepted"`
    53  	Delivered    Delivered `json:"delivered"`
    54  	Failed       Failed    `json:"failed"`
    55  	Stored       Total     `json:"stored"`
    56  	Opened       Total     `json:"opened"`
    57  	Clicked      Total     `json:"clicked"`
    58  	Unsubscribed Total     `json:"unsubscribed"`
    59  	Complained   Total     `json:"complained"`
    60  }
    61  
    62  type statsTotalResponse struct {
    63  	End        string  `json:"end"`
    64  	Resolution string  `json:"resolution"`
    65  	Start      string  `json:"start"`
    66  	Stats      []Stats `json:"stats"`
    67  }
    68  
    69  // Used by GetStats() to specify the resolution stats are for
    70  type Resolution string
    71  
    72  // Indicate which resolution a stat response for request is for
    73  const (
    74  	ResolutionHour  = Resolution("hour")
    75  	ResolutionDay   = Resolution("day")
    76  	ResolutionMonth = Resolution("month")
    77  )
    78  
    79  // Options for GetStats()
    80  type GetStatOptions struct {
    81  	Resolution Resolution
    82  	Duration   string
    83  	Start      time.Time
    84  	End        time.Time
    85  }
    86  
    87  // GetStats returns total stats for a given domain for the specified time period
    88  func (mg *MailgunImpl) GetStats(ctx context.Context, events []string, opts *GetStatOptions) ([]Stats, error) {
    89  	r := newHTTPRequest(generateApiUrl(mg, statsTotalEndpoint))
    90  
    91  	if opts != nil {
    92  		if !opts.Start.IsZero() {
    93  			r.addParameter("start", strconv.Itoa(int(opts.Start.Unix())))
    94  		}
    95  		if !opts.End.IsZero() {
    96  			r.addParameter("end", strconv.Itoa(int(opts.End.Unix())))
    97  		}
    98  		if opts.Resolution != "" {
    99  			r.addParameter("resolution", string(opts.Resolution))
   100  		}
   101  		if opts.Duration != "" {
   102  			r.addParameter("duration", opts.Duration)
   103  		}
   104  	}
   105  
   106  	for _, e := range events {
   107  		r.addParameter("event", e)
   108  	}
   109  
   110  	r.setClient(mg.Client())
   111  	r.setBasicAuth(basicAuthUser, mg.APIKey())
   112  
   113  	var res statsTotalResponse
   114  	err := getResponseFromJSON(ctx, r, &res)
   115  	if err != nil {
   116  		return nil, err
   117  	} else {
   118  		return res.Stats, nil
   119  	}
   120  }