<commit_before>package awsutil

import (
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatch"
	"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

type CloudwatchMetricsUtil struct {
	ec2Api ec2iface.EC2API
	cwApi  cloudwatchiface.CloudWatchAPI
}

func NewCloudwatchMetricsUtilFromRegion(region string) (*CloudwatchMetricsUtil, error) {

	session, err := session.NewSession()
	if err != nil {
		return nil, err
	}

	cwApi := newCloudwatchAPI(session, region)
	ec2Api := newEC2API(session, region)

	return NewCloudwatchMetricsUtil(ec2Api, cwApi)

}

func NewCloudwatchMetricsUtil(ec2Api ec2iface.EC2API, cwApi cloudwatchiface.CloudWatchAPI) (*CloudwatchMetricsUtil, error) {

	cwmUtil := &CloudwatchMetricsUtil{
		cwApi:  cwApi,
		ec2Api: ec2Api,
	}

	return cwmUtil, nil

}

func (cwmu CloudwatchMetricsUtil) GetEc2InstancesWithStates(states []string) ([]*ec2.Instance, error) {

	matchingInstances := []*ec2.Instance{}

	statesPointers := []*string {}
	for _, state := range states {
		statesPointers = append(statesPointers, aws.String(state))
	}

	diInput := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			&ec2.Filter{
				Name: aws.String("instance-state-name"),
				Values: statesPointers,
			},
		},
	}

	diOutput, err := cwmu.ec2Api.DescribeInstances(diInput)
	if err != nil {
		return matchingInstances, err
	}

	if diOutput.NextToken != nil {
		log.Printf("Warning: got non empty NextToken, but ignoring since cannot handle yet: %v.  Some results skipped.", diOutput.NextToken)
	}

	for _, r := range diOutput.Reservations {
		for _, i := range r.Instances {
			matchingInstances = append(matchingInstances, i)
		}
	}

	return matchingInstances, nil
}


// FetchRunningEc2InstanceMetrics fetches certain metrics for running EC2 instances
//
// Steps
//
// - For each of the running ec2 instances
//   - Collect metrics with time range based on Frequency param and --dimensions Name=InstanceId,Value=<cur_instance>
// - Return struct result
//
func (cwmu CloudwatchMetricsUtil) FetchRunningEc2InstanceMetrics(input FetchRunningEc2InstanceMetricsInput) ([]FetchRunningEc2InstanceMetricsOuput, error) {

	metricsOutputs := []FetchRunningEc2InstanceMetricsOuput{}

	ec2Instances, err := cwmu.GetEc2InstancesWithStates([]string{EC2_INSTANCE_STATE_RUNNING})
	if err != nil {
		return metricsOutputs, err
	}

	for _, ec2Instance := range ec2Instances {

		now := time.Now()
		oneHourAgo := now.Add(time.Hour * -1)
		metricStatsParams := &cloudwatch.GetMetricStatisticsInput{
			Namespace:  aws.String(input.Namespace),
			MetricName: aws.String(input.MetricName),
			Dimensions: []*cloudwatch.Dimension{
				&cloudwatch.Dimension{
					Name: aws.String("InstanceId"),
					Value: ec2Instance.InstanceId,
				},
			},
			Period:     aws.Int64(3600),

			StartTime:  &oneHourAgo,
			EndTime:    &now,
			Statistics: []*string{aws.String("Average")},
		}

		metricStats, err := cwmu.cwApi.GetMetricStatistics(metricStatsParams)
		if err != nil {
			return metricsOutputs, err
		}

		metricOutput := FetchRunningEc2InstanceMetricsOuput{
			Namespace: input.Namespace,
			Ec2InstanceId: *ec2Instance.InstanceId,
			MetricName: input.MetricName,
			Metrics: metricStats,
		}

		metricsOutputs = append(metricsOutputs, metricOutput)


	}

	return metricsOutputs, nil


}
<commit_msg>gofmt<commit_after>package awsutil

