Jelajahi Sumber

poll: fix match error due to quotation marks around str

serde_json deserializes strings with quotation marks, and you can have
to explicitly convert them using to_str() to extract the inner value.
lunar-mining 4 tahun lalu
induk
melakukan
e6dce6d3f7
2 mengubah file dengan 117 tambahan dan 74 penghapusan
  1. 5 4
      bin/dnetview/src/main.rs
  2. 112 70
      bin/dnetview/src/ui.rs

+ 5 - 4
bin/dnetview/src/main.rs

@@ -152,7 +152,7 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
         let reply = client.get_info().await?;
 
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
-            debug!("reply: {:?}", reply);
+            //debug!("reply: {:?}", reply);
             // TODO: we are ignoring this value for now
             let _ext_addr = reply.as_object().unwrap().get("external_addr");
 
@@ -182,8 +182,8 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
                 for k in ic.keys() {
                     let addr = k.to_string();
                     for v in ic.values() {
-                        let msg = v.get("last_msg").unwrap().to_string();
-                        let status = v.get("last_status").unwrap().to_string();
+                        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);
                         iconnects.push(iinfo);
@@ -235,7 +235,7 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
             }
         } else {
             // TODO: error handling
-            debug!("Reply is empty");
+            //debug!("Reply is empty");
         }
         async_util::sleep(2).await;
     }
@@ -275,5 +275,6 @@ async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> io
                 _ => (),
             }
         }
+        //async_util::sleep(3).await;
     }
 }

+ 112 - 70
bin/dnetview/src/ui.rs

@@ -1,5 +1,5 @@
 use crate::view::View;
