public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH datacenter-manager 3/4] ui: dashboard: refactor loading logic
Date: Fri,  6 Jun 2025 09:27:19 +0200	[thread overview]
Message-ID: <20250606072720.664054-5-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250606072720.664054-1-d.csapak@proxmox.com>

We'll want to reload regularly, so refactor the logic to make that
easier. Only create one future, so we can reduce the amount of messages
and load everything in lockstep.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 ui/src/dashboard/mod.rs | 67 +++++++++++++++++++++++------------------
 1 file changed, 37 insertions(+), 30 deletions(-)

diff --git a/ui/src/dashboard/mod.rs b/ui/src/dashboard/mod.rs
index 960766e..a1cac31 100644
--- a/ui/src/dashboard/mod.rs
+++ b/ui/src/dashboard/mod.rs
@@ -1,6 +1,7 @@
 use std::rc::Rc;
 
 use anyhow::Error;
+use futures::future::join;
 use serde::{Deserialize, Serialize};
 use serde_json::json;
 use yew::{
@@ -60,9 +61,13 @@ pub struct DashboardConfig {
     max_age: Option<u64>,
 }
 
+pub type LoadingResult = (
+    Result<ResourcesStatus, Error>,
+    Result<pdm_client::types::TopEntities, proxmox_client::Error>,
+);
+
 pub enum Msg {
-    LoadingFinished(Result<ResourcesStatus, Error>),
-    TopEntitiesLoadResult(Result<pdm_client::types::TopEntities, proxmox_client::Error>),
+    LoadingFinished(LoadingResult),
     RemoteListChanged(RemoteList),
     CreateWizard(bool),
 }
@@ -76,8 +81,8 @@ pub struct PdmDashboard {
     remote_list: RemoteList,
     show_wizard: bool,
     _context_listener: ContextHandle<RemoteList>,
-    _async_pool: AsyncPool,
-    _config: PersistentState<DashboardConfig>,
+    async_pool: AsyncPool,
+    config: PersistentState<DashboardConfig>,
 }
 
 impl PdmDashboard {
@@ -171,6 +176,22 @@ impl PdmDashboard {
                     .map(|err| error_message(&err.to_string())),
             )
     }
+
+    fn reload(&mut self, ctx: &yew::Context<Self>) {
+        let link = ctx.link().clone();
+        let max_age = self.config.max_age.unwrap_or(DEFAULT_MAX_AGE_S);
+
+        self.async_pool.spawn(async move {
+            let client = crate::pdm_client();
+
+            let top_entities_future = client.get_top_entities();
+            let status_future = http_get("/resources/status", Some(json!({"max-age": max_age})));
+
+            let (top_entities_res, status_res) = join(top_entities_future, status_future).await;
+
+            link.send_message(Msg::LoadingFinished((status_res, top_entities_res)));
+        });
+    }
 }
 
 impl Component for PdmDashboard {
@@ -178,30 +199,15 @@ impl Component for PdmDashboard {
     type Properties = Dashboard;
 
     fn create(ctx: &yew::Context<Self>) -> Self {
-        let link = ctx.link().clone();
-        let _config: PersistentState<DashboardConfig> =
-            PersistentState::new(StorageLocation::local("dashboard-config"));
-        let max_age = _config.max_age.unwrap_or(DEFAULT_MAX_AGE_S);
-
+        let config = PersistentState::new(StorageLocation::local("dashboard-config"));
         let async_pool = AsyncPool::new();
 
-        async_pool.spawn(async move {
-            let result = http_get("/resources/status", Some(json!({"max-age": max_age}))).await;
-            link.send_message(Msg::LoadingFinished(result));
-        });
-        async_pool.spawn({
-            let link = ctx.link().clone();
-            async move {
-                let result = crate::pdm_client().get_top_entities().await;
-                link.send_message(Msg::TopEntitiesLoadResult(result));
-            }
-        });
         let (remote_list, _context_listener) = ctx
             .link()
             .context(ctx.link().callback(Msg::RemoteListChanged))
             .expect("No Remote list context provided");
 
-        Self {
+        let mut this = Self {
             status: ResourcesStatus::default(),
             last_error: None,
             top_entities: None,
@@ -210,14 +216,18 @@ impl Component for PdmDashboard {
             remote_list,
             show_wizard: false,
             _context_listener,
-            _async_pool: async_pool,
-            _config,
-        }
+            async_pool,
+            config,
+        };
+
+        this.reload(ctx);
+
+        this
     }
 
     fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
         match msg {
-            Msg::LoadingFinished(resources_status) => {
+            Msg::LoadingFinished((resources_status, top_entities)) => {
                 match resources_status {
                     Ok(status) => {
                         self.last_error = None;
@@ -225,17 +235,14 @@ impl Component for PdmDashboard {
                     }
                     Err(err) => self.last_error = Some(err),
                 }
-                self.loading = false;
-                true
-            }
-            Msg::TopEntitiesLoadResult(res) => {
-                match res {
+                match top_entities {
                     Ok(data) => {
                         self.last_top_entities_error = None;
                         self.top_entities = Some(data);
                     }
                     Err(err) => self.last_top_entities_error = Some(err),
                 }
+                self.loading = false;
                 true
             }
             Msg::RemoteListChanged(remote_list) => {
-- 
2.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-06-06  7:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-06  7:27 [pdm-devel] [PATCH datacenter-manager/yew-widget-toolkit-assets 0/5] make dashboard refreshable Dominik Csapak
2025-06-06  7:27 ` [pdm-devel] [PATCH yew-widget-toolkit-assets 1/1] content-spacer: add helper class for color Dominik Csapak
2025-06-06  7:27 ` [pdm-devel] [PATCH datacenter-manager 1/4] ui: dashboard: use builtin types and methods for styles Dominik Csapak
2025-06-06  7:27 ` [pdm-devel] [PATCH datacenter-manager 2/4] ui: dashboard: introduce DashboardConfig Dominik Csapak
2025-06-06  7:27 ` Dominik Csapak [this message]
2025-06-06  7:27 ` [pdm-devel] [PATCH datacenter-manager 4/4] ui: dashboard: add status row and configuration window Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250606072720.664054-5-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal