]> git.proxmox.com Git - pmg-api.git/commitdiff
PMG::Postfix::mailq - implement paging
authorDietmar Maurer <dietmar@proxmox.com>
Sun, 2 Apr 2017 09:03:08 +0000 (11:03 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Sun, 2 Apr 2017 09:03:08 +0000 (11:03 +0200)
PMG/API2/Postfix.pm
PMG/Postfix.pm

index 9a0ce7a346e739b5220930c2c7e63711ee7bd103..8fdd5a104e6a0732f9631bf8f95ec1ed827e1663 100644 (file)
@@ -95,6 +95,16 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
+           start => {
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
+           limit => {
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
            filter => {
                description => "Filter string.",
                type => 'string',
@@ -113,7 +123,12 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $res = PMG::Postfix::mailq($param->{filter});
+       my $restenv = PVE::RESTEnvironment::get();
+
+       my ($count, $res) = PMG::Postfix::mailq(
+           $param->{filter}, $param->{start}, $param->{limit});
+
+       $restenv->set_result_attrib('total', $count);
 
        return $res;
     }});
index ca0d7efb7e0fa6e63f73b4307d6e273a95f14339..0463f55fbce8c1c13367260e76c1f444d1847a3d 100644 (file)
@@ -126,11 +126,15 @@ sub qshape {
 }
 
 sub mailq {
-    my ($filter, $limit) = @_;
+    my ($filter, $start, $limit) = @_;
 
     open(my $fh, '-|', '/usr/sbin/postqueue', '-j') || die "ERROR: unable to run postqueue - $!\n";
 
     my $count = 0;
+
+    $start = 0 if !$start;
+    $limit = 50 if !$limit;
+
     my $res = [];
     my $line;
     while (defined($line = <$fh>)) {
@@ -140,6 +144,9 @@ sub mailq {
        foreach my $entry (@$recipients) {
            if (!$filter || $entry->{address} =~ m/$filter/i ||
                $rec->{sender} =~ m/$filter/i) {
+               next if $count++ < $start;
+               next if $limit-- <= 0;
+
                my $data = {};
                foreach my $k (qw(queue_name queue_id arrival_time message_size sender)) {
                    $data->{$k} = $rec->{$k};
@@ -147,14 +154,13 @@ sub mailq {
                $data->{receiver} = $entry->{address};
                $data->{reason} = $entry->{delay_reason};
                push @$res, $data;
-               last if $limit && (++$count >= $limit);
            }
        }
     }
 
     close (CMD);
 
-    return $res;
+    return ($count, $res);
 }
 
 1;