source.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. )
  8. type AllocationSource interface {
  9. ComputeAllocation(start, end time.Time, resolution time.Duration) (*opencost.AllocationSet, error)
  10. }
  11. type AllocationComputeSource struct {
  12. src AllocationSource
  13. }
  14. // NewAllocationComputeSource creates an `exporter.ComputeSource[opencost.AllocationSet]` implementation
  15. func NewAllocationComputeSource(src AllocationSource) exporter.ComputeSource[opencost.AllocationSet] {
  16. return &AllocationComputeSource{
  17. src: src,
  18. }
  19. }
  20. // CanCompute should return true iff the ComputeSource can effectively act as
  21. // a source of T data for the given time range. For example, a ComputeSource
  22. // with two-day coverage cannot fulfill a range from three days ago, and should
  23. // not be left to return an error in Compute. Instead, it should report that is
  24. // cannot compute and allow another Source to handle the computation.
  25. func (acs *AllocationComputeSource) CanCompute(start, end time.Time) bool {
  26. return true
  27. }
  28. // Compute should compute a single T for the given time range, optionally using the given resolution.
  29. func (acs *AllocationComputeSource) Compute(start, end time.Time, resolution time.Duration) (*opencost.AllocationSet, error) {
  30. return acs.src.ComputeAllocation(start, end, resolution)
  31. }
  32. // Name returns the name of the ComputeSource
  33. func (acs *AllocationComputeSource) Name() string {
  34. return pipelines.AllocationPipelineName
  35. }