Skip to content

Commit

Permalink
convert sunday from golang time to vector time
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Oct 18, 2024
1 parent 0c4d4ea commit e724d90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ func (phf *PerHourFileLayout) GetLogFileLayoutFormat() string {
}

func (phf *PerHourFileLayout) GetLogFilePath(time time.Time, enclaveUuid, serviceUuid string) string {
year, week := time.ISOWeek()
day := time.Weekday()
hour := time.Hour()
return phf.getHourlyLogFilePath(year, week, int(day), hour, enclaveUuid, serviceUuid)
year, week, day, hour := TimeToWeekDayHour(time)
return phf.getHourlyLogFilePath(year, week, day, hour, enclaveUuid, serviceUuid)
}

func (phf *PerHourFileLayout) GetLogFilePaths(
Expand Down Expand Up @@ -124,7 +122,7 @@ func (phf *PerHourFileLayout) getLogFilePathsBeyondRetentionPeriod(fs volume_fil
}

func (phf *PerHourFileLayout) getHourlyLogFilePath(year, week, day, hour int, enclaveUuid, serviceUuid string) string {
// these match the format in which Vector outputs week, hours, days
// match the format in which Vector outputs week, hours, days
formattedWeekNum := fmt.Sprintf("%02d", week)
formattedHourNum := fmt.Sprintf("%02d", hour)
return fmt.Sprintf(perHourFilePathFmtSt, phf.baseLogsFilePath, strconv.Itoa(year), formattedWeekNum, strconv.Itoa(day), formattedHourNum, enclaveUuid, serviceUuid, volume_consts.Filetype)
Expand All @@ -134,6 +132,10 @@ func TimeToWeekDayHour(time time.Time) (int, int, int, int) {
year, week := time.ISOWeek()
hour := time.Hour()
day := int(time.Weekday())
// convert sunday in golang's time(0) to sunday (0) in strftime/Vector log aggregator time(7)
if day == 0 {
day = 7
}
return year, week, day, hour
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,16 @@ func TestGetLogFilePathsWithHourlyRetentionAcrossYears(t *testing.T) {

}

func TestGetLogFilePathsWithHourlyRetentionWithinSameDay(t *testing.T) {
func TestSundayIsConvertedFromStrftimeToGolangTime(t *testing.T) {
expectedFilepath := "/var/log/kurtosis/2024/02/7/05/test-enclave/test-user-service-1.json"

mockTime := logs_clock.NewMockLogsClockPerHour(2024, 2, 0, 5)
fileLayout := NewPerHourFileLayout(mockTime, volume_consts.LogsStorageDirpath)

actualFilePath := fileLayout.GetLogFilePath(mockTime.Now(), testEnclaveUuid, testUserService1Uuid)
require.Equal(t, expectedFilepath, actualFilePath)
}

//
//func TestGetLogFilePathsWithHourlyRetentionReturnsCorrectPathsIfHoursMissingInBetween(t *testing.T) {
// filesystem := volume_filesystem.NewMockedVolumeFilesystem()
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func (pwf *PerWeekFileLayout) getLogFilePathsBeyondRetentionPeriod(fs volume_fil
return paths
}

func DurationToWeeks(d time.Duration) int {
return int(math.Round(d.Hours() / float64(oneWeekInHours)))
}

func (pwf *PerWeekFileLayout) getWeeklyFilePath(year, week int, enclaveUuid, serviceUuid string) string {
formattedWeekNum := fmt.Sprintf("%02d", week)
return fmt.Sprintf(PerWeekFilePathFmtStr, pwf.baseLogsFilePath, strconv.Itoa(year), formattedWeekNum, enclaveUuid, serviceUuid, volume_consts.Filetype)
}

func DurationToWeeks(d time.Duration) int {
return int(math.Round(d.Hours() / float64(oneWeekInHours)))
}

0 comments on commit e724d90

Please sign in to comment.