From c99e452a6928472ccdbd61c51cbc18aaf73f3f5d Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 27 Sep 2015 13:08:38 +0200 Subject: [PATCH] SONAR-6834 search activity by dates --- .../computation/ws/ActivityWsAction.java | 23 +++++++++- .../java/org/sonar/db/ce/CeActivityQuery.java | 22 ++++++++++ .../org/sonar/db/ce/CeActivityMapper.xml | 12 ++++++ .../org/sonar/db/ce/CeActivityDaoTest.java | 43 +++++++++++++++++-- 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActivityWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActivityWsAction.java index 99aecc48195..dc0b9985784 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActivityWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActivityWsAction.java @@ -19,11 +19,15 @@ */ package org.sonar.server.computation.ws; +import java.util.Date; import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.DateUtils; import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; @@ -42,6 +46,8 @@ public class ActivityWsAction implements CeWsAction { private static final String PARAM_TYPE = "type"; private static final String PARAM_STATUS = "status"; private static final String PARAM_ONLY_CURRENTS = "onlyCurrents"; + private static final String PARAM_MIN_SUBMITTED_AT = "minSubmittedAt"; + private static final String PARAM_MAX_FINISHED_AT = "maxFinishedAt"; private final UserSession userSession; private final DbClient dbClient; @@ -72,6 +78,12 @@ public class ActivityWsAction implements CeWsAction { action.createParam(PARAM_TYPE) .setDescription("Optional filter on task type") .setExampleValue(CeTaskTypes.REPORT); + action.createParam(PARAM_MIN_SUBMITTED_AT) + .setDescription("Optional filter on minimum date of task submission") + .setExampleValue(DateUtils.formatDateTime(new Date())); + action.createParam(PARAM_MAX_FINISHED_AT) + .setDescription("Optional filter on the maximum date of end of task processing") + .setExampleValue(DateUtils.formatDateTime(new Date())); action.addPagingParams(10); } @@ -101,12 +113,14 @@ public class ActivityWsAction implements CeWsAction { CeActivityQuery query = new CeActivityQuery(); query.setType(wsRequest.param(PARAM_TYPE)); query.setOnlyCurrents(wsRequest.mandatoryParamAsBoolean(PARAM_ONLY_CURRENTS)); - + query.setMinSubmittedAt(toTime(wsRequest.paramAsDateTime(PARAM_MIN_SUBMITTED_AT))); + query.setMaxFinishedAt(toTime(wsRequest.paramAsDateTime(PARAM_MAX_FINISHED_AT))); + String status = wsRequest.param(PARAM_STATUS); if (status != null) { query.setStatus(CeActivityDto.Status.valueOf(status)); } - + String componentUuid = wsRequest.param(PARAM_COMPONENT_UUID); if (componentUuid == null) { userSession.checkGlobalPermission(UserRole.ADMIN); @@ -122,4 +136,9 @@ public class ActivityWsAction implements CeWsAction { int pageSize = wsRequest.mandatoryParamAsInt(WebService.Param.PAGE_SIZE); return new RowBounds((pageIndex - 1) * pageSize, pageSize); } + + @CheckForNull + private static Long toTime(@Nullable Date date) { + return date == null ? null : date.getTime(); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityQuery.java b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityQuery.java index 98e0269eb6e..04f6d0ca7f4 100644 --- a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityQuery.java +++ b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityQuery.java @@ -28,6 +28,8 @@ public class CeActivityQuery { private String componentUuid; private CeActivityDto.Status status; private String type; + private Long minSubmittedAt; + private Long maxFinishedAt; @CheckForNull public String getComponentUuid() { @@ -67,4 +69,24 @@ public class CeActivityQuery { this.type = type; return this; } + + @CheckForNull + public Long getMaxFinishedAt() { + return maxFinishedAt; + } + + public CeActivityQuery setMaxFinishedAt(@Nullable Long l) { + this.maxFinishedAt = l; + return this; + } + + @CheckForNull + public Long getMinSubmittedAt() { + return minSubmittedAt; + } + + public CeActivityQuery setMinSubmittedAt(@Nullable Long l) { + this.minSubmittedAt = l; + return this; + } } 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 0b6090577a8..d6a13c87fac 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 @@ -58,6 +58,12 @@ and ca.task_type=#{query.type} + + and ca.submitted_at >= #{query.minSubmittedAt} + + + and ca.finished_at <= #{query.maxFinishedAt} + order by ca.id desc @@ -78,6 +84,12 @@ and ca.task_type=#{query.type} + + and ca.submitted_at >= #{query.minSubmittedAt} + + + and ca.finished_at <= #{query.maxFinishedAt} + 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 e9830ab9b9b..b52f6da2272 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 @@ -145,11 +145,46 @@ public class CeActivityDaoTest { assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); } + @Test + public void select_and_count_by_date() throws Exception { + insertWithDates("UUID1", 1_450_000_000_000L, 1_470_000_000_000L); + insertWithDates("UUID2", 1_460_000_000_000L, 1_480_000_000_000L); + + // search by min submitted date + CeActivityQuery query = new CeActivityQuery().setMinSubmittedAt(1_455_000_000_000L); + assertThat(underTest.selectByQuery(db.getSession(), query, new RowBounds(0, 10))).extracting("uuid").containsOnly("UUID2"); + assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); + + // search by max finished date + query = new CeActivityQuery().setMaxFinishedAt(1_475_000_000_000L); + assertThat(underTest.selectByQuery(db.getSession(), query, new RowBounds(0, 10))).extracting("uuid").containsOnly("UUID1"); + assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); + + // search by both dates + query = new CeActivityQuery() + .setMinSubmittedAt(1_400_000_000_000L) + .setMaxFinishedAt(1_475_000_000_000L); + assertThat(underTest.selectByQuery(db.getSession(), query, new RowBounds(0, 10))).extracting("uuid").containsOnly("UUID1"); + assertThat(underTest.countByQuery(db.getSession(), query)).isEqualTo(1); + + } + + private void insertWithDates(String uuid, long submittedAt, long finishedAt) { + CeQueueDto queueDto = new CeQueueDto(); + queueDto.setUuid(uuid); + queueDto.setTaskType("fake"); + CeActivityDto dto = new CeActivityDto(queueDto); + dto.setStatus(CeActivityDto.Status.SUCCESS); + dto.setSubmittedAt(submittedAt); + dto.setFinishedAt(finishedAt); + underTest.insert(db.getSession(), dto); + } + @Test public void deleteOlderThan() throws Exception { - insertWithDate("TASK_1", 1_450_000_000_000L); - insertWithDate("TASK_2", 1_460_000_000_000L); - insertWithDate("TASK_3", 1_470_000_000_000L); + insertWithCreationDate("TASK_1", 1_450_000_000_000L); + insertWithCreationDate("TASK_2", 1_460_000_000_000L); + insertWithCreationDate("TASK_3", 1_470_000_000_000L); underTest.deleteOlderThan(db.getSession(), 1_465_000_000_000L); db.getSession().commit(); @@ -175,7 +210,7 @@ public class CeActivityDaoTest { underTest.insert(db.getSession(), dto); } - private void insertWithDate(String uuid, long date) { + private void insertWithCreationDate(String uuid, long date) { CeQueueDto queueDto = new CeQueueDto(); queueDto.setUuid(uuid); queueDto.setTaskType("fake"); -- 2.39.5