<commit_before>package mpawsses

import (
	"errors"
	"flag"
	"net/url"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ses"
	mp "github.com/mackerelio/go-mackerel-plugin"
)

var graphdef = map[string]mp.Graphs{
	"ses.send24h": {
		Label: "SES Send (last 24h)",
		Unit:  "float",
		Metrics: []mp.Metrics{
			{Name: "Max24HourSend", Label: "Max"},
			{Name: "SentLast24Hours", Label: "Sent"},
		},
	},
	"ses.max_send_rate": {
		Label: "SES Max Send Rate",
		Unit:  "float",
		Metrics: []mp.Metrics{
			{Name: "MaxSendRate", Label: "MaxRate"},
		},
	},
	"ses.stats": {
		Label: "SES Stats",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "Complaints", Label: "Complaints"},
			{Name: "DeliveryAttempts", Label: "DeliveryAttempts"},
			{Name: "Bounces", Label: "Bounces"},
			{Name: "Rejects", Label: "Rejects"},
		},
	},
}

// SESPlugin mackerel plugin for Amazon SES
type SESPlugin struct {
	Region          string
	Endpoint        string
	AccessKeyID     string
	SecretAccessKey string
}

// FetchMetrics interface for mackerel plugin
func (p SESPlugin) FetchMetrics() (map[string]float64, error) {
	sess, err := session.NewSession()
	if err != nil {
		return nil, err
	}

	config := aws.NewConfig()

	if p.AccessKeyID != "" && p.SecretAccessKey != "" {
		config = config.WithCredentials(credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, ""))
	}
	if p.Region != "" && p.Endpoint != "" {
		return nil, errors.New("--region and --endpoint are exclusive.")
	}
	if p.Region != "" {
		config = config.WithRegion(p.Region)
	}
    // for backward compatibility
	if p.Endpoint != "" {
		u, err := url.Parse(p.Endpoint)
		if err != nil {
			return nil, err
		}
		hosts := strings.Split(u.Host, ".")
		if len(hosts) != 4 && hosts[2] == "amazonaws" {
			return nil, errors.New("--endpoint is invalid.")
		}
		config = config.WithRegion(hosts[1])
	}

	svc := ses.New(sess, config)

	stat := make(map[string]float64)
	quota, err := svc.GetSendQuota(&ses.GetSendQuotaInput{})
	if err != nil {<commit_msg>go fmt<commit_after>package mpawsses

import (
	"errors"
	"flag"
	"net/url"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ses"
	mp "github.com/mackerelio/go-mackerel-plugin"
)

var graphdef = map[string]mp.Graphs{
	"ses.send24h": {
		Label: "SES Send (last 24h)",
		Unit:  "float",
		Metrics: []mp.Metrics{
			{Name: "Max24HourSend", Label: "Max"},
			{Name: "SentLast24Hours", Label: "Sent"},
		},
	},
	"ses.max_send_rate": {
		Label: "SES Max Send Rate",
		Unit:  "float",
		Metrics: []mp.Metrics{
			{Name: "MaxSendRate", Label: "MaxRate"},
		},
	},
	"ses.stats": {
		Label: "SES Stats",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "Complaints", Label: "Complaints"},
			{Name: "DeliveryAttempts", Label: "DeliveryAttempts"},
			{Name: "Bounces", Label: "Bounces"},
			{Name: "Rejects", Label: "Rejects"},
		},
	},
}

// SESPlugin mackerel plugin for Amazon SES
type SESPlugin struct {
	Region          string
	Endpoint        string
	AccessKeyID     string
	SecretAccessKey string
}

// FetchMetrics interface for mackerel plugin
func (p SESPlugin) FetchMetrics() (map[string]float64, error) {
	sess, err := session.NewSession()
	if err != nil {
		return nil, err
	}

	config := aws.NewConfig()

	if p.AccessKeyID != "" && p.SecretAccessKey != "" {
		config = config.WithCredentials(credentials.NewStaticCredentials(p.AccessKeyID, p.SecretAccessKey, ""))
	}
	if p.Region != "" && p.Endpoint != "" {
		return nil, errors.New("--region and --endpoint are exclusive.")
	}
	if p.Region != "" {
		config = config.WithRegion(p.Region)
	}
	// for backward compatibility
	if p.Endpoint != "" {
		u, err := url.Parse(p.Endpoint)
		if err != nil {
			return nil, err
		}
		hosts := strings.Split(u.Host, ".")
		if len(hosts) != 4 && hosts[2] == "amazonaws" {
			return nil, errors.New("--endpoint is invalid.")
		}
		config = config.WithRegion(hosts[1])
	}

	svc := ses.New(sess, config)

	stat := make(map[string]float64)
	quota, err := svc.GetSendQuota(&ses.GetSendQuotaInput{})
	if err != nil {
{'fraction_non_alphanumeric': 0.1793071161048689, 'fraction_numerical': 0.007022471910112359, 'mean_word_length': 3.6496191512513603, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6256033', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1615, 'n_words': 343}