mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-12-13 02:01:10 +08:00
PMM-7 Update sort.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"cmp"
|
||||||
|
|
||||||
"github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/utils"
|
"github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/utils"
|
||||||
"github.com/xlab/treeprint"
|
"github.com/xlab/treeprint"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
@@ -98,17 +100,24 @@ func (n *WhoisNode) valuesSortedByTimestamps() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keep nil timestamps at the top
|
// keep nil timestamps at the top
|
||||||
slices.SortFunc(values, func(a, b string) bool {
|
slices.SortFunc(values, func(a, b string) int {
|
||||||
if n.Values[a].Timestamp == nil && n.Values[b].Timestamp == nil {
|
va, vb := n.Values[a].Timestamp, n.Values[b].Timestamp
|
||||||
return a < b
|
switch {
|
||||||
|
case va == nil && vb == nil:
|
||||||
|
return cmp.Compare(a, b)
|
||||||
|
case va == nil:
|
||||||
|
return -1 // nil < non-nil
|
||||||
|
case vb == nil:
|
||||||
|
return 1 // non-nil > nil
|
||||||
|
default:
|
||||||
|
if va.Before(*vb) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if va.After(*vb) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return cmp.Compare(a, b)
|
||||||
}
|
}
|
||||||
if n.Values[a].Timestamp == nil { // implied b!=nil
|
|
||||||
return true // meaning, nil < nonnil, a < b
|
|
||||||
}
|
|
||||||
if n.Values[b].Timestamp == nil { // implied a!=nil
|
|
||||||
return false // meaning a is greater than b
|
|
||||||
}
|
|
||||||
return n.Values[a].Timestamp.Before(*n.Values[b].Timestamp)
|
|
||||||
})
|
})
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user