bingenpath.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pathing
  2. import (
  3. "fmt"
  4. "path"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/exporter/pathing/pathutils"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/pipelines"
  9. "github.com/opencost/opencost/core/pkg/util/timeutil"
  10. )
  11. const (
  12. DefaultRootDir string = "federated"
  13. BaseStorageDir string = "etl/bingen"
  14. )
  15. // BingenStoragePathFormatter is an implementation of the StoragePathFormatter interface for
  16. // a cluster separated storage path of the format:
  17. //
  18. // <root>/<cluster>/etl/bingen/<pipeline>/<resolution>/<epoch-start>-<epoch-end>
  19. type BingenStoragePathFormatter struct {
  20. rootDir string
  21. clusterId string
  22. pipeline string
  23. resolution string
  24. }
  25. func NewDefaultStoragePathFormatter(appName, clusterID, clusterName, pipeline string, resolution *time.Duration) (StoragePathFormatter[opencost.Window], error) {
  26. res := "."
  27. if resolution != nil {
  28. res = timeutil.FormatStoreResolution(*resolution)
  29. }
  30. // KubeModel uses a distinct pathing pattern which breaks with the original
  31. // Allocations and Assets bingen pathing.
  32. if pipeline == pipelines.KubeModelPipelineName {
  33. return NewKubeModelStoragePathFormatter(appName, clusterID, res)
  34. }
  35. return NewBingenStoragePathFormatter(DefaultRootDir, clusterName, pipeline, res)
  36. }
  37. // NewBingenStoragePathFormatter creates a StoragePathFormatter for a cluster separated storage path
  38. // with the given root directory, cluster id, pipeline, and resolution. To omit the resolution directory
  39. // structure, provide a `nil` resolution.
  40. func NewBingenStoragePathFormatter(rootDir, clusterId, pipeline, resolution string) (StoragePathFormatter[opencost.Window], error) {
  41. if clusterId == "" {
  42. return nil, fmt.Errorf("cluster id cannot be empty")
  43. }
  44. if pipeline == "" {
  45. return nil, fmt.Errorf("pipeline cannot be empty")
  46. }
  47. return &BingenStoragePathFormatter{
  48. rootDir: rootDir,
  49. clusterId: clusterId,
  50. pipeline: pipeline,
  51. resolution: resolution,
  52. }, nil
  53. }
  54. // Dir returns the director that files will be placed in
  55. func (bsf *BingenStoragePathFormatter) Dir() string {
  56. return path.Join(
  57. bsf.rootDir,
  58. bsf.clusterId,
  59. BaseStorageDir,
  60. bsf.pipeline,
  61. bsf.resolution,
  62. )
  63. }
  64. // ToFullPath returns the full path to a file name within the storage directory using the format:
  65. //
  66. // <root>/<cluster>/etl/bingen/<pipeline>/<resolution>/<prefix>.<start-epoch>-<end-epoch>
  67. func (bsf *BingenStoragePathFormatter) ToFullPath(prefix string, window opencost.Window, fileExt string) string {
  68. fileName := toBingenFileName(prefix, window, fileExt)
  69. return path.Join(
  70. bsf.rootDir,
  71. bsf.clusterId,
  72. BaseStorageDir,
  73. bsf.pipeline,
  74. bsf.resolution,
  75. fileName,
  76. )
  77. }
  78. // toBingenFileName formats the file name as <prefix>.<start-epoch>-<end-epoch> if a prefix is non-empty.
  79. // If prefix is an empty string, then just the format <start-epoch>-<end-epoch> is returned.
  80. func toBingenFileName(prefix string, window opencost.Window, fileExt string) string {
  81. start, end := derefTimeOrZero(window.Start()), derefTimeOrZero(window.End())
  82. suffix := pathutils.FormatEpochRange(start, end)
  83. if fileExt != "" {
  84. suffix = fmt.Sprintf("%s.%s", suffix, fileExt)
  85. }
  86. if prefix == "" {
  87. return suffix
  88. }
  89. return fmt.Sprintf("%s.%s", prefix, suffix)
  90. }
  91. // derefTimeOrZero dereferences a time.Time pointer and returns the zero value if the pointer is nil.
  92. // This prevents nil pointer dereference errors when using windows. This is mostly an assertion, as
  93. // generally windows for pathing will be pre-validated.
  94. func derefTimeOrZero(t *time.Time) time.Time {
  95. if t == nil {
  96. return time.Time{}
  97. }
  98. return *t
  99. }