import (
	"fmt"
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatch"
	"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

type CloudwatchMetricsUtil struct {
	ec2Api ec2iface.EC2API
	cwApi  cloudwatchiface.CloudWatchAPI
}

func NewCloudwatchMetricsUtilFromRegion(region string) (*CloudwatchMetricsUtil, error) {

	session, err := session.NewSession()
	if err != nil {
		return nil, err
	}

	cwApi := newCloudwatchAPI(session, region)
	ec2Api := newEC2API(session, region)

	return NewCloudwatchMetricsUtil(ec2Api, cwApi)

}

func NewCloudwatchMetricsUtil(ec2Api ec2iface.EC2API, cwApi cloudwatchiface.CloudWatchAPI) (*CloudwatchMetricsUtil, error) {

	cwmUtil := &CloudwatchMetricsUtil{
		cwApi:  cwApi,
		ec2Api: ec2Api,
	}

	return cwmUtil, nil

}

func (cwmu CloudwatchMetricsUtil) GetEc2InstancesWithStates(states []string) ([]*ec2.Instance, error) {

	matchingInstances := []*ec2.Instance{}

	statesPointers := []*string{}
	for _, state := range states {
		statesPointers = append(statesPointers, aws.String(state))
	}

	diInput := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			&ec2.Filter{
				Name:   aws.String("instance-state-name"),
				Values: statesPointers,
			},
		},
	}

	diOutput, err := cwmu.ec2Api.DescribeInstances(diInput)
	if err != nil {
		return matchingInstances, err
	}

	if diOutput.NextToken != nil {
		log.Printf("Warning: got non empty NextToken, but ignoring since cannot handle yet: %v.  Some results skipped.", diOutput.NextToken)
	}

	for _, r := range diOutput.Reservations {
		for _, i := range r.Instances {
			matchingInstances = append(matchingInstances, i)
		}
	}

	return matchingInstances, nil
}

// FetchRunningEc2InstanceMetrics fetches certain metrics for running EC2 instances
//
// Steps
//
// - For each of the running ec2 instances
//   - Collect metrics with time range based on Frequency param and --dimensions Name=InstanceId,Value=<cur_instance>
// - Return struct result
//
func (cwmu CloudwatchMetricsUtil) FetchRunningEc2InstanceMetrics(input FetchRunningEc2InstanceMetricsInput) ([]FetchRunningEc2InstanceMetricsOuput, error) {

	metricsOutputs := []FetchRunningEc2InstanceMetricsOuput{}

	ec2Instances, err := cwmu.GetEc2InstancesWithStates([]string{EC2_INSTANCE_STATE_RUNNING})
	if err != nil {
		return metricsOutputs, fmt.Errorf("Error getting running ec2 instances: %v", err)
	}

	for _, ec2Instance := range ec2Instances {

		now := time.Now()
		oneHourAgo := now.Add(time.Hour * -1)
		metricStatsParams := &cloudwatch.GetMetricStatisticsInput{
			Namespace:  aws.String(input.Namespace),
			MetricName: aws.String(input.MetricName),
			Dimensions: []*cloudwatch.Dimension{
				&cloudwatch.Dimension{
					Name:  aws.String("InstanceId"),
					Value: ec2Instance.InstanceId,
				},
			},
			Period: aws.Int64(3600),

			StartTime:  &oneHourAgo,
			EndTime:    &now,
			Statistics: []*string{aws.String("Average")},
		}

		metricStats, err := cwmu.cwApi.GetMetricStatistics(metricStatsParams)
		if err != nil {
			return metricsOutputs, fmt.Errorf("Error getting metric stats for instance [%v]: %v", *ec2Instance.InstanceId, err)
		}

		metricOutput := FetchRunningEc2InstanceMetricsOuput{
			Namespace:     input.Namespace,
			Ec2InstanceId: *ec2Instance.InstanceId,
			MetricName:    input.MetricName,
			Metrics:       metricStats,
		}

		metricsOutputs = append(metricsOutputs, metricOutput)

	}

	return metricsOutputs, nil

}

{'fraction_non_alphanumeric': 0.11953311770496414, 'fraction_numerical': 0.012656447756996203, 'mean_word_length': 4.560594214229867, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4699729', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2431, 'n_words': 478}