Skip to content

Commit b9aa3de

Browse files
author
Matteo Biggio
committed
fix(console): fix column sorting in resources tab (#488)
In the Resources tab, column names were being associated to the wrong sorting keys, and some columns names were just missing from the `SortBy` implementation. Sorting for attributes, that was previously missing, has been implemented by sorting lexicographically on the first attribute of each resource row, even if that is probably sub-optimal
1 parent 8269b5f commit b9aa3de

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

tokio-console/src/state/resources.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::view;
88
use console_api as proto;
99
use ratatui::{style::Color, text::Span};
1010
use std::{
11+
borrow::Cow,
1112
collections::HashMap,
1213
convert::{TryFrom, TryInto},
1314
rc::Rc,
@@ -29,11 +30,15 @@ pub(crate) enum TypeVisibility {
2930
#[derive(Debug, Copy, Clone)]
3031
#[repr(usize)]
3132
pub(crate) enum SortBy {
32-
Rid = 0,
33-
Kind = 1,
34-
ConcreteType = 2,
35-
Target = 3,
36-
Total = 4,
33+
Id = 0,
34+
ParentId = 1,
35+
Kind = 2,
36+
Total = 3,
37+
Target = 4,
38+
ConcreteType = 5,
39+
Visibility = 6,
40+
Location = 7,
41+
Attributes = 8,
3742
}
3843

3944
#[derive(Debug)]
@@ -71,27 +76,50 @@ struct ResourceStats {
7176

7277
impl Default for SortBy {
7378
fn default() -> Self {
74-
Self::Rid
79+
Self::Id
7580
}
7681
}
7782

7883
impl SortBy {
7984
pub fn sort(&self, now: SystemTime, resources: &mut [ResourceRef]) {
8085
match self {
81-
Self::Rid => {
86+
Self::Id => {
8287
resources.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().id))
8388
}
89+
Self::ParentId => resources.sort_unstable_by_key(|resource| {
90+
resource.upgrade().map(|r| r.borrow().parent_id.clone())
91+
}),
8492
Self::Kind => resources.sort_unstable_by_key(|resource| {
8593
resource.upgrade().map(|r| r.borrow().kind.clone())
8694
}),
95+
Self::Total => resources
96+
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().total(now))),
97+
Self::Target => resources.sort_unstable_by_key(|resource| {
98+
resource.upgrade().map(|r| r.borrow().target.clone())
99+
}),
87100
Self::ConcreteType => resources.sort_unstable_by_key(|resource| {
88101
resource.upgrade().map(|r| r.borrow().concrete_type.clone())
89102
}),
90-
Self::Target => resources.sort_unstable_by_key(|resource| {
91-
resource.upgrade().map(|r| r.borrow().target.clone())
103+
Self::Visibility => resources
104+
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().visibility)),
105+
Self::Location => resources.sort_unstable_by_key(|resource| {
106+
resource.upgrade().map(|r| r.borrow().location.clone())
107+
}),
108+
Self::Attributes => resources.sort_unstable_by_key(|resource| {
109+
resource.upgrade().map(|r| {
110+
// FIXME - we are taking only the first attribute as sorting key here,
111+
// and we are sorting each attribute as a String.
112+
// Instead, attributes should probably be parsed and sorted according to their actual values,
113+
// not just as strings.
114+
r.borrow()
115+
.formatted_attributes()
116+
.first()
117+
.into_iter()
118+
.flat_map(|v| v.iter())
119+
.map(|s| <Cow<str> as std::borrow::Borrow<str>>::borrow(&s.content))
120+
.collect::<String>()
121+
})
92122
}),
93-
Self::Total => resources
94-
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().total(now))),
95123
}
96124
}
97125
}
@@ -100,11 +128,15 @@ impl TryFrom<usize> for SortBy {
100128
type Error = ();
101129
fn try_from(idx: usize) -> Result<Self, Self::Error> {
102130
match idx {
103-
idx if idx == Self::Rid as usize => Ok(Self::Rid),
131+
idx if idx == Self::Id as usize => Ok(Self::Id),
132+
idx if idx == Self::ParentId as usize => Ok(Self::ParentId),
104133
idx if idx == Self::Kind as usize => Ok(Self::Kind),
105-
idx if idx == Self::ConcreteType as usize => Ok(Self::ConcreteType),
106-
idx if idx == Self::Target as usize => Ok(Self::Target),
107134
idx if idx == Self::Total as usize => Ok(Self::Total),
135+
idx if idx == Self::Target as usize => Ok(Self::Target),
136+
idx if idx == Self::ConcreteType as usize => Ok(Self::ConcreteType),
137+
idx if idx == Self::Visibility as usize => Ok(Self::Visibility),
138+
idx if idx == Self::Location as usize => Ok(Self::Location),
139+
idx if idx == Self::Attributes as usize => Ok(Self::Attributes),
108140
_ => Err(()),
109141
}
110142
}

0 commit comments

Comments
 (0)