fedimint_server_ui/dashboard/
latency.rs

1use std::collections::BTreeMap;
2use std::time::Duration;
3
4use fedimint_core::PeerId;
5use fedimint_server_core::dashboard_ui::ConnectionType;
6use maud::{Markup, html};
7
8pub fn render(
9    consensus_ord_latency: Option<Duration>,
10    p2p_connection_status: &BTreeMap<PeerId, Option<Duration>>,
11    p2p_connection_type_status: &BTreeMap<PeerId, ConnectionType>,
12) -> Markup {
13    html! {
14        div class="card h-100" id="consensus-latency" {
15            div class="card-header dashboard-header" { "System Latency" }
16            div class="card-body" {
17                @if let Some(duration) = consensus_ord_latency {
18                    div class=(format!("alert {}", if duration.as_millis() < 1000 {
19                        "alert-success"
20                    } else if duration.as_millis() < 2000 {
21                        "alert-warning"
22                    } else {
23                        "alert-danger"
24                    })) {
25                        "Consensus Latency: " strong {
26                            (format!("{} ms", duration.as_millis()))
27                        }
28                    }
29                }
30                @if p2p_connection_status.is_empty() {
31                    p { "No peer connections available." }
32                } @else {
33                    table class="table table-striped" {
34                        thead {
35                            tr {
36                                th { "ID" }
37                                th { "Status" }
38                                th { "Connection Type" }
39                                th { "Round Trip" }
40                            }
41                        }
42                        tbody {
43                            @for (peer_id, rtt) in p2p_connection_status {
44                                tr {
45                                    td { (peer_id.to_string()) }
46                                    td {
47                                        @match rtt {
48                                            Some(_) => {
49                                                span class="badge bg-success" { "Connected" }
50                                            }
51                                            None => {
52                                                span class="badge bg-danger" { "Disconnected" }
53                                            }
54                                        }
55                                    }
56                                    td {
57                                        @if let Some(connection_type) = p2p_connection_type_status.get(peer_id) {
58                                            @match connection_type {
59                                                ConnectionType::Direct => {
60                                                    span class="badge bg-success" { "Direct" }
61                                                }
62                                                ConnectionType::Relay => {
63                                                    span class="badge bg-warning" { "Relay" }
64                                                }
65                                                ConnectionType::Unknown => {
66                                                    span class="badge bg-secondary" { "Unknown" }
67                                                }
68                                            }
69                                        } @else {
70                                            span class="text-muted" { "Unknown" }
71                                        }
72                                    }
73                                    td {
74                                        @match rtt {
75                                            Some(duration) if duration.as_millis() > 0 => {
76                                                (format!("{} ms", duration.as_millis()))
77                                            }
78                                            Some(_) | None => {
79                                                span class="text-muted" { "N/A" }
80                                            }
81                                        }
82                                    }
83                                }
84                            }
85                        }
86                    }
87                }
88            }
89        }
90    }
91}