Skip to content

Commit

Permalink
Merge pull request #18 from tetsuya28/fix-collecto-days
Browse files Browse the repository at this point in the history
Fix collect data days
  • Loading branch information
tetsuya28 authored Jan 3, 2024
2 parents 4c6fec0 + 52514c5 commit 43e5bf4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,3 @@ cp .env{.sample,}
```
make run
```

## Known issues
### Got daily cost as $0.000
If you set CloudWatch Events Schedule near AM 0:00 in UTC, AWS has not reflect daily cost yet.
So, you need to set the schedule for more later.
8 changes: 6 additions & 2 deletions external/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetIconURL(service string) string {
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/BusinessApplications/SimpleEmailService.png?raw=true"
case "Amazon DynamoDB":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Database/DynamoDB.png?raw=true"
case "Amazon EC2 Container Registry (ECR)":
case "Amazon EC2 Container Registry (ECR)", "Amazon Elastic Container Registry Public":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/ElasticContainerRegistry.png?raw=true"
case "Amazon Elastic Container Service":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/ElasticContainerService.png?raw=true"
Expand Down Expand Up @@ -69,6 +69,10 @@ func GetIconURL(service string) string {
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/GuardDuty.png?raw=true"
case "Amazon Elastic Container Service for Kubernetes":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/Containers/EKSCloud.png?raw=true"
case "Amazon Cognito":
return "https://github.com/awslabs/aws-icons-for-plantuml/blob/main/dist/SecurityIdentityCompliance/Cognito.png?raw=true"
case "Tax":
return ""
default:
return ""
}
Expand All @@ -77,7 +81,7 @@ func GetIconURL(service string) string {
func GetCost() (*costexplorer.GetCostAndUsageOutput, error) {
now := time.Now()
end := now.Format("2006-01-02")
twoDaysBefore := now.AddDate(0, 0, -2).Format("2006-01-02")
twoDaysBefore := now.AddDate(0, 0, -3).Format("2006-01-02")

granularity := "DAILY"
metrics := []string{
Expand Down
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func handler() error {
text := i18y.Translate(Language, "title", fullName, yesterday.Format("2006-01-02"), cost[1].Total)
option := slack.MsgOptionText(text, false)

attachments := toAttachment(cost)
attachments, err := toAttachment(cost)
if err != nil {
log.Warn("failed to convert attachment, err=%w", err)
return err
}

err = slk.PostMessage(cfg.SlackChannel, option, slack.MsgOptionAttachments(attachments...))
if err != nil {
log.Warn("failed to post message to Slack, err=%w", err)
Expand Down Expand Up @@ -158,11 +163,10 @@ func toCost(result *costexplorer.Group) (ServiceDetail, error) {
}, nil
}

func toAttachment(cost []DailyCost) []slack.Attachment {
func toAttachment(cost []DailyCost) ([]slack.Attachment, error) {
// Just day before yesterday and yesterday
if len(cost) != 2 {
log.Warn("cost length is not 2")
return nil
if len(cost) != 3 {
return nil, fmt.Errorf("cost length is not 3")
}

attachments := make([]slack.Attachment, len(cost[1].Services))
Expand All @@ -172,14 +176,14 @@ func toAttachment(cost []DailyCost) []slack.Attachment {
priceDiffStatement := ""
before, ok := cost[0].Services[name]
if ok {
diff := (detail.CostAmount / before.CostAmount) * 100
diff := ((detail.CostAmount / before.CostAmount) - 1) * 100

if !math.IsNaN(diff) {
diffMark := ""
// Set red color if diff is over 100%
if diff == 100 {
if diff == 0 {
color = "#ffffff"
} else if diff > 100 {
} else if diff > 0 {
color = "#ff0000"
diffMark = "📈"
} else {
Expand All @@ -188,7 +192,7 @@ func toAttachment(cost []DailyCost) []slack.Attachment {
}

priceDiffStatement = fmt.Sprintf(
" ( %s %.1f%% )",
" ( %s %.3f%% )",
diffMark,
diff,
)
Expand Down Expand Up @@ -222,5 +226,5 @@ func toAttachment(cost []DailyCost) []slack.Attachment {
attachments = append(attachments, attachment)
}

return attachments
return attachments, nil
}

0 comments on commit 43e5bf4

Please sign in to comment.