Skip to content

Commit

Permalink
Merge pull request #656 from NAICNO/larstha-600-sort-profile-data
Browse files Browse the repository at this point in the history
For #600 - sort profile data better
  • Loading branch information
lars-t-hansen authored Oct 29, 2024
2 parents 26df263 + 94c38b1 commit 6d9713b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
19 changes: 16 additions & 3 deletions code/sonalyze/profile/perform.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package profile

import (
"cmp"
"errors"
"io"
"math"
"slices"
"sort"

"go-utils/config"
"go-utils/hostglob"
"go-utils/maps"

. "sonalyze/common"
"sonalyze/db"
"sonalyze/sonarlog"
Expand Down Expand Up @@ -63,10 +66,20 @@ func (pc *ProfileCommand) Perform(
// `processes` has the event streams for the processes (or group of rolled-up processes).
//
// We want these sorted in the order in which they start being shown, so that there is a natural
// feel to the list of processes for each timestamp. Sorting ascending by first timestamp will
// accomplish that.
// feel to the list of processes for each timestamp. Sorting ascending by first timestamp, then
// by command name and finally by PID will accomplish that as well as it is possible. (There
// are still going to be cases where two runs might print different data: see processId().)
processes := maps.Values(streams)
sort.Stable(sonarlog.TimeSortableSampleStreams(processes))
slices.SortStableFunc(processes, func(a, b *sonarlog.SampleStream) int {
c := cmp.Compare((*a)[0].S.Timestamp, (*b)[0].S.Timestamp)
if c == 0 {
c = cmp.Compare((*a)[0].S.Cmd.String(), (*b)[0].S.Cmd.String())
if c == 0 {
c = cmp.Compare((*a)[0].S.Pid, (*b)[0].S.Pid)
}
}
return c
})

userName := (*processes[0])[0].S.User.String()

Expand Down
16 changes: 0 additions & 16 deletions code/sonalyze/sonarlog/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,3 @@ func (sss HostSortableSampleStreams) Swap(i, j int) {
func (sss HostSortableSampleStreams) Less(i, j int) bool {
return (*sss[i])[0].S.Host.String() < (*sss[j])[0].S.Host.String()
}

// Sort SampleStreams by time

type TimeSortableSampleStreams SampleStreams

func (sss TimeSortableSampleStreams) Len() int {
return len(sss)
}

func (sss TimeSortableSampleStreams) Swap(i, j int) {
sss[i], sss[j] = sss[j], sss[i]
}

func (sss TimeSortableSampleStreams) Less(i, j int) bool {
return (*sss[i])[0].S.Timestamp < (*sss[j])[0].S.Timestamp
}

0 comments on commit 6d9713b

Please sign in to comment.