]> git.proxmox.com Git - pmg-api.git/commitdiff
fix #1976: optionally sort postfix queue result
authorStoiko Ivanov <s.ivanov@proxmox.com>
Thu, 28 May 2020 08:04:58 +0000 (10:04 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 4 Jun 2020 06:34:43 +0000 (08:34 +0200)
The PostfixMailQueue widget uses an Ext.data.BufferedStore, due to
the potential size of the resultset, which does only support remoteSorting [0]

By adding two optional parameters ('sortfield' and 'sortdir') we can use
them for sorting the mailq output accordingly.

The sorting is kept in PMG::API2::Postfix instead of PMG::Postfix, because
sorting (as opposed to filtering) needs to happen after the complete result
is known, and there is no gain in pushing it further down.

[0] only mentioned in the source-code - not in the referencedoc

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
src/PMG/API2/Postfix.pm

index 0866c0eefb4e3441581483614114295cdf170af9..ba226373804e0160baf38c8a945caf60f1124a09 100644 (file)
@@ -161,6 +161,19 @@ __PACKAGE__->register_method ({
                maxLength => 64,
                optional => 1,
            },
+           sortfield => {
+               description => "Sort field.",
+               type => 'string',
+               optional => 1,
+               enum => ['arrival_time', 'message_size', 'sender', 'receiver', 'reason'],
+           },
+           sortdir => {
+               description => "Sort direction.",
+               type => 'string',
+               optional => 1,
+               enum => ['ASC', 'DESC'],
+               requires => 'sortfield',
+           },
        },
     },
     returns => {
@@ -181,6 +194,21 @@ __PACKAGE__->register_method ({
 
        $restenv->set_result_attrib('total', $count);
 
+       my $sortfield = $param->{sortfield};
+       if (defined($sortfield)) {
+           my $sort_func = sub {
+               my ($c, $d) = ($param->{sortdir} eq 'DESC') ? ($b, $a) : ($a, $b);
+               if ($sortfield eq 'message_size' || $sortfield eq 'arrival_time') {
+                   return $c->{$sortfield} <=> $d->{$sortfield};
+               } else {
+                   return $c->{$sortfield} cmp $d->{$sortfield};
+               }
+           };
+
+           $res = [ sort $sort_func @$res ] ;
+       }
+
+
        return $res;
     }});