Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Allow sort by multiple keys
Browse files Browse the repository at this point in the history
Signed-off-by: GuessWhoSamFoo <[email protected]>
  • Loading branch information
GuessWhoSamFoo committed Oct 30, 2020
1 parent 8fe97ca commit 4545b2f
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 48 deletions.
2 changes: 1 addition & 1 deletion internal/modules/configuration/plugin_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (d *PluginListDescriber) Describe(ctx context.Context, namespace string, op
tbl.Add(row)
}

tbl.Sort("Name", false, false)
tbl.Sort(false, false, "Name")

return component.ContentResponse{
Components: []component.Component{list},
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func describeConfigMapData(cm *corev1.ConfigMap) (*component.Table, error) {
table.Add(row)
}

table.Sort("Key", false, false)
table.Sort(false, false, "Key")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/customresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func CreateCustomResourceList(crdObject *unstructured.Unstructured, resources *u
table.Add(row)
}

table.Sort("Name", false, false)
table.Sort(false, false, "Name")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func createDeploymentConditionsView(deployment *appsv1.Deployment) (*component.T
table.Add(row)
}

table.Sort("Type", false, false)
table.Sort(false, false, "Type")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func EventListHandler(ctx context.Context, list *corev1.EventList, opts Options)
table.Add(row)
}

table.Sort("Last Seen", true, false)
table.Sort(true, false, "Last Seen")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func createHorizontalPodAutoscalerConditionsView(horizontalPodAutoscaler *autosc
table.Add(row)
}

table.Sort("Type", false, false)
table.Sort(false, false, "Type")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func printNamespaceResourceQuotas(quotas []corev1.ResourceQuota) map[string]comp
row["Limit"] = component.NewText(q["hard"][resource])
table.Add(row)
}
table.Sort("Resource", false, false)
table.Sort(false, false, "Resource")
items[quotas[i].Name] = component.FlexLayoutItem{Width: component.WidthHalf, View: table}
}
return items
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func createNodeConditionsView(node *corev1.Node) (*component.Table, error) {
table.Add(row)
}

table.Sort("Type", false, false)
table.Sort(false, false, "Type")

return table, nil
}
Expand All @@ -345,7 +345,7 @@ func createNodeImagesView(node *corev1.Node) (*component.Table, error) {
table.Add(row)
}

table.Sort("Names", false, false)
table.Sort(false, false, "Names")

return table, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/object_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (ol *ObjectTable) ToComponent() (component.Component, error) {
}

if so := ol.sortOrder; so != nil {
table.Sort(so.name, so.reverse, so.hasStatus)
table.Sort(so.reverse, so.hasStatus, so.name)
}

return table, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func describeSecretData(secret corev1.Secret) (*component.Table, error) {
table.Add(row)
}

table.Sort("Key", false, false)
table.Sort(false, false, "Key")

return table, nil
}
Expand Down
78 changes: 42 additions & 36 deletions pkg/view/component/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,53 +162,59 @@ func (t *Table) IsEmpty() bool {
return len(t.Config.Rows) == 0
}

// SetPlaceholder adds placeholder text to an empty table.
func (t *Table) SetPlaceholder(placeholder string) {
t.Config.EmptyContent = placeholder
}

func (t *Table) Sort(name string, reverse bool, status bool) {
// Sort sorts a table by one or more keys with booleans for reverse and object status.
func (t *Table) Sort(reverse bool, status bool, keys ...string) {
t.mu.Lock()
defer t.mu.Unlock()

statusFunc := func(i, j TableRow) bool {
a, ok := i[name].(*Link)
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", name, i, j), t.Config.Rows)
return false
}

b, ok := j[name].(*Link)
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", name, i, j), t.Config.Rows)
return false
}
return a.Config.Status > b.Config.Status
}

nameFunc := func(i, j TableRow) bool {
a, ok := i[name]
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", name, i, j), t.Config.Rows)
return false
}

b, ok := j[name]
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", name, i, j), t.Config.Rows)
return false
var sortFuncs []lessFunc

for _, key := range keys {
if status {
statusFunc := func(i, j TableRow) bool {
a, ok := i[key].(*Link)
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", key, i, j), t.Config.Rows)
return false
}

b, ok := j[key].(*Link)
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", key, i, j), t.Config.Rows)
return false
}
return a.Config.Status > b.Config.Status
}
sortFuncs = append(sortFuncs, statusFunc)
}

if reverse {
return !a.LessThan(b)
nameFunc := func(i, j TableRow) bool {
a, ok := i[key]
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", key, i, j), t.Config.Rows)
return false
}

b, ok := j[key]
if !ok {
spew.Dump(fmt.Sprintf("%s:%v/%v", key, i, j), t.Config.Rows)
return false
}

if reverse {
return !a.LessThan(b)
}
return a.LessThan(b)
}
return a.LessThan(b)
sortFuncs = append(sortFuncs, nameFunc)
}

if status {
OrderedBy(statusFunc, nameFunc).Sort(t.Rows())
} else {
OrderedBy(nameFunc).Sort(t.Rows())
}
OrderedBy(sortFuncs).Sort(t.Rows())
}

type lessFunc func(p1, p2 TableRow) bool
Expand All @@ -223,7 +229,7 @@ func (ms *multiSorter) Sort(tableRow []TableRow) {
sort.Sort(ms)
}

func OrderedBy(less ...lessFunc) *multiSorter {
func OrderedBy(less []lessFunc) *multiSorter {
return &multiSorter{
less: less,
}
Expand Down
43 changes: 42 additions & 1 deletion pkg/view/component/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func Test_Table_Sort(t *testing.T) {
rows []TableRow
reverse bool
status bool
multiple bool
expected []TableRow
}{
{
Expand Down Expand Up @@ -222,12 +223,52 @@ func Test_Table_Sort(t *testing.T) {
{"a": NewLink("", "a", "/a", genObjectStatus(TextStatusOK, []string{""}))},
},
},
{
name: "multiple keys",
reverse: false,
status: false,
multiple: true,
rows: []TableRow{
{"a": NewText("1"), "b": NewText("2")},
{"a": NewText("1"), "b": NewText("1")},
{"a": NewText("1"), "b": NewText("3")},
},
expected: []TableRow{
{"a": NewText("1"), "b": NewText("1")},
{"a": NewText("1"), "b": NewText("2")},
{"a": NewText("1"), "b": NewText("3")},
},
},
{
name: "multiple keys, desc, and status",
reverse: true,
status: true,
multiple: true,
rows: []TableRow{
{"a": NewText("1"), "b": NewLink("", "2", "/2", genObjectStatus(TextStatusOK, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusOK, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusWarning, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "3", "/3", genObjectStatus(TextStatusOK, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusError, []string{""}))},
},
expected: []TableRow{
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusError, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusWarning, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "3", "/3", genObjectStatus(TextStatusOK, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "2", "/2", genObjectStatus(TextStatusOK, []string{""}))},
{"a": NewText("1"), "b": NewLink("", "1", "/1", genObjectStatus(TextStatusOK, []string{""}))},
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
table := NewTableWithRows("table", "placeholder", NewTableCols("a"), tc.rows)
table.Sort("a", tc.reverse, tc.status)
if tc.multiple {
table.Sort(tc.reverse, tc.status, "a", "b")
} else {
table.Sort(tc.reverse, tc.status, "a")
}
expected := NewTableWithRows("table", "placeholder", NewTableCols("a"), tc.expected)

assert.Equal(t, expected, table)
Expand Down

0 comments on commit 4545b2f

Please sign in to comment.