|
|
@@ -13,6 +13,7 @@ import (
|
|
|
"github.com/opencost/opencost/core/pkg/clustercache"
|
|
|
"github.com/opencost/opencost/core/pkg/clusters"
|
|
|
coreenv "github.com/opencost/opencost/core/pkg/env"
|
|
|
+ "github.com/opencost/opencost/core/pkg/filter/allocation"
|
|
|
"github.com/opencost/opencost/core/pkg/log"
|
|
|
"github.com/opencost/opencost/core/pkg/opencost"
|
|
|
"github.com/opencost/opencost/core/pkg/source"
|
|
|
@@ -1554,7 +1555,7 @@ func measureTime(start time.Time, threshold time.Duration, name string) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (cm *CostModel) QueryAllocation(window opencost.Window, step time.Duration, aggregate []string, includeIdle, idleByNode, includeProportionalAssetResourceCosts, includeAggregatedMetadata, sharedLoadBalancer bool, accumulateBy opencost.AccumulateOption, shareIdle bool) (*opencost.AllocationSetRange, error) {
|
|
|
+func (cm *CostModel) QueryAllocation(window opencost.Window, step time.Duration, aggregate []string, includeIdle, idleByNode, includeProportionalAssetResourceCosts, includeAggregatedMetadata, sharedLoadBalancer bool, accumulateBy opencost.AccumulateOption, shareIdle bool, filterString string) (*opencost.AllocationSetRange, error) {
|
|
|
// Validate window is legal
|
|
|
if window.IsOpen() || window.IsNegative() {
|
|
|
return nil, fmt.Errorf("illegal window: %s", window)
|
|
|
@@ -1624,6 +1625,33 @@ func (cm *CostModel) QueryAllocation(window opencost.Window, step time.Duration,
|
|
|
stepEnd = stepStart.Add(step)
|
|
|
}
|
|
|
|
|
|
+ // Apply allocation filter BEFORE aggregation if provided
|
|
|
+ if filterString != "" {
|
|
|
+ parser := allocation.NewAllocationFilterParser()
|
|
|
+ filterNode, err := parser.Parse(filterString)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("invalid filter: %w", err)
|
|
|
+ }
|
|
|
+ compiler := opencost.NewAllocationMatchCompiler(nil)
|
|
|
+ matcher, err := compiler.Compile(filterNode)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("failed to compile filter: %w", err)
|
|
|
+ }
|
|
|
+ filteredASR := opencost.NewAllocationSetRange()
|
|
|
+ for _, as := range asr.Slice() {
|
|
|
+ filteredAS := opencost.NewAllocationSet(as.Start(), as.End())
|
|
|
+ for _, alloc := range as.Allocations {
|
|
|
+ if matcher.Matches(alloc) {
|
|
|
+ filteredAS.Set(alloc)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if filteredAS.Length() > 0 {
|
|
|
+ filteredASR.Append(filteredAS)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ asr = filteredASR
|
|
|
+ }
|
|
|
+
|
|
|
// Set aggregation options and aggregate
|
|
|
var shareIdleOpt string
|
|
|
if shareIdle {
|