Kaynağa Gözat

dnetview: bug fixes and cleanup

Fix bug causing slot info to replicate via nested vectors. Also make
state and Option<String>.
lunar-mining 3 yıl önce
ebeveyn
işleme
11250d1b6c
3 değiştirilmiş dosya ile 138 ekleme ve 137 silme
  1. 4 4
      bin/dnetview/src/model.rs
  2. 62 70
      bin/dnetview/src/parser.rs
  3. 72 63
      bin/dnetview/src/view.rs

+ 4 - 4
bin/dnetview/src/model.rs

@@ -86,8 +86,8 @@ pub struct SessionInfo {
     pub dnet_id: String,
     pub dnet_id: String,
     pub node_id: String,
     pub node_id: String,
     pub addr: String,
     pub addr: String,
-    pub state: String,
-    pub info: Vec<SlotInfo>,
+    pub state: Option<String>,
+    pub info: SlotInfo,
     pub is_empty: bool,
     pub is_empty: bool,
 }
 }
 
 
@@ -96,8 +96,8 @@ impl SessionInfo {
         dnet_id: String,
         dnet_id: String,
         node_id: String,
         node_id: String,
         addr: String,
         addr: String,
-        state: String,
-        info: Vec<SlotInfo>,
+        state: Option<String>,
+        info: SlotInfo,
         is_empty: bool,
         is_empty: bool,
     ) -> Self {
     ) -> Self {
         Self { dnet_id, node_id, addr, state, info, is_empty }
         Self { dnet_id, node_id, addr, state, info, is_empty }

+ 62 - 70
bin/dnetview/src/parser.rs

@@ -33,7 +33,7 @@ use crate::{
         LilithInfo, Model, NetworkInfo, NodeInfo, SelectableObject, Session, SessionInfo, SlotInfo,
         LilithInfo, Model, NetworkInfo, NodeInfo, SelectableObject, Session, SessionInfo, SlotInfo,
     },
     },
     rpc::RpcConnect,
     rpc::RpcConnect,
