From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pdm-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 2B5571FF15C for <inbox@lore.proxmox.com>; Wed, 19 Feb 2025 13:28:34 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E4854285BB; Wed, 19 Feb 2025 13:28:27 +0100 (CET) From: Dominik Csapak <d.csapak@proxmox.com> To: pdm-devel@lists.proxmox.com Date: Wed, 19 Feb 2025 13:28:20 +0100 Message-Id: <20250219122824.2043990-4-d.csapak@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250219122824.2043990-1-d.csapak@proxmox.com> References: <20250219122824.2043990-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.022 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 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. [lib.rs] Subject: [pdm-devel] [PATCH datacenter-manager v2 3/7] server: api: add remote-tasks statistics X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion <pdm-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/> List-Post: <mailto:pdm-devel@lists.proxmox.com> List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox Datacenter Manager development discussion <pdm-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" <pdm-devel-bounces@lists.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 | 55 +++++++++++++++++++++++ server/src/api/remote_tasks.rs | 82 +++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/lib/pdm-api-types/src/lib.rs b/lib/pdm-api-types/src/lib.rs index 3844907..47e5894 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}; @@ -232,6 +233,20 @@ pub enum TaskStateType { Unknown, } +impl From<&str> for TaskStateType { + fn from(s: &str) -> Self { + if s == "OK" { + TaskStateType::OK + } else if s.starts_with("WARNINGS: ") { + TaskStateType::Warning + } else if !s.is_empty() { + TaskStateType::Error + } else { + TaskStateType::Unknown + } + } +} + #[api( properties: { upid: { schema: UPID::API_SCHEMA }, @@ -263,6 +278,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 d327272..28def2a 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 @@ -50,3 +61,70 @@ 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: { + "max-age": { + type: Integer, + optional: true, + // TODO: sensible default max-age + default: 300, + description: "Maximum age of cached task data", + }, + filters: { + type: TaskFilters, + flatten: true, + }, + remote: { + schema: REMOTE_ID_SCHEMA, + optional: true, + }, + }, + }, +)] +/// Get task statistics for the specified filters. +async fn task_statistics( + max_age: i64, + filters: TaskFilters, + remote: Option<String>, +) -> Result<TaskStatistics, Error> { + let tasks = task_cache::get_tasks(max_age, 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) => status.into(), + 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.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel