net, dnetview: pass util::Timestamp instead of u64

use featureful darkfi time utils instead of basic function
unix_timestamp()
This commit is contained in:
lunar-mining
2022-05-15 11:34:58 +02:00
parent 3db3662b14
commit 513cb2b56d
4 changed files with 36 additions and 26 deletions

View File

@@ -21,7 +21,7 @@ use darkfi::{
util::{
async_util,
cli::{log_config, spawn_config, Config},
join_config_path,
join_config_path, Timestamp,
},
};
@@ -281,9 +281,10 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
let state = "state".to_string();
let parent = parent.clone();
let msg_values = node.unwrap().get("log").unwrap().as_array().unwrap();
let mut msg_log: Vec<(u64, String, String)> = Vec::new();
let mut msg_log: Vec<(Timestamp, String, String)> = Vec::new();
for msg in msg_values {
let msg: (u64, String, String) = serde_json::from_value(msg.clone())?;
let msg: (Timestamp, String, String) =
serde_json::from_value(msg.clone())?;
msg_log.push(msg);
}
let is_empty = false;
@@ -388,9 +389,10 @@ async fn parse_outbound(outbound: &Value, node_id: &String) -> DnetViewResult<Se
let state = state.as_str().unwrap().to_string();
let parent = parent.clone();
let msg_values = channel["log"].as_array().unwrap();
let mut msg_log: Vec<(u64, String, String)> = Vec::new();
let mut msg_log: Vec<(Timestamp, String, String)> = Vec::new();
for msg in msg_values {
let msg: (u64, String, String) = serde_json::from_value(msg.clone())?;
let msg: (Timestamp, String, String) =
serde_json::from_value(msg.clone())?;
msg_log.push(msg);
}
let is_empty = false;

View File

@@ -1,16 +1,16 @@
use async_std::sync::Mutex;
use darkfi::util::Timestamp;
use fxhash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Session {
Inbound,
Outbound,
Manual,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum SelectableObject {
Node(NodeInfo),
Session(SessionInfo),
@@ -20,7 +20,7 @@ pub enum SelectableObject {
pub struct Model {
pub ids: Mutex<FxHashSet<String>>,
pub nodes: Mutex<FxHashMap<String, NodeInfo>>,
pub msg_log: Mutex<FxHashMap<String, Vec<(u64, String, String)>>>,
pub msg_log: Mutex<FxHashMap<String, Vec<(Timestamp, String, String)>>>,
pub selectables: Mutex<FxHashMap<String, SelectableObject>>,
}
@@ -28,14 +28,14 @@ impl Model {
pub fn new(
ids: Mutex<FxHashSet<String>>,
nodes: Mutex<FxHashMap<String, NodeInfo>>,
msg_log: Mutex<FxHashMap<String, Vec<(u64, String, String)>>>,
msg_log: Mutex<FxHashMap<String, Vec<(Timestamp, String, String)>>>,
selectables: Mutex<FxHashMap<String, SelectableObject>>,
) -> Model {
Model { ids, nodes, msg_log, selectables }
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
pub id: String,
pub name: String,
@@ -54,7 +54,7 @@ impl NodeInfo {
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
pub id: String,
pub name: String,
@@ -75,13 +75,13 @@ impl SessionInfo {
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ConnectInfo {
pub id: String,
pub addr: String,
pub state: String,
pub parent: String,
pub msg_log: Vec<(u64, String, String)>,
pub msg_log: Vec<(Timestamp, String, String)>,
pub is_empty: bool,
pub last_msg: String,
pub last_status: String,
@@ -93,7 +93,7 @@ impl ConnectInfo {
addr: String,
state: String,
parent: String,
msg_log: Vec<(u64, String, String)>,
msg_log: Vec<(Timestamp, String, String)>,
is_empty: bool,
last_msg: String,
last_status: String,

View File

@@ -11,16 +11,18 @@ use tui::{
Frame,
};
use darkfi::util::Timestamp;
use crate::{
error::{DnetViewError, DnetViewResult},
model::{NodeInfo, SelectableObject},
};
use log::debug;
//use log::debug;
#[derive(Debug)]
pub struct View {
pub nodes: NodeInfoView,
pub msg_log: FxHashMap<String, Vec<(u64, String, String)>>,
pub msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
pub active_ids: IdListView,
pub selectables: FxHashMap<String, SelectableObject>,
}
@@ -28,7 +30,7 @@ pub struct View {
impl View {
pub fn new(
nodes: NodeInfoView,
msg_log: FxHashMap<String, Vec<(u64, String, String)>>,
msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
active_ids: IdListView,
selectables: FxHashMap<String, SelectableObject>,
) -> View {
@@ -38,7 +40,7 @@ impl View {
pub fn update(
&mut self,
nodes: FxHashMap<String, NodeInfo>,
msg_log: FxHashMap<String, Vec<(u64, String, String)>>,
msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
selectables: FxHashMap<String, SelectableObject>,
) {
self.update_nodes(nodes);
@@ -73,7 +75,7 @@ impl View {
}
}
fn update_msg_log(&mut self, msg_log: FxHashMap<String, Vec<(u64, String, String)>>) {
fn update_msg_log(&mut self, msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>) {
for (id, msg) in msg_log {
self.msg_log.insert(id, msg);
}
@@ -209,8 +211,12 @@ impl View {
Some(values) => {
for (t, k, v) in values {
lines.push(Spans::from(match k.as_str() {
"send" => Span::styled(format!("S: {}", v), style),
"recv" => Span::styled(format!("R: {}", v), style),
"send" => {
Span::styled(format!("{} S: {}", t, v), style)
}
"recv" => {
Span::styled(format!("{} R: {}", t, v), style)
}
data => {
return Err(DnetViewError::UnexpectedData(data.to_string()))
}

View File

@@ -12,7 +12,7 @@ use url::Url;
use crate::{
system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
util::time,
util::Timestamp,
Error, Result,
};
@@ -30,7 +30,7 @@ struct ChannelInfo {
last_msg: String,
last_status: String,
// Message log which is cleared on querying get_info
log: Mutex<Vec<(u64, String, String)>>,
log: Mutex<Vec<(Timestamp, String, String)>>,
}
impl ChannelInfo {
@@ -192,7 +192,8 @@ impl Channel {
let mut payload = Vec::new();
message.encode(&mut payload)?;
let packet = message::Packet { command: String::from(M::name()), payload };
let time = time::unix_timestamp()?;
let time = Timestamp::current_time();
//let time = time::unix_timestamp()?;
{
let info = &mut *self.info.lock().await;
@@ -278,7 +279,8 @@ impl Channel {
let info = &mut *self.info.lock().await;
info.last_msg = packet.command.clone();
info.last_status = "recv".to_string();
let time = time::unix_timestamp()?;
let time = Timestamp::current_time();
//let time = time::unix_timestamp()?;
info.log.lock().await.push((time, "recv".to_string(), packet.command.clone()));
}