From: Lukas Wagner <l.wagner@proxmox.com>
To: Wolfgang Bumiller <w.bumiller@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox-backup 02/10] notifications: add type for GC notification template data
Date: Mon, 24 Mar 2025 09:32:58 +0100 [thread overview]
Message-ID: <747888cd-71f3-4d8d-b223-14654e3f6681@proxmox.com> (raw)
In-Reply-To: <mypjw222ja54377hq5mewvloov4j4bfll34c73cl6h6ednejzv@pirmd3mjqtfp>
On 2025-03-21 14:28, Wolfgang Bumiller wrote:
> On Fri, Mar 21, 2025 at 01:25:13PM +0100, Lukas Wagner wrote:
>> This commit adds a separate type for the data passed to this type of
>> notification template. Also we make sure that we do not expose any
>> non-primitive types to the template renderer, any data
>> needed in the template is mapped into the new dedicated
>> template data type.
>> This ensures that any changes in types defined in other places
>> do not leak into the template rendering process by accident.
>>
>> This commit also tries to unify the style and naming of template
>> variables.
>>
>> Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
>> ---
>> src/server/notifications/mod.rs | 50 ++++----
>> src/server/notifications/template_data.rs | 132 ++++++++++++++++++++++
>> templates/default/gc-err-body.txt.hbs | 2 +-
>> templates/default/gc-err-subject.txt.hbs | 2 +-
>> templates/default/gc-ok-body.txt.hbs | 22 ++--
>> templates/default/gc-ok-subject.txt.hbs | 2 +-
>> 6 files changed, 170 insertions(+), 40 deletions(-)
>> create mode 100644 src/server/notifications/template_data.rs
>>
>> diff --git a/src/server/notifications/mod.rs b/src/server/notifications/mod.rs
>> index eea55202..182af213 100644
>> --- a/src/server/notifications/mod.rs
>> +++ b/src/server/notifications/mod.rs
>> @@ -21,6 +21,10 @@ use proxmox_notify::{Endpoint, Notification, Severity};
>>
>> const SPOOL_DIR: &str = concatcp!(pbs_buildcfg::PROXMOX_BACKUP_STATE_DIR, "/notifications");
>>
>> +mod template_data;
>> +
>> +use template_data::{GcErrTemplateData, GcOkTemplateData};
>> +
>> /// Initialize the notification system by setting context in proxmox_notify
>> pub fn init() -> Result<(), Error> {
>> proxmox_notify::context::set_context(&PBS_CONTEXT);
>> @@ -146,38 +150,32 @@ pub fn send_gc_status(
>> status: &GarbageCollectionStatus,
>> result: &Result<(), Error>,
>> ) -> Result<(), Error> {
>> - let (fqdn, port) = get_server_url();
>> - let mut data = json!({
>> - "datastore": datastore,
>> - "fqdn": fqdn,
>> - "port": port,
>> - });
>> -
>> - let (severity, template) = match result {
>> - Ok(()) => {
>> - let deduplication_factor = if status.disk_bytes > 0 {
>> - (status.index_data_bytes as f64) / (status.disk_bytes as f64)
>> - } else {
>> - 1.0
>> - };
>> -
>> - data["status"] = json!(status);
>> - data["deduplication-factor"] = format!("{:.2}", deduplication_factor).into();
>> -
>> - (Severity::Info, "gc-ok")
>> - }
>> - Err(err) => {
>> - data["error"] = err.to_string().into();
>> - (Severity::Error, "gc-err")
>> - }
>> - };
>> let metadata = HashMap::from([
>> ("datastore".into(), datastore.into()),
>> ("hostname".into(), proxmox_sys::nodename().into()),
>> ("type".into(), "gc".into()),
>> ]);
>>
>> - let notification = Notification::from_template(severity, template, data, metadata);
>> + let notification = match result {
>> + Ok(()) => {
>> + let template_data = GcOkTemplateData::new(datastore.to_string(), status);
>> + Notification::from_template(
>> + Severity::Info,
>> + "gc-ok",
>> + serde_json::to_value(template_data)?,
>> + metadata,
>> + )
>> + }
>> + Err(err) => {
>> + let template_data = GcErrTemplateData::new(datastore.to_string(), err.to_string());
>
> ^ While at it, we could consider switching this error to use
> `format!{"{err:#}")` which will include the context as a short form.
> This goes for all the templates/commits - but can just be a follow up.
> Noticing this since Chris got me looking at anyhow's display
> representations[1]
>
> [1] https://docs.rs/anyhow/latest/anyhow/struct.Error.html#display-representations
>
Good point, I'll check it out. Will do that in a followup if nothing else comes up.
Thanks!
--
- Lukas
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2025-03-24 8:33 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-21 12:25 [pbs-devel] [PATCH proxmox-backup 00/10] notifications: cleanup in preparation of overridable templates Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 01/10] notifications: move make notifications module a dir-style module Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 02/10] notifications: add type for GC notification template data Lukas Wagner
2025-03-21 13:28 ` Wolfgang Bumiller
2025-03-24 8:32 ` Lukas Wagner [this message]
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 03/10] notifications: add type for ACME " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 04/10] notifications: add type for APT " Lukas Wagner
2025-03-24 10:39 ` Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 05/10] notifications: add type for prune " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 06/10] notifications: add type for sync " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 07/10] notifications: add type for tape backup " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 08/10] notifications: add type for tape load " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 09/10] notifications: add type for verify " Lukas Wagner
2025-03-21 12:25 ` [pbs-devel] [PATCH proxmox-backup 10/10] notifications: remove HTML template for test notification Lukas Wagner
2025-03-21 12:38 ` [pbs-devel] [PATCH proxmox-backup 00/10] notifications: cleanup in preparation of overridable templates Maximiliano Sandoval
2025-03-27 14:57 ` [pbs-devel] superseded: " Lukas Wagner
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=747888cd-71f3-4d8d-b223-14654e3f6681@proxmox.com \
--to=l.wagner@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
--cc=w.bumiller@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.