bingenpath.go 3.4 KB

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