-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/multi account selection #177
Open
daniebrill
wants to merge
34
commits into
similarweb:master
Choose a base branch
from
daniebrill:feature/multi_account_selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
8daf59a
#SOS-15
Jackafive753 35a3f71
#SOS-22
Jackafive753 5866b6e
#SOS-15
Jackafive753 839ae2f
#SOS-23 Add getAccounts + save account information in event_status
daniebrill 6cd0a85
#SOS-23 Use querylimit in aggregation + add error handling for type a…
daniebrill f8e931b
#SOS-23 Fix existing tests + code format
daniebrill 259ccf7
#SOS-23 Modify GetAccounts for specific execution ID
daniebrill 45db9b4
#SOS-23 Fit MockStorage to modified interface StorageDescriber
daniebrill 2f3c15c
#SOS-23 Add tests in interpolation_test.go and server_test.go
daniebrill 1fd641a
#SOS-20
Jackafive753 9c157f2
#SOS-23 Add test for elasticsearch GetAccounts
daniebrill 0f68ad2
#SOS-16
Jackafive753 2b48f30
Merge remote-tracking branch 'origin/feature_multi_account_selection'…
Jackafive753 bf4c57d
#SOS18 Add multiaccount selection functionality to tables
daniebrill ecbe7ef
#SOS-16
Jackafive753 5a7065e
Merge remote-tracking branch 'origin/feature_multi_account_selection'…
Jackafive753 5ea12e5
#SOS-18
Jackafive753 2f99548
#SOS-18
daniebrill 4df494c
fix warnings: unused statements
Jackafive753 ab6405e
# SOS-57
Jackafive753 f8adc70
set account to filters if an account specific Chart is clicked
Jackafive753 bbc3bb9
if config not a valid aws-account then give account a default id
Jackafive753 9c31755
list all accounts also with duplicate ids
Jackafive753 801bb42
better logging
Jackafive753 8de13fd
rollback arn assembling
Jackafive753 61ead41
Add comment to GetAccounts method in elasticsearch.go
daniebrill 9376f00
Merge remote-tracking branch 'origin/feature/multi_account_selection'…
daniebrill 263001f
rename AccountID to AccountId
Jackafive753 f76f47e
Merge remote-tracking branch 'origin/feature/multi_account_selection'…
Jackafive753 e61e776
split AccountInformation in AccountName and AccountId
Jackafive753 63f7b85
Remove ExtractAccountInformation from interpolation.go
daniebrill 2eebe76
fix bug: account with no id and no name returned
Jackafive753 ec38174
Merge remote-tracking branch 'origin/feature/multi_account_selection'…
daniebrill 399e4ee
fix bug: no data return when filter
Jackafive753 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"fmt" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
elastic "github.com/olivere/elastic/v7" | ||
|
@@ -110,14 +111,24 @@ func (sm *StorageManager) getDynamicMatchQuery(filters map[string]string, operat | |
dynamicMatchQuery := []elastic.Query{} | ||
var mq *elastic.MatchQuery | ||
for name, value := range filters { | ||
mq = elastic.NewMatchQuery(name, value) | ||
// Minimum number of clauses that must match for a document to be returned | ||
mq.MinimumShouldMatch("100%") | ||
if operator == "and" { | ||
mq = mq.Operator("and") | ||
if name == "Data.AccountId" { | ||
var accountIds = strings.Split(value, ",") | ||
var accountBoolQuery = elastic.NewBoolQuery() | ||
for _, accountId := range accountIds { | ||
accountBoolQuery.Should(elastic.NewMatchQuery(name, accountId)) | ||
} | ||
accountBoolQuery.MinimumShouldMatch("1") | ||
dynamicMatchQuery = append(dynamicMatchQuery, accountBoolQuery) | ||
} else { | ||
mq = elastic.NewMatchQuery(name, value) | ||
// Minimum number of clauses that must match for a document to be returned | ||
mq.MinimumShouldMatch("100%") | ||
if operator == "and" { | ||
mq = mq.Operator("and") | ||
} | ||
log.Info("Query ", mq) | ||
dynamicMatchQuery = append(dynamicMatchQuery, mq) | ||
} | ||
|
||
dynamicMatchQuery = append(dynamicMatchQuery, mq) | ||
} | ||
return dynamicMatchQuery | ||
} | ||
|
@@ -183,60 +194,76 @@ func (sm *StorageManager) GetSummary(executionID string, filters map[string]stri | |
for resourceName, resourceData := range summary { | ||
filters["ResourceName"] = resourceName | ||
log.WithField("filters", filters).Debug("Going to get resources summary details with the following filters") | ||
totalSpent, resourceCount, err := sm.getResourceSummaryDetails(executionID, filters) | ||
totalSpent, resourceCount, spentAccounts, err := sm.getResourceSummaryDetails(executionID, filters) | ||
|
||
if err != nil { | ||
continue | ||
} | ||
newResourceData := resourceData | ||
newResourceData.TotalSpent = totalSpent | ||
newResourceData.ResourceCount = resourceCount | ||
newResourceData.SpentAccounts = spentAccounts | ||
summary[resourceName] = newResourceData | ||
|
||
} | ||
|
||
return summary, nil | ||
|
||
} | ||
|
||
// getResourceSummaryDetails returns total resource spent and total resources detected | ||
func (sm *StorageManager) getResourceSummaryDetails(executionID string, filters map[string]string) (float64, int64, error) { | ||
func (sm *StorageManager) getResourceSummaryDetails(executionID string, filters map[string]string) (float64, int64, map[string]float64, error) { | ||
|
||
var totalSpent float64 | ||
var resourceCount int64 | ||
var spentAccounts = make(map[string]float64) | ||
|
||
dynamicMatchQuery := sm.getDynamicMatchQuery(filters, "or") | ||
dynamicMatchQuery = append(dynamicMatchQuery, elastic.NewTermQuery("ExecutionID", executionID)) | ||
dynamicMatchQuery = append(dynamicMatchQuery, elastic.NewTermQuery("EventType", "resource_detected")) | ||
|
||
searchResult, err := sm.client.Search(). | ||
searchResultAccount, err := sm.client.Search(). | ||
Query(elastic.NewBoolQuery().Must(dynamicMatchQuery...)). | ||
Aggregation("sum", elastic.NewSumAggregation().Field("Data.PricePerMonth")). | ||
Aggregation("accounts", elastic.NewTermsAggregation().Field("Data.AccountId.keyword"). | ||
SubAggregation("accountSum", elastic.NewSumAggregation().Field("Data.PricePerMonth"))). | ||
Size(0).Do(context.Background()) | ||
|
||
if err != nil { | ||
log.WithError(err).WithFields(log.Fields{ | ||
"filters": filters, | ||
}).Error("error when trying to get summary details") | ||
|
||
return totalSpent, resourceCount, err | ||
log.WithError(err).Error("error when trying to get executions collectors") | ||
return totalSpent, resourceCount, spentAccounts, ErrInvalidQuery | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"filters": filters, | ||
"milliseconds": searchResult.TookInMillis, | ||
}).Debug("get execution details") | ||
respAccount, ok := searchResultAccount.Aggregations.Terms("accounts") | ||
if !ok { | ||
log.Error("accounts field term does not exist") | ||
return totalSpent, resourceCount, spentAccounts, ErrAggregationTermNotFound | ||
} | ||
|
||
resp, ok := searchResult.Aggregations.Terms("sum") | ||
if ok { | ||
if val, ok := resp.Aggregations["value"]; ok { | ||
for _, AccountIdBucket := range respAccount.Buckets { | ||
|
||
spent, ok := AccountIdBucket.Aggregations.Terms("accountSum") | ||
if ok { | ||
if val, ok := spent.Aggregations["value"]; ok { | ||
accountID, ok := AccountIdBucket.Key.(string) | ||
if !ok { | ||
log.WithFields(log.Fields{ | ||
"search_account_id": AccountIdBucket.Key, | ||
"val": val, | ||
}).Error("type assertion to string failed") | ||
continue | ||
} | ||
spentAccounts[accountID], err = strconv.ParseFloat(string(val), 64) | ||
if err != nil { | ||
log.WithError(err).Error("parse failure") | ||
continue | ||
} | ||
|
||
totalSpent, _ = strconv.ParseFloat(string(val), 64) | ||
resourceCount = searchResult.Hits.TotalHits.Value | ||
totalSpent += spentAccounts[accountID] | ||
resourceCount += AccountIdBucket.DocCount | ||
} | ||
} | ||
} | ||
|
||
return totalSpent, resourceCount, nil | ||
return totalSpent, resourceCount, spentAccounts, nil | ||
} | ||
|
||
// GetExecutions returns collector executions | ||
|
@@ -298,6 +325,61 @@ func (sm *StorageManager) GetExecutions(queryLimit int) ([]storage.Executions, e | |
return executions, nil | ||
} | ||
|
||
// GetAccounts returns a list of all checked accounts for the given execution ID | ||
func (sm *StorageManager) GetAccounts(executionID string, querylimit int) ([]storage.Accounts, error) { | ||
accounts := []storage.Accounts{} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add logger instance with global fields that will show for each log that you write (better visibility ): logger := log.WithField("execution_id", executionID) then use logger for any log line. |
||
logger := log.WithField("execution_id", executionID) | ||
|
||
searchResult, err := sm.client.Search().Query(elastic.NewMatchQuery("ExecutionID", executionID)). | ||
Aggregation("AccountIds", | ||
elastic.NewTermsAggregation().Field("Data.AccountId.keyword"). | ||
SubAggregation("AccountNames", elastic.NewTermsAggregation().Field("Data.AccountName.keyword")). | ||
Size(querylimit)). | ||
Do(context.Background()) | ||
|
||
if err != nil { | ||
logger.WithError(err).Error("error when trying to get AccountIDs") | ||
return accounts, ErrInvalidQuery | ||
} | ||
|
||
resp, ok := searchResult.Aggregations.Terms("AccountIds") | ||
if !ok { | ||
logger.Error("accountIds field term does not exist") | ||
return accounts, ErrAggregationTermNotFound | ||
} | ||
|
||
for _, accountIdsBucket := range resp.Buckets { | ||
accountId, ok := accountIdsBucket.Key.(string) | ||
if !ok { | ||
logger.Error("type assertion to string failed") | ||
continue | ||
} | ||
|
||
names, ok := accountIdsBucket.Aggregations.Terms("AccountNames") | ||
if !ok { | ||
logger.Error("accountNames field term does not exist") | ||
return accounts, ErrAggregationTermNotFound | ||
} | ||
|
||
for _, namesBucket := range names.Buckets { | ||
accountName, ok := namesBucket.Key.(string) | ||
if !ok { | ||
logger.Error("type assertion to string failed") | ||
continue | ||
} | ||
|
||
if accountId != "" || accountName != "" { | ||
accounts = append(accounts, storage.Accounts{ | ||
ID: accountId, | ||
Name: accountName, | ||
}) | ||
} | ||
} | ||
} | ||
return accounts, nil | ||
} | ||
|
||
// GetResources return resource data | ||
func (sm *StorageManager) GetResources(resourceType string, executionID string, filters map[string]string) ([]map[string]interface{}, error) { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document the fucntion
// GetAccounts .....