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 B320F1FF164
	for <inbox@lore.proxmox.com>; Fri, 14 Feb 2025 14:08:14 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id AE73419F7F;
	Fri, 14 Feb 2025 14:08:13 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Date: Fri, 14 Feb 2025 14:06:44 +0100
Message-Id: <20250214130653.283012-20-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250214130653.283012-1-l.wagner@proxmox.com>
References: <20250214130653.283012-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.140 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
 POISEN_SPAM_PILL          0.1 Meta: its spam
 POISEN_SPAM_PILL_1        0.1 random spam to be learned in bayes
 POISEN_SPAM_PILL_3        0.1 random spam to be learned in bayes
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pdm-devel] [PATCH proxmox-datacenter-manager v2 19/28] api: add
 endpoint for updating metric collection settings
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 one lives under /config/metric-collection.
At the moment, we do not offer full CRUD, but only offer a hard-coded
collection settings instance at /config/metric-collection/default, which
can be retrieved via GET and updated via PUT.

Later, we might update this to full CRUD, e.g. to have different
settings for different 'poll-groups' consisting of a sub-set of remotes.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 server/src/api/config/metric_collection.rs | 156 +++++++++++++++++++++
 server/src/api/config/mod.rs               |   2 +
 2 files changed, 158 insertions(+)
 create mode 100644 server/src/api/config/metric_collection.rs

