Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hk21702 committed Feb 3, 2025
1 parent 87ed3d6 commit 0378ed1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
16 changes: 7 additions & 9 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,27 @@ func getReportTime(path string) (int64, error) {
}

// GetPeriodStart returns the start of the period window for a given period in seconds.
func GetPeriodStart(period int) (int64, error) {
func GetPeriodStart(period int, t time.Time) (int64, error) {
if period <= 0 {
return 0, ErrInvalidPeriod
}
utcTime := time.Now().UTC().Unix()
return utcTime - (utcTime % int64(period)), nil
return t.Unix() - (t.Unix() % int64(period)), nil
}

// GetForPeriod returns the most recent report within a period window for a given directory.
// Not inclusive of the period end (periodStart + period).
//
// For example, given reports 1 and 7, with time 2 and period 7, the function will return the path for report 1.
func GetForPeriod(dir string, time time.Time, period int) (Report, error) {
if period <= 0 {
return Report{}, ErrInvalidPeriod
func GetForPeriod(dir string, t time.Time, period int) (Report, error) {
periodStart, err := GetPeriodStart(period, t)
if err != nil {
return Report{}, err
}

periodStart := time.Unix() - (time.Unix() % int64(period))
periodEnd := periodStart + int64(period)

// Reports names are utc timestamps. Get the most recent report within the period window.
var report Report
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
err = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("failed to access path: %v", err)
}
Expand Down
11 changes: 8 additions & 3 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ func TestGetPeriodStart(t *testing.T) {

tests := map[string]struct {
period int
time int64

wantErr error
}{
"Valid Period": {period: 500},
"Valid Period": {period: 500, time: 100000},
"Negative Time:": {period: 500, time: -100000},
"Non-Multiple Time": {period: 500, time: 1051},

"Invalid Negative Period": {period: -500, wantErr: report.ErrInvalidPeriod},
"Invalid Zero Period": {period: 0, wantErr: report.ErrInvalidPeriod},
Expand All @@ -30,14 +33,16 @@ func TestGetPeriodStart(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := report.GetPeriodStart(tc.period)
got, err := report.GetPeriodStart(tc.period, time.Unix(tc.time, 0))
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
return
}
require.NoError(t, err, "got an unexpected error")

require.IsType(t, int64(0), got)
want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
require.Equal(t, want, got, "GetPeriodStart should return the expect start of the period window")
})
}
}
Expand Down Expand Up @@ -233,7 +238,7 @@ func TestGetAll(t *testing.T) {
}
require.NoError(t, err, "got an unexpected error")

got := make([]report.Report, len(reports))
got := make([]report.Report, 0, len(reports))
for _, r := range reports {
got = append(got, sanitizeReportPath(t, r, dir))
}
Expand Down
6 changes: 0 additions & 6 deletions internal/report/testdata/TestGetAll/golden/files_in_subdir
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: 1.json
name: 1.json
timestamp: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: ""
name: ""
timestamp: 0
- path: 1.json
name: 1.json
timestamp: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-100000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100000

0 comments on commit 0378ed1

Please sign in to comment.