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

dnetview/ View, Model: simplify data structure

removed unnecessary data structures from View and simplified data
structures into 3 types:

model {
    id_list,
    node_info,
    selectables,
    }

view {
    active_ids,
    node_info,
    active_selectables,
}
lunar-mining 4 лет назад
Родитель
Сommit
6a41fa5124
4 измененных файлов с 191 добавлено и 266 удалено
  1. 33 36
      bin/dnetview/src/main.rs
  2. 8 4
      bin/dnetview/src/model.rs
  3. 150 225
      bin/dnetview/src/view.rs
  4. 0 1
      bin/tau/taud/src/main.rs

+ 33 - 36
bin/dnetview/src/main.rs

@@ -30,7 +30,7 @@ use dnetview::{
     model::{ConnectInfo, Model, NodeInfo, SelectableObject, Session, SessionInfo},
     options::ProgramOptions,
     util::{generate_id, make_connect_id, make_empty_id, make_node_id, make_session_id},
-    view::{ConnectInfoView, IdListView, InfoListView, NodeInfoView, SessionInfoView, View},
+    view::{IdListView, NodeInfoView, View},
 };
 
 struct DNetView {
@@ -107,9 +107,10 @@ async fn main() -> Result<()> {
     terminal.clear()?;
 
     let ids = Mutex::new(FxHashSet::default());
-    let infos = Mutex::new(Vec::new());
+    let node_info = Mutex::new(FxHashMap::default());
+    let select_info = Mutex::new(FxHashMap::default());
 
-    let model = Arc::new(Model::new(ids, infos));
+    let model = Arc::new(Model::new(ids, node_info, select_info));
 
     let nthreads = num_cpus::get();
     let (signal, shutdown) = async_channel::unbounded::<()>();
@@ -172,15 +173,15 @@ async fn parse_data(
     let out_session = parse_outbound(outbound, node_id.clone()).await?;
     let man_session = parse_manual(manual, node_id.clone()).await?;
 
-    sessions.push(in_session);
-    sessions.push(out_session);
-    sessions.push(man_session);
+    sessions.push(in_session.clone());
+    sessions.push(out_session.clone());
+    sessions.push(man_session.clone());
 
     let node_info = NodeInfo::new(node_id.clone(), node_name.to_string(), sessions.clone());
-    let node = SelectableObject::Node(node_info.clone());
 
-    // we might simplify things by making this a Vec<SelectableObject>
-    update_model(model.clone(), sessions.clone(), node_info, node).await?;
+    update_ids(model.clone(), node_id.clone()).await;
+    update_node_info(model.clone(), node_info.clone(), node_id.clone()).await;
+    update_select_info(model.clone(), sessions.clone(), node_info.clone()).await?;
 
     //debug!("IDS: {:?}", model.ids.lock().await);
     //debug!("INFOS: {:?}", model.infos.lock().await);
@@ -188,28 +189,29 @@ async fn parse_data(
     Ok(())
 }
 
-async fn update_model(
+async fn update_ids(model: Arc<Model>, id: String) {
+    model.ids.lock().await.insert(id);
+}
+
+async fn update_node_info(model: Arc<Model>, node: NodeInfo, id: String) {
+    model.node_info.lock().await.insert(id, node);
+}
+
+async fn update_select_info(
     model: Arc<Model>,
     sessions: Vec<SessionInfo>,
     node_info: NodeInfo,
-    node: SelectableObject,
 ) -> Result<()> {
-    model.ids.lock().await.insert(node_info.node_id.clone());
-    model.infos.lock().await.push(node);
-
+    let node_obj = SelectableObject::Node(node_info.clone());
+    model.select_info.lock().await.insert(node_info.node_id.clone(), node_obj);
     for session in sessions.clone() {
-        model.ids.lock().await.insert(session.clone().session_id);
-
         let session_obj = SelectableObject::Session(session.clone());
-        model.infos.lock().await.push(session_obj);
-
+        model.select_info.lock().await.insert(session.clone().session_id, session_obj);
         for connect in session.children {
-            model.ids.lock().await.insert(connect.clone().connect_id);
             let connect_obj = SelectableObject::Connect(connect.clone());
-            model.infos.lock().await.push(connect_obj);
+            model.select_info.lock().await.insert(connect.clone().connect_id, connect_obj);
         }
     }
-
     Ok(())
 }
 
@@ -379,24 +381,17 @@ async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> Re
     terminal.clear()?;
 
     let id_list = IdListView::new(FxHashSet::default());
-    let info_list = InfoListView::new(FxHashMap::default());
-
-    let node_view = NodeInfoView::default();
-    let session_view = SessionInfoView::default();
-    let connect_view = ConnectInfoView::default();
-
-    let mut view = View::new(
-        id_list.clone(),
-        info_list.clone(),
-        node_view.clone(),
-        session_view.clone(),
-        connect_view.clone(),
-    );
+    let info_list = NodeInfoView::new(FxHashMap::default());
+    let selectable = FxHashMap::default();
+
+    let mut view = View::new(id_list.clone(), info_list.clone(), selectable);
     view.id_list.state.select(Some(0));
     view.info_list.index = 0;
 
     loop {
-        view.update(model.infos.lock().await.clone())?;
+        view.update_ids(model.ids.lock().await.clone());
+        view.update_node_info(model.node_info.lock().await.clone());
+        view.update_selectable(model.select_info.lock().await.clone());
 
         terminal.draw(|f| {
             view.clone().render(f);
@@ -409,10 +404,12 @@ async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> Re
                 }
                 Key::Char('j') => {
                     view.id_list.next();
-                    //debug!("ID LIST STATE {:?}", view.id_list.state);
+                    //view.info_list.next();
+                    debug!("ID LIST STATE {:?}", view.id_list.state);
                 }
                 Key::Char('k') => {
                     view.id_list.previous();
+                    //view.info_list.previous();
                     //debug!("ID LIST STATE {:?}", view.id_list.state);
                 }
                 _ => (),

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

@@ -19,13 +19,17 @@ pub enum SelectableObject {
 
 pub struct Model {
     pub ids: Mutex<FxHashSet<String>>,
-    pub infos: Mutex<Vec<SelectableObject>>,
-    //pub infos: Mutex<FxHashMap<String, SelectableObject>>,
+    pub node_info: Mutex<FxHashMap<String, NodeInfo>>,
+    pub select_info: Mutex<FxHashMap<String, SelectableObject>>,
 }
 
 impl Model {
-    pub fn new(ids: Mutex<FxHashSet<String>>, infos: Mutex<Vec<SelectableObject>>) -> Model {
-        Model { ids, infos }
+    pub fn new(
+        ids: Mutex<FxHashSet<String>>,
+        node_info: Mutex<FxHashMap<String, NodeInfo>>,
+        select_info: Mutex<FxHashMap<String, SelectableObject>>,
+    ) -> Model {
+        Model { ids, node_info, select_info }
     }
 }
 

+ 150 - 225
bin/dnetview/src/view.rs

@@ -1,4 +1,3 @@
-use async_std::sync::Arc;
 use darkfi::error::Result;
 use fxhash::{FxHashMap, FxHashSet};
 use log::debug;
@@ -19,51 +18,69 @@ use crate::model::{ConnectInfo, Model, NodeInfo, SelectableObject, SessionInfo};
 #[derive(Debug, Clone)]
 pub struct View {
     pub id_list: IdListView,
-    pub info_list: InfoListView,
-    pub node_info: NodeInfoView,
-    pub session_info: SessionInfoView,
-    pub connect_info: ConnectInfoView,
+    pub info_list: NodeInfoView,
+    pub selectables: FxHashMap<String, SelectableObject>,
 }
 
 impl View {
     pub fn new(
         id_list: IdListView,
-        info_list: InfoListView,
-        node_info: NodeInfoView,
-        session_info: SessionInfoView,
-        connect_info: ConnectInfoView,
+        info_list: NodeInfoView,
+        selectables: FxHashMap<String, SelectableObject>,
     ) -> View {
-        View { id_list, info_list, node_info, session_info, connect_info }
+        View { id_list, info_list, selectables }
     }
 
-    pub fn update(&mut self, model: Vec<SelectableObject>) -> Result<()> {
-        for obj in model {
-            let obj_clone = obj.clone();
-            match obj {
-                SelectableObject::Node(node) => {
-                    let node1 = node.clone();
-                    self.node_info.clone().update(node1.clone())?;
-                    self.id_list.ids.insert(node1.clone().node_id);
-                    self.info_list.infos.insert(node.node_id, obj_clone);
-                }
-                SelectableObject::Session(session) => {
-                    let session1 = session.clone();
-                    self.session_info.clone().update(session1.clone())?;
-                    self.id_list.ids.insert(session1.clone().session_id);
-                    self.info_list.infos.insert(session1.clone().session_id, obj_clone);
-                }
-                SelectableObject::Connect(connect) => {
-                    let connect1 = connect.clone();
-                    self.connect_info.clone().update(connect)?;
-                    self.id_list.ids.insert(connect1.clone().connect_id);
-                    self.info_list.infos.insert(connect1.clone().connect_id, obj_clone);
-                }
-            }
+    pub fn update_ids(&mut self, ids: FxHashSet<String>) {
+        // TODO: check if it's active
+        for id in ids {
+            self.id_list.ids.insert(id);
+        }
+    }
+
+    pub fn update_node_info(&mut self, nodes: FxHashMap<String, NodeInfo>) {
+        for (id, node) in nodes {
+            self.info_list.infos.insert(id, node);
         }
+    }
 
-        Ok(())
+    pub fn update_selectable(&mut self, selectables: FxHashMap<String, SelectableObject>) {
+        for (id, obj) in selectables {
+            self.selectables.insert(id, obj);
+        }
     }
 
+    // we only need to update NodeInfo
+    //pub fn update(&mut self, model: Vec<SelectableObject>) -> Result<()> {
+    //    for obj in model {
+    //        let obj_clone = obj.clone();
+    //        match obj {
+    //            SelectableObject::Node(node) => {
+    //                let node1 = node.clone();
+    //                self.id_list.ids.insert(node1.clone().node_id);
+    //                self.info_list.infos.insert(node.node_id, obj_clone);
+    //            }
+    //            SelectableObject::Session(session) => {
+    //                let session1 = session.clone();
+    //                // only write to the id list if not empty
+    //                if !session.children.iter().all(|session| session.is_empty) {
+    //                    self.id_list.ids.insert(session1.clone().session_id);
+    //                }
+    //                //self.id_list.ids.insert(session1.clone().session_id);
+    //                self.info_list.infos.insert(session1.clone().session_id, obj_clone);
+    //            }
+    //            SelectableObject::Connect(connect) => {
+    //                let connect1 = connect.clone();
+    //                self.id_list.ids.insert(connect1.clone().connect_id);
+    //                self.info_list.infos.insert(connect1.clone().connect_id, obj_clone);
+    //            }
+    //        }
+    //    }
+    //    //debug!("INFO LIST VIEW {:?}", self.info_list.infos);
+
+    //    Ok(())
+    //}
+
     pub fn render<B: Backend>(mut self, f: &mut Frame<'_, B>) {
         //debug!("VIEW AT RENDER {:?}", self.id_list.ids);
         //let mut nodes = Vec::new();
@@ -72,200 +89,108 @@ impl View {
         let list_direction = Direction::Horizontal;
         let list_cnstrnts = vec![Constraint::Percentage(50), Constraint::Percentage(50)];
 
-        let mut nodes = Vec::new();
+        //let mut nodes = Vec::new();
 
-        for id in self.id_list.ids {
-            match self.info_list.infos.get(&id) {
-                Some(obj) => {
-                    match obj {
-                        SelectableObject::Node(info) => {
-                            let name_span = Span::raw(&info.node_name);
-                            let lines = vec![Spans::from(name_span)];
-                            let names = ListItem::new(lines);
-                            nodes.push(names);
-                            for child in &info.children {
-                                if !child.children.iter().all(|session| session.is_empty) {
-                                    //let name_span = Span::raw(&child.session_name);
-                                    let name =
-                                        Span::styled(format!("    {}", child.session_name), style);
-                                    let lines = vec![Spans::from(name)];
-                                    let names = ListItem::new(lines);
-                                    nodes.push(names);
-                                    for child in &child.children {
-                                        //let name_span = Span::raw(&child.connect_id);
-                                        let name =
-                                            Span::styled(format!("        {}", child.addr), style);
-                                        let lines = vec![Spans::from(name)];
-                                        let names = ListItem::new(lines);
-                                        nodes.push(names);
-                                    }
-                                    // thing
-                                }
-                            }
-                        }
-                        SelectableObject::Session(info) => {
-                            //let name_span = Span::raw(&info.session_name);
-                            //let lines = vec![Spans::from(name_span)];
-                            //let names = ListItem::new(lines);
-                            //nodes.push(names);
-                            //self.session_info.clone().render(info),
-                        }
-                        SelectableObject::Connect(info) => {
-                            //let name_span = Span::raw(&info.connect_id);
-                            //let lines = vec![Spans::from(name_span)];
-                            //let names = ListItem::new(lines);
-                            //nodes.push(names);
-                            //self.connect_info.clone().render(info),
-                        }
-                    }
-                    //
-                }
-                None => {
-                    // TODO
-                } //
-            }
-            //
+        // this would return node info
+        // for the left list
+        for info in self.info_list.infos.values() {
+            debug!("FOUND INFO: {:?}", info);
+            // test
         }
-        let slice = Layout::default()
-            .direction(list_direction)
-            .margin(list_margin)
-            .constraints(list_cnstrnts)
-            .split(f.size());
-
-        let nodes =
-            List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
 
-        f.render_stateful_widget(nodes, slice[0], &mut self.id_list.state);
+        //for id in self.id_list.ids {
+        //    match self.info_list.infos.get(&id) {
+        //        Some(obj) => {
+        //            match obj {
+        //                SelectableObject::Node(info) => {
+        //                    //let node_name = info.node_name.clone();
+        //                    //draw_node(info.clone(), nodes.clone(), node_name);
+        //                    let name_span = Span::raw(&info.node_name);
+        //                    let lines = vec![Spans::from(name_span)];
+        //                    let names = ListItem::new(lines);
+        //                    nodes.push(names);
+        //                    for child in &info.children {
+        //                        //if !child.children.iter().all(|session| session.is_empty) {
+        //                        //let name_span = Span::raw(&child.session_name);
+        //                        let name =
+        //                            Span::styled(format!("    {}", child.session_name), style);
+        //                        let lines = vec![Spans::from(name)];
+        //                        let names = ListItem::new(lines);
+        //                        nodes.push(names);
+        //                        for child in &child.children {
+        //                            //let name_span = Span::raw(&child.connect_id);
+        //                            let name =
+        //                                Span::styled(format!("        {}", child.addr), style);
+        //                            let lines = vec![Spans::from(name)];
+        //                            let names = ListItem::new(lines);
+        //                            nodes.push(names);
+        //                        }
+        //                        // thing
+        //                        //}
+        //                    }
+        //                }
+        //                SelectableObject::Session(info) => {
+        //                    //let name_span = Span::raw(&info.session_name);
+        //                    //let lines = vec![Spans::from(name_span)];
+        //                    //let names = ListItem::new(lines);
+        //                    //nodes.push(names);
+        //                    //self.session_info.clone().render(info),
+        //                }
+        //                SelectableObject::Connect(info) => {
+        //                    //let name_span = Span::raw(&info.connect_id);
+        //                    //let lines = vec![Spans::from(name_span)];
+        //                    //let names = ListItem::new(lines);
+        //                    //nodes.push(names);
+        //                    //self.connect_info.clone().render(info),
+        //                }
+        //            }
+        //            //
+        //        }
+        //        None => {
+        //            // TODO
+        //        } //
+        //    }
+        //    //
+        //}
+        //let slice = Layout::default()
+        //    .direction(list_direction)
+        //    .margin(list_margin)
+        //    .constraints(list_cnstrnts)
+        //    .split(f.size());
+
+        //let nodes =
+        //    List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
+
+        //f.render_stateful_widget(nodes, slice[0], &mut self.id_list.state);
     }
 }
 
-#[derive(Debug, Clone)]
-pub struct NodeInfoView {
-    pub node_id: String,
-    pub node_name: String,
-    pub children: Vec<SessionInfo>,
-}
-
-impl NodeInfoView {
-    pub fn default() -> NodeInfoView {
-        let node_id = String::new();
-        let node_name = String::new();
-        let children: Vec<SessionInfo> = Vec::new();
-        NodeInfoView { node_id, node_name, children }
-    }
-
-    pub fn update(mut self, data: NodeInfo) -> Result<()> {
-        //self.state.slect(Some(0));
-        self.node_id = data.node_id;
-        self.node_name = data.node_name;
-        self.children = data.children;
-        Ok(())
-    }
-
-    pub fn render(self, node: &NodeInfo) -> List {
-        let mut nodes = Vec::new();
-        let mut lines: Vec<Spans> = Vec::new();
-        let name_span = Span::raw(&node.node_name);
-        lines.push(Spans::from(name_span));
-        let ids = ListItem::new(lines);
-        nodes.push(ids);
-        let nodes =
-            List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");
-        nodes
-    }
-}
-
-#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
-pub struct SessionInfoView {
-    pub session_name: String,
-    pub session_id: String,
-    pub parent: String,
-    pub children: Vec<ConnectInfo>,
-}
-
-impl SessionInfoView {
-    pub fn default() -> SessionInfoView {
-        let session_name = String::new();
-        let session_id = String::new();
-        let parent = String::new();
-        let children: Vec<ConnectInfo> = Vec::new();
-        SessionInfoView { session_name, session_id, parent, children }
-    }
-
-    pub fn update(mut self, data: SessionInfo) -> Result<()> {
-        self.session_name = data.session_name;
-        self.session_id = data.session_id;
-        self.parent = data.parent;
-        self.children = data.children;
-        Ok(())
-    }
-
-    pub fn render(self, session: &SessionInfo) {
-        //let mut lines: Vec<Spans> = Vec::new();
-        //let name_span = Span::raw(self.session_name);
-        //let ids = ListItem::new(lines);
-    }
-}
-
-#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
-pub struct ConnectInfoView {
-    pub connect_id: String,
-    pub addr: String,
-    pub is_empty: bool,
-    pub last_msg: String,
-    pub last_status: String,
-    pub state: String,
-    pub msg_log: Vec<String>,
-    pub parent: String,
-}
-
-impl ConnectInfoView {
-    pub fn default() -> ConnectInfoView {
-        let connect_id = String::new();
-        let addr = String::new();
-        let is_empty = true;
-        let last_msg = String::new();
-        let last_status = String::new();
-        let state = String::new();
-        let msg_log: Vec<String> = Vec::new();
-        let parent = String::new();
-        ConnectInfoView {
-            connect_id,
-            addr,
-            is_empty,
-            last_msg,
-            last_status,
-            state,
-            msg_log,
-            parent,
+fn draw_node(info: NodeInfo, mut nodes: Vec<ListItem>, node_name: String) {
+    let style = Style::default();
+    let name_span = Span::raw(node_name);
+    let lines = vec![Spans::from(name_span)];
+    let names = ListItem::new(lines);
+    nodes.push(names);
+    for child in &info.children {
+        if !child.children.iter().all(|session| session.is_empty) {
+            //let name_span = Span::raw(&child.session_name);
+            let name = Span::styled(format!("    {}", child.session_name), style);
+            let lines = vec![Spans::from(name)];
+            let names = ListItem::new(lines);
+            nodes.push(names);
+            for child in &child.children {
+                //let name_span = Span::raw(&child.connect_id);
+                let name = Span::styled(format!("        {}", child.addr), style);
+                let lines = vec![Spans::from(name)];
+                let names = ListItem::new(lines);
+                nodes.push(names);
+            }
+            // thing
         }
     }
-
-    pub fn update(mut self, data: ConnectInfo) -> Result<()> {
-        self.connect_id = data.connect_id;
-        self.addr = data.addr;
-        self.is_empty = data.is_empty;
-        self.last_msg = data.last_msg;
-        self.last_status = data.last_status;
-        self.state = data.state;
-        self.msg_log = data.msg_log;
-        self.parent = data.parent;
-        Ok(())
-    }
-
-    pub fn render(self, connect: &ConnectInfo) {
-        let mut lines: Vec<Spans> = Vec::new();
-        //let name_span = Span::raw(self.connect_id);
-    }
+    //nodes // thing
 }
 
-//#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
-//pub enum SelectableObject {
-//    Node(NodeInfoView),
-//    Session(SessionInfoView),
-//    Connect(ConnectInfoViewView),
-//}
-
 #[derive(Debug, Clone)]
 pub struct IdListView {
     pub state: ListState,
@@ -316,23 +241,23 @@ impl IdListView {
 }
 
 #[derive(Debug, Clone)]
-pub struct InfoListView {
+pub struct NodeInfoView {
     pub index: usize,
-    pub infos: FxHashMap<String, SelectableObject>,
+    pub infos: FxHashMap<String, NodeInfo>,
 }
 
-impl InfoListView {
-    pub fn new(infos: FxHashMap<String, SelectableObject>) -> InfoListView {
+impl NodeInfoView {
+    pub fn new(infos: FxHashMap<String, NodeInfo>) -> NodeInfoView {
         let index = 0;
 
-        InfoListView { index, infos }
+        NodeInfoView { index, infos }
     }
 
-    pub async fn next(&mut self) {
+    pub fn next(&mut self) {
         self.index = (self.index + 1) % self.infos.len();
     }
 
-    pub async fn previous(&mut self) {
+    pub fn previous(&mut self) {
         if self.index > 0 {
             self.index -= 1;
         } else {

+ 0 - 1
bin/tau/taud/src/main.rs

@@ -157,7 +157,6 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
 
                     let task: TaskInfo = deserialize(&message)?;
                     info!(target: "tau", "receive update from the commits {:?}", task);
-                    
                     task.save(&datastore_path_cloned)?;
                 }