Skip to content

Commit

Permalink
Show piece cache sync progress, UI fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Dec 9, 2023
1 parent 9d02c70 commit b83e0ec
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub enum FarmerNotification {
farm_index: usize,
state: PlottingState,
},
PieceCacheSyncProgress {
/// Progress so far in %
progress: f32,
},
}

/// Notification messages send from backend about its operation
Expand Down Expand Up @@ -343,6 +347,26 @@ async fn run(
}
})
});
let _on_piece_cache_sync_progress_handler_id = farmer.on_piece_cache_sync_progress({
let notifications_sender = notifications_sender.clone();

Arc::new(move |&progress| {
let notification = FarmerNotification::PieceCacheSyncProgress { progress };

let mut notifications_sender = notifications_sender.clone();

if let Err(error) = notifications_sender
.try_send(BackendNotification::Farmer(notification))
.or_else(|error| {
tokio::task::block_in_place(|| {
Handle::current().block_on(notifications_sender.send(error.into_inner()))
})
})
{
warn!(%error, "Failed to send piece cache sync progress backend notification");
}
})
});

let consensus_node_fut = pin!(consensus_node.run());
let farmer_fut = pin!(farmer.run());
Expand Down
18 changes: 17 additions & 1 deletion src/backend/farmer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(super) mod maybe_node_client;

use crate::backend::farmer::maybe_node_client::MaybeNodeRpcClient;
use crate::backend::utils::{Handler2, Handler2Fn};
use crate::backend::utils::{Handler, Handler2, Handler2Fn, HandlerFn};
use crate::PosTable;
use anyhow::anyhow;
use atomic::Atomic;
Expand Down Expand Up @@ -62,6 +62,7 @@ pub enum PlottingState {
#[derive(Default, Debug)]
struct Handlers {
plotting_state_change: Handler2<usize, PlottingState>,
piece_cache_sync_progress: Handler<f32>,
}

pub(super) struct Farmer {
Expand Down Expand Up @@ -112,6 +113,10 @@ impl Farmer {
) -> HandlerId {
self.handlers.plotting_state_change.add(callback)
}

pub(super) fn on_piece_cache_sync_progress(&self, callback: HandlerFn<f32>) -> HandlerId {
self.handlers.piece_cache_sync_progress.add(callback)
}
}

impl fmt::Debug for Farmer {
Expand Down Expand Up @@ -353,6 +358,17 @@ pub(super) async fn create_farmer(farmer_options: FarmerOptions) -> anyhow::Resu
info!("Finished collecting already plotted pieces successfully");

let handlers = Arc::new(Handlers::default());

piece_cache
.on_sync_progress(Arc::new({
let handlers = Arc::clone(&handlers);

move |progress| {
handlers.piece_cache_sync_progress.call_simple(progress);
}
}))
.detach();

let mut single_disk_farms_stream = single_disk_farms
.into_iter()
.enumerate()
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct ConfigurationView {

#[relm4::component(pub)]
impl Component for ConfigurationView {
type Init = ();
type Init = gtk::Window;
type Input = ConfigurationInput;
type Output = ConfigurationOutput;
type CommandOutput = ();
Expand Down Expand Up @@ -343,12 +343,12 @@ impl Component for ConfigurationView {
}

fn init(
_init: Self::Init,
parent_root: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let open_dialog = OpenDialog::builder()
.transient_for_native(root)
.transient_for_native(&parent_root)
.launch(OpenDialogSettings {
folder_mode: true,
accept_label: "Select".to_string(),
Expand Down
40 changes: 36 additions & 4 deletions src/frontend/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct NodeState {
struct FarmerState {
/// One entry per farm
plotting_state: Vec<PlottingState>,
piece_cache_sync_progress: f32,
}

#[derive(Debug)]
Expand Down Expand Up @@ -134,13 +135,40 @@ impl Component for RunningView {
// TODO: Render all farms, not just the first one
// TODO: Match only because `if let Some(x) = y` is not yet supported here: https://github.com/Relm4/Relm4/issues/582
#[transition = "SlideUpDown"]
match (model.farmer_state.plotting_state[0], model.node_state.sync_state) {
(PlottingState::Plotting { kind, progress, speed }, SyncState::Idle) => gtk::Box {
match (
model.farmer_state.piece_cache_sync_progress,
model.farmer_state.plotting_state.get(0).copied().unwrap_or_default(),
model.node_state.sync_state
) {
(progress, _, _) if progress < 100.0 => gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 10,

gtk::Box {
set_spacing: 10,
gtk::Label {
set_halign: gtk::Align::Start,

#[watch]
set_label: &format!("Piece cache sync {progress:.2}%"),
set_tooltip: "Plotting starts after piece cache sync is complete",
},

gtk::Spinner {
set_margin_start: 5,
start: (),
},
},

gtk::ProgressBar {
#[watch]
set_fraction: progress as f64 / 100.0,
},
},
(_, PlottingState::Plotting { kind, progress, speed }, SyncState::Idle) => gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 10,

gtk::Box {
gtk::Label {
set_halign: gtk::Align::Start,

Expand Down Expand Up @@ -174,7 +202,7 @@ impl Component for RunningView {
set_fraction: progress as f64 / 100.0,
},
},
(PlottingState::Idle, SyncState::Idle) => gtk::Box {
(_, PlottingState::Idle, SyncState::Idle) => gtk::Box {
gtk::Label {
#[watch]
set_label: "Farming",
Expand Down Expand Up @@ -224,6 +252,7 @@ impl RunningView {
};
self.farmer_state = FarmerState {
plotting_state: vec![PlottingState::default(); num_farms],
piece_cache_sync_progress: 0.0,
};
}
RunningInput::NodeNotification(node_notification) => match node_notification {
Expand All @@ -244,6 +273,9 @@ impl RunningView {
warn!(%farm_index, "Unexpected plotting farm index");
}
}
FarmerNotification::PieceCacheSyncProgress { progress } => {
self.farmer_state.piece_cache_sync_progress = progress;
}
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl AsyncComponent for App {
let loading_view = LoadingView::builder().launch(()).detach();

let configuration_view = ConfigurationView::builder()
.launch(())
.launch(root.clone())
.forward(sender.input_sender(), AppInput::Configuration);

let running_view = RunningView::builder().launch(()).detach();
Expand Down

0 comments on commit b83e0ec

Please sign in to comment.