From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <yew-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 44D3F1FF164
	for <inbox@lore.proxmox.com>; Fri,  9 May 2025 09:16:43 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 4CF4137095;
	Fri,  9 May 2025 09:17:02 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Date: Fri,  9 May 2025 09:16:27 +0200
Message-Id: <20250509071627.331064-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.021 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [tasks.rs]
Subject: [yew-devel] [PATCH yew-comp] fix #6382: tasks: don't add duplicate
 tasks into the list
X-BeenThere: yew-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Yew framework devel list at Proxmox <yew-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/yew-devel/>
List-Post: <mailto:yew-devel@lists.proxmox.com>
List-Help: <mailto:yew-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Yew framework devel list at Proxmox <yew-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: yew-devel-bounces@lists.proxmox.com
Sender: "yew-devel" <yew-devel-bounces@lists.proxmox.com>

When loading tasks due to reaching the end of the list, the amount of
existing tasks was used as the start parameter for the next load.

New tasks are added at the beginning of the list, so if new tasks were
added in the meantime, the API call would return tasks we already had
in the list. The UPID of the tasks are getting used as keys for the
store to uniquely identify the records. Since those get also used as yew
keys for the vdom by the data table and duplicate keys are not supported
in yew (can lead to a crash) we must avoid this.

To fix this, instead of using the amount of tasks as offset, use the
last task's start time as new 'until' value. With this we know there will
be overlap, but only the tasks started the same second. We can skip them
by finding the cutoff in the newly returned data which is the index of
the UPID of the last one of the current store content.

This makes the 'start' parameter of the LoadBatch update unnecessary,
and we replace it with a 'fresh load' flag that get's reset after every
load, and only gets used by the refesh button and when filters change.
(like we used 'start == 0' before)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tasks.rs | 51 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 10 deletions(-)

diff --git a/src/tasks.rs b/src/tasks.rs
index 67fa7b1..3bde629 100644
--- a/src/tasks.rs
+++ b/src/tasks.rs
@@ -93,7 +93,8 @@ pub enum ViewDialog {
 pub enum Msg {
     Redraw,
     ToggleFilter,
-    LoadBatch(u64), // start
+    LoadBatch(bool), // fresh load
+    LoadFinished,
     UpdateFilter,
     ShowTask,
 }
@@ -103,7 +104,7 @@ pub struct ProxmoxTasks {
     show_filter: PersistentState<bool>,
     filter_form_context: FormContext,
     row_render_callback: DataTableRowRenderCallback<TaskListItem>,
-    start: u64,
+    fresh_load: bool,
     last_filter: serde_json::Value,
     load_timeout: Option<Timeout>,
     columns: Rc<Vec<DataTableHeader<TaskListItem>>>,
@@ -172,7 +173,7 @@ impl LoadableComponent for ProxmoxTasks {
             let link = link.clone();
             move |args: &mut _| {
                 if args.row_index() > store.data_len().saturating_sub(LOAD_BUFFER_ROWS) {
-                    link.send_message(Msg::LoadBatch(store.data_len() as u64));
+                    link.send_message(Msg::LoadBatch(false));
                 }
                 let record: &TaskListItem = args.record();
                 match record.status.as_deref() {
@@ -192,7 +193,7 @@ impl LoadableComponent for ProxmoxTasks {
             filter_form_context,
             row_render_callback,
             last_filter: serde_json::Value::Object(Map::new()),
-            start: 0,
+            fresh_load: true,
             load_timeout: None,
             columns: Self::columns(ctx),
         }
@@ -233,17 +234,43 @@ impl LoadableComponent for ProxmoxTasks {
             filter["until"] = until.into();
         }
 
-        let start = self.start;
-        filter["start"] = start.into();
+        let start = match (self.fresh_load, self.store.read().last()) {
+            (false, Some(last)) => last.starttime,
+            _ => 0,
+        };
+        if start > 0 && filter["until"].as_i64().unwrap_or(i64::MAX) > start {
+            filter["until"] = start.into();
+        }
         filter["limit"] = BATCH_LIMIT.into();
 
+        let link = ctx.link().clone();
         Box::pin(async move {
             let mut data: Vec<_> = crate::http_get(&path, Some(filter)).await?;
             if start == 0 {
                 store.write().set_data(data);
             } else {
-                store.write().append(&mut data);
+                // since we used the starttime from the last existing element,
+                // we know there should be an overlap, so find it
+                let mut store = store.write();
+                if let Some(last) = store.last() {
+                    let mut cutoff = None;
+                    for (idx, new) in data.iter().enumerate() {
+                        if new.upid == last.upid {
+                            cutoff = Some(idx);
+                            break;
+                        }
+                        if new.starttime < last.starttime {
+                            // we did not find the last element, append all
+                            break;
+                        }
+                    }
+                    if let Some(cutoff) = cutoff {
+                        data.drain(0..=cutoff);
+                    }
+                }
+                store.append(&mut data);
             }
+            link.send_message(Msg::LoadFinished);
             Ok(())
         })
     }
@@ -266,7 +293,7 @@ impl LoadableComponent for ProxmoxTasks {
                 }
 
                 self.last_filter = filter_params;
-                self.start = 0;
+                self.fresh_load = true;
 
                 let link = ctx.link().clone();
                 self.load_timeout = Some(Timeout::new(FILTER_UPDATE_BUFFER_MS, move || {
@@ -275,13 +302,17 @@ impl LoadableComponent for ProxmoxTasks {
                 true
             }
             Msg::LoadBatch(start) => {
-                self.start = start;
+                self.fresh_load = start;
                 let link = ctx.link().clone();
                 self.load_timeout = Some(Timeout::new(FILTER_UPDATE_BUFFER_MS, move || {
                     link.send_reload();
                 }));
                 false
             }
+            Msg::LoadFinished => {
+                self.fresh_load = false;
+                false
+            }
             Msg::ShowTask => {
                 if let Some(on_show_task) = &ctx.props().on_show_task {
                     let selected_item = self
@@ -339,7 +370,7 @@ impl LoadableComponent for ProxmoxTasks {
             .with_child({
                 let loading = ctx.loading();
                 let link = ctx.link();
-                Button::refresh(loading).onclick(move |_| link.send_message(Msg::LoadBatch(0)))
+                Button::refresh(loading).onclick(move |_| link.send_message(Msg::LoadBatch(true)))
             });
 
         let filter_classes = classes!(
-- 
2.39.5



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