Skip to content

Commit

Permalink
Merge pull request #63 from similarweb/feature/slack-add-total-potent…
Browse files Browse the repository at this point in the history
…ial-cost-savings

Add the total potential costs for a notification group
  • Loading branch information
cregev authored Jun 16, 2020
2 parents 5ccf772 + 34532de commit f0a7c68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
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

0 comments on commit f0a7c68

Please # to comment.