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 v3 3/8] server: api: add remote-tasks statistics
Date: Mon, 25 Aug 2025 10:08:39 +0200	[thread overview]
Message-ID: <20250825081042.797559-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250825081042.797559-1-d.csapak@proxmox.com>

this new api call returns task status counts by remote and by type, so
that the ui can display that without having to count these on the client
side.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 lib/pdm-api-types/src/lib.rs   | 41 +++++++++++++++++++
 server/src/api/remote_tasks.rs | 74 +++++++++++++++++++++++++++++++++-
 2 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/lib/pdm-api-types/src/lib.rs b/lib/pdm-api-types/src/lib.rs
index 37da134..c506544 100644
--- a/lib/pdm-api-types/src/lib.rs
+++ b/lib/pdm-api-types/src/lib.rs
@@ -1,5 +1,6 @@
 //! Basic API types used by most of the PDM code.
 
+use std::collections::HashMap;
 use std::fmt;
 
 use anyhow::{bail, Error};
@@ -280,6 +281,46 @@ pub struct TaskListItem {
     pub status: Option<String>,
 }
 
+#[api]
+/// Count of tasks by status
+#[derive(Clone, Serialize, Deserialize, Default, PartialEq)]
+#[serde(rename_all = "lowercase")]
+pub struct TaskCount {
+    /// The number of successful tasks
+    pub ok: u64,
+    /// The number of tasks with warnings
+    pub warning: u64,
+    /// The number of failed tasks
+    pub error: u64,
+    /// The number of tasks with an unknown status
+    pub unknown: u64,
+}
+
+#[api{
+    properties: {
+        "by-type": {
+            type: Object,
+            properties: {},
+            additional_properties: true,
+        },
+        "by-remote": {
+            type: Object,
+            properties: {},
+            additional_properties: true,
+        },
+    },
+}]
+/// Lists the task status counts by type and by remote
+#[derive(Clone, Serialize, Deserialize, PartialEq)]
+#[serde(rename_all = "kebab-case")]
+pub struct TaskStatistics {
+    /// A map of worker-types to status counts
+    pub by_type: HashMap<String, TaskCount>,
+    /// A map of remotes to status counts
+    #[serde(default)]
+    pub by_remote: HashMap<String, TaskCount>,
+}
+
 pub const NODE_TASKS_LIST_TASKS_RETURN_TYPE: ReturnType = ReturnType {
     optional: false,
     schema: &ArraySchema::new("A list of tasks.", &TaskListItem::API_SCHEMA).schema(),
diff --git a/server/src/api/remote_tasks.rs b/server/src/api/remote_tasks.rs
index db9fadf..7b97b9c 100644
--- a/server/src/api/remote_tasks.rs
+++ b/server/src/api/remote_tasks.rs
@@ -1,6 +1,11 @@
+use std::collections::HashMap;
+
 use anyhow::Error;
 
-use pdm_api_types::{remotes::REMOTE_ID_SCHEMA, TaskFilters, TaskListItem};
+use pdm_api_types::{
+    remotes::REMOTE_ID_SCHEMA, RemoteUpid, TaskCount, TaskFilters, TaskListItem, TaskStateType,
+    TaskStatistics,
+};
 use proxmox_router::{list_subdirs_api_method, Permission, Router, SubdirMap};
 use proxmox_schema::api;
 use proxmox_sortable_macro::sortable;
@@ -12,7 +17,13 @@ pub const ROUTER: Router = Router::new()
     .subdirs(SUBDIRS);
 
 #[sortable]
-const SUBDIRS: SubdirMap = &sorted!([("list", &Router::new().get(&API_METHOD_LIST_TASKS)),]);
+const SUBDIRS: SubdirMap = &sorted!([
+    ("list", &Router::new().get(&API_METHOD_LIST_TASKS)),
+    (
+        "statistics",
+        &Router::new().get(&API_METHOD_TASK_STATISTICS)
+    ),
+]);
 
 #[api(
     // FIXME:: see list-like API calls in resource routers, we probably want more fine-grained
@@ -42,3 +53,62 @@ async fn list_tasks(
 
     Ok(tasks)
 }
+
+#[api(
+    // FIXME:: see list-like API calls in resource routers, we probably want more fine-grained
+    // checks..
+    access: {
+        permission: &Permission::Anybody,
+    },
+    input: {
+        properties: {
+            filters: {
+                type: TaskFilters,
+                flatten: true,
+            },
+            remote: {
+                schema: REMOTE_ID_SCHEMA,
+                optional: true,
+            },
+        },
+    },
+)]
+/// Get task statistics for the specified filters.
+async fn task_statistics(
+    filters: TaskFilters,
+    remote: Option<String>,
+) -> Result<TaskStatistics, Error> {
+    let tasks = remote_tasks::get_tasks(filters, remote).await?;
+
+    let mut by_type: HashMap<String, TaskCount> = HashMap::new();
+    let mut by_remote: HashMap<String, TaskCount> = HashMap::new();
+
+    for task in tasks {
+        let status: TaskStateType = match task.status.as_deref() {
+            Some(status) => TaskStateType::new_from_str(status),
+            None => continue,
+        };
+        let entry = by_type.entry(task.worker_type).or_default();
+        match status {
+            TaskStateType::OK => entry.ok += 1,
+            TaskStateType::Warning => entry.warning += 1,
+            TaskStateType::Error => entry.error += 1,
+            TaskStateType::Unknown => entry.unknown += 1,
+        }
+
+        let remote = match task.upid.parse::<RemoteUpid>() {
+            Ok(upid) => upid.remote().to_owned(),
+            Err(_) => continue,
+        };
+
+        let entry = by_remote.entry(remote).or_default();
+        match status {
+            TaskStateType::OK => entry.ok += 1,
+            TaskStateType::Warning => entry.warning += 1,
+            TaskStateType::Error => entry.error += 1,
+            TaskStateType::Unknown => entry.unknown += 1,
+        }
+    }
+
+    Ok(TaskStatistics { by_type, by_remote })
+}
-- 
2.47.2



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


  parent reply	other threads:[~2025-08-25  8:10 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25  8:08 [pdm-devel] [PATCH datacenter-manager v3 0/8] add task summary panels in dashboard Dominik Csapak
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 1/8] server: task cache: treat a limit of 0 as unbounded Dominik Csapak
2025-08-25  9:55   ` Stefan Hanreich
2025-08-25 10:49     ` Dominik Csapak
2025-08-25 13:40       ` Stefan Hanreich
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 2/8] server: api: remote tasks: add 'remote' filter option Dominik Csapak
2025-08-25  8:08 ` Dominik Csapak [this message]
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 4/8] ui: refactor remote upid formatter Dominik Csapak
2025-08-25 10:56   ` Stefan Hanreich
2025-08-26  9:48     ` Dominik Csapak
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 5/8] ui: tasks: add helper to summarize task categories Dominik Csapak
2025-08-25  9:54   ` Stefan Hanreich
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 6/8] ui: add dialog to show filtered tasks Dominik Csapak
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 7/8] ui: dashboard: add task summaries Dominik Csapak
2025-08-25 10:52   ` Stefan Hanreich
2025-08-25  8:08 ` [pdm-devel] [PATCH datacenter-manager v3 8/8] ui: dashboard: make task summary time range configurable Dominik Csapak
2025-08-25  9:54   ` Stefan Hanreich
2025-08-25 13:17 ` [pdm-devel] [PATCH datacenter-manager v3 0/8] add task summary panels in dashboard Stefan Hanreich
2025-08-26 11:22 ` [pdm-devel] superseded: " 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=20250825081042.797559-4-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