From 1e7c8c09eb033f28860a32fe3b6e698aded1fc2f Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Fri, 18 Mar 2022 12:20:39 +0100 Subject: [PATCH] model: add is_empty param to Slot, OutboundInfo and InboundInfo discard empty connections efficiently and only render when is_empty == false. for outbound connections, use iter().all() to ensure that is_empty is only set to false when all slots are empty. --- bin/dnetview/src/main.rs | 17 +++++++++--- bin/dnetview/src/model.rs | 15 ++++++----- bin/dnetview/src/ui.rs | 56 +++++++++++++++++---------------------- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/bin/dnetview/src/main.rs b/bin/dnetview/src/main.rs index 5c67e3018..1be3d8189 100644 --- a/bin/dnetview/src/main.rs +++ b/bin/dnetview/src/main.rs @@ -171,7 +171,8 @@ async fn poll(client: Map, model: Arc) -> Result<()> { let msg = "Null".to_string(); let status = "Null".to_string(); let channel = Channel::new(msg, status); - let iinfo = InboundInfo::new(connected, channel); + let is_empty = true; + let iinfo = InboundInfo::new(is_empty, connected, channel); iconnects.push(iinfo); } else { // channel is not empty. initialize with whole values @@ -182,7 +183,8 @@ async fn poll(client: Map, model: Arc) -> Result<()> { let msg = v.get("last_msg").unwrap().as_str().unwrap().to_string(); let status = v.get("last_status").unwrap().as_str().unwrap().to_string(); let channel = Channel::new(msg, status); - let iinfo = InboundInfo::new(addr.clone(), channel); + let is_empty = false; + let iinfo = InboundInfo::new(is_empty, addr.clone(), channel); iconnects.push(iinfo); } } @@ -197,11 +199,13 @@ async fn poll(client: Map, model: Arc) -> Result<()> { for slot in outbound_slots.as_array().unwrap() { if slot["channel"].is_null() { // channel is empty. initialize with empty values + let is_empty = true; let state = &slot["state"]; let msg = "Null".to_string(); let status = "Null".to_string(); let channel = Channel::new(msg, status); let new_slot = Slot::new( + is_empty, String::from("Empty"), channel, state.as_str().unwrap().to_string(), @@ -209,17 +213,20 @@ async fn poll(client: Map, model: Arc) -> Result<()> { slots.push(new_slot.clone()) } else { // channel is not empty. initialize with whole values + let is_empty = false; let addr = &slot["addr"]; let state = &slot["state"]; let channel: Channel = serde_json::from_value(slot["channel"].clone())?; let new_slot = Slot::new( + is_empty, addr.as_str().unwrap().to_string(), channel, state.as_str().unwrap().to_string(), ); slots.push(new_slot) } - let oinfo = OutboundInfo::new(slots.clone()); + let is_empty = is_empty_outbound(slots.clone()); + let oinfo = OutboundInfo::new(is_empty, slots.clone()); oconnects.push(oinfo); } @@ -243,6 +250,10 @@ async fn poll(client: Map, model: Arc) -> Result<()> { } } +fn is_empty_outbound(slots: Vec) -> bool { + return slots.iter().all(|slot| slot.is_empty == true) +} + async fn render(terminal: &mut Terminal, model: Arc) -> io::Result<()> { let mut asi = async_stdin(); diff --git a/bin/dnetview/src/model.rs b/bin/dnetview/src/model.rs index 326776ba1..9adc80e3e 100644 --- a/bin/dnetview/src/model.rs +++ b/bin/dnetview/src/model.rs @@ -79,25 +79,27 @@ impl ManualInfo { #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)] pub struct OutboundInfo { + pub is_empty: bool, pub slots: Vec, } impl OutboundInfo { - pub fn new(slots: Vec) -> OutboundInfo { - OutboundInfo { slots } + pub fn new(is_empty: bool, slots: Vec) -> OutboundInfo { + OutboundInfo { is_empty, slots } } } #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)] pub struct Slot { + pub is_empty: bool, pub addr: String, pub channel: Channel, pub state: String, } impl Slot { - pub fn new(addr: String, channel: Channel, state: String) -> Slot { - Slot { addr, channel, state } + pub fn new(is_empty: bool, addr: String, channel: Channel, state: String) -> Slot { + Slot { is_empty, addr, channel, state } } } @@ -115,12 +117,13 @@ impl Channel { #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)] pub struct InboundInfo { + pub is_empty: bool, pub connected: String, pub channel: Channel, } impl InboundInfo { - pub fn new(connected: String, channel: Channel) -> InboundInfo { - InboundInfo { connected, channel } + pub fn new(is_empty: bool, connected: String, channel: Channel) -> InboundInfo { + InboundInfo { is_empty, connected, channel } } } diff --git a/bin/dnetview/src/ui.rs b/bin/dnetview/src/ui.rs index 26e719fca..cce75504f 100644 --- a/bin/dnetview/src/ui.rs +++ b/bin/dnetview/src/ui.rs @@ -23,15 +23,16 @@ pub fn ui(f: &mut Frame<'_, B>, mut view: View) { for id in &view.id_list.node_id { let id_span = Span::raw(id.to_string()); let mut lines = vec![Spans::from(id_span)]; + data.push(id.to_string()); match &view.info_list.infos.get(id) { Some(node) => { for outbound in &node.outbound.clone() { - lines.push(Spans::from(Span::styled(" Outgoing", style))); - data.push("Outgoing".to_string()); - for slot in outbound.slots.clone() { - let addr = Span::styled(format!(" {}", slot.addr), style); - data.push(format!("{}", slot.addr)); - if slot.channel.last_status.as_str() != "Null" { + if outbound.is_empty == false { + lines.push(Spans::from(Span::styled(" Outbound", Style::default()))); + data.push("Outbound".to_string()); + for slot in outbound.slots.clone() { + let addr = Span::styled(format!(" {}", slot.addr), style); + data.push(format!("{}", slot.addr)); let msg: Span = match slot.channel.last_status.as_str() { "recv" => Span::styled( format!(" [R: {}]", slot.channel.last_msg), @@ -45,35 +46,28 @@ pub fn ui(f: &mut Frame<'_, B>, mut view: View) { }; data.push(format!("{}", slot.channel.last_msg)); lines.push(Spans::from(vec![addr, msg])); - } else { - // discard Null values for now - lines.push(Spans::from(addr)); } } } - for connect in &node.inbound { - if connect.connected != "Empty" { + for inbound in &node.inbound { + if inbound.is_empty == false { lines.push(Spans::from(Span::styled(" Incoming", Style::default()))); data.push("Incoming".to_string()); - let addr = Span::styled(format!(" {}", connect.connected), style); - data.push(format!("{}", connect.connected)); - if connect.channel.last_status.as_str() != "Null" { - let msg: Span = match connect.channel.last_status.as_str() { - "recv" => Span::styled( - format!(" [R: {}]", connect.channel.last_msg), - style, - ), - "sent" => Span::styled( - format!(" [R: {}]", connect.channel.last_msg), - style, - ), - a => Span::styled(a.to_string(), style), - }; - data.push(format!("{}", connect.channel.last_msg)); - lines.push(Spans::from(vec![addr, msg])); - } else { - lines.push(Spans::from(addr)); - } + let addr = Span::styled(format!(" {}", inbound.connected), style); + data.push(format!("{}", inbound.connected)); + let msg: Span = match inbound.channel.last_status.as_str() { + "recv" => Span::styled( + format!(" [R: {}]", inbound.channel.last_msg), + style, + ), + "sent" => Span::styled( + format!(" [R: {}]", inbound.channel.last_msg), + style, + ), + a => Span::styled(a.to_string(), style), + }; + data.push(format!("{}", inbound.channel.last_msg)); + lines.push(Spans::from(vec![addr, msg])); } } for connect in &node.manual { @@ -89,7 +83,7 @@ pub fn ui(f: &mut Frame<'_, B>, mut view: View) { } } - debug!("{:?}", data); + //debug!("{:?}", data); let ids = ListItem::new(lines); nodes.push(ids); }