source.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package allocation
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/exporter"
  5. "github.com/opencost/opencost/core/pkg/opencost"
  6. "github.com/opencost/opencost/core/pkg/pipelines"
  7. "github.com/opencost/opencost/pkg/costmodel"
  8. )
  9. type AllocationComputeSource struct {
  10. cm *costmodel.CostModel
  11. }
  12. // NewAllocationComputeSource creates an `exporter.ComputeSource[opencost.AssetSet]` implementation
  13. func NewAllocationComputeSource(cm *costmodel.CostModel) exporter.ComputeSource[opencost.AllocationSet] {
  14. return &AllocationComputeSource{
  15. cm: cm,
  16. }
  17. }
  18. // CanCompute should return true iff the ComputeSource can effectively act as
  19. // a source of T data for the given time range. For example, a ComputeSource
  20. // with two-day coverage cannot fulfill a range from three days ago, and should
  21. // not be left to return an error in Compute. Instead, it should report that is
  22. // cannot compute and allow another Source to handle the computation.
  23. func (acs *AllocationComputeSource) CanCompute(start, end time.Time) bool {
  24. return true
  25. }
  26. // Compute should compute a single T for the given time range, optionally using the given resolution.
  27. func (acs *AllocationComputeSource) Compute(start, end time.Time, resolution time.Duration) (*opencost.AllocationSet, error) {
  28. return acs.cm.ComputeAllocation(start, end, resolution)
  29. }
  30. // Name returns the name of the ComputeSource
  31. func (acs *AllocationComputeSource) Name() string {
  32. return pipelines.AllocationPipelineName
  33. }