Browse Source

Merge pull request #261 from kubecost/AjayTripathy-filter-changes

filter function changes
Ajay Tripathy 6 years ago
parent
commit
085a510bc7
2 changed files with 19 additions and 6 deletions
  1. 1 0
      cloud/provider.go
  2. 18 6
      costmodel/router.go

+ 1 - 0
cloud/provider.go

@@ -138,6 +138,7 @@ type CustomPricing struct {
 	AthenaTable           string            `json:"athenaTable"`
 	BillingDataDataset    string            `json:"billingDataDataset,omitempty"`
 	CustomPricesEnabled   string            `json:"customPricesEnabled"`
+	DefaultIdle           string            `json:"defaultIdle"`
 	AzureSubscriptionID   string            `json:"azureSubscriptionID"`
 	AzureClientID         string            `json:"azureClientID"`
 	AzureClientSecret     string            `json:"azureClientSecret"`

+ 18 - 6
costmodel/router.go

@@ -73,17 +73,29 @@ type DataEnvelope struct {
 	Message string      `json:"message,omitempty"`
 }
 
-// FilterFunc is a filter that returns true iff the given CostData should be filtered out
-type FilterFunc func(*CostData) bool
+// FilterFunc is a filter that returns true iff the given CostData should be filtered out, and the environment that was used as the filter criteria, if it was an aggregate
+type FilterFunc func(*CostData) (bool, string)
 
 // FilterCostData allows through only CostData that matches all the given filter functions
-func FilterCostData(data map[string]*CostData, filters ...FilterFunc) map[string]*CostData {
+func FilterCostData(data map[string]*CostData, retains []FilterFunc, filters []FilterFunc) (map[string]*CostData, int, map[string]int) {
 	result := make(map[string]*CostData)
-
+	filteredEnvironments := make(map[string]int)
+	filteredContainers := 0
 DataLoop:
 	for key, datum := range data {
+		for _, rf := range retains {
+			if ok, _ := rf(datum); ok {
+				result[key] = datum
+				// if any retain function passes, the data is retained and move on
+				continue DataLoop
+			}
+		}
 		for _, ff := range filters {
-			if !ff(datum) {
+			if ok, environment := ff(datum); !ok {
+				if environment != "" {
+					filteredEnvironments[environment]++
+				}
+				filteredContainers++
 				// if any filter function check fails, move on to the next datum
 				continue DataLoop
 			}
@@ -91,7 +103,7 @@ DataLoop:
 		result[key] = datum
 	}
 
-	return result
+	return result, filteredContainers, filteredEnvironments
 }
 
 func filterFields(fields string, data map[string]*CostData) map[string]CostData {