Skip to content

Commit

Permalink
feat(webui): display connections user agent (#1019)
Browse files Browse the repository at this point in the history
Description
---
Display user agent in connections pages for VN and indexer
Update bindings
Fix displayDuration function

Motivation and Context
---

![image](https://github.com/tari-project/tari-dan/assets/1057902/5976ee94-fcee-4b80-b3c7-743615c8b9e9)

How Has This Been Tested?
---
Manually

What process can a PR reviewer use to test or verify this change?
---
Connections in web uis

Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
sdbondi authored Apr 26, 2024
1 parent f53fb55 commit a77eda6
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 53 deletions.
1 change: 1 addition & 0 deletions applications/tari_indexer/src/json_rpc/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl JsonRpcHandlers {
},
age: conn.age(),
ping_latency: conn.ping_latency,
user_agent: conn.user_agent,
})
.collect();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ import { Form } from "react-router-dom";
import Fade from "@mui/material/Fade";
import CopyToClipboard from "../../../Components/CopyToClipboard";
import type { Connection } from "@tariproject/typescript-bindings/tari-indexer-client";
import { displayDuration } from "../../../utils/helpers";

interface Duration {
secs: number;
nanos: number;
}

const useInterval = (fn: () => Promise<unknown>, ms: number) => {
const timeout = useRef<number>();
Expand Down Expand Up @@ -132,16 +129,17 @@ function Connections() {
<Table>
<TableHead>
<TableRow>
<TableCell>Peer id</TableCell>
<TableCell>Peer ID</TableCell>
<TableCell>Address</TableCell>
<TableCell>Age</TableCell>
<TableCell>Direction</TableCell>
<TableCell>Latency</TableCell>
<TableCell>User Agent</TableCell>
</TableRow>
</TableHead>
<TableBody>
{connections &&
connections.map(({ connection_id, address, age, direction, peer_id, ping_latency }) => (
connections.map(({ connection_id, address, age, direction, peer_id, ping_latency, user_agent }) => (
<TableRow key={connection_id}>
<DataTableCell>
{peer_id ? shortenString(peer_id) : "--"}
Expand All @@ -151,6 +149,7 @@ function Connections() {
<DataTableCell>{displayDuration(age)}</DataTableCell>
<DataTableCell>{direction}</DataTableCell>
<DataTableCell>{ping_latency ? displayDuration(ping_latency) : "--"}</DataTableCell>
<DataTableCell>{user_agent ? user_agent.replace(/^\/tari\//, "") : "--"}</DataTableCell>
</TableRow>
))}
</TableBody>
Expand All @@ -159,23 +158,4 @@ function Connections() {
);
}

function displayDuration(duration: Duration) {
if (duration.secs === 0) {
if (duration.nanos > 1000000) {
return `${(duration.nanos / 1000000).toFixed(2)}ms`;
}
if (duration.nanos > 1000) {
return `${(duration.nanos / 1000).toFixed(2)}µs`;
}
return `${duration.nanos / 1000}ns`;
}
if (duration.secs > 60 * 60) {
return `${(duration.secs / 60 / 60).toFixed(0)}h${(duration.secs / 60).toFixed(0)}m`;
}
if (duration.secs > 60) {
return `${(duration.secs / 60).toFixed(0)}m${(duration.secs % 60).toFixed(0)}s`;
}
return `${duration.secs}.${(duration.nanos / 1000000).toFixed(0)}s`;
}

export default Connections;
27 changes: 27 additions & 0 deletions applications/tari_indexer_web_ui/src/utils/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,31 @@ const renderJson = (json: any) => {
}
};

export interface Duration {
secs: number;
nanos: number;
}

export function displayDuration(duration: Duration) {
if (duration.secs === 0) {
if (duration.nanos > 1000000) {
return `${(duration.nanos / 1000000).toFixed(2)}ms`;
}
if (duration.nanos > 1000) {
return `${(duration.nanos / 1000).toFixed(2)}µs`;
}
return `${duration.nanos}ns`;
}
if (duration.secs >= 60 * 60) {
const minutes_secs = duration.secs - Math.floor(duration.secs / 60 / 60) * 60 * 60;
return `${(duration.secs / 60 / 60).toFixed(0)}h${Math.floor(minutes_secs / 60)}m`;
}
if (duration.secs >= 60) {
const secs = duration.secs - Math.floor(duration.secs / 60) * 60;
return `${(duration.secs / 60).toFixed(0)}m${secs.toFixed(0)}s`;
}
return `${duration.secs}s`;
}


export { renderJson };
1 change: 1 addition & 0 deletions applications/tari_validator_node/src/json_rpc/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ impl JsonRpcHandlers {
},
age: conn.age(),
ping_latency: conn.ping_latency,
user_agent: conn.user_agent,
})
.collect();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { Form } from "react-router-dom";
import Fade from "@mui/material/Fade";
import CopyToClipboard from "../../../Components/CopyToClipboard";
import type { Connection } from "@tariproject/typescript-bindings/validator-node-client";
import { displayDuration } from "../../../utils/helpers";

const useInterval = (fn: () => Promise<unknown>, ms: number) => {
const timeout = useRef<number>();
Expand Down Expand Up @@ -128,25 +129,27 @@ function Connections() {
<Table>
<TableHead>
<TableRow>
<TableCell>Peer id</TableCell>
<TableCell>Peer ID</TableCell>
<TableCell>Address</TableCell>
<TableCell>Age</TableCell>
<TableCell>Direction</TableCell>
<TableCell>Latency</TableCell>
<TableCell>User Agent</TableCell>
</TableRow>
</TableHead>
<TableBody>
{connections &&
connections.map(({ connection_id, address, age, direction, peer_id, ping_latency }) => (
connections.map(({ connection_id, address, age, direction, peer_id, ping_latency, user_agent }) => (
<TableRow key={connection_id}>
<DataTableCell>
<DataTableCell title={peer_id}>
{peer_id ? shortenString(peer_id) : "--"}
<CopyToClipboard copy={peer_id} />
</DataTableCell>
<DataTableCell>{address}</DataTableCell>
<DataTableCell>{displayDuration(age)}</DataTableCell>
<DataTableCell>{direction}</DataTableCell>
<DataTableCell>{ping_latency ? displayDuration(ping_latency) : "--"}</DataTableCell>
<DataTableCell>{user_agent ? user_agent.replace(/^\/tari\//, "") : "--"}</DataTableCell>
</TableRow>
))}
</TableBody>
Expand All @@ -156,23 +159,4 @@ function Connections() {
);
}

function displayDuration(duration: { secs: number; nanos: number }) {
if (duration.secs === 0) {
if (duration.nanos > 1000000) {
return `${(duration.nanos / 1000000).toFixed(2)}ms`;
}
if (duration.nanos > 1000) {
return `${(duration.nanos / 1000).toFixed(2)}µs`;
}
return `${duration.nanos / 1000}ns`;
}
if (duration.secs > 60 * 60) {
return `${(duration.secs / 60 / 60).toFixed(0)}h${(duration.secs / 60).toFixed(0)}m`;
}
if (duration.secs > 60) {
return `${(duration.secs / 60).toFixed(0)}m${(duration.secs % 60).toFixed(0)}s`;
}
return `${duration.secs}.${(duration.nanos / 1000000).toFixed(0)}s`;
}

export default Connections;
8 changes: 5 additions & 3 deletions applications/tari_validator_node_web_ui/src/utils/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ export function displayDuration(duration: Duration) {
return `${duration.nanos}ns`;
}
if (duration.secs >= 60 * 60) {
return `${(duration.secs / 60 / 60).toFixed(0)}h${(duration.secs / 60).toFixed(0)}m`;
const minutes_secs = duration.secs - Math.floor(duration.secs / 60 / 60) * 60 * 60;
return `${(duration.secs / 60 / 60).toFixed(0)}h${Math.floor(minutes_secs / 60)}m`;
}
if (duration.secs >= 60) {
return `${(duration.secs / 60).toFixed(0)}m${(duration.secs % 60).toFixed(0)}s`;
const secs = duration.secs - Math.floor(duration.secs / 60) * 60;
return `${(duration.secs / 60).toFixed(0)}m${secs.toFixed(0)}s`;
}
return `${duration.secs}.${(duration.nanos / 1000000).toFixed(0).padStart(3, "0").replace(/0+$/, "")}s`;
return `${duration.secs}s`;
}
1 change: 1 addition & 0 deletions bindings/src/types/tari-indexer-client/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Connection {
direction: ConnectionDirection;
age: { secs: number; nanos: number };
ping_latency: { secs: number; nanos: number } | null;
user_agent: string | null;
}
1 change: 1 addition & 0 deletions bindings/src/types/validator-node-client/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Connection {
direction: ConnectionDirection;
age: { secs: number; nanos: number };
ping_latency: { secs: number; nanos: number } | null;
user_agent: string | null;
}
3 changes: 2 additions & 1 deletion clients/tari_indexer_client/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::time::Duration;
use std::{sync::Arc, time::Duration};

use multiaddr::Multiaddr;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -438,6 +438,7 @@ pub struct Connection {
pub age: Duration,
#[cfg_attr(feature = "ts", ts(type = "{secs: number, nanos: number} | null"))]
pub ping_latency: Option<Duration>,
pub user_agent: Option<Arc<String>>,
}

#[derive(Serialize, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion clients/validator_node_client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{ops::RangeInclusive, time::Duration};
use std::{ops::RangeInclusive, sync::Arc, time::Duration};

use multiaddr::Multiaddr;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -731,6 +731,7 @@ pub struct Connection {
pub age: Duration,
#[cfg_attr(feature = "ts", ts(type = "{secs: number, nanos: number} | null"))]
pub ping_latency: Option<Duration>,
pub user_agent: Option<Arc<String>>,
}

#[derive(Serialize, Debug)]
Expand Down
6 changes: 5 additions & 1 deletion networking/core/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::time::{Duration, Instant};
use std::{
sync::Arc,
time::{Duration, Instant},
};

use libp2p::{core::ConnectedPoint, swarm::ConnectionId, PeerId};

Expand All @@ -15,6 +18,7 @@ pub struct Connection {
pub num_concurrent_dial_errors: usize,
pub established_in: Duration,
pub ping_latency: Option<Duration>,
pub user_agent: Option<Arc<String>>,
}

impl Connection {
Expand Down
15 changes: 15 additions & 0 deletions networking/core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::{
collections::{hash_map::Entry, HashMap},
hash::Hash,
sync::Arc,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -764,6 +765,7 @@ where
num_concurrent_dial_errors,
established_in,
ping_latency: None,
user_agent: None,
});

let Some(waiters) = self.pending_dial_requests.remove(&peer_id) else {
Expand Down Expand Up @@ -793,6 +795,8 @@ where
return Ok(());
}

self.update_connected_peers(&peer_id, &info);

let is_relay = info.protocols.iter().any(|p| *p == relay::HOP_PROTOCOL_NAME);

let is_connected_through_relay = self
Expand Down Expand Up @@ -840,6 +844,17 @@ where
Ok(())
}

fn update_connected_peers(&mut self, peer_id: &PeerId, info: &identify::Info) {
let Some(conns_mut) = self.active_connections.get_mut(peer_id) else {
return;
};

let user_agent = Arc::new(info.agent_version.clone());
for conn_mut in conns_mut {
conn_mut.user_agent = Some(user_agent.clone());
}
}

/// Establishes a relay circuit for the given peer if the it is the selected relay peer. Returns true if the circuit
/// was established from this call.
fn establish_relay_circuit_on_connect(&mut self, peer_id: &PeerId) -> bool {
Expand Down

0 comments on commit a77eda6

Please sign in to comment.