diff options
9 files changed, 43 insertions, 104 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java index 69781bece76..1c3a6355655 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java @@ -37,8 +37,8 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.DateUtils; -import org.sonar.api.utils.Paging; import org.sonar.api.web.UserRole; +import org.sonar.ce.taskprocessor.CeTaskProcessor; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; @@ -50,10 +50,8 @@ import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDtoFunctions; import org.sonar.db.component.ComponentQuery; -import org.sonar.ce.taskprocessor.CeTaskProcessor; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.user.UserSession; -import org.sonarqube.ws.Common; import org.sonarqube.ws.WsCe; import org.sonarqube.ws.WsCe.ActivityResponse; import org.sonarqube.ws.client.ce.ActivityWsRequest; @@ -75,6 +73,7 @@ import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE; public class ActivityAction implements CeWsAction { + private static final int PAGE_INDEX = 1; private static final int MAX_PAGE_SIZE = 1000; private final UserSession userSession; @@ -98,7 +97,8 @@ public class ActivityAction implements CeWsAction { WebService.NewAction action = controller.createAction("activity") .setDescription(format("Search for tasks.<br> " + "Requires the system administration permission, " + - "or project administration permission if %s is set.", PARAM_COMPONENT_ID)) + "or project administration permission if %s is set.<br/>" + + "Since 5.5, it's no more posible to specify the page parameter", PARAM_COMPONENT_ID)) .setResponseExample(getClass().getResource("activity-example.json")) .setHandler(this) .setSince("5.2"); @@ -147,7 +147,11 @@ public class ActivityAction implements CeWsAction { action.createParam(PARAM_MAX_EXECUTED_AT) .setDescription("Maximum date of end of task processing (inclusive)") .setExampleValue(DateUtils.formatDateTime(new Date())); - action.addPagingParams(100, MAX_PAGE_SIZE); + action.createParam(Param.PAGE) + .setDescription("Deprecated parameter") + .setDeprecatedSince("5.5") + .setDeprecatedKey("pageIndex"); + action.addPageSize(100, MAX_PAGE_SIZE); } @Override @@ -165,7 +169,7 @@ public class ActivityAction implements CeWsAction { return buildResponse( singletonList(taskSearchedById.get()), Collections.<WsCe.Task>emptyList(), - Paging.forPageIndex(1).withPageSize(request.getPageSize()).andTotal(1)); + request.getPageSize()); } CeTaskQuery query = buildQuery(dbSession, request); @@ -176,10 +180,7 @@ public class ActivityAction implements CeWsAction { return buildResponse( queuedTasks.tasks, pastTasks.tasks, - Paging.forPageIndex(request.getPage()) - .withPageSize(request.getPageSize()) - .andTotal(queuedTasks.total + pastTasks.total)); - + request.getPageSize()); } finally { dbClient.closeSession(dbSession); } @@ -236,22 +237,18 @@ public class ActivityAction implements CeWsAction { private TaskResult loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) { int total = dbClient.ceQueueDao().countByQuery(dbSession, query); - List<CeQueueDto> dtos = dbClient.ceQueueDao().selectByQueryInDescOrder(dbSession, query, - Paging.forPageIndex(request.getPage()) - .withPageSize(request.getPageSize()) - .andTotal(total)); + List<CeQueueDto> dtos = dbClient.ceQueueDao().selectByQueryInDescOrder(dbSession, query, request.getPageSize()); Iterable<WsCe.Task> tasks = formatter.formatQueue(dbSession, dtos); return new TaskResult(tasks, total); } private TaskResult loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query, int totalQueuedTasks) { - int total = dbClient.ceActivityDao().countByQuery(dbSession, query); // we have to take into account the total number of queue tasks found - int offset = Math.max(0, offset(request.getPage(), request.getPageSize()) - totalQueuedTasks); + int offset = Math.max(0, offset(PAGE_INDEX, request.getPageSize()) - totalQueuedTasks); List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, offset, request.getPageSize()); Iterable<WsCe.Task> ceTasks = formatter.formatActivity(dbSession, dtos); - return new TaskResult(ceTasks, total); + return new TaskResult(ceTasks, MAX_PAGE_SIZE); } private void checkPermissions(CeTaskQuery query) { @@ -285,29 +282,23 @@ public class ActivityAction implements CeWsAction { return userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN) || userSession.hasComponentUuidPermission(UserRole.ADMIN, componentUuid); } - private static ActivityResponse buildResponse(Iterable<WsCe.Task> queuedTasks, Iterable<WsCe.Task> pastTasks, Paging paging) { + private static ActivityResponse buildResponse(Iterable<WsCe.Task> queuedTasks, Iterable<WsCe.Task> pastTasks, int pageSize) { WsCe.ActivityResponse.Builder wsResponseBuilder = WsCe.ActivityResponse.newBuilder(); int nbInsertedTasks = 0; for (WsCe.Task queuedTask : queuedTasks) { - if (nbInsertedTasks < paging.pageSize()) { + if (nbInsertedTasks < pageSize) { wsResponseBuilder.addTasks(queuedTask); nbInsertedTasks++; } } for (WsCe.Task pastTask : pastTasks) { - if (nbInsertedTasks < paging.pageSize()) { + if (nbInsertedTasks < pageSize) { wsResponseBuilder.addTasks(pastTask); nbInsertedTasks++; } } - - wsResponseBuilder.setPaging(Common.Paging.newBuilder() - .setPageIndex(paging.pageIndex()) - .setPageSize(paging.pageSize()) - .setTotal(paging.total())); - return wsResponseBuilder.build(); } @@ -320,7 +311,6 @@ public class ActivityAction implements CeWsAction { .setMinSubmittedAt(request.param(PARAM_MIN_SUBMITTED_AT)) .setMaxExecutedAt(request.param(PARAM_MAX_EXECUTED_AT)) .setOnlyCurrents(request.paramAsBoolean(PARAM_ONLY_CURRENTS)) - .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getQuery() == null, "%s and %s must not be set at the same time", diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java index 59b6d464533..eeb4690492e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java @@ -34,15 +34,15 @@ import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; +import org.sonar.ce.log.CeLogging; +import org.sonar.ce.log.LogFileRef; +import org.sonar.ce.taskprocessor.CeTaskProcessor; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbTester; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDbTester; -import org.sonar.ce.log.CeLogging; -import org.sonar.ce.log.LogFileRef; -import org.sonar.ce.taskprocessor.CeTaskProcessor; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; @@ -169,28 +169,23 @@ public class ActivityActionTest { } @Test - public void paginate_results() { + public void limit_results() { userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS); insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED); insertQueue("T3", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS); - assertPage(1, 1, 3, asList("T3")); - assertPage(2, 1, 3, asList("T2")); - assertPage(1, 10, 3, asList("T3", "T2", "T1")); - assertPage(2, 10, 3, Collections.<String>emptyList()); + assertPage(1, asList("T3")); + assertPage(2, asList("T3", "T2")); + assertPage(10, asList("T3", "T2", "T1")); + assertPage(0, Collections.<String>emptyList()); } - private void assertPage(int pageIndex, int pageSize, int expectedTotal, List<String> expectedOrderedTaskIds) { + private void assertPage(int pageSize, List<String> expectedOrderedTaskIds) { ActivityResponse activityResponse = call(ws.newRequest() - .setParam(Param.PAGE, Integer.toString(pageIndex)) .setParam(Param.PAGE_SIZE, Integer.toString(pageSize)) .setParam(PARAM_STATUS, "SUCCESS,FAILED,CANCELED,IN_PROGRESS,PENDING")); - assertThat(activityResponse.getPaging().getPageIndex()).isEqualTo(pageIndex); - assertThat(activityResponse.getPaging().getPageSize()).isEqualTo(pageSize); - assertThat(activityResponse.getPaging().getTotal()).isEqualTo(expectedTotal); - assertThat(activityResponse.getTasksCount()).isEqualTo(expectedOrderedTaskIds.size()); for (int i = 0; i < expectedOrderedTaskIds.size(); i++) { String expectedTaskId = expectedOrderedTaskIds.get(i); diff --git a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java index e32a1be1535..1055de867d5 100644 --- a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java +++ b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java @@ -72,13 +72,6 @@ public class CeActivityDao implements Dao { return mapper(dbSession).selectByQuery(query, offset, pageSize); } - public int countByQuery(DbSession dbSession, CeTaskQuery query) { - if (query.isShortCircuitedByComponentUuids()) { - return 0; - } - return mapper(dbSession).countByQuery(query); - } - private static CeActivityMapper mapper(DbSession dbSession) { return dbSession.getMapper(CeActivityMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/ce/CeQueueDao.java b/sonar-db/src/main/java/org/sonar/db/ce/CeQueueDao.java index 9670c8c8068..779f7372bbf 100644 --- a/sonar-db/src/main/java/org/sonar/db/ce/CeQueueDao.java +++ b/sonar-db/src/main/java/org/sonar/db/ce/CeQueueDao.java @@ -22,7 +22,6 @@ package org.sonar.db.ce; import com.google.common.base.Optional; import java.util.List; import org.apache.ibatis.session.RowBounds; -import org.sonar.api.utils.Paging; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -48,14 +47,14 @@ public class CeQueueDao implements Dao { return mapper(session).selectAllInAscOrder(); } - public List<CeQueueDto> selectByQueryInDescOrder(DbSession dbSession, CeTaskQuery query, Paging paging) { + public List<CeQueueDto> selectByQueryInDescOrder(DbSession dbSession, CeTaskQuery query, int pageSize) { if (query.isShortCircuitedByComponentUuids() || query.isOnlyCurrents() || query.getMaxExecutedAt() != null) { return emptyList(); } - return mapper(dbSession).selectByQueryInDescOrder(query, new RowBounds(paging.offset(), paging.pageSize())); + return mapper(dbSession).selectByQueryInDescOrder(query, new RowBounds(0, pageSize)); } public int countByQuery(DbSession dbSession, CeTaskQuery query) { diff --git a/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml b/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml index 08247b4faa1..765c5f28a6b 100644 --- a/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml @@ -74,11 +74,6 @@ WHERE rn BETWEEN (#{offset} * #{pageSize} + 1) AND ((#{offset} + 1) * #{pageSize}) </select> - <select id="countByQuery" parameterType="map" resultType="int"> - select count(ca.id) - <include refid="sqlSelectByQuery" /> - </select> - <sql id="sqlSelectByQuery"> from ce_activity ca <where> diff --git a/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java b/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java index bb92b76097b..14f3a2e4c71 100644 --- a/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java @@ -141,36 +141,6 @@ public class CeActivityDaoTest { } @Test - public void test_countByQuery() { - insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS); - insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED); - insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS); - insert("TASK_4", "views", null, CeActivityDto.Status.SUCCESS); - - // no filters - CeTaskQuery query = new CeTaskQuery(); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(4); - - // select by component uuid - query = new CeTaskQuery().setComponentUuid("PROJECT_1"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(2); - - // select by status - query = new CeTaskQuery().setStatuses(singletonList(CeActivityDto.Status.SUCCESS.name())); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(3); - - // select by type - query = new CeTaskQuery().setType(REPORT); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(3); - query = new CeTaskQuery().setType("views"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); - - // select by multiple conditions - query = new CeTaskQuery().setType(REPORT).setOnlyCurrents(true).setComponentUuid("PROJECT_1"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); - } - - @Test public void selectByQuery_no_results_if_shortcircuited_by_component_uuids() { insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS); @@ -180,15 +150,6 @@ public class CeActivityDaoTest { } @Test - public void countByQuery_no_results_if_shortcircuited_by_component_uuids() { - insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS); - - CeTaskQuery query = new CeTaskQuery(); - query.setComponentUuids(Collections.<String>emptyList()); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(0); - } - - @Test public void select_and_count_by_date() { insertWithDates("UUID1", 1_450_000_000_000L, 1_470_000_000_000L); insertWithDates("UUID2", 1_460_000_000_000L, 1_480_000_000_000L); @@ -196,19 +157,16 @@ public class CeActivityDaoTest { // search by min submitted date CeTaskQuery query = new CeTaskQuery().setMinSubmittedAt(1_455_000_000_000L); assertThat(underTest.selectByQuery(db.getSession(), query, 0, 5)).extracting("uuid").containsOnly("UUID2"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); // search by max executed date query = new CeTaskQuery().setMaxExecutedAt(1_475_000_000_000L); assertThat(underTest.selectByQuery(db.getSession(), query, 0, 5)).extracting("uuid").containsOnly("UUID1"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); // search by both dates query = new CeTaskQuery() .setMinSubmittedAt(1_400_000_000_000L) .setMaxExecutedAt(1_475_000_000_000L); assertThat(underTest.selectByQuery(db.getSession(), query, 0, 5)).extracting("uuid").containsOnly("UUID1"); - assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); } diff --git a/sonar-db/src/test/java/org/sonar/db/ce/CeQueueDaoTest.java b/sonar-db/src/test/java/org/sonar/db/ce/CeQueueDaoTest.java index 479bd23bf76..70fc652ad83 100644 --- a/sonar-db/src/test/java/org/sonar/db/ce/CeQueueDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/ce/CeQueueDaoTest.java @@ -29,7 +29,6 @@ import java.util.Map; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.utils.Paging; import org.sonar.api.utils.internal.TestSystem2; import org.sonar.db.DbTester; @@ -227,7 +226,7 @@ public class CeQueueDaoTest { .setType(CeTaskTypes.REPORT) .setMinSubmittedAt(100_000L); - List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, Paging.forPageIndex(1).withPageSize(1_000).andTotal(1_000)); + List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, 1_000); int total = underTest.countByQuery(db.getSession(), query); assertThat(result).extracting("uuid").containsExactly("TASK_5", TASK_UUID_2); @@ -244,7 +243,7 @@ public class CeQueueDaoTest { CeTaskQuery query = new CeTaskQuery().setOnlyCurrents(true); - List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, Paging.forPageIndex(1).withPageSize(1_000).andTotal(1_000)); + List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, 1_000); int total = underTest.countByQuery(db.getSession(), query); assertThat(result).isEmpty(); @@ -261,7 +260,7 @@ public class CeQueueDaoTest { CeTaskQuery query = new CeTaskQuery().setMaxExecutedAt(1_000_000_000_000L); - List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, Paging.forPageIndex(1).withPageSize(1_000).andTotal(1_000)); + List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, 1_000); int total = underTest.countByQuery(db.getSession(), query); assertThat(result).isEmpty(); @@ -278,7 +277,7 @@ public class CeQueueDaoTest { CeTaskQuery query = new CeTaskQuery().setComponentUuids(Collections.<String>emptyList()); - List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, Paging.forPageIndex(1).withPageSize(1_000).andTotal(1_000)); + List<CeQueueDto> result = underTest.selectByQueryInDescOrder(db.getSession(), query, 1_000); int total = underTest.countByQuery(db.getSession(), query); assertThat(result).isEmpty(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java index 484956d7a68..3a9b87fe478 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java @@ -349,12 +349,21 @@ public interface WebService extends Definable<WebService.Context> { * Note the maximum is a documentation only feature. It does not check anything. */ public NewAction addPagingParams(int defaultPageSize, int maxPageSize) { + addPageParam(); + addPageSize(defaultPageSize, maxPageSize); + return this; + } + + public NewAction addPageParam() { createParam(Param.PAGE) .setDescription("1-based page number") .setExampleValue("42") .setDeprecatedKey("pageIndex") .setDefaultValue("1"); + return this; + } + public NewAction addPageSize(int defaultPageSize, int maxPageSize) { createParam(Param.PAGE_SIZE) .setDescription("Page size. Must be greater than 0 and less than " + maxPageSize) .setExampleValue("20") diff --git a/sonar-ws/src/main/protobuf/ws-ce.proto b/sonar-ws/src/main/protobuf/ws-ce.proto index 1db7111abcf..126122a0561 100644 --- a/sonar-ws/src/main/protobuf/ws-ce.proto +++ b/sonar-ws/src/main/protobuf/ws-ce.proto @@ -39,7 +39,8 @@ message TaskResponse { // GET api/ce/activity message ActivityResponse { - optional sonarqube.ws.commons.Paging paging = 1; + // paging has been deprecated in 5.5 + optional sonarqube.ws.commons.Paging unusedPaging = 1; repeated Task tasks = 2; } |