forked from mailgun/mailgun-go
/
stats.go
141 lines (118 loc) · 3.5 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package mailgun
import (
"strconv"
"time"
)
const iso8601date = "2006-01-02"
type Stat struct {
Event string `json:"event"`
TotalCount int `json:"total_count"`
CreatedAt string `json:"created_at"`
Id string `json:"id"`
Tags map[string]int `json:"tags"`
}
type statsEnvelope struct {
TotalCount int `json:"total_count"`
Items []Stat `json:"items"`
}
type Accepted struct {
Incoming int `json:"incoming"`
Outgoing int `json:"outgoing"`
Total int `json:"total"`
}
type Delivered struct {
Smtp int `json:"smtp"`
Http int `json:"http"`
Total int `json:"total"`
}
type Temporary struct {
Espblock int `json:"espblock"`
}
type Permanent struct {
SuppressBounce int `json:"suppress-bounce"`
SuppressUnsubscribe int `json:"suppress-unsubscribe"`
SuppressComplaint int `json:"suppress-complaint"`
Bounce int `json:"bounce"`
DelayedBounce int `json:"delayed-bounce"`
Total int `json:"total"`
}
type Failed struct {
Temporary Temporary `json:"temporary"`
Permanent Permanent `json:"permanent"`
}
type Total struct {
Total int `json:"total"`
}
type StatsTotal struct {
Time string `json:"time"`
Accepted Accepted `json:"accepted"`
Delivered Delivered `json:"delivered"`
Failed Failed `json:"failed"`
Stored Total `json:"stored"`
Opened Total `json:"opened"`
Clicked Total `json:"clicked"`
Unsubscribed Total `json:"unsubscribed"`
Complained Total `json:"complained"`
}
type StatsTotalResponse struct {
End string `json:"end"`
Resolution string `json:"resolution"`
Start string `json:"start"`
Stats []StatsTotal `json:"stats"`
}
// GetStats returns a basic set of statistics for different events.
// Events start at the given start date, if one is provided.
// If not, this function will consider all stated events dating to the creation of the sending domain.
func (m *MailgunImpl) GetStats(limit int, skip int, startDate *time.Time, event ...string) (int, []Stat, error) {
r := newHTTPRequest(generateApiUrl(m, statsEndpoint))
if limit != -1 {
r.addParameter("limit", strconv.Itoa(limit))
}
if skip != -1 {
r.addParameter("skip", strconv.Itoa(skip))
}
if startDate != nil {
r.addParameter("start-date", startDate.Format(iso8601date))
}
for _, e := range event {
r.addParameter("event", e)
}
r.setClient(m.Client())
r.setBasicAuth(basicAuthUser, m.APIKey())
var res statsEnvelope
err := getResponseFromJSON(r, &res)
if err != nil {
return -1, nil, err
} else {
return res.TotalCount, res.Items, nil
}
}
// GetStatsTotal returns a basic set of statistics for different events.
// https://documentation.mailgun.com/en/latest/api-stats.html#stats
func (m *MailgunImpl) GetStatsTotal(start *time.Time, end *time.Time, resolution string, duration string, event ...string) (*StatsTotalResponse, error) {
r := newHTTPRequest(generateApiUrl(m, statsTotalEndpoint))
if start != nil {
r.addParameter("start", start.Format(iso8601date))
}
if end != nil {
r.addParameter("end", end.Format(iso8601date))
}
if resolution != "" {
r.addParameter("resolution", resolution)
}
if duration != "" {
r.addParameter("duration", duration)
}
for _, e := range event {
r.addParameter("event", e)
}
r.setClient(m.Client())
r.setBasicAuth(basicAuthUser, m.APIKey())
var res StatsTotalResponse
err := getResponseFromJSON(r, &res)
if err != nil {
return nil, err
} else {
return &res, nil
}
}