-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2629 from nik-kc/nik/plugin_aggs_filters
- Loading branch information
Showing
6 changed files
with
174 additions
and
32 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package customcost | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/opencost/opencost/core/pkg/filter/ast" | ||
"github.com/opencost/opencost/core/pkg/filter/matcher" | ||
"github.com/opencost/opencost/core/pkg/filter/transform" | ||
) | ||
|
||
func NewCustomCostMatchCompiler() *matcher.MatchCompiler[*CustomCost] { | ||
passes := []transform.CompilerPass{ | ||
transform.UnallocatedReplacementPass(), | ||
} | ||
|
||
return matcher.NewMatchCompiler( | ||
customCostFieldMap, | ||
customCostSliceFieldMap, | ||
customCostMapFieldMap, | ||
passes..., | ||
) | ||
} | ||
|
||
// Maps fields from a custom cost to a string value based on an identifier | ||
func customCostFieldMap(cc *CustomCost, identifier ast.Identifier) (string, error) { | ||
if cc == nil { | ||
return "", fmt.Errorf("cannot map to nil custom cost") | ||
} | ||
if identifier.Field == nil { | ||
return "", fmt.Errorf("cannot map field from identifier with nil field") | ||
} | ||
switch CustomCostProperty(identifier.Field.Name) { | ||
case CustomCostZoneProp: | ||
return cc.Zone, nil | ||
case CustomCostAccountNameProp: | ||
return cc.AccountName, nil | ||
case CustomCostChargeCategoryProp: | ||
return cc.ChargeCategory, nil | ||
case CustomCostDescriptionProp: | ||
return cc.Description, nil | ||
case CustomCostResourceNameProp: | ||
return cc.ResourceName, nil | ||
case CustomCostResourceTypeProp: | ||
return cc.ResourceType, nil | ||
case CustomCostProviderIdProp: | ||
return cc.ProviderId, nil | ||
case CustomCostUsageUnitProp: | ||
return cc.UsageUnit, nil | ||
case CustomCostDomainProp: | ||
return cc.Domain, nil | ||
} | ||
|
||
return "", fmt.Errorf("failed to find string identifier on CustomCost: %s", identifier.Field.Name) | ||
} | ||
|
||
// Maps slice fields from an asset to a []string value based on an identifier | ||
func customCostSliceFieldMap(cc *CustomCost, identifier ast.Identifier) ([]string, error) { | ||
return nil, fmt.Errorf("custom costs have no slice fields") | ||
} | ||
|
||
// Maps map fields from a custom cost to a map[string]string value based on an identifier | ||
func customCostMapFieldMap(cc *CustomCost, identifier ast.Identifier) (map[string]string, error) { | ||
return nil, fmt.Errorf("custom costs have no map fields") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package customcost | ||
|
||
import "github.com/opencost/opencost/core/pkg/filter/ast" | ||
|
||
// a slice of all the custom costs field instances the lexer should recognize as | ||
// valid left-hand comparators | ||
var customCostFilterFields = []*ast.Field{ | ||
ast.NewField(CustomCostZoneProp), | ||
ast.NewField(CustomCostAccountNameProp), | ||
ast.NewField(CustomCostChargeCategoryProp), | ||
ast.NewField(CustomCostDescriptionProp), | ||
ast.NewField(CustomCostResourceNameProp), | ||
ast.NewField(CustomCostResourceTypeProp), | ||
ast.NewField(CustomCostProviderIdProp), | ||
ast.NewField(CustomCostUsageUnitProp), | ||
ast.NewField(CustomCostDomainProp), | ||
} | ||
|
||
// NewCustomCostFilterParser creates a new `ast.FilterParser` implementation | ||
// which uses CustomCost specific fields | ||
func NewCustomCostFilterParser() ast.FilterParser { | ||
return ast.NewFilterParser(customCostFilterFields) | ||
} |
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