-    util::{is_empty_session, make_empty_id, make_info_id, make_node_id, make_session_id},
+    util::{make_empty_id, make_info_id, make_node_id, make_session_id},
 };
 };
 
 
 pub struct DataParser {
 pub struct DataParser {
@@ -123,14 +123,13 @@ impl DataParser {
         let name = "Offline".to_string();
         let name = "Offline".to_string();
         let session_type = Session::Offline;
         let session_type = Session::Offline;
 
 
-        let mut slots: Vec<SlotInfo> = Vec::new();
         let mut sessions: Vec<SessionInfo> = Vec::new();
         let mut sessions: Vec<SessionInfo> = Vec::new();
         let hosts = Vec::new();
         let hosts = Vec::new();
 
 
         let node_id = make_node_id(&node_name)?;
         let node_id = make_node_id(&node_name)?;
         let dnet_id = make_empty_id(&node_id, &session_type, 0)?;
         let dnet_id = make_empty_id(&node_id, &session_type, 0)?;
         let addr = "Null".to_string();
         let addr = "Null".to_string();
-        let state = "Null".to_string();
+        let state = None;
         let random_id = 0;
         let random_id = 0;
         let remote_id = "Null".to_string();
         let remote_id = "Null".to_string();
         let log = Vec::new();
         let log = Vec::new();
@@ -145,7 +144,6 @@ impl DataParser {
             log,
             log,
             is_empty,
             is_empty,
         );
         );
-        slots.push(slot.clone());
 
 
         let session_info = SessionInfo::new(
         let session_info = SessionInfo::new(
             dnet_id,
             dnet_id,
@@ -153,7 +151,7 @@ impl DataParser {
             //name.clone(),
             //name.clone(),
             addr.clone(),
             addr.clone(),
             state,
             state,
-            slots,
+            slot,
             is_empty,
             is_empty,
         );
         );
         sessions.push(session_info);
         sessions.push(session_info);
@@ -233,40 +231,44 @@ impl DataParser {
         outbounds: Vec<SessionInfo>,
         outbounds: Vec<SessionInfo>,
     ) -> DnetViewResult<()> {
     ) -> DnetViewResult<()> {
         for inbound in inbounds {
         for inbound in inbounds {
-            for info in inbound.info {
-                if !self.model.msg_map.lock().await.contains_key(&info.dnet_id) {
-                    // we don't have this ID: it is a new node
-                    self.model.msg_map.lock().await.insert(info.dnet_id, info.log.clone());
-                } else {
-                    // we have this id: append the msg values
-                    match self.model.msg_map.lock().await.entry(info.dnet_id) {
-                        Entry::Vacant(e) => {
-                            e.insert(info.log);
-                        }
-                        Entry::Occupied(mut e) => {
-                            for msg in info.log {
-                                e.get_mut().push(msg);
-                            }
+            if !self.model.msg_map.lock().await.contains_key(&inbound.info.dnet_id) {
+                // we don't have this ID: it is a new node
+                self.model
+                    .msg_map
+                    .lock()
+                    .await
+                    .insert(inbound.info.dnet_id, inbound.info.log.clone());
+            } else {
+                // we have this id: append the msg values
+                match self.model.msg_map.lock().await.entry(inbound.info.dnet_id) {
+                    Entry::Vacant(e) => {
+                        e.insert(inbound.info.log);
+                    }
+                    Entry::Occupied(mut e) => {
+                        for msg in inbound.info.log {
+                            e.get_mut().push(msg);
                         }
                         }
                     }
                     }
                 }
                 }
             }
             }
         }
         }
         for outbound in outbounds {
         for outbound in outbounds {
-            for info in outbound.info {
-                if !self.model.msg_map.lock().await.contains_key(&info.dnet_id) {
-                    // we don't have this ID: it is a new node
-                    self.model.msg_map.lock().await.insert(info.dnet_id, info.log.clone());
-                } else {
-                    // we have this id: append the msg values
-                    match self.model.msg_map.lock().await.entry(info.dnet_id) {
-                        Entry::Vacant(e) => {
-                            e.insert(info.log);
-                        }
-                        Entry::Occupied(mut e) => {
-                            for msg in info.log {
-                                e.get_mut().push(msg);
-                            }
+            if !self.model.msg_map.lock().await.contains_key(&outbound.info.dnet_id) {
+                // we don't have this ID: it is a new node
+                self.model
+                    .msg_map
+                    .lock()
+                    .await
+                    .insert(outbound.info.dnet_id, outbound.info.log.clone());
+            } else {
+                // we have this id: append the msg values
+                match self.model.msg_map.lock().await.entry(outbound.info.dnet_id) {
+                    Entry::Vacant(e) => {
+                        e.insert(outbound.info.log);
+                    }
+                    Entry::Occupied(mut e) => {
+                        for msg in outbound.info.log {
+                            e.get_mut().push(msg);
                         }
                         }
                     }
                     }
                 }
                 }
@@ -291,14 +293,12 @@ impl DataParser {
                         .lock()
                         .lock()
                         .await
                         .await
                         .insert(inbound.clone().dnet_id, inbound_obj.clone());
                         .insert(inbound.clone().dnet_id, inbound_obj.clone());
-                    for info in inbound.info {
-                        let info_obj = SelectableObject::Connect(info.clone());
-                        self.model
-                            .selectables
-                            .lock()
-                            .await
-                            .insert(info.clone().dnet_id, info_obj.clone());
-                    }
+                    let info_obj = SelectableObject::Connect(inbound.info.clone());
+                    self.model
+                        .selectables
+                        .lock()
+                        .await
+                        .insert(inbound.info.clone().dnet_id, info_obj.clone());
                 }
                 }
             }
             }
             for outbound in node.outbound {
             for outbound in node.outbound {
@@ -309,14 +309,12 @@ impl DataParser {
                         .lock()
                         .lock()
                         .await
                         .await
                         .insert(outbound.clone().dnet_id, outbound_obj.clone());
                         .insert(outbound.clone().dnet_id, outbound_obj.clone());
-                    for info in outbound.info {
-                        let info_obj = SelectableObject::Connect(info.clone());
-                        self.model
-                            .selectables
-                            .lock()
-                            .await
-                            .insert(info.clone().dnet_id, info_obj.clone());
-                    }
+                    let info_obj = SelectableObject::Connect(outbound.info.clone());
+                    self.model
+                        .selectables
+                        .lock()
+                        .await
+                        .insert(outbound.info.clone().dnet_id, info_obj.clone());
                 }
                 }
             }
             }
         }
         }
@@ -330,7 +328,6 @@ impl DataParser {
         prefix: Session,
         prefix: Session,
     ) -> DnetViewResult<Vec<SessionInfo>> {
     ) -> DnetViewResult<Vec<SessionInfo>> {
         let session_id = make_session_id(&node_id, &prefix)?;
         let session_id = make_session_id(&node_id, &prefix)?;
-        let mut slots: Vec<SlotInfo> = Vec::new();
         let mut session_info: Vec<SessionInfo> = Vec::new();
         let mut session_info: Vec<SessionInfo> = Vec::new();
 
 
         // TODO: improve this ugly hack.
         // TODO: improve this ugly hack.
@@ -357,33 +354,31 @@ impl DataParser {
                 log,
                 log,
                 is_empty,
                 is_empty,
             );
             );
-            slots.push(slot);
-            // Check whether the session is empty.
-            let is_empty = is_empty_session(&slots);
+            let is_empty = true;
 
 
             let addr = "Null".to_string();
             let addr = "Null".to_string();
-            let state = "Null".to_string();
-            let session = SessionInfo::new(
-                session_id.clone(),
-                node_id.clone(),
-                addr,
-                state,
-                slots.clone(),
-                is_empty,
-            );
+            let state = None;
+            let session =
+                SessionInfo::new(session_id.clone(), node_id.clone(), addr, state, slot, is_empty);
             session_info.push(session);
             session_info.push(session);
 
 
             return Ok(session_info)
             return Ok(session_info)
         }
         }
 
 
         let sessions = reply.as_array().unwrap();
         let sessions = reply.as_array().unwrap();
