Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 32 / Add pagination to list commands #72

Merged
merged 8 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions conf/reflect-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,24 @@
"allDeclaredMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.seqera.tower.cli.commands.global.PaginationOptions",
"allDeclaredFields":true,
"allDeclaredMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.seqera.tower.cli.commands.global.PaginationOptions$Pageable",
"allDeclaredFields":true,
"allDeclaredMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.seqera.tower.cli.commands.global.PaginationOptions$Sizeable",
"allDeclaredFields":true,
"allDeclaredMethods":true,
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"io.seqera.tower.cli.commands.members.AbstractMembersClass",
"allDeclaredFields":true,
Expand Down Expand Up @@ -2617,16 +2635,16 @@
{
"name":"org.glassfish.jersey.internal.util.collection.ConcurrentHashMapV8",
"fields":[
{"name":"baseCount", "allowUnsafeAccess":true},
{"name":"cellsBusy", "allowUnsafeAccess":true},
{"name":"sizeCtl", "allowUnsafeAccess":true},
{"name":"transferIndex", "allowUnsafeAccess":true},
{"name":"transferOrigin", "allowUnsafeAccess":true}
{"name":"baseCount"},
{"name":"cellsBusy"},
{"name":"sizeCtl"},
{"name":"transferIndex"},
{"name":"transferOrigin"}
]
},
{
"name":"org.glassfish.jersey.internal.util.collection.ConcurrentHashMapV8$CounterCell",
"fields":[{"name":"value", "allowUnsafeAccess":true}]
"fields":[{"name":"value"}]
},
{
"name":"org.glassfish.jersey.jackson.JacksonFeature",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.seqera.tower.cli.commands.global;

import io.seqera.tower.cli.exceptions.TowerException;
import picocli.CommandLine;

public class PaginationOptions {

public static final int MAX = 100;

// TODO: The use of the validate option is a work around to an existing Picocli issue. Please refer to https://github.com/seqeralabs/tower-cli/pull/72#issuecomment-952588876
@CommandLine.ArgGroup(validate = false)
public Pageable pageable;

// TODO: The use of the validate option is a work around to an existing Picocli issue. Please refer to https://github.com/seqeralabs/tower-cli/pull/72#issuecomment-952588876
@CommandLine.ArgGroup(validate = false)
public Sizeable sizeable;

public static class Pageable {
@CommandLine.Option(names = {"--page"}, description = "Page to display to display (default is 1)")
public Integer page;

@CommandLine.Option(names = {"--offset"}, description = "Rows record's offset (default is 0)")
grananda marked this conversation as resolved.
Show resolved Hide resolved
public Integer offset;
}

public static class Sizeable {
@CommandLine.Option(names = {"--max"}, description = "Maximum number of records to display (default is " + MAX + ")")
public Integer max;

@CommandLine.Option(names = {"--no-max"}, description = "Show all records")
public Boolean noMax = false;
}

public static Integer getMax(PaginationOptions paginationOptions) {
Integer max = PaginationOptions.MAX;

if (paginationOptions.sizeable != null) {
max = paginationOptions.sizeable.noMax ? null : paginationOptions.sizeable.max != null ? paginationOptions.sizeable.max : max;
}

return max;
}

public static Integer getOffset(PaginationOptions paginationOptions, Integer max) throws TowerException {
int offset = 0;

if (paginationOptions.pageable != null) {

offset = max == null ? offset : paginationOptions.pageable.offset != null ? paginationOptions.pageable.offset : offset;

if (offset < 0) {
throw new TowerException("Record offset number must be a positive value.");
}

if (max != null && paginationOptions.pageable.page != null) {

if (paginationOptions.pageable.page < 1) {
throw new TowerException("Page number must be greater than zero.");
}

offset = (paginationOptions.pageable.page - 1) * max;
}
}

return offset;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import io.seqera.tower.ApiException;
import io.seqera.tower.cli.commands.global.PaginationOptions;
import io.seqera.tower.cli.responses.Response;
import io.seqera.tower.cli.responses.members.MembersList;
import io.seqera.tower.model.ListMembersResponse;
Expand All @@ -21,11 +22,17 @@ public class ListCmd extends AbstractMembersClass {
@CommandLine.Option(names = {"-f", "--filter"}, description = "Show only members with usernames that starts with the given word")
public String startsWith;

@CommandLine.Mixin
PaginationOptions paginationOptions;

@Override
protected Response exec() throws ApiException, IOException {
Integer max = PaginationOptions.getMax(paginationOptions);
Integer offset = PaginationOptions.getOffset(paginationOptions, max);

OrgAndWorkspaceDbDto orgAndWorkspaceDbDto = findOrganizationByName(organizationName);

ListMembersResponse response = api().listOrganizationMembers(orgAndWorkspaceDbDto.getOrgId(), null, null, startsWith);
ListMembersResponse response = api().listOrganizationMembers(orgAndWorkspaceDbDto.getOrgId(), max, offset, startsWith);

return new MembersList(organizationName, response.getMembers());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.seqera.tower.cli.commands.participants;

import io.seqera.tower.ApiException;
import io.seqera.tower.cli.commands.global.PaginationOptions;
import io.seqera.tower.cli.responses.Response;
import io.seqera.tower.cli.responses.participants.ParticipantsList;
import io.seqera.tower.model.OrgAndWorkspaceDbDto;
Expand All @@ -24,9 +25,15 @@ public class ListCmd extends AbstractParticipantsCmd {
@CommandLine.Option(names = {"-f", "--filter"}, description = "Show only participants that it's name starts with the given word")
public String startsWith;

@CommandLine.Mixin
PaginationOptions paginationOptions;

@Override
protected Response exec() throws ApiException, IOException {
List<ParticipantDbDto> response = api().listWorkspaceParticipants(orgId(), workspaceId(), null, null, startsWith).getParticipants();
Integer max = PaginationOptions.getMax(paginationOptions);
Integer offset = PaginationOptions.getOffset(paginationOptions, max);

List<ParticipantDbDto> response = api().listWorkspaceParticipants(orgId(), workspaceId(), max, offset, startsWith).getParticipants();

if (response != null && type != null) {
response = response.stream().filter(it -> it.getType() == type).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.seqera.tower.cli.commands.pipelines;

import io.seqera.tower.ApiException;
import io.seqera.tower.cli.commands.global.PaginationOptions;
import io.seqera.tower.cli.responses.PipelinesList;
import io.seqera.tower.cli.responses.Response;
import io.seqera.tower.model.ListPipelinesResponse;
Expand All @@ -18,9 +19,15 @@ public class ListCmd extends AbstractPipelinesCmd {
@CommandLine.Option(names = {"-f", "--filter"}, description = "Show only pipelines that contain the given word")
public String filter;

@CommandLine.Mixin
PaginationOptions paginationOptions;

@Override
protected Response exec() throws ApiException, IOException {
ListPipelinesResponse response = api().listPipelines(workspaceId(), null, null, filter);
Integer max = PaginationOptions.getMax(paginationOptions);
Integer offset = PaginationOptions.getOffset(paginationOptions, max);

ListPipelinesResponse response = api().listPipelines(workspaceId(), max, offset, filter);
return new PipelinesList(workspaceRef(), response.getPipelines());
}
}
9 changes: 8 additions & 1 deletion src/main/java/io/seqera/tower/cli/commands/runs/ListCmd.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.seqera.tower.cli.commands.runs;

import io.seqera.tower.ApiException;
import io.seqera.tower.cli.commands.global.PaginationOptions;
import io.seqera.tower.cli.responses.Response;
import io.seqera.tower.cli.responses.RunList;
import io.seqera.tower.model.ListWorkflowsResponse;
Expand All @@ -17,9 +18,15 @@ public class ListCmd extends AbstractRunsCmd {
@CommandLine.Option(names = {"-f", "--filter"}, description = "Show only pipeline's runs that it's name starts with the given word")
public String startsWith;

@CommandLine.Mixin
PaginationOptions paginationOptions;

@Override
protected Response exec() throws ApiException, IOException {
ListWorkflowsResponse response = api().listWorkflows(workspaceId(), null, null, startsWith);
Integer max = PaginationOptions.getMax(paginationOptions);
Integer offset = PaginationOptions.getOffset(paginationOptions, max);

ListWorkflowsResponse response = api().listWorkflows(workspaceId(), max, offset, startsWith);
return new RunList(workspaceRef(), response.getWorkflows());
}
}
13 changes: 10 additions & 3 deletions src/main/java/io/seqera/tower/cli/commands/teams/ListCmd.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.seqera.tower.cli.commands.teams;

import java.io.IOException;

import io.seqera.tower.ApiException;
import io.seqera.tower.cli.commands.global.PaginationOptions;
import io.seqera.tower.cli.responses.Response;
import io.seqera.tower.cli.responses.teams.TeamsList;
import io.seqera.tower.model.ListTeamResponse;
import io.seqera.tower.model.OrgAndWorkspaceDbDto;
import picocli.CommandLine;

import java.io.IOException;

@CommandLine.Command(
name = "list",
description = "List all the teams of a given organization"
Expand All @@ -18,11 +19,17 @@ public class ListCmd extends AbstractTeamsCmd {
@CommandLine.Option(names = {"-o", "--organization"}, description = "Organization's name identifier", required = true)
public String organizationName;

@CommandLine.Mixin
PaginationOptions paginationOptions;

@Override
protected Response exec() throws ApiException, IOException {
Integer max = PaginationOptions.getMax(paginationOptions);
Integer offset = PaginationOptions.getOffset(paginationOptions, max);

OrgAndWorkspaceDbDto orgAndWorkspaceDbDto = findOrganizationByName(organizationName);

ListTeamResponse teamResponse = api().listOrganizationTeams(orgAndWorkspaceDbDto.getOrgId(), null, null);
ListTeamResponse teamResponse = api().listOrganizationTeams(orgAndWorkspaceDbDto.getOrgId(), max, offset);

return new TeamsList(organizationName, teamResponse.getTeams());
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/io/seqera/tower/cli/PipelinesCmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,56 @@ void testList(MockServerClient mock) {
assertEquals(0, out.exitCode);
}

@Test
void testListWithOffset(MockServerClient mock) {

mock.when(
request().withMethod("GET").withPath("/pipelines")
.withQueryStringParameter("offset", "1")
.withQueryStringParameter("max", "2"), exactly(1)
).respond(
response().withStatusCode(200).withBody(loadResource("pipelines_list")).withContentType(MediaType.APPLICATION_JSON)
);

ExecOut out = exec(mock, "pipelines", "list", "--offset", "1", "--max", "2");

assertEquals("", out.stdErr);
assertEquals(chop(new PipelinesList(USER_WORKSPACE_NAME, List.of(
new PipelineDbDto()
.pipelineId(183522618315672L)
.name("sleep_one_minute")
.repository("https://github.com/pditommaso/nf-sleep")
.userId(4L)
.userName("jordi")
)).toString()), out.stdOut);
assertEquals(0, out.exitCode);
}

@Test
void testListWithPage(MockServerClient mock) {

mock.when(
request().withMethod("GET").withPath("/pipelines")
.withQueryStringParameter("offset", "0")
.withQueryStringParameter("max", "2"), exactly(1)
).respond(
response().withStatusCode(200).withBody(loadResource("pipelines_list")).withContentType(MediaType.APPLICATION_JSON)
);

ExecOut out = exec(mock, "pipelines", "list", "--page", "1", "--max", "2");

assertEquals("", out.stdErr);
assertEquals(chop(new PipelinesList(USER_WORKSPACE_NAME, List.of(
new PipelineDbDto()
.pipelineId(183522618315672L)
.name("sleep_one_minute")
.repository("https://github.com/pditommaso/nf-sleep")
.userId(4L)
.userName("jordi")
)).toString()), out.stdOut);
assertEquals(0, out.exitCode);
}

@Test
void testListEmpty(MockServerClient mock) {

Expand Down
Loading