*/
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;
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;
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);
}
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);
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();
+ }
}
private String componentUuid;
private CeActivityDto.Status status;
private String type;
+ private Long minSubmittedAt;
+ private Long maxFinishedAt;
@CheckForNull
public String getComponentUuid() {
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;
+ }
}
<if test="query.type != null">
and ca.task_type=#{query.type}
</if>
+ <if test="query.minSubmittedAt != null">
+ and ca.submitted_at >= #{query.minSubmittedAt}
+ </if>
+ <if test="query.maxFinishedAt != null">
+ and ca.finished_at <= #{query.maxFinishedAt}
+ </if>
</where>
order by ca.id desc
</select>
<if test="query.type != null">
and ca.task_type=#{query.type}
</if>
+ <if test="query.minSubmittedAt != null">
+ and ca.submitted_at >= #{query.minSubmittedAt}
+ </if>
+ <if test="query.maxFinishedAt != null">
+ and ca.finished_at <= #{query.maxFinishedAt}
+ </if>
</where>
</select>
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();
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");