gui: added node metrics, refactor views

This commit is contained in:
ardocrat
2023-05-18 03:53:38 +03:00
parent 59990f9ee2
commit f4ec1b9ab0
12 changed files with 453 additions and 257 deletions
+63 -132
View File
@@ -12,22 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::time::SystemTime;
use chrono::{DateTime, Local, Offset, TimeZone, Utc};
use chrono::format::DelayedFormat;
use eframe::emath::Vec2;
use eframe::epaint::{FontId, Stroke};
use eframe::epaint::text::{LayoutJob, TextFormat, TextWrapping};
use eframe::epaint::Stroke;
use egui::{Color32, RichText, Rounding, ScrollArea, Spinner, Widget};
use egui_extras::{Size, StripBuilder};
use grin_servers::common::stats::TxStats;
use grin_servers::PeerStats;
use crate::gui::colors::{COLOR_DARK, COLOR_GRAY, COLOR_LIGHT, COLOR_YELLOW};
use crate::gui::icons::{AT, CUBE, DEVICES, DOWNLOAD_SIMPLE, FLOW_ARROW, HANDSHAKE, PACKAGE, PLUGS_CONNECTED, SHARE_NETWORK};
use crate::gui::views::{DEFAULT_STROKE, NetworkTab};
use crate::gui::views::common::sub_title;
use crate::gui::colors::{COLOR_DARK, COLOR_GRAY};
use crate::gui::icons::{AT, CUBE, DEVICES, FLOW_ARROW, HANDSHAKE, PACKAGE, PLUGS_CONNECTED, SHARE_NETWORK};
use crate::gui::views::{NetworkTab, View};
use crate::node::Node;
pub struct NetworkNode {
@@ -43,6 +35,10 @@ impl Default for NetworkNode {
}
impl NetworkTab for NetworkNode {
fn name(&self) -> &String {
&self.title
}
fn ui(&mut self, ui: &mut egui::Ui, node: &mut Node) {
let server_stats = node.state.get_stats();
if !server_stats.is_some() {
@@ -54,97 +50,86 @@ impl NetworkTab for NetworkNode {
let stats = server_stats.as_ref().unwrap();
// Make scroll bar thinner
ui.style_mut().spacing.scroll_bar_width = 4.0;
ScrollArea::vertical()
.auto_shrink([false; 2])
.show(ui, |ui| {
// Disable item spacing
ui.style_mut().spacing.item_spacing = Vec2::new(0.0, 0.0);
// Show header stats
ui.vertical_centered_justified(|ui| {
sub_title(ui, format!("{} {}", FLOW_ARROW, t!("header")), COLOR_DARK);
View::sub_title(ui, format!("{} {}", FLOW_ARROW, t!("header")), COLOR_DARK);
});
ui.add_space(4.0);
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
draw_stat_box(ui,
stats.header_stats.last_block_h.to_string(),
t!("hash"),
StatBoxRounding::TopLeft);
View::rounded_box(ui,
stats.header_stats.last_block_h.to_string(),
t!("hash"),
[true, false, false, false]);
});
columns[1].vertical_centered(|ui| {
draw_stat_box(ui,
stats.header_stats.height.to_string(),
t!("height"),
StatBoxRounding::TopRight);
View::rounded_box(ui,
stats.header_stats.height.to_string(),
t!("height"),
[false, true, false, false]);
});
});
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
draw_stat_box(ui,
stats.header_stats.total_difficulty.to_string(),
t!("difficulty"),
StatBoxRounding::BottomLeft);
View::rounded_box(ui,
stats.header_stats.total_difficulty.to_string(),
t!("difficulty"),
[false, false, true, false]);
});
columns[1].vertical_centered(|ui| {
let ts = stats.header_stats.latest_timestamp;
draw_stat_box(ui,
format!("{}", ts.format("%d/%m/%Y %H:%M")),
t!("time_utc"),
StatBoxRounding::BottomRight);
View::rounded_box(ui,
format!("{}", ts.format("%d/%m/%Y %H:%M")),
t!("time_utc"),
[false, false, false, true]);
});
});
// Show block stats
ui.add_space(5.0);
ui.vertical_centered_justified(|ui| {
sub_title(ui, format!("{} {}", CUBE, t!("block")), COLOR_DARK);
View::sub_title(ui, format!("{} {}", CUBE, t!("block")), COLOR_DARK);
});
ui.add_space(4.0);
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
draw_stat_box(ui,
stats.chain_stats.last_block_h.to_string(),
t!("hash"),
StatBoxRounding::TopLeft);
View::rounded_box(ui,
stats.chain_stats.last_block_h.to_string(),
t!("hash"),
[true, false, false, false]);
});
columns[1].vertical_centered(|ui| {
draw_stat_box(ui,
stats.chain_stats.height.to_string(),
t!("height"),
StatBoxRounding::TopRight);
View::rounded_box(ui,
stats.chain_stats.height.to_string(),
t!("height"),
[false, true, false, false]);
});
});
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
draw_stat_box(ui,
stats.chain_stats.total_difficulty.to_string(),
t!("difficulty"),
StatBoxRounding::BottomLeft);
View::rounded_box(ui,
stats.chain_stats.total_difficulty.to_string(),
t!("difficulty"),
[false, false, true, false]);
});
columns[1].vertical_centered(|ui| {
let ts = stats.chain_stats.latest_timestamp;
draw_stat_box(ui,
format!("{}", ts.format("%d/%m/%Y %H:%M")),
t!("time_utc"),
StatBoxRounding::BottomRight);
View::rounded_box(ui,
format!("{}", ts.format("%d/%m/%Y %H:%M")),
t!("time_utc"),
[false, false, false, true]);
});
});
// Show data stats
ui.add_space(5.0);
ui.vertical_centered_justified(|ui| {
sub_title(ui, format!("{} {}", SHARE_NETWORK, t!("data")), COLOR_DARK);
View::sub_title(ui, format!("{} {}", SHARE_NETWORK, t!("data")), COLOR_DARK);
});
ui.add_space(4.0);
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
let tx_stat = match &stats.tx_stats {
@@ -153,7 +138,10 @@ impl NetworkTab for NetworkNode {
format!("{} ({})", tx.tx_pool_size, tx.tx_pool_kernels)
}
};
draw_stat_box(ui, tx_stat, t!("main_pool"), StatBoxRounding::TopLeft);
View::rounded_box(ui,
tx_stat,
t!("main_pool"),
[true, false, false, false]);
});
columns[1].vertical_centered(|ui| {
let stem_tx_stat = match &stats.tx_stats {
@@ -162,23 +150,25 @@ impl NetworkTab for NetworkNode {
format!("{} ({})", stx.stem_pool_size, stx.stem_pool_kernels)
}
};
draw_stat_box(ui, stem_tx_stat, t!("stem_pool"), StatBoxRounding::TopRight);
View::rounded_box(ui,
stem_tx_stat,
t!("stem_pool"),
[false, true, false, false]);
});
});
ui.columns(2, |columns| {
columns[0].vertical_centered(|ui| {
draw_stat_box(ui,
stats.disk_usage_gb.to_string(),
t!("size"),
StatBoxRounding::BottomLeft);
View::rounded_box(ui,
stats.disk_usage_gb.to_string(),
t!("size"),
[false, false, true, false]);
});
columns[1].vertical_centered(|ui| {
let ts = stats.chain_stats.latest_timestamp;
draw_stat_box(ui,
stats.peer_count.to_string(),
t!("peers"),
StatBoxRounding::BottomRight);
View::rounded_box(ui,
stats.peer_count.to_string(),
t!("peers"),
[false, false, false, true]);
});
});
@@ -186,15 +176,14 @@ impl NetworkTab for NetworkNode {
if stats.peer_count > 0 {
ui.add_space(5.0);
ui.vertical_centered_justified(|ui| {
sub_title(ui, format!("{} {}", HANDSHAKE, t!("peers")), COLOR_DARK);
View::sub_title(ui,format!("{} {}", HANDSHAKE, t!("peers")), COLOR_DARK);
});
ui.add_space(4.0);
for (index, ps) in stats.peer_stats.iter().enumerate() {
let rounding = if index == 0 {
if stats.peer_count == 1 {
[true, true];
}
let rounding = if stats.peer_count == 1 {
[true, true]
} else if index == 0 {
[true, false]
} else if index == &stats.peer_stats.len() - 1 {
[false, true]
@@ -208,10 +197,6 @@ impl NetworkTab for NetworkNode {
}
});
}
fn name(&self) -> &String {
&self.title
}
}
fn draw_peer_stats(ui: &mut egui::Ui, peer: &PeerStats, rounding: [bool; 2]) {
@@ -231,14 +216,11 @@ fn draw_peer_stats(ui: &mut egui::Ui, peer: &PeerStats, rounding: [bool; 2]) {
);
ui.add_space(2.0);
ui.horizontal_top(|ui| {
ui.add_space(6.0);
ui.heading(RichText::new(PLUGS_CONNECTED)
.color(Color32::BLACK)
.size(18.0));
ui.add_space(6.0);
// Draw peer address
@@ -246,14 +228,11 @@ fn draw_peer_stats(ui: &mut egui::Ui, peer: &PeerStats, rounding: [bool; 2]) {
.color(Color32::BLACK)
.size(18.0));
});
ui.horizontal_top(|ui| {
ui.add_space(6.0);
ui.heading(RichText::new(PACKAGE)
.color(COLOR_DARK)
.size(16.0));
ui.add_space(6.0);
// Draw peer difficulty and height
@@ -270,11 +249,9 @@ fn draw_peer_stats(ui: &mut egui::Ui, peer: &PeerStats, rounding: [bool; 2]) {
ui.horizontal_top(|ui| {
ui.add_space(6.0);
ui.heading(RichText::new(DEVICES)
.color(COLOR_GRAY)
.size(16.0));
ui.add_space(6.0);
// Draw peer user-agent
@@ -282,51 +259,5 @@ fn draw_peer_stats(ui: &mut egui::Ui, peer: &PeerStats, rounding: [bool; 2]) {
.color(COLOR_GRAY)
.size(16.0));
});
ui.add_space(2.0);
}
#[derive(PartialEq)]
enum StatBoxRounding {
TopLeft, TopRight, BottomRight, BottomLeft
}
fn draw_stat_box(ui: &mut egui::Ui, value: String, label: String, rounding: StatBoxRounding) {
let mut rect = ui.available_rect_before_wrap();
rect.set_height(46.0);
// Draw box background
ui.painter().rect(
rect,
Rounding {
nw: if rounding == StatBoxRounding::TopLeft { 8.0 } else { 0.0 },
ne: if rounding == StatBoxRounding::TopRight { 8.0 } else { 0.0 },
sw: if rounding == StatBoxRounding::BottomLeft { 8.0 } else { 0.0 },
se: if rounding == StatBoxRounding::BottomRight { 8.0 } else { 0.0 },
},
Color32::WHITE,
Stroke { width: 1.0, color: Color32::from_gray(230) },
);
ui.vertical_centered_justified(|ui| {
// Correct vertical spacing between items
ui.style_mut().spacing.item_spacing.y = -4.0;
// Draw box value
let mut job = LayoutJob::single_section(value, TextFormat {
font_id: FontId::proportional(18.0),
color: Color32::BLACK,
.. Default::default()
});
job.wrap = TextWrapping {
max_rows: 1,
break_anywhere: false,
overflow_character: Option::from('﹍'),
..Default::default()
};
ui.label(job);
// Draw box label
ui.label(RichText::new(label).color(COLOR_GRAY).size(15.0));
});
}