+        debug!(target: "dnetview", "parse_outbound() len session{:?}", sessions.len());
 
 
         for session in sessions {
         for session in sessions {
             match session.as_object() {
             match session.as_object() {
                 Some(obj) => {
                 Some(obj) => {
                     debug!(target: "dnetview", "parse_outbound() obj {:?}", session);
                     debug!(target: "dnetview", "parse_outbound() obj {:?}", session);
                     let addr = obj.get("addr").unwrap().as_str().unwrap().to_string();
                     let addr = obj.get("addr").unwrap().as_str().unwrap().to_string();
-                    let state = obj.get("state").unwrap().as_str().unwrap().to_string();
+
+                    let state: Option<String> = match obj.get("state") {
+                        Some(state) => Some(state.as_str().unwrap().to_string()),
+                        None => None,
+                    };
+
                     let info: serde_json::Map<String, Value> =
                     let info: serde_json::Map<String, Value> =
                         serde_json::from_value(obj.get("info").unwrap().clone()).unwrap();
                         serde_json::from_value(obj.get("info").unwrap().clone()).unwrap();
 
 
@@ -408,14 +403,13 @@ impl DataParser {
                         log,
                         log,
                         is_empty,
                         is_empty,
                     );
                     );
-                    slots.push(slot);
 
 
                     let session = SessionInfo::new(
                     let session = SessionInfo::new(
                         session_id.clone(),
                         session_id.clone(),
                         node_id.clone(),
                         node_id.clone(),
                         addr.clone(),
                         addr.clone(),
                         state,
                         state,
-                        slots.clone(),
+                        slot,
                         is_empty,
                         is_empty,
                     );
                     );
                     session_info.push(session);
                     session_info.push(session);
@@ -440,17 +434,15 @@ impl DataParser {
                         log,
                         log,
                         is_empty,
                         is_empty,
                     );
                     );
-                    slots.push(slot);
-                    // Check whether the session is empty.
-                    let is_empty = is_empty_session(&slots);
+                    let is_empty = true;
 
 
-                    let state = "Null".to_string();
+                    let state = None;
                     let session = SessionInfo::new(
                     let session = SessionInfo::new(
                         session_id.clone(),
                         session_id.clone(),
                         node_id.clone(),
                         node_id.clone(),
                         addr.clone(),
                         addr.clone(),
                         state,
                         state,
-                        slots.clone(),
+                        slot,
                         is_empty,
                         is_empty,
                     );
                     );
                     session_info.push(session);
                     session_info.push(session);

+ 72 - 63
bin/dnetview/src/view.rs

@@ -98,11 +98,11 @@ impl<'a> View {
                                 if !self.ordered_list.iter().any(|i| i == &inbound.dnet_id) {
                                 if !self.ordered_list.iter().any(|i| i == &inbound.dnet_id) {
                                     self.ordered_list.push(inbound.dnet_id.clone());
                                     self.ordered_list.push(inbound.dnet_id.clone());
                                 }
                                 }
-                                for info in &inbound.info {
-                                    if !self.ordered_list.iter().any(|i| i == &info.dnet_id) {
-                                        self.ordered_list.push(info.dnet_id.clone());
-                                    }
+                                //for info in &inbound.info {
+                                if !self.ordered_list.iter().any(|i| i == &inbound.info.dnet_id) {
+                                    self.ordered_list.push(inbound.info.dnet_id.clone());
                                 }
                                 }
+                                //}
                             }
                             }
                         }
                         }
                         for outbound in &node.outbound {
                         for outbound in &node.outbound {
@@ -110,11 +110,11 @@ impl<'a> View {
                                 if !self.ordered_list.iter().any(|i| i == &outbound.dnet_id) {
                                 if !self.ordered_list.iter().any(|i| i == &outbound.dnet_id) {
                                     self.ordered_list.push(outbound.dnet_id.clone());
                                     self.ordered_list.push(outbound.dnet_id.clone());
                                 }
                                 }
-                                for info in &outbound.info {
-                                    if !self.ordered_list.iter().any(|i| i == &info.dnet_id) {
-                                        self.ordered_list.push(info.dnet_id.clone());
-                                    }
+                                //for info in &outbound.info {
+                                if !self.ordered_list.iter().any(|i| i == &outbound.info.dnet_id) {
+                                    self.ordered_list.push(outbound.info.dnet_id.clone());
                                 }
                                 }
+                                //}
                             }
                             }
                         }
                         }
                     }
                     }
