[pmg-devel] [PATCH pmg-api v2] fix #2452: pmgqm support 1-24h timespans for status/send

Hannes Laimer h.laimer at proxmox.com
Fri Aug 8 12:28:36 CEST 2025


Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
I didn't find a section for this in the docs(maybe I missed it), if
there is one, and depending on what it says maybe we have to update that
as well. (I can send a followup in that case)

since v1:
 - update default templates
 - 1..24h instead of 1..23h (thought `today` would be 24h, but it's not)

 src/PMG/CLI/pmgqm.pm                |  8 ++++----
 src/PMG/Utils.pm                    | 10 ++++++++--
 src/templates/spamreport-short.tt   |  4 +++-
 src/templates/spamreport-verbose.tt |  4 +++-
 4 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/PMG/CLI/pmgqm.pm b/src/PMG/CLI/pmgqm.pm
index 23968f2..41f9f1a 100755
--- a/src/PMG/CLI/pmgqm.pm
+++ b/src/PMG/CLI/pmgqm.pm
@@ -90,9 +90,9 @@ __PACKAGE__->register_method({
         additionalProperties => 0,
         properties => {
             timespan => {
-                description => "Select time span.",
+                description => "Select time span. Accepts 'today', 'yesterday', 'week' or '1h'..'24h' for the last N hours.",
                 type => 'string',
-                enum => ['today', 'yesterday', 'week'],
+                pattern => '^(today|yesterday|week|([1-9]|1[0-9]|2[0-4])h)$',
                 default => 'today',
                 optional => 1,
             },
@@ -167,9 +167,9 @@ __PACKAGE__->register_method({
                 },
             ),
             timespan => {
-                description => "Select time span.",
+                description => "Select time span. Accepts 'today', 'yesterday', 'week' or '1h'..'24h' for the last N hours.",
                 type => 'string',
-                enum => ['today', 'yesterday', 'week'],
+                pattern => '^(today|yesterday|week|([1-9]|1[0-9]|2[0-4])h)$',
                 default => 'today',
                 optional => 1,
             },
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 890096f..ddfa732 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1392,13 +1392,19 @@ sub finalize_report {
 sub lookup_timespan {
     my ($timespan) = @_;
 
-    my (undef, undef, undef, $mday, $mon, $year) = localtime(time());
+    my $now = time();
+    my (undef, undef, undef, $mday, $mon, $year) = localtime($now);
     my $daystart = timelocal(0, 0, 0, $mday, $mon, $year);
 
     my $start;
     my $end;
 
-    if ($timespan eq 'today') {
+    if ($timespan =~ m/^(\d{1,2})h$/) {
+        my $hours = int($1);
+        die "invalid hour timespan '$timespan'\n" if $hours < 1 || $hours > 24;
+        $end = $now;
+        $start = $end - $hours * 3600;
+    } elsif ($timespan eq 'today') {
         $start = $daystart;
         $end = $start + 86400;
     } elsif ($timespan eq 'yesterday') {
diff --git a/src/templates/spamreport-short.tt b/src/templates/spamreport-short.tt
index 5c88389..53c6b15 100644
--- a/src/templates/spamreport-short.tt
+++ b/src/templates/spamreport-short.tt
@@ -1,6 +1,8 @@
 [%- IF timespan == 'week' -%]
 [%- SET title = "Weekly Spam Report for '${pmail}' - ${date}'" -%]
-[%- ELSE %]
+[%- ELSIF timespan.substr(timespan.length - 1) == 'h' -%]
+[%- SET title = "Spam Report (last ${timespan}) for '${pmail}' - ${date}" -%]
+[%- ELSE -%]
 [%- SET title = "Daily Spam Report for '${pmail}' - ${date}" -%]
 [%- END -%]
 <html>
diff --git a/src/templates/spamreport-verbose.tt b/src/templates/spamreport-verbose.tt
index 2b2915d..9b7032e 100644
--- a/src/templates/spamreport-verbose.tt
+++ b/src/templates/spamreport-verbose.tt
@@ -1,6 +1,8 @@
 [%- IF timespan == 'week' -%]
 [%- SET title = "Weekly Spam Report for '${pmail}' - ${date}'" -%]
-[%- ELSE %]
+[%- ELSIF timespan.substr(timespan.length - 1) == 'h' -%]
+[%- SET title = "Spam Report (last ${timespan}) for '${pmail}' - ${date}" -%]
+[%- ELSE -%]
 [%- SET title = "Daily Spam Report for '${pmail}' - ${date}" -%]
 [%- END -%]
 
-- 
2.47.2





More information about the pmg-devel mailing list