rows age sort bust. Fix #536

mine
derailed 2020-02-10 15:36:46 -07:00
parent 67c0702bf2
commit 1a6ff3bfa3
2 changed files with 46 additions and 4 deletions

View File

@ -4,8 +4,10 @@ import (
"fmt"
"reflect"
"sort"
"time"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/util/duration"
)
const (
@ -139,19 +141,31 @@ func (rr RowEvents) FindIndex(id string) (int, bool) {
return 0, false
}
func (rr RowEvents) isAgeCol(col int) bool {
var age bool
if len(rr) == 0 {
return age
}
return col == len(rr[0].Row.Fields)-1
}
// Sort rows based on column index and order.
func (rr RowEvents) Sort(ns string, col int, asc bool) {
t := RowEventSorter{NS: ns, Events: rr, Index: col, Asc: asc}
sort.Sort(t)
ageCol := rr.isAgeCol(col)
gg, kk := map[string][]string{}, make(StringSet, 0, len(rr))
for _, e := range rr {
g := e.Row.Fields[col]
for _, r := range rr {
g := r.Row.Fields[col]
if ageCol {
g = toAgeDuration(g)
}
kk = kk.Add(g)
if ss, ok := gg[g]; ok {
gg[g] = append(ss, e.Row.ID)
gg[g] = append(ss, r.Row.ID)
} else {
gg[g] = []string{e.Row.ID}
gg[g] = []string{r.Row.ID}
}
}
@ -164,6 +178,14 @@ func (rr RowEvents) Sort(ns string, col int, asc bool) {
sort.Sort(s)
}
func toAgeDuration(dur string) string {
d, err := time.ParseDuration(dur)
if err != nil {
return "n/a"
}
return duration.HumanDuration(d)
}
// ----------------------------------------------------------------------------
// RowEventSorter sorts row events by a given colon.

View File

@ -81,6 +81,26 @@ func TestSort(t *testing.T) {
{Row: render.Row{ID: "C", Fields: render.Fields{"10", "2", "3"}}},
},
},
"id_preserve": {
re: render.RowEvents{
{Row: render.Row{ID: "ns1/B", Fields: render.Fields{"B", "2", "3"}}},
{Row: render.Row{ID: "ns1/A", Fields: render.Fields{"A", "2", "3"}}},
{Row: render.Row{ID: "ns1/C", Fields: render.Fields{"C", "2", "3"}}},
{Row: render.Row{ID: "ns2/B", Fields: render.Fields{"B", "2", "3"}}},
{Row: render.Row{ID: "ns2/A", Fields: render.Fields{"A", "2", "3"}}},
{Row: render.Row{ID: "ns2/C", Fields: render.Fields{"C", "2", "3"}}},
},
col: 1,
asc: true,
e: render.RowEvents{
{Row: render.Row{ID: "ns1/A", Fields: render.Fields{"A", "2", "3"}}},
{Row: render.Row{ID: "ns1/B", Fields: render.Fields{"B", "2", "3"}}},
{Row: render.Row{ID: "ns1/C", Fields: render.Fields{"C", "2", "3"}}},
{Row: render.Row{ID: "ns2/A", Fields: render.Fields{"A", "2", "3"}}},
{Row: render.Row{ID: "ns2/B", Fields: render.Fields{"B", "2", "3"}}},
{Row: render.Row{ID: "ns2/C", Fields: render.Fields{"C", "2", "3"}}},
},
},
}
for k := range uu {