Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add the total potential costs for a notification group #63

Merged
merged 3 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/storage/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ type StorageDescriber interface {
GetResources(resourceType string, executionID string) ([]map[string]interface{}, error)
}

// Executions define the execution collectors data
// Executions defines the collectors execution data
type Executions struct {
ID string
Name string
Time time.Time
}

// CollectorsSummary define unused resource summery
// CollectorsSummary defines unused resource summary
type CollectorsSummary struct {
ResourceName string `json:"ResourceName"`
ResourceCount int64 `json:"ResourceCount"`
Expand Down
29 changes: 23 additions & 6 deletions notifiers/providers/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"finala/notifiers/common"
notifierCommon "finala/notifiers/common"
"fmt"
"math"
"strings"

"github.com/dustin/go-humanize"
Expand All @@ -21,8 +22,10 @@ var (
const (
// AuthorName Slack will use while sending the name
AuthorName = "Finala Notifier"
// MessageColor color to use
MessageColor = "#2EB67D"
// greenMessageColor color to use
greenMessageColor = "#2EB67D"
// blueMessageColor color to use
blueMessageColor = "#3aa3e3"
)

// NewManager returns the notifier
Expand Down Expand Up @@ -76,31 +79,45 @@ func (sm *Manager) GetNotifyByTags(notifierConfig common.ConfigByName) map[strin

// prepareAttachmentFields will prepare all the Attachment and all the fields
func (sm *Manager) prepareAttachment(message common.NotifierReport, tags []string) []slackApi.Attachment {
// Finala's intro message Attachment
slackAttachments := []slackApi.Attachment{
{
Color: MessageColor,
Color: greenMessageColor,
AuthorName: AuthorName,
Pretext: fmt.Sprintf("Here is the <%s|Cost report> for your notification group: %s filtered by: %s",
Pretext: fmt.Sprintf("Here is the *Monthly* <%s|Cost report> for your notification group: %s *filtered by: %s*",
message.UIAddr,
message.GroupName,
strings.Join(tags, " AND ")),
}}
var totalPotentialSaving float64
for _, executionData := range message.ExecutionSummaryData {
// If the total spent is 0 or small than minimum cost to present we don't want to show it in Slack
if executionData.TotalSpent == 0 || executionData.TotalSpent <= message.NotifyByTag.MinimumCostToPresent {
continue
}
totalPotentialSaving = totalPotentialSaving + executionData.TotalSpent
slackAttachments = append(slackAttachments, slackApi.Attachment{
Color: MessageColor,
Color: greenMessageColor,
Fields: []slackApi.AttachmentField{
{
Title: strings.ToUpper(executionData.ResourceName),
Value: fmt.Sprintf("Potential Saving: $%s", humanize.Commaf(executionData.TotalSpent)),
Value: fmt.Sprintf("Potential Saving: $%s", humanize.Commaf(math.Floor(executionData.TotalSpent))),
Short: false,
},
},
})
}

// Add the total potential savings as the last attachment
slackAttachments = append(slackAttachments, slackApi.Attachment{
Color: blueMessageColor,
Fields: []slackApi.AttachmentField{
{
Title: fmt.Sprintf("Total Potential Savings: $%s", humanize.Commaf(math.Floor(totalPotentialSaving))),
Short: false,
},
},
})
return slackAttachments
}

Expand Down
4 changes: 2 additions & 2 deletions notifiers/providers/slack/slack_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func TestPrepareAttachment(t *testing.T) {

attachments := slackManager.prepareAttachment(*notifierReport, formattedTags)
t.Run("check slack attachments", func(t *testing.T) {
if len(attachments) != 2 {
t.Fatalf("unexpected len of slack attachments , got %d expected %d", len(attachments), 2)
if len(attachments) != 3 {
t.Fatalf("unexpected len of slack attachments , got %d expected %d", len(attachments), 3)
}
})
}
Expand Down