Kaynağa Gözat

src/util: create NanoTimestamp tool to get and display nanoseconds

bin/dnetview, src/net/channel.rs: add NanoTimestamp to net and dnetview
lunar-mining 4 yıl önce
ebeveyn
işleme
9b8ba6329f

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

@@ -21,7 +21,7 @@ use darkfi::{
     util::{
         async_util,
         cli::{log_config, spawn_config, Config},
-        join_config_path, Timestamp,
+        join_config_path, NanoTimestamp,
     },
 };
 
@@ -296,9 +296,9 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
                         let state = "state".to_string();
                         let parent = parent.clone();
                         let msg_values = info2.unwrap().get("log").unwrap().as_array().unwrap();
-                        let mut msg_log: Vec<(Timestamp, String, String)> = Vec::new();
+                        let mut msg_log: Vec<(NanoTimestamp, String, String)> = Vec::new();
                         for msg in msg_values {
-                            let msg: (Timestamp, String, String) =
+                            let msg: (NanoTimestamp, String, String) =
                                 serde_json::from_value(msg.clone())?;
                             msg_log.push(msg);
                         }
@@ -421,9 +421,9 @@ 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<(Timestamp, String, String)> = Vec::new();
+                        let mut msg_log: Vec<(NanoTimestamp, String, String)> = Vec::new();
                         for msg in msg_values {
-                            let msg: (Timestamp, String, String) =
+                            let msg: (NanoTimestamp, String, String) =
                                 serde_json::from_value(msg.clone())?;
                             msg_log.push(msg);
                         }

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

@@ -1,5 +1,5 @@
 use async_std::sync::Mutex;
-use darkfi::util::Timestamp;
+use darkfi::util::NanoTimestamp;
 use fxhash::{FxHashMap, FxHashSet};
 use serde::{Deserialize, Serialize};
 
@@ -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<(Timestamp, String, String)>>>,
+    pub msg_log: Mutex<FxHashMap<String, Vec<(NanoTimestamp, String, String)>>>,
     pub selectables: Mutex<FxHashMap<String, SelectableObject>>,
 }
 
