Просмотр исходного кода

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.
lunar-mining 4 лет назад
Родитель
Сommit
1e7c8c09eb
3 измененных файлов с 48 добавлено и 40 удалено
  1. 14 3
      bin/dnetview/src/main.rs
  2. 9 6
      bin/dnetview/src/model.rs
  3. 25 31
      bin/dnetview/src/ui.rs

+ 14 - 3
bin/dnetview/src/main.rs

@@ -171,7 +171,8 @@ async fn poll(client: Map, model: Arc<Model>) -> 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<Model>) -> 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<Model>) -> 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<Model>) -> 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<Model>) -> Result<()> {
     }
 }
 
+fn is_empty_outbound(slots: Vec<Slot>) -> bool {
+    return slots.iter().all(|slot| slot.is_empty == true)
+}
+
 async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> io::Result<()> {
     let mut asi = async_stdin();
 

+ 9 - 6
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<Slot>,
 }
 
 impl OutboundInfo {
-    pub fn new(slots: Vec<Slot>) -> OutboundInfo {
-        OutboundInfo { slots }
+    pub fn new(is_empty: bool, slots: Vec<Slot>) -> 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 }
     }
 }

+ 25 - 31
bin/dnetview/src/ui.rs

@@ -23,15 +23,16 @@ pub fn ui<B: Backend>(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<B: Backend>(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<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
             }
         }
 
-        debug!("{:?}", data);
+        //debug!("{:?}", data);
         let ids = ListItem::new(lines);
         nodes.push(ids);
     }