In order to have no more full scan on the ce_activity table, we had to remove paging as it was executing a select count(* from ce_activity)
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;
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;
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;
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");
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
return buildResponse(
singletonList(taskSearchedById.get()),
Collections.<WsCe.Task>emptyList(),
- Paging.forPageIndex(1).withPageSize(request.getPageSize()).andTotal(1));
+ request.getPageSize());
}
CeTaskQuery query = buildQuery(dbSession, request);
return buildResponse(
queuedTasks.tasks,
pastTasks.tasks,
- Paging.forPageIndex(request.getPage())
- .withPageSize(request.getPageSize())
- .andTotal(queuedTasks.total + pastTasks.total));
-
+ request.getPageSize());
} finally {
dbClient.closeSession(dbSession);
}
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) {
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();
}
.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",
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;
}
@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);
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);
}
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;
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) {
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>
assertThat(selectPageOfUuids(10, 2)).isEmpty();
}
- @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);
assertThat(underTest.selectByQuery(db.getSession(), query, 0, 0)).isEmpty();
}
- @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);
// 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);
}
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;
.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);
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();
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();
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();
* 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")
// 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;
}