Skip to content

Commit

Permalink
[pkg/stanza] Take into account the ascending option when sorting by…
Browse files Browse the repository at this point in the history
… mtime (#32820)

**Description:** <Describe what has changed.>
* Allow ascending sort when sorting by mtime

**Link to tracking Issue:** Closes #32792

**Testing:**
* Unit tests
  • Loading branch information
BinaryFissionGames authored May 2, 2024
1 parent fa35c2b commit bea4bb1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .chloggen/feat_mtime-ascending-sort.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: "pkg/stanza"

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow sorting by ascending order when using the mtime sort_type.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32792]
28 changes: 21 additions & 7 deletions pkg/stanza/fileconsumer/matcher/internal/filter/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ func (t TopNOption) apply(items []*item) ([]*item, error) {
return items[:t], nil
}

type mtimeSortOption struct{}
type mtimeSortOption struct {
ascending bool
}

type mtimeItem struct {
mtime time.Time
Expand All @@ -168,10 +170,20 @@ func (m mtimeSortOption) apply(items []*item) ([]*item, error) {
})
}

sort.SliceStable(mtimeItems, func(i, j int) bool {
// This checks if item i > j, in order to reverse the sort (most recently modified file is first in the list)
return mtimeItems[i].mtime.After(mtimeItems[j].mtime)
})
var lessFunc func(i, j int) bool
if m.ascending {
lessFunc = func(i, j int) bool {
// This checks if item i < j
return mtimeItems[i].mtime.Before(mtimeItems[j].mtime)
}
} else {
lessFunc = func(i, j int) bool {
// This checks if item i > j, in order to reverse the sort (most recently modified file is first in the list)
return mtimeItems[i].mtime.After(mtimeItems[j].mtime)
}
}

sort.SliceStable(mtimeItems, lessFunc)

filteredValues := make([]*item, 0, len(items))
for _, mtimeItem := range mtimeItems {
Expand All @@ -181,6 +193,8 @@ func (m mtimeSortOption) apply(items []*item) ([]*item, error) {
return filteredValues, errs
}

func SortMtime() Option {
return mtimeSortOption{}
func SortMtime(ascending bool) Option {
return mtimeSortOption{
ascending: ascending,
}
}
17 changes: 16 additions & 1 deletion pkg/stanza/fileconsumer/matcher/internal/filter/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func TestMTimeFilter(t *testing.T) {
name string
files []string
fileMTimes []time.Time
ascending bool
expectedErr string
expect []string
}{
Expand All @@ -200,6 +201,20 @@ func TestMTimeFilter(t *testing.T) {
fileMTimes: []time.Time{epoch, epoch.Add(time.Hour)},
expect: []string{"b.log", "a.log"},
},
{
name: "Multiple files in order, ascending sort",
files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{epoch, epoch.Add(time.Hour)},
ascending: true,
expect: []string{"a.log", "b.log"},
},
{
name: "Multiple files out of order, ascending sort",
files: []string{"a.log", "b.log"},
fileMTimes: []time.Time{epoch.Add(time.Hour), epoch},
ascending: true,
expect: []string{"b.log", "a.log"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -221,7 +236,7 @@ func TestMTimeFilter(t *testing.T) {
items = append(items, it)
}

f := SortMtime()
f := SortMtime(tc.ascending)
result, err := f.apply(items)
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/fileconsumer/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func New(c Criteria) (*Matcher, error) {
if !mtimeSortTypeFeatureGate.IsEnabled() {
return nil, fmt.Errorf("the %q feature gate must be enabled to use %q sort type", mtimeSortTypeFeatureGate.ID(), sortTypeMtime)
}
m.filterOpts = append(m.filterOpts, filter.SortMtime())
m.filterOpts = append(m.filterOpts, filter.SortMtime(sc.Ascending))
default:
return nil, fmt.Errorf("'sort_type' must be specified")
}
Expand Down

0 comments on commit bea4bb1

Please sign in to comment.