Skip to content

Commit

Permalink
fix(console): fix column sorting in resources tab (#488)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matteo Biggio committed Nov 22, 2023
1 parent 8269b5f commit c2eb05c
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions tokio-console/src/state/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::state::{
use crate::view;
use console_api as proto;
use ratatui::{style::Color, text::Span};
use std::borrow::Cow;
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
Expand All @@ -29,11 +30,15 @@ pub(crate) enum TypeVisibility {
#[derive(Debug, Copy, Clone)]
#[repr(usize)]
pub(crate) enum SortBy {
Rid = 0,
Kind = 1,
ConcreteType = 2,
Target = 3,
Total = 4,
Id = 0,
ParentId = 1,
Kind = 2,
Total = 3,
Target = 4,
ConcreteType = 5,
Visibility = 6,
Location = 7,
Attributes = 8,
}

#[derive(Debug)]
Expand Down Expand Up @@ -71,27 +76,50 @@ struct ResourceStats {

impl Default for SortBy {
fn default() -> Self {
Self::Rid
Self::Id
}
}

impl SortBy {
pub fn sort(&self, now: SystemTime, resources: &mut [ResourceRef]) {
match self {
Self::Rid => {
Self::Id => {
resources.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().id))
}
Self::ParentId => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().parent_id.clone())
}),
Self::Kind => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().kind.clone())
}),
Self::Total => resources
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().total(now))),
Self::Target => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().target.clone())
}),
Self::ConcreteType => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().concrete_type.clone())
}),
Self::Target => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().target.clone())
Self::Visibility => resources
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().visibility)),
Self::Location => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| r.borrow().location.clone())
}),
Self::Attributes => resources.sort_unstable_by_key(|resource| {
resource.upgrade().map(|r| {
// FIXME - we are taking only the first attribute as sorting key here,
// and we are sorting each attribute as a String.
// Instead, attributes should probably be parsed and sorted according to their actual values,
// not just as strings.
r.borrow()
.formatted_attributes()
.first()
.into_iter()
.flat_map(|v| v.iter())
.map(|s| <Cow<str> as std::borrow::Borrow<str>>::borrow(&s.content))
.collect::<String>()
})
}),
Self::Total => resources
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().total(now))),
}
}
}
Expand All @@ -100,11 +128,15 @@ impl TryFrom<usize> for SortBy {
type Error = ();
fn try_from(idx: usize) -> Result<Self, Self::Error> {
match idx {
idx if idx == Self::Rid as usize => Ok(Self::Rid),
idx if idx == Self::Id as usize => Ok(Self::Id),
idx if idx == Self::ParentId as usize => Ok(Self::ParentId),
idx if idx == Self::Kind as usize => Ok(Self::Kind),
idx if idx == Self::ConcreteType as usize => Ok(Self::ConcreteType),
idx if idx == Self::Target as usize => Ok(Self::Target),
idx if idx == Self::Total as usize => Ok(Self::Total),
idx if idx == Self::Target as usize => Ok(Self::Target),
idx if idx == Self::ConcreteType as usize => Ok(Self::ConcreteType),
idx if idx == Self::Visibility as usize => Ok(Self::Visibility),
idx if idx == Self::Location as usize => Ok(Self::Location),
idx if idx == Self::Attributes as usize => Ok(Self::Attributes),
_ => Err(()),
}
}
Expand Down

0 comments on commit c2eb05c

Please sign in to comment.