| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package pathing
- import (
- "fmt"
- "path"
- "time"
- )
- // 2006-01-02T15:04:05Z07:00
- // EventStorageTimeFormat is YYYYMMDDHHmmss
- const EventStorageTimeFormat = "20060102150405"
- // EventStoragePathFormatter is an implementation of the StoragePathFormatter interface for
- // a cluster separated storage path of the format:
- //
- // <root>/federated/<cluster>/<event>/<sub-paths...>/YYYYMMDDHHmmss
- type EventStoragePathFormatter struct {
- rootDir string
- clusterId string
- event string
- subPaths []string
- }
- // NewBingenStoragePathFormatter creates a StoragePathFormatter for a cluster separated storage path
- // with the given root directory, cluster id, pipeline, and resolution. To omit the resolution directory
- // structure, provide a `nil` resolution.
- func NewEventStoragePathFormatter(rootDir, clusterId, event string, subPaths ...string) (StoragePathFormatter[time.Time], error) {
- if clusterId == "" {
- return nil, fmt.Errorf("cluster id cannot be empty")
- }
- if event == "" {
- return nil, fmt.Errorf("event cannot be empty")
- }
- for _, subPath := range subPaths {
- if subPath == "" {
- return nil, fmt.Errorf("subpaths cannot be empty")
- }
- }
- return &EventStoragePathFormatter{
- rootDir: rootDir,
- clusterId: clusterId,
- event: event,
- subPaths: subPaths,
- }, nil
- }
- // RootDir returns the root directory of the storage path formatter.
- func (espf *EventStoragePathFormatter) RootDir() string {
- return espf.rootDir
- }
- // ToFullPath returns the full path to a file name within the storage directory using the format:
- //
- // <root>/federated/<cluster>/<event>/YYYYMMDDHHmm.json
- func (espf *EventStoragePathFormatter) ToFullPath(prefix string, timestamp time.Time, fileExt string) string {
- fileName := toEventFileName(prefix, timestamp, fileExt)
- return path.Join(
- espf.rootDir,
- espf.clusterId,
- espf.event,
- path.Join(espf.subPaths...),
- fileName,
- )
- }
- // toEventFileName formats the file name as <prefix>.<timestamp>. if a non-empty fileExt is provided,
- // then the file extension is appended to the file name.
- func toEventFileName(prefix string, timestamp time.Time, fileExt string) string {
- suffix := timestamp.Format(EventStorageTimeFormat)
- if fileExt != "" {
- suffix = fmt.Sprintf("%s.%s", suffix, fileExt)
- }
- if prefix == "" {
- return suffix
- }
- return fmt.Sprintf("%s.%s", prefix, suffix)
- }
|