@@ -221,37 +221,36 @@ impl<'a> View {
                                 let lines = vec![Spans::from(name)];
                                 let lines = vec![Spans::from(name)];
                                 let names = ListItem::new(lines);
                                 let names = ListItem::new(lines);
                                 nodes.push(names);
                                 nodes.push(names);
-                                for info in &inbound.info {
-                                    let mut infos = Vec::new();
-                                    match info.addr.as_str() {
-                                        "Null" => {
-                                            let style = Style::default()
-                                                .fg(Color::Blue)
-                                                .add_modifier(Modifier::ITALIC);
-                                            let name = Span::styled(
-                                                format!("        {} ", info.addr),
+                                //for info in &inbound.info {
+                                let mut infos = Vec::new();
+                                match inbound.info.addr.as_str() {
+                                    "Null" => {
+                                        let style = Style::default()
+                                            .fg(Color::Blue)
+                                            .add_modifier(Modifier::ITALIC);
+                                        let name = Span::styled(
+                                            format!("        {} ", inbound.info.addr),
+                                            style,
+                                        );
+                                        infos.push(name);
+                                    }
+                                    addr => {
+                                        let name = Span::styled(format!("        {}", addr), style);
+                                        infos.push(name);
+                                        if !inbound.info.remote_id.is_empty() {
+                                            let remote_id = Span::styled(
+                                                format!("({})", inbound.info.remote_id),
                                                 style,
                                                 style,
                                             );
                                             );
-                                            infos.push(name);
-                                        }
-                                        addr => {
-                                            let name =
-                                                Span::styled(format!("        {}", addr), style);
-                                            infos.push(name);
-                                            if !info.remote_id.is_empty() {
-                                                let remote_id = Span::styled(
-                                                    format!("({})", info.remote_id),
-                                                    style,
-                                                );
-                                                infos.push(remote_id)
-                                            }
+                                            infos.push(remote_id)
                                         }
                                         }
                                     }
                                     }
-
-                                    let lines = vec![Spans::from(infos)];
-                                    let names = ListItem::new(lines);
-                                    nodes.push(names);
                                 }
                                 }
