Skip to content

Commit

Permalink
Merge pull request #5006 from os-autoinst/k/workers_pagination
Browse files Browse the repository at this point in the history
Add pagination for GET /api/v1/workers
  • Loading branch information
mergify[bot] authored Feb 17, 2023
2 parents 28357ed + dde33e0 commit f6075b0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/OpenQA/WebAPI/Controller/API/V1/Worker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-2.0-or-later

package OpenQA::WebAPI::Controller::API::V1::Worker;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Base 'Mojolicious::Controller', -signatures;

use OpenQA::App;
use OpenQA::Log 'log_warning';
Expand Down Expand Up @@ -42,21 +42,30 @@ websocket status.
=cut

sub list {
my ($self) = @_;
my $limits = OpenQA::App->singleton->config->{misc_limits};
my $limit = min($limits->{generic_max_limit}, $self->param('limit') // $limits->{generic_default_limit});
sub list ($self) {
my $validation = $self->validation;
$validation->optional('limit')->num;
$validation->optional('live')->num(1);
$validation->optional('offset')->num;
return $self->reply->validation_error({format => 'json'}) if $validation->has_error;

my $limits = OpenQA::App->singleton->config->{misc_limits};
my $limit = min($limits->{generic_max_limit}, $validation->param('limit') // $limits->{generic_default_limit});
my $offset = $validation->param('offset') // 0;
my $live = $validation->param('live');
my $workers = $self->schema->resultset("Workers")->search({}, {rows => $limit});
my $ret = [];

while (my $w = $workers->next) {
next unless ($w->id);
push(@$ret, $w->info($live));
my @all = $self->schema->resultset('Workers')->search({}, {rows => $limit + 1, offset => $offset})->all;

# Pagination
pop @all if my $has_more = @all > $limit;
$self->pagination_links_header($limit, $offset, $has_more);

my $ret = [];
for my $worker (@all) {
next unless $worker->id;
push @$ret, $worker->info($live);
}

$self->render(json => {workers => $ret});
}

Expand Down
45 changes: 45 additions & 0 deletions t/api/01-workers.t
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,51 @@ subtest 'server-side limit has precedence over user-specified limit' => sub {
is ref $workers, 'ARRAY', 'data returned (3)' and is scalar @$workers, 2, 'default limit for workers is effective';
};

subtest 'server-side limit with pagination' => sub {
subtest 'input validation' => sub {
$t->get_ok('/api/v1/workers?limit=a')->status_is(400)
->json_is({error_status => 400, error => 'Erroneous parameters (limit invalid)'});
$t->get_ok('/api/v1/workers?offset=a')->status_is(400)
->json_is({error_status => 400, error => 'Erroneous parameters (offset invalid)'});
};

subtest 'navigation with limit' => sub {
$t->get_ok('/api/v1/workers?limit=3')->status_is(200)->json_has('/workers/0')->json_has('/workers/2')
->json_hasnt('/workers/3');
my $links = $t->tx->res->headers->links;
ok $links->{first}, 'has first page';
ok $links->{next}, 'has next page';
ok !$links->{prev}, 'no previous page';

$t->get_ok($links->{next}{link})->status_is(200)->json_has('/workers/0')->json_has('/workers/2')
->json_hasnt('/workers/3');
$links = $t->tx->res->headers->links;
ok $links->{first}, 'has first page';
ok $links->{next}, 'has next page';
ok $links->{prev}, 'has previous page';

$t->get_ok($links->{next}{link})->status_is(200)->json_has('/workers/0')->json_hasnt('/workers/1');
$links = $t->tx->res->headers->links;
ok $links->{first}, 'has first page';
ok !$links->{next}, 'no next page';
ok $links->{prev}, 'has previous page';

$t->get_ok($links->{prev}{link})->status_is(200)->status_is(200)->json_has('/workers/0')
->json_has('/workers/2')->json_hasnt('/workers/3');
$links = $t->tx->res->headers->links;
ok $links->{first}, 'has first page';
ok $links->{next}, 'has next page';
ok $links->{prev}, 'has previous page';

$t->get_ok($links->{first}{link})->status_is(200)->status_is(200)->json_has('/workers/0')
->json_has('/workers/2')->json_hasnt('/workers/3');
$links = $t->tx->res->headers->links;
ok $links->{first}, 'has first page';
ok $links->{next}, 'has next page';
ok !$links->{prev}, 'no previous page';
};
};

subtest 'delete offline worker' => sub {
my $offline_worker_id = 9;
$workers->create(
Expand Down

0 comments on commit f6075b0

Please sign in to comment.