Просмотр исходного кода

Add allocation compute source and pipeline names

Matt Bolt 1 год назад
Родитель
Сommit
75953b47b8
2 измененных файлов с 78 добавлено и 0 удалено
  1. 46 0
      core/pkg/pipelines/name.go
  2. 32 0
      pkg/exporter/allocationsource.go

+ 46 - 0
core/pkg/pipelines/name.go

@@ -0,0 +1,46 @@
+package pipelines
+
+import (
+	"github.com/opencost/opencost/core/pkg/opencost"
+	"github.com/opencost/opencost/core/pkg/util/typeutil"
+)
+
+const (
+	AllocationPipelineName     string = "allocations"
+	AssetsPipelineName         string = "assets"
+	CloudCostsPipelineName     string = "cloudcosts"
+	NetworkInsightPipelineName string = "networkinsights"
+	CustomCostsPipelineName    string = "customcosts"
+)
+
+var nameByType map[string]string
+
+// initializes the package, creates type -> pipeline mapping
+func init() {
+	allocSetKey := typeutil.TypeOf[opencost.AllocationSet]()
+	allocKey := typeutil.TypeOf[opencost.Allocation]()
+
+	assetSetKey := typeutil.TypeOf[opencost.AssetSet]()
+	assetKey := typeutil.TypeOf[opencost.Asset]()
+
+	cloudCostsSetKey := typeutil.TypeOf[opencost.CloudCostSet]()
+	cloudCostKey := typeutil.TypeOf[opencost.CloudCost]()
+
+	networkInsightSetKey := typeutil.TypeOf[opencost.NetworkInsightSet]()
+	networkInsightKey := typeutil.TypeOf[opencost.NetworkInsight]()
+
+	nameByType = map[string]string{
+		allocSetKey:          AllocationPipelineName,
+		allocKey:             AllocationPipelineName,
+		assetSetKey:          AssetsPipelineName,
+		assetKey:             AssetsPipelineName,
+		cloudCostsSetKey:     CloudCostsPipelineName,
+		cloudCostKey:         CloudCostsPipelineName,
+		networkInsightSetKey: NetworkInsightPipelineName,
+		networkInsightKey:    NetworkInsightPipelineName,
+	}
+}
+
+func NameFor[T any]() string {
+	return nameByType[typeutil.TypeOf[T]()]
+}

+ 32 - 0
pkg/exporter/allocationsource.go

@@ -0,0 +1,32 @@
+package source
+
+import (
+	"time"
+
+	"github.com/opencost/opencost/core/pkg/opencost"
+	"github.com/opencost/opencost/core/pkg/pipelines"
+	"github.com/opencost/opencost/pkg/costmodel"
+)
+
+type AllocationComputeSource struct {
+	cm *costmodel.CostModel
+}
+
+// CanCompute should return true iff the ComputeSource can effectively act as
+// a source of T data for the given time range. For example, a ComputeSource
+// with two-day coverage cannot fulfill a range from three days ago, and should
+// not be left to return an error in Compute. Instead, it should report that is
+// cannot compute and allow another Source to handle the computation.
+func (acs *AllocationComputeSource) CanCompute(start, end time.Time) bool {
+	return true
+}
+
+// Compute should compute a single T for the given time range, optionally using the given resolution.
+func (acs *AllocationComputeSource) Compute(start, end time.Time, resolution time.Duration) (*opencost.AllocationSet, error) {
+	return acs.cm.ComputeAllocation(start, end, resolution)
+}
+
+// Name returns the name of the ComputeSource
+func (acs *AllocationComputeSource) Name() string {
+	return pipelines.AllocationPipelineName
+}