diff --git a/server/src/api/config/metric_collection.rs b/server/src/api/config/metric_collection.rs
new file mode 100644
index 00000000..0915179d
--- /dev/null
+++ b/server/src/api/config/metric_collection.rs
@@ -0,0 +1,156 @@
+use anyhow::{bail, Error};
+
+use proxmox_config_digest::ConfigDigest;
+use proxmox_router::{list_subdirs_api_method, Router, RpcEnvironment, SubdirMap};
+use proxmox_schema::api;
+use proxmox_sortable_macro::sortable;
+
+use pdm_api_types::{
+    CollectionSettings, CollectionSettingsUpdater, DeletableCollectionSettingsProperty,
+};
+use pdm_config::metric_collection::COLLECTION_SETTINGS_TYPE;
+
+#[sortable]
+const SUBDIRS: SubdirMap = &sorted!([("default", &DEFAULT_ROUTER),]);
+
+pub const ROUTER: Router = Router::new()
+    .get(&list_subdirs_api_method!(SUBDIRS))
+    .subdirs(SUBDIRS);
+
+const DEFAULT_ROUTER: Router = Router::new()
+    .get(&API_METHOD_GET_SETTINGS)
+    .put(&API_METHOD_UPDATE_SETTINGS);
+
+#[api]
+/// Get metric collection settings.
+pub fn get_settings(rpcenv: &mut dyn RpcEnvironment) -> Result<CollectionSettings, Error> {
+    let (config, digest) = pdm_config::metric_collection::config()?;
+    rpcenv["digest"] = digest.to_hex().into();
+
+    if config.sections.contains_key("default") {
+        config.lookup(COLLECTION_SETTINGS_TYPE, "default")
+    } else {
+        Ok(CollectionSettings::new("default"))
+    }
+}
+
+#[api(
+    input: {
+        properties: {
+            updater: {
+                type: CollectionSettingsUpdater,
+            },
+            delete: {
+                type: Array,
+                items: {
+                    type: DeletableCollectionSettingsProperty,
+                },
+                description: "Array of properties that should be deleted.",
+                optional: true,
+            },
+            digest: {
+                type: ConfigDigest,
+                optional: true,
+            },
+        },
+    },
+)]
+/// Update metric collection settings.
+pub fn update_settings(
+    updater: CollectionSettingsUpdater,
+    delete: Option<Vec<DeletableCollectionSettingsProperty>>,
+    digest: Option<ConfigDigest>,
+) -> Result<(), Error> {
+    let (mut config, existing_digest) = pdm_config::metric_collection::config()?;
+    if digest.is_some() {
+        existing_digest.detect_modification(digest.as_ref())?;
+    }
+
+    let mut entry = if config.sections.contains_key("default") {
+        config.lookup(COLLECTION_SETTINGS_TYPE, "default")?
+    } else {
+        CollectionSettings::new("default")
+    };
+
+    let mut interval_offset_changed = false;
+    let mut connection_delay_changed = false;
+
+    if let Some(delete) = delete {
+        for property in delete {
+            use DeletableCollectionSettingsProperty::*;
+
+            match property {
+                MaxIntervalOffset => {
+                    entry.max_interval_offset = None;
+                    interval_offset_changed = true;
+                }
+                MinIntervalOffset => {
+                    entry.min_interval_offset = None;
+                    interval_offset_changed = true;
+                }
+                MaxConnectionDelay => {
+                    entry.max_connection_delay = None;
+                    connection_delay_changed = true;
+                }
+                MinConnectionDelay => {
+                    entry.min_connection_delay = None;
+                    connection_delay_changed = true;
+                }
+                MaxConcurrentConnections => entry.max_concurrent_connections = None,
+                CollectionInterval => entry.collection_interval = None,
+            }
+        }
+    }
+
+    let CollectionSettingsUpdater {
+        collection_interval,
+        max_concurrent_connections,
+        max_interval_offset,
+        min_interval_offset,
+        max_connection_delay,
+        min_connection_delay,
+    } = updater;
+
+    if let Some(value) = collection_interval {
+        entry.collection_interval = Some(value);
+    }
+    if let Some(value) = max_concurrent_connections {
+        entry.max_concurrent_connections = Some(value);
+    }
+    if let Some(value) = max_interval_offset {
+        entry.max_interval_offset = Some(value);
+        interval_offset_changed = true;
+    }
+    if let Some(value) = min_interval_offset {
+        entry.min_interval_offset = Some(value);
+        interval_offset_changed = true;
+    }
+    if let Some(value) = max_connection_delay {
+        entry.max_connection_delay = Some(value);
+        connection_delay_changed = true;
+    }
+    if let Some(value) = min_connection_delay {
+        entry.min_connection_delay = Some(value);
+        connection_delay_changed = true;
+    }
+
+    // Ensure that the minimum value is <= the maximum value - but only if we have
+    // actually changed the value. We do not want to fail if the config already contains
+    // something invalid and we are changing *other* settings.
+    if entry.min_interval_offset_or_default() > entry.max_interval_offset_or_default()
+        && interval_offset_changed
+    {
+        bail!("min-interval-offset must not be larger than max-interval-offset");
+    }
+
+    if (entry.min_connection_delay_or_default() > entry.max_connection_delay_or_default())
+        && connection_delay_changed
+    {
+        bail!("min-connection-delay must not be larger than max-connection-delay");
+    }
+
+    config.set_data("default", COLLECTION_SETTINGS_TYPE, entry)?;
+    pdm_config::metric_collection::save_config(&config)?;
+
+    Ok(())
+}
diff --git a/server/src/api/config/mod.rs b/server/src/api/config/mod.rs
index 7b58c756..15dc42c8 100644
--- a/server/src/api/config/mod.rs
+++ b/server/src/api/config/mod.rs
@@ -5,6 +5,7 @@ use proxmox_sortable_macro::sortable;
 pub mod access;
 pub mod acme;
 pub mod certificate;
+pub mod metric_collection;
 pub mod notes;
 
 #[sortable]
@@ -12,6 +13,7 @@ const SUBDIRS: SubdirMap = &sorted!([
     ("access", &access::ROUTER),
     ("acme", &acme::ROUTER),
     ("certificate", &certificate::ROUTER),
+    ("metric-collection", &metric_collection::ROUTER),
     ("notes", &notes::ROUTER),
 ]);
 
-- 
2.39.5



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