-//use log::debug;
+use log::debug;
 
 use tui::{
     backend::Backend,
@@ -10,6 +10,7 @@ use tui::{
     Frame,
 };
 
+// create top level outgoing widget
 pub fn make_oframe() -> NetFrame {
     let ot_len = 4;
     let ot_width = 8;
@@ -17,41 +18,36 @@ pub fn make_oframe() -> NetFrame {
     let ot_cnstrnt = vec![Constraint::Percentage(100)];
     let ot_widget = NetWidget::new(ot_len, ot_width, ot_align, ot_cnstrnt);
 
-    // create outbound msg widget
     let om_len = 5;
     let om_width = 8;
     let om_align = Alignment::Right;
     let om_cnstrnt = vec![Constraint::Percentage(45), Constraint::Percentage(55)];
     let om_widget = NetWidget::new(om_len, om_width, om_align, om_cnstrnt);
 
-    // create outbound slot widget
     let os_len = 5;
     let os_width = 10;
     let os_align = Alignment::Left;
     let os_cnstrnt = vec![Constraint::Percentage(45), Constraint::Percentage(55)];
     let os_widget = NetWidget::new(os_len, os_width, os_align, os_cnstrnt);
 
-    // get total size
     let oframe = NetFrame::new(ot_widget, os_widget, om_widget);
     oframe
 }
 
+// create top level ingoing widget
 pub fn make_iframe(oframe: NetFrame) -> NetFrame {
-    // create inbound title widget
     let it_len = oframe.addrs.len;
     let it_width = 8;
     let it_align = Alignment::Left;
     let it_cnstrnt = vec![Constraint::Percentage(100)];
     let it_widget = NetWidget::new(it_len, it_width, it_align, it_cnstrnt);
 
-    // create inbound addr widget
     let is_len = oframe.addrs.len + oframe.title.len + 1;
     let is_width = 10;
     let is_align = Alignment::Left;
     let is_cnstrnt = vec![Constraint::Percentage(45), Constraint::Percentage(55)];
     let is_widget = NetWidget::new(is_len, is_width, is_align, is_cnstrnt);
 
-    // create inbound msg widget
     let im_len = is_len;
     let im_width = 8;
     let im_align = Alignment::Right;
@@ -62,6 +58,7 @@ pub fn make_iframe(oframe: NetFrame) -> NetFrame {
     iframe
 }
 
+// create top level manual widget
 pub fn make_mframe(iframe: NetFrame) -> NetFrame {
     let mt_len = iframe.title.len + iframe.addrs.len + 1;
     let mt_width = 8;
@@ -69,14 +66,12 @@ pub fn make_mframe(iframe: NetFrame) -> NetFrame {
     let mt_cnstrnt = vec![Constraint::Percentage(100)];
     let mt_widget = NetWidget::new(mt_len, mt_width, mt_align, mt_cnstrnt);
 
-    // create manual data widget
     let mk_len = mt_len + 1;
     let mk_width = 10;
     let mk_align = Alignment::Left;
     let mk_cnstrnt = vec![Constraint::Percentage(45), Constraint::Percentage(55)];
     let mk_widget = NetWidget::new(mk_len, mk_width, mk_align, mk_cnstrnt);
 
-    // create manual data widget
     let mm_len = mt_len + 1;
     let mm_width = 10;
     let mm_align = Alignment::Left;
@@ -87,13 +82,11 @@ pub fn make_mframe(iframe: NetFrame) -> NetFrame {
     mframe
 }
 
-pub fn ui<B: Backend>(f: &mut Frame<'_, B>, view: View) {
+pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
     let oframe = make_oframe();
-    draw_outbound(f, view.clone(), oframe.clone());
-
+    get_and_draw_outbound(f, view.clone(), oframe.clone());
     let iframe = make_iframe(oframe.clone());
-    draw_inbound(f, view.clone(), iframe.clone(), oframe.clone());
-
+    get_and_draw_inbound(f, view.clone(), iframe.clone(), oframe.clone());
     let mframe = make_mframe(iframe.clone());
     draw_manual(f, view.clone(), mframe.clone());
 
@@ -104,7 +97,28 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, view: View) {
     let list_cnstrnts = vec![Constraint::Percentage(50), Constraint::Percentage(50)];
 
     let prev_len = top_widget.manual.addrs.len;
-    draw_list(list_margin, prev_len, list_direction, list_cnstrnts, view.clone(), f);
+    //draw_list(list_margin, prev_len, list_direction, list_cnstrnts, view.clone(), top_widget, f);
+    let mut nodes = Vec::new();
+
+    for id in &view.id_list.node_id {
+        let mut lines = vec![Spans::from(id.to_string())];
+        for _i in 1..prev_len {
+            lines.push(Spans::from(""));
+        }
+        let ids = ListItem::new(lines).style(Style::default());
+        nodes.push(ids)
+    }
+
+    let nodes =
+        List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
+    // this is just the box around the list. not the actual list
+    let slice = Layout::default()
+        .direction(list_direction)
+        .margin(list_margin)
+        .constraints(list_cnstrnts)
+        .split(f.size());
+
+    f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
     //render_info_right(view.clone(), f, slice);
 }
 
@@ -183,41 +197,55 @@ impl NetWidget {
     }
 }
 
-fn draw_outbound<B: Backend>(f: &mut Frame<'_, B>, view: View, oframe: NetFrame) {
+// loop through all connected nodes in Model
+// parse outbound data by creating a text object called Vec<Spans>
+// send Vec<Spans> to render_widget()
+fn get_and_draw_outbound<B: Backend>(f: &mut Frame<'_, B>, view: View, oframe: NetFrame) {
     let mut titles = Vec::new();
     let mut msgs = Vec::new();
     let mut slots = Vec::new();
+    let mut data = Vec::new();
 
     titles.push(Spans::from(Span::styled("Outgoing", Style::default())));
+    data.push("Outgoing".to_string());
     for id in &view.id_list.node_id {
+        //debug!("Looping through nodes: {}", id);
         match &view.info_list.infos.get(id) {
             Some(connects) => {
                 // there is only a single outbound connection so this works
+                // this is probably a bug lol
                 for slot in &connects.outbound[0].slots {
                     if slot.addr.is_empty() {
                         slots.push(Spans::from(format!("Empty")));
+                        data.push("empty".to_string());
                     } else {
                         slots.push(Spans::from(format!("{}", slot.addr)));
+                        data.push(format!("{}", slot.addr));
                     }
                     match slot.channel.last_status.as_str() {
                         "recv" => {
                             msgs.push(Spans::from(format!("[R: {}]", slot.channel.last_msg)));
+                            data.push(format!("{}", slot.channel.last_msg));
                         }
                         "sent" => {
                             msgs.push(Spans::from(format!("[S: {}]", slot.channel.last_msg)));
+                            data.push(format!("{}", slot.channel.last_msg));
                         }
-                        _ => {
+                        e => {
+                            //debug!("data getting lost {}", e);
                             // TODO: right now we do nothing with these values
                         }
                     }
                 }
             }
             None => {
+                debug!("NONE VALUE TRIGGERD");
                 // TODO: Error
             }
         }
     }
 
+    //debug!("{:?}", data);
     oframe.title.clone().draw(titles.clone(), f);
     let t_len2 = titles.clone().len();
     oframe.title.clone().update(t_len2);
@@ -231,7 +259,13 @@ fn draw_outbound<B: Backend>(f: &mut Frame<'_, B>, view: View, oframe: NetFrame)
     oframe.msgs.clone().update(m_len2);
 }
 
-fn draw_inbound<B: Backend>(
+fn print_type_of<T>(_: &T) {
+    debug!("{}", std::any::type_name::<T>())
+}
+// loop through all connected nodes in Model
+// parse inbound data by creating a text object called Vec<Spans>
+// send Vec<Spans> to render_widget()
+fn get_and_draw_inbound<B: Backend>(
     f: &mut Frame<'_, B>,
     view: View,
     iframe: NetFrame,
@@ -241,48 +275,52 @@ fn draw_inbound<B: Backend>(
     let mut titles = Vec::new();
     let mut addrs = Vec::new();
     let mut msgs = Vec::new();
+    let mut data = Vec::new();
 
     // need to have access to slots_len
     for _i in 1..slots_len {
         titles.push(Spans::from(""));
     }
     titles.push(Spans::from(Span::styled("Incoming", Style::default())));
+    data.push("Incoming".to_string());
     for id in &view.id_list.node_id {
+        //debug!("Looping through nodes: {}", id);
         match &view.info_list.infos.get(id) {
-            Some(connection) => {
-                if !connection.inbound.is_empty() {
-                    for connect in &connection.inbound {
-                        addrs.push(Spans::from(connect.connected.clone()));
-                        match connect.channel.last_status.as_str() {
-                            "recv" => {
-                                msgs.push(Spans::from(""));
-                                msgs.push(Spans::from(format!(
-                                    "[R: {}]",
-                                    connect.channel.last_msg
-                                )));
-                            }
-                            "sent" => {
-                                msgs.push(Spans::from(""));
-                                msgs.push(Spans::from(format!(
-                                    "[S: {}]",
-                                    connect.channel.last_msg
-                                )));
-                            }
-                            _ => {
-                                // Do nothing for now
-                            }
+            Some(node) => {
+                data.push(id.to_string());
+                for connect in &node.inbound {
+                    addrs.push(Spans::from(connect.connected.clone()));
+                    data.push(connect.connected.clone());
+
+                    match connect.channel.last_status.as_str() {
+                        "recv" => {
+                            data.push("".to_string());
+                            msgs.push(Spans::from(format!("[R: {}]", connect.channel.last_msg)));
+                            data.push(format!("[R: {}]", connect.channel.last_msg));
+                        }
+                        "sent" => {
+                            data.push("".to_string());
+                            msgs.push(Spans::from(format!("[S: {}]", connect.channel.last_msg)));
+                            data.push(format!("[S: {}]", connect.channel.last_msg));
+                        }
+                        "Null" => {
+                            data.push("Null".to_string());
+                        }
+                        _ => {
+                            // TODO
+                            debug!("This is a bug");
                         }
                     }
-                } else {
-                    // Do nothing for now
                 }
             }
             None => {
+                debug!("NONE VALUE TRIGGERD");
                 // This should never happen. TODO: make this an error.
             }
         }
     }
 
+    //debug!("{:?}", data);
     iframe.title.clone().draw(titles.clone(), f);
     let t_len2 = titles.clone().len();
     iframe.title.clone().update(t_len2);
@@ -323,31 +361,35 @@ fn draw_manual<B: Backend>(f: &mut Frame<'_, B>, view: View, mframe: NetFrame) {
     mframe.addrs.clone().update(s_len2);
 }
 
-fn draw_list<B: Backend>(
-    margin: u16,
-    prev_len: usize,
-    direction: Direction,
-    cnstrnts: Vec<Constraint>,
-    mut view: View,
-    f: &mut Frame<'_, B>,
-) {
-    let mut nodes = Vec::new();
-
-    for id in &view.id_list.node_id {
-        let mut lines = vec![Spans::from(id.to_string())];
-        // need total_len
-        for _i in 1..prev_len {
-            lines.push(Spans::from(""));
-        }
-        let ids = ListItem::new(lines).style(Style::default());
-        nodes.push(ids)
-    }
-
-    let nodes =
-        List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
-    // this is just the box around the list. not the actual list
-    let slice =
-        Layout::default().direction(direction).margin(margin).constraints(cnstrnts).split(f.size());
-
-    f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
-}
+//fn draw_list<B: Backend>(
+//    margin: u16,
+//    prev_len: usize,
+//    direction: Direction,
+//    cnstrnts: Vec<Constraint>,
+//    mut view: View,
+//    connects: ListObject,
+//    f: &mut Frame<'_, B>,
+//) {
+//    let mut nodes = Vec::new();
+//
+//    for id in &view.id_list.node_id {
+//        let mut lines = vec![Spans::from(id.to_string())];
+//        // need total_len
+//        // process_outbound
+//        // process_inbound
+//        // process_manual
+//        for _i in 1..prev_len {
+//            lines.push(Spans::from(""));
+//        }
+//        let ids = ListItem::new(lines).style(Style::default());
+//        nodes.push(ids)
+//    }
+//
+//    let nodes =
+//        List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
+//    // this is just the box around the list. not the actual list
+//    let slice =
+//        Layout::default().direction(direction).margin(margin).constraints(cnstrnts).split(f.size());
+//
+//    f.render_stateful_widget(nodes, slice[0], &mut view.id_list.state);
+//}