[pmg-devel] [PATCH pmg-api 1/1] fix #1976: optionally sort postfix queue result

Stoiko Ivanov s.ivanov at proxmox.com
Thu May 28 10:04:58 CEST 2020


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 at proxmox.com>
---
 src/PMG/API2/Postfix.pm | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/PMG/API2/Postfix.pm b/src/PMG/API2/Postfix.pm
index 0866c0e..ba22637 100644
--- a/src/PMG/API2/Postfix.pm
+++ b/src/PMG/API2/Postfix.pm
@@ -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;
     }});
 
-- 
2.20.1




More information about the pmg-devel mailing list