@@ -28,7 +28,7 @@ impl Model {
     pub fn new(
         ids: Mutex<FxHashSet<String>>,
         nodes: Mutex<FxHashMap<String, NodeInfo>>,
-        msg_log: Mutex<FxHashMap<String, Vec<(Timestamp, String, String)>>>,
+        msg_log: Mutex<FxHashMap<String, Vec<(NanoTimestamp, String, String)>>>,
         selectables: Mutex<FxHashMap<String, SelectableObject>>,
     ) -> Model {
         Model { ids, nodes, msg_log, selectables }
@@ -85,7 +85,7 @@ pub struct ConnectInfo {
     pub addr: String,
     pub state: String,
     pub parent: String,
-    pub msg_log: Vec<(Timestamp, String, String)>,
+    pub msg_log: Vec<(NanoTimestamp, String, String)>,
     pub is_empty: bool,
     pub last_msg: String,
     pub last_status: String,
@@ -97,7 +97,7 @@ impl ConnectInfo {
         addr: String,
         state: String,
         parent: String,
-        msg_log: Vec<(Timestamp, String, String)>,
+        msg_log: Vec<(NanoTimestamp, String, String)>,
         is_empty: bool,
         last_msg: String,
         last_status: String,

+ 5 - 5
bin/dnetview/src/view.rs

@@ -11,7 +11,7 @@ use tui::{
     Frame,
 };
 
-use darkfi::util::Timestamp;
+use darkfi::util::NanoTimestamp;
 
 use crate::{
     error::{DnetViewError, DnetViewResult},
@@ -22,7 +22,7 @@ use crate::{
 #[derive(Debug)]
 pub struct View {
     pub nodes: NodeInfoView,
-    pub msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
+    pub msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
     pub active_ids: IdListView,
     pub selectables: FxHashMap<String, SelectableObject>,
 }
@@ -30,7 +30,7 @@ pub struct View {
 impl View {
     pub fn new(
         nodes: NodeInfoView,
-        msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
+        msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
         active_ids: IdListView,
         selectables: FxHashMap<String, SelectableObject>,
     ) -> View {
@@ -40,7 +40,7 @@ impl View {
     pub fn update(
         &mut self,
         nodes: FxHashMap<String, NodeInfo>,
-        msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>,
+        msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>,
         selectables: FxHashMap<String, SelectableObject>,
     ) {
         self.update_nodes(nodes);
@@ -75,7 +75,7 @@ impl View {
         }
     }
 
-    fn update_msg_log(&mut self, msg_log: FxHashMap<String, Vec<(Timestamp, String, String)>>) {
+    fn update_msg_log(&mut self, msg_log: FxHashMap<String, Vec<(NanoTimestamp, String, String)>>) {
         for (id, msg) in msg_log {
             self.msg_log.insert(id, msg);
         }

+ 4 - 4
src/net/channel.rs

@@ -12,7 +12,7 @@ use url::Url;
 
 use crate::{
     system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
-    util::Timestamp,
+    util::NanoTimestamp,
     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<(Timestamp, String, String)>>,
+    log: Mutex<Vec<(NanoTimestamp, String, String)>>,
 }
 
 impl ChannelInfo {
@@ -192,7 +192,7 @@ impl Channel {
         let mut payload = Vec::new();
         message.encode(&mut payload)?;
         let packet = message::Packet { command: String::from(M::name()), payload };
-        let time = Timestamp::current_time();
+        let time = NanoTimestamp::current_time();
         //let time = time::unix_timestamp()?;
 
         {
@@ -279,7 +279,7 @@ impl Channel {
                 let info = &mut *self.info.lock().await;
                 info.last_msg = packet.command.clone();
                 info.last_status = "recv".to_string();
-                let time = Timestamp::current_time();
+                let time = NanoTimestamp::current_time();
                 //let time = time::unix_timestamp()?;
                 info.log.lock().await.push((time, "recv".to_string(), packet.command.clone()));
             }

+ 1 - 1
src/util/mod.rs

@@ -17,4 +17,4 @@ pub use async_util::sleep;
 pub use net_name::NetworkName;
 pub use parse::{decode_base10, encode_base10};
 pub use path::{expand_path, join_config_path, load_keypair_to_str};
-pub use time::{check_clock, unix_timestamp, Timestamp};
+pub use time::{check_clock, unix_timestamp, NanoTimestamp, Timestamp};

+ 31 - 0
src/util/time.rs

@@ -55,6 +55,31 @@ impl std::fmt::Display for Timestamp {
     }
 }
 
+#[derive(
+    Clone,
+    Copy,
+    Debug,
+    Serialize,
+    Deserialize,
+    SerialEncodable,
+    SerialDecodable,
+    PartialEq,
+    PartialOrd,
+)]
+pub struct NanoTimestamp(pub i64);
+
+impl NanoTimestamp {
+    pub fn current_time() -> Self {
+        Self(Utc::now().timestamp_nanos())
+    }
+}
+impl std::fmt::Display for NanoTimestamp {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        let date = timestamp_to_date(self.0, "nanos");
+        write!(f, "{}", date)
+    }
+}
+
 // Clock sync parameters
 const RETRIES: u8 = 10;
 const WORLDTIMEAPI_ADDRESS: &str = "worldtimeapi.org";
@@ -157,6 +182,12 @@ pub fn timestamp_to_date(timestamp: i64, dt: &str) -> String {
         "datetime" => {
             NaiveDateTime::from_timestamp(timestamp, 0).format("%H:%M:%S %A %-d %B").to_string()
         }
+        "nanos" => {
+            const A_BILLION: i64 = 1_000_000_000;
+            NaiveDateTime::from_timestamp(timestamp / A_BILLION, (timestamp % A_BILLION) as u32)
+                .format("%H:%M:%S.%f")
+                .to_string()
+        }
         _ => "".to_string(),
     }
 }