Skip to content

Commit

Permalink
better namings
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc committed Oct 25, 2024
1 parent 519cb67 commit 016fc78
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
22 changes: 11 additions & 11 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/mailgun/errors"
)

type MetricsPagination struct {
type AnalyticsMetricsPagination struct {
// Colon-separated value indicating column name and sort direction e.g. 'domain:asc'.
Sort string `json:"sort"`
// The number of items to skip over when satisfying the request. To get the first page of data set skip to zero. Then increment the skip by the limit for subsequent calls.
Expand All @@ -24,17 +24,17 @@ type MetricsPagination struct {
// as `https://api.mailgun.net/v1` or set `mg.SetAPIBase("https://api.mailgun.net/v1")`
//
// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Metrics/
func (mg *MailgunImpl) ListMetrics(opts MetricsOptions) (*MetricsIterator, error) {
func (mg *MailgunImpl) ListMetrics(opts AnalyticsMetricsOptions) (*AnalyticsMetricsIterator, error) {
if !strings.HasSuffix(mg.APIBase(), "/v1") {
return nil, errors.New("only v1 API is supported")
}

domain := mg.Domain()
if domain != "" {
domainFilter := MetricsFilterPredicate{
domainFilter := AnalyticsMetricsFilterPredicate{
Attribute: "domain",
Comparator: "=",
LabeledValues: []MetricsLabeledValue{{Label: domain, Value: domain}},
LabeledValues: []AnalyticsMetricsLabeledValue{{Label: domain, Value: domain}},
}

opts.Filter.BoolGroupAnd = append(opts.Filter.BoolGroupAnd, domainFilter)
Expand All @@ -48,26 +48,26 @@ func (mg *MailgunImpl) ListMetrics(opts MetricsOptions) (*MetricsIterator, error
req.setClient(mg.Client())
req.setBasicAuth(basicAuthUser, mg.APIKey())

return &MetricsIterator{
return &AnalyticsMetricsIterator{
opts: opts,
req: req,
}, nil
}

type MetricsIterator struct {
opts MetricsOptions
type AnalyticsMetricsIterator struct {
opts AnalyticsMetricsOptions
req *httpRequest
err error
}

func (iter *MetricsIterator) Err() error {
func (iter *AnalyticsMetricsIterator) Err() error {
return iter.err
}

// Next retrieves the next page of items from the api. Returns false when there are
// no more pages to retrieve or if there was an error.
// Use `.Err()` to retrieve the error
func (iter *MetricsIterator) Next(ctx context.Context, resp *MetricsResponse) (more bool) {
func (iter *AnalyticsMetricsIterator) Next(ctx context.Context, resp *AnalyticsMetricsResponse) (more bool) {
if iter.err != nil {
return false
}
Expand All @@ -86,7 +86,7 @@ func (iter *MetricsIterator) Next(ctx context.Context, resp *MetricsResponse) (m
return true
}

func (iter *MetricsIterator) fetch(ctx context.Context, resp *MetricsResponse) error {
func (iter *AnalyticsMetricsIterator) fetch(ctx context.Context, resp *AnalyticsMetricsResponse) error {
if resp == nil {
return errors.New("resp cannot be nil")
}
Expand All @@ -99,7 +99,7 @@ func (iter *MetricsIterator) fetch(ctx context.Context, resp *MetricsResponse) e
}

// preallocate
resp.Items = make([]MetricsItem, 0, iter.opts.Pagination.Limit)
resp.Items = make([]AnalyticsMetricsItem, 0, iter.opts.Pagination.Limit)

err = httpResp.parseFromJSON(resp)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions analytics_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mailgun

type MetricsOptions struct {
type AnalyticsMetricsOptions struct {
// A start date (default: 7 days before current time).
Start RFC2822Time `json:"start,omitempty"`
// An end date (default: current time).
Expand All @@ -15,26 +15,26 @@ type MetricsOptions struct {
// Name of the metrics to receive the stats for such as 'accepted_count' 'delivered_count' 'accepted_rate'.
Metrics []string `json:"metrics,omitempty"`
// Filters to apply to the query.
Filter MetricsFilterPredicateGroup `json:"filter,omitempty"`
Filter AnalyticsMetricsFilterPredicateGroup `json:"filter,omitempty"`
// Include stats from all subaccounts.
IncludeSubaccounts bool `json:"include_subaccounts,omitempty"`
// Include top-level aggregate metrics.
IncludeAggregates bool `json:"include_aggregates,omitempty"`
// Attributes used for pagination and sorting.
Pagination MetricsPagination `json:"pagination,omitempty"`
Pagination AnalyticsMetricsPagination `json:"pagination,omitempty"`
}

type MetricsLabeledValue struct {
type AnalyticsMetricsLabeledValue struct {
Label string `json:"label"`
Value string `json:"value"`
}

type MetricsFilterPredicate struct {
Attribute string `json:"attribute"`
Comparator string `json:"comparator"`
LabeledValues []MetricsLabeledValue `json:"values,omitempty"`
type AnalyticsMetricsFilterPredicate struct {
Attribute string `json:"attribute"`
Comparator string `json:"comparator"`
LabeledValues []AnalyticsMetricsLabeledValue `json:"values,omitempty"`
}

type MetricsFilterPredicateGroup struct {
BoolGroupAnd []MetricsFilterPredicate `json:"AND,omitempty"`
type AnalyticsMetricsFilterPredicateGroup struct {
BoolGroupAnd []AnalyticsMetricsFilterPredicate `json:"AND,omitempty"`
}
32 changes: 16 additions & 16 deletions analytics_response.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package mailgun

type MetricsResponse struct {
Start RFC2822Time `json:"start"`
End RFC2822Time `json:"end"`
Resolution Resolution `json:"resolution"`
Duration string `json:"duration"`
Dimensions []string `json:"dimensions"`
Aggregates MetricsAggregates `json:"aggregates"`
Items []MetricsItem `json:"items"`
Pagination MetricsPagination `json:"pagination"`
type AnalyticsMetricsResponse struct {
Start RFC2822Time `json:"start"`
End RFC2822Time `json:"end"`
Resolution Resolution `json:"resolution"`
Duration string `json:"duration"`
Dimensions []string `json:"dimensions"`
Aggregates AnalyticsMetricsAggregates `json:"aggregates"`
Items []AnalyticsMetricsItem `json:"items"`
Pagination AnalyticsMetricsPagination `json:"pagination"`
}

type MetricsItem struct {
Dimensions []MetricsDimension `json:"dimensions"`
Metrics Metrics `json:"metrics"`
type AnalyticsMetricsItem struct {
Dimensions []AnalyticsMetricsDimension `json:"dimensions"`
Metrics AnalyticsMetrics `json:"metrics"`
}

type MetricsAggregates struct {
Metrics Metrics `json:"metrics"`
type AnalyticsMetricsAggregates struct {
Metrics AnalyticsMetrics `json:"metrics"`
}

type Metrics struct {
type AnalyticsMetrics struct {
AcceptedIncomingCount *uint64 `json:"accepted_incoming_count,omitempty"`
AcceptedOutgoingCount *uint64 `json:"accepted_outgoing_count,omitempty"`
AcceptedCount *uint64 `json:"accepted_count,omitempty"`
Expand Down Expand Up @@ -85,7 +85,7 @@ type Metrics struct {
SeedTestCount *uint64 `json:"seed_test_count,omitempty"`
}

type MetricsDimension struct {
type AnalyticsMetricsDimension struct {
// The dimension
Dimension string `json:"dimension"`
// The dimension value
Expand Down
16 changes: 8 additions & 8 deletions analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ func TestListMetrics(t *testing.T) {
start, _ := mailgun.NewRFC2822Time("Tue, 24 Sep 2024 00:00:00 +0000")
end, _ := mailgun.NewRFC2822Time("Tue, 24 Oct 2024 00:00:00 +0000")

opts := mailgun.MetricsOptions{
opts := mailgun.AnalyticsMetricsOptions{
Start: start,
End: end,
Pagination: mailgun.MetricsPagination{
Pagination: mailgun.AnalyticsMetricsPagination{
Limit: 10,
},
}

wantResp := mailgun.MetricsResponse{
wantResp := mailgun.AnalyticsMetricsResponse{
Start: start,
End: end,
Resolution: "day",
Duration: "30d",
Dimensions: []string{"time"},
Items: []mailgun.MetricsItem{
Items: []mailgun.AnalyticsMetricsItem{
{
Dimensions: []mailgun.MetricsDimension{{
Dimensions: []mailgun.AnalyticsMetricsDimension{{
Dimension: "time",
Value: "Tue, 24 Sep 2024 00:00:00 +0000",
DisplayValue: "Tue, 24 Sep 2024 00:00:00 +0000",
}},
Metrics: mailgun.Metrics{
Metrics: mailgun.AnalyticsMetrics{
SentCount: ptr(uint64(4)),
DeliveredCount: ptr(uint64(3)),
OpenedCount: ptr(uint64(2)),
FailedCount: ptr(uint64(1)),
},
},
},
Pagination: mailgun.MetricsPagination{
Pagination: mailgun.AnalyticsMetricsPagination{
Sort: "",
Skip: 0,
Limit: 10,
Expand All @@ -56,7 +56,7 @@ func TestListMetrics(t *testing.T) {
it, err := mg.ListMetrics(opts)
require.NoError(t, err)

var page mailgun.MetricsResponse
var page mailgun.AnalyticsMetricsResponse
ctx := context.Background()
more := it.Next(ctx, &page)
require.Nil(t, it.Err())
Expand Down
6 changes: 3 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestIntegrationMailgunImpl_ListMetrics(t *testing.T) {
require.NoError(t, err)
}

opts := mailgun.MetricsOptions{
opts := mailgun.AnalyticsMetricsOptions{
End: mailgun.RFC2822Time(time.Now().UTC()),
Duration: "30d",
Pagination: mailgun.MetricsPagination{
Pagination: mailgun.AnalyticsMetricsPagination{
Limit: 10,
},
}
Expand All @@ -35,7 +35,7 @@ func TestIntegrationMailgunImpl_ListMetrics(t *testing.T) {
defer cancel()

for i := 0; i < 2; i++ {
var resp mailgun.MetricsResponse
var resp mailgun.AnalyticsMetricsResponse
more := iter.Next(ctx, &resp)
if iter.Err() != nil {
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ type Mailgun interface {
DeleteBounce(ctx context.Context, address string) error
DeleteBounceList(ctx context.Context) error

ListMetrics(opts MetricsOptions) (*MetricsIterator, error)
ListMetrics(opts AnalyticsMetricsOptions) (*AnalyticsMetricsIterator, error)

// Deprecated: Use ListMetrics instead.
GetStats(ctx context.Context, events []string, opts *GetStatOptions) ([]Stats, error)
Expand Down
10 changes: 5 additions & 5 deletions mock_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ func (ms *mockServer) listMetrics(w http.ResponseWriter, _ *http.Request) {
start, _ := NewRFC2822Time("Tue, 24 Sep 2024 00:00:00 +0000")
end, _ := NewRFC2822Time("Tue, 24 Oct 2024 00:00:00 +0000")

resp := MetricsResponse{
resp := AnalyticsMetricsResponse{
Start: start,
End: end,
Resolution: "day",
Duration: "30d",
Dimensions: []string{"time"},
Items: []MetricsItem{
Items: []AnalyticsMetricsItem{
{
Dimensions: []MetricsDimension{{
Dimensions: []AnalyticsMetricsDimension{{
Dimension: "time",
Value: "Tue, 24 Sep 2024 00:00:00 +0000",
DisplayValue: "Tue, 24 Sep 2024 00:00:00 +0000",
}},
Metrics: Metrics{
Metrics: AnalyticsMetrics{
SentCount: ptr(uint64(4)),
DeliveredCount: ptr(uint64(3)),
OpenedCount: ptr(uint64(2)),
FailedCount: ptr(uint64(1)),
},
},
},
Pagination: MetricsPagination{
Pagination: AnalyticsMetricsPagination{
Sort: "",
Skip: 0,
Limit: 10,
Expand Down

0 comments on commit 016fc78

Please sign in to comment.