From e724d908456e4373322afd0dc10a9f7c5a4281a2 Mon Sep 17 00:00:00 2001 From: Tedi Mitiku Date: Fri, 18 Oct 2024 16:55:40 +0900 Subject: [PATCH] convert sunday from golang time to vector time --- .../file_layout/per_hour_file_layout.go | 12 +++++++----- .../file_layout/per_hour_file_layout_test.go | 9 +++++++-- .../file_layout/per_week_file_layout.go | 8 ++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout.go b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout.go index fafc063967..ec7d352cf9 100644 --- a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout.go +++ b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout.go @@ -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( @@ -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) @@ -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 } diff --git a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout_test.go b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout_test.go index f3e8c0169d..b96775633b 100644 --- a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout_test.go +++ b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_hour_file_layout_test.go @@ -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() // diff --git a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_week_file_layout.go b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_week_file_layout.go index 5e5126db5e..d54e8f1fd2 100644 --- a/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_week_file_layout.go +++ b/engine/server/engine/centralized_logs/client_implementations/persistent_volume/file_layout/per_week_file_layout.go @@ -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))) +}