2023-05-04 20:09:26 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
2023-06-27 02:11:07 +03:00
|
|
|
use egui::{RichText, Rounding, ScrollArea, Stroke};
|
2023-05-18 03:53:38 +03:00
|
|
|
use grin_servers::DiffBlock;
|
|
|
|
|
2023-06-03 11:22:51 +03:00
|
|
|
use crate::gui::Colors;
|
2023-06-15 23:54:41 +03:00
|
|
|
use crate::gui::icons::{AT, COINS, CUBE_TRANSPARENT, HASH, HOURGLASS_LOW, HOURGLASS_MEDIUM, TIMER};
|
2023-06-21 02:13:47 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-07-03 21:17:49 +03:00
|
|
|
use crate::gui::views::network::{NetworkTab, NetworkTabType};
|
|
|
|
use crate::gui::views::{Modal, NetworkContainer, View};
|
2023-05-17 00:36:59 +03:00
|
|
|
use crate::node::Node;
|
|
|
|
|
2023-06-03 11:22:51 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NetworkMetrics;
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
const BLOCK_REWARD: f64 = 60.0;
|
|
|
|
// 1 year is calculated as 365 days and 6 hours (31557600).
|
|
|
|
const YEARLY_SUPPLY: f64 = ((60 * 60 * 24 * 365) + 6 * 60 * 60) as f64;
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
impl NetworkTab for NetworkMetrics {
|
2023-06-17 13:23:31 +03:00
|
|
|
fn get_type(&self) -> NetworkTabType {
|
|
|
|
NetworkTabType::Metrics
|
|
|
|
}
|
|
|
|
|
2023-06-21 02:13:47 +03:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
2023-05-23 01:46:42 +03:00
|
|
|
let server_stats = Node::get_stats();
|
2023-07-03 21:17:49 +03:00
|
|
|
// Show message to enable node when it's not running.
|
|
|
|
if !Node::is_running() {
|
|
|
|
NetworkContainer::disabled_node_ui(ui);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show loading spinner when node is stopping.
|
|
|
|
if Node::is_stopping() {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
View::big_loading_spinner(ui);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show message when metrics are not available.
|
2023-07-01 21:04:52 +03:00
|
|
|
if server_stats.is_none() || Node::is_restarting()
|
|
|
|
|| server_stats.as_ref().unwrap().diff_stats.height == 0 {
|
2023-07-03 21:17:49 +03:00
|
|
|
View::center_content(ui, 162.0, |ui| {
|
|
|
|
View::big_loading_spinner(ui);
|
|
|
|
ui.add_space(18.0);
|
|
|
|
ui.label(RichText::new(t!("network_metrics.loading"))
|
|
|
|
.size(16.0)
|
|
|
|
.color(Colors::INACTIVE_TEXT)
|
|
|
|
);
|
|
|
|
});
|
2023-05-18 03:53:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stats = server_stats.as_ref().unwrap();
|
|
|
|
|
2023-06-17 13:23:31 +03:00
|
|
|
// Show emission info.
|
2023-06-20 00:38:25 +03:00
|
|
|
View::sub_title(ui, format!("{} {}", COINS, t!("network_metrics.emission")));
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.columns(3, |columns| {
|
2023-06-20 00:38:25 +03:00
|
|
|
let supply = stats.header_stats.height as f64 * BLOCK_REWARD;
|
|
|
|
let rate = (YEARLY_SUPPLY * 100.0) / supply;
|
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
columns[0].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{}ツ", BLOCK_REWARD),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_metrics.reward"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[true, false, true, false]);
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{:.2}%", rate),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_metrics.inflation"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[false, false, false, false]);
|
|
|
|
});
|
|
|
|
columns[2].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
supply.to_string(),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_metrics.supply"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[false, true, false, true]);
|
|
|
|
});
|
|
|
|
});
|
2023-06-19 01:29:15 +03:00
|
|
|
ui.add_space(4.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2023-05-23 01:46:42 +03:00
|
|
|
// Show difficulty adjustment window info
|
2023-06-20 00:38:25 +03:00
|
|
|
let difficulty_title = t!(
|
2023-06-02 02:05:34 +03:00
|
|
|
"network_metrics.difficulty_window",
|
|
|
|
"size" => stats.diff_stats.window_size
|
|
|
|
);
|
2023-06-20 00:38:25 +03:00
|
|
|
View::sub_title(ui, format!("{} {}", HOURGLASS_MEDIUM, difficulty_title));
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.columns(3, |columns| {
|
|
|
|
columns[0].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
stats.diff_stats.height.to_string(),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_node.height"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[true, false, true, false]);
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{}s", stats.diff_stats.average_block_time),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_metrics.block_time"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[false, false, false, false]);
|
|
|
|
});
|
|
|
|
columns[2].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
stats.diff_stats.average_difficulty.to_string(),
|
2023-06-02 02:05:34 +03:00
|
|
|
t!("network_node.difficulty"),
|
2023-05-18 03:53:38 +03:00
|
|
|
[false, true, false, true]);
|
|
|
|
});
|
|
|
|
});
|
2023-06-19 01:29:15 +03:00
|
|
|
ui.add_space(4.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2023-05-23 01:46:42 +03:00
|
|
|
// Show difficulty adjustment window blocks
|
2023-05-18 03:53:38 +03:00
|
|
|
let blocks_size = stats.diff_stats.last_blocks.len();
|
2023-05-23 01:46:42 +03:00
|
|
|
ScrollArea::vertical()
|
2023-06-23 22:12:30 +03:00
|
|
|
.id_source("difficulty_scroll")
|
2023-05-23 01:46:42 +03:00
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.stick_to_bottom(true)
|
|
|
|
.show_rows(
|
|
|
|
ui,
|
|
|
|
DIFF_BLOCK_UI_HEIGHT,
|
|
|
|
blocks_size,
|
|
|
|
|ui, row_range| {
|
|
|
|
for index in row_range {
|
|
|
|
let db = stats.diff_stats.last_blocks.get(index).unwrap();
|
|
|
|
let rounding = if blocks_size == 1 {
|
|
|
|
[true, true]
|
|
|
|
} else if index == 0 {
|
|
|
|
[true, false]
|
|
|
|
} else if index == blocks_size - 1 {
|
|
|
|
[false, true]
|
|
|
|
} else {
|
|
|
|
[false, false]
|
|
|
|
};
|
|
|
|
draw_diff_block(ui, db, rounding)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-05-18 03:53:38 +03:00
|
|
|
}
|
2023-06-23 22:12:30 +03:00
|
|
|
|
2023-06-27 02:11:07 +03:00
|
|
|
fn on_modal_ui(&mut self, ui: &mut egui::Ui, modal: &Modal, cb: &dyn PlatformCallbacks) {}
|
2023-05-18 03:53:38 +03:00
|
|
|
}
|
|
|
|
|
2023-06-20 00:38:25 +03:00
|
|
|
const DIFF_BLOCK_UI_HEIGHT: f32 = 76.60;
|
2023-05-18 03:53:38 +03:00
|
|
|
|
|
|
|
fn draw_diff_block(ui: &mut egui::Ui, db: &DiffBlock, rounding: [bool; 2]) {
|
2023-06-20 00:38:25 +03:00
|
|
|
// Add space before the first item.
|
2023-05-18 21:08:23 +03:00
|
|
|
if rounding[0] {
|
2023-05-18 22:48:36 +03:00
|
|
|
ui.add_space(4.0);
|
2023-05-18 21:08:23 +03:00
|
|
|
}
|
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
ui.vertical(|ui| {
|
|
|
|
let mut rect = ui.available_rect_before_wrap();
|
2023-05-18 22:48:36 +03:00
|
|
|
rect.set_height(DIFF_BLOCK_UI_HEIGHT);
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.painter().rect(
|
|
|
|
rect,
|
|
|
|
Rounding {
|
|
|
|
nw: if rounding[0] { 8.0 } else { 0.0 },
|
|
|
|
ne: if rounding[0] { 8.0 } else { 0.0 },
|
|
|
|
sw: if rounding[1] { 8.0 } else { 0.0 },
|
|
|
|
se: if rounding[1] { 8.0 } else { 0.0 },
|
|
|
|
},
|
2023-06-20 00:38:25 +03:00
|
|
|
Colors::WHITE,
|
2023-06-03 11:22:51 +03:00
|
|
|
Stroke { width: 1.0, color: Colors::ITEM_STROKE }
|
2023-05-18 03:53:38 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
ui.add_space(2.0);
|
|
|
|
ui.horizontal_top(|ui| {
|
2023-05-18 22:48:36 +03:00
|
|
|
ui.add_space(5.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.heading(RichText::new(HASH)
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::BLACK)
|
2023-05-18 22:48:36 +03:00
|
|
|
.size(18.0));
|
|
|
|
ui.add_space(2.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2023-06-20 00:38:25 +03:00
|
|
|
// Draw block hash.
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.heading(RichText::new(db.block_hash.to_string())
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::BLACK)
|
2023-05-18 22:48:36 +03:00
|
|
|
.size(18.0));
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
|
|
|
ui.horizontal_top(|ui| {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
ui.heading(RichText::new(CUBE_TRANSPARENT)
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::TITLE)
|
2023-05-18 03:53:38 +03:00
|
|
|
.size(16.0));
|
2023-06-20 00:38:25 +03:00
|
|
|
ui.add_space(3.0);
|
|
|
|
// Draw block difficulty and height.
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.heading(RichText::new(db.difficulty.to_string())
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::TITLE)
|
2023-05-18 03:53:38 +03:00
|
|
|
.size(16.0));
|
|
|
|
ui.add_space(2.0);
|
2023-06-03 11:22:51 +03:00
|
|
|
ui.heading(RichText::new(AT).color(Colors::TITLE).size(16.0));
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.add_space(2.0);
|
|
|
|
ui.heading(RichText::new(db.block_height.to_string())
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::TITLE)
|
2023-05-18 03:53:38 +03:00
|
|
|
.size(16.0));
|
|
|
|
});
|
|
|
|
ui.horizontal_top(|ui| {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
ui.heading(RichText::new(TIMER)
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::GRAY)
|
2023-05-18 03:53:38 +03:00
|
|
|
.size(16.0));
|
2023-06-20 00:38:25 +03:00
|
|
|
ui.add_space(3.0);
|
|
|
|
// Draw block date.
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.heading(RichText::new(format!("{}s", db.duration))
|
2023-06-03 11:22:51 +03:00
|
|
|
.color(Colors::GRAY)
|
2023-05-18 03:53:38 +03:00
|
|
|
.size(16.0));
|
2023-06-20 00:38:25 +03:00
|
|
|
ui.add_space(4.0);
|
|
|
|
|
2023-06-03 11:22:51 +03:00
|
|
|
ui.heading(RichText::new(HOURGLASS_LOW).color(Colors::GRAY).size(16.0));
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.add_space(2.0);
|
2023-06-20 00:38:25 +03:00
|
|
|
// Draw block time.
|
|
|
|
let block_time = NaiveDateTime::from_timestamp_opt(db.time as i64, 0).unwrap();
|
|
|
|
let block_datetime: DateTime<Utc> = DateTime::from_utc(block_time, Utc);
|
|
|
|
ui.heading(RichText::new(block_datetime.to_string())
|
|
|
|
.color(Colors::GRAY)
|
|
|
|
.size(16.0));
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
2023-05-18 22:48:36 +03:00
|
|
|
ui.add_space(2.0);
|
2023-05-18 21:08:23 +03:00
|
|
|
});
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
2023-05-17 00:36:59 +03:00
|
|
|
}
|