+
+                                let lines = vec![Spans::from(infos)];
+                                let names = ListItem::new(lines);
+                                nodes.push(names);
+                                //}
                             }
                             }
                         }
                         }
 
 
@@ -261,37 +260,36 @@ impl<'a> View {
                                 let lines = vec![Spans::from(name)];
                                 let lines = vec![Spans::from(name)];
                                 let names = ListItem::new(lines);
                                 let names = ListItem::new(lines);
                                 nodes.push(names);
                                 nodes.push(names);
-                                for info in &outbound.info {
-                                    let mut infos = Vec::new();
-                                    match info.addr.as_str() {
-                                        "Null" => {
-                                            let style = Style::default()
-                                                .fg(Color::Blue)
-                                                .add_modifier(Modifier::ITALIC);
-                                            let name = Span::styled(
-                                                format!("        {} ", info.addr),
+                                //for info in &outbound.info {
+                                let mut infos = Vec::new();
+                                match outbound.info.addr.as_str() {
+                                    "Null" => {
+                                        let style = Style::default()
+                                            .fg(Color::Blue)
+                                            .add_modifier(Modifier::ITALIC);
+                                        let name = Span::styled(
+                                            format!("        {} ", outbound.info.addr),
+                                            style,
+                                        );
+                                        infos.push(name);
+                                    }
+                                    addr => {
+                                        let name = Span::styled(format!("        {}", addr), style);
+                                        infos.push(name);
+                                        if !outbound.info.remote_id.is_empty() {
+                                            let remote_id = Span::styled(
+                                                format!("({})", outbound.info.remote_id),
                                                 style,
                                                 style,
                                             );
                                             );
-                                            infos.push(name);
-                                        }
-                                        addr => {
-                                            let name =
-                                                Span::styled(format!("        {}", addr), style);
-                                            infos.push(name);
-                                            if !info.remote_id.is_empty() {
-                                                let remote_id = Span::styled(
-                                                    format!("({})", info.remote_id),
-                                                    style,
-                                                );
-                                                infos.push(remote_id)
-                                            }
+                                            infos.push(remote_id)
                                         }
                                         }
                                     }
                                     }
-
-                                    let lines = vec![Spans::from(infos)];
-                                    let names = ListItem::new(lines);
-                                    nodes.push(names);
                                 }
                                 }
+
+                                let lines = vec![Spans::from(infos)];
+                                let names = ListItem::new(lines);
+                                nodes.push(names);
+                                //}
                             }
                             }
                         }
                         }
                     }
                     }
@@ -381,11 +379,22 @@ impl<'a> View {
                     let addr = Span::styled(format!("Addr: {}", session.addr), style);
                     let addr = Span::styled(format!("Addr: {}", session.addr), style);
                     lines.push(Spans::from(addr));
                     lines.push(Spans::from(addr));
 
 
-                    // TODO: this will be an option
-                    let addr = Span::styled(format!("State: {}", session.state), style);
-                    lines.push(Spans::from(addr));
+                    if session.state.is_some() {
+                        let addr = Span::styled(
+                            format!("State: {}", session.state.as_ref().unwrap()),
+                            style,
+                        );
+                        lines.push(Spans::from(addr));
+                    }
                 }
                 }
                 Some(SelectableObject::Connect(connect)) => {
                 Some(SelectableObject::Connect(connect)) => {
+                    // TODO: this renders in the top right and overwrites the msg log
+                    //let addr = Span::styled(format!("Addr: {}", connect.addr), style);
+                    //let random_id = Span::styled(format!("Random id: {}", connect.random_id), style);
+                    //let remote_id = Span::styled(format!("Remote id: {}", connect.remote_id), style);
+                    //lines.push(Spans::from(addr));
+                    //lines.push(Spans::from(random_id));
+                    //lines.push(Spans::from(remote_id));
                     //debug!(target: "dnetview", "render_info()::SelectableObject::Connect");
                     //debug!(target: "dnetview", "render_info()::SelectableObject::Connect");
                     let text = self.parse_msg_list(connect.dnet_id.clone())?;
                     let text = self.parse_msg_list(connect.dnet_id.clone())?;
                     f.render_stateful_widget(text, slice[1], &mut self.msg_list.state);
                     f.render_stateful_widget(text, slice[1], &mut self.msg_list.state);