diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-11-25 16:38:00 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-11-26 10:50:11 +0100 |
commit | 1d23caa08309a7b9e2becde9e9fb41147774b3d4 (patch) | |
tree | 65842418327cbd8e7df09df48230dfb2ed9be9e9 | |
parent | a17af69ca315f1df6d8c3f475e3ce53268564192 (diff) | |
download | sonarqube-1d23caa08309a7b9e2becde9e9fb41147774b3d4.tar.gz sonarqube-1d23caa08309a7b9e2becde9e9fb41147774b3d4.zip |
SONAR-7080 Web service must provide execution time of the background task in progress
6 files changed, 48 insertions, 8 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/TaskFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/TaskFormatter.java index 57754f32bf7..1346e6e654d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/TaskFormatter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/TaskFormatter.java @@ -28,6 +28,7 @@ import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; @@ -45,10 +46,12 @@ public class TaskFormatter { private final DbClient dbClient; private final CeLogging ceLogging; + private final System2 system2; - public TaskFormatter(DbClient dbClient, CeLogging ceLogging) { + public TaskFormatter(DbClient dbClient, CeLogging ceLogging, System2 system2) { this.dbClient = dbClient; this.ceLogging = ceLogging; + this.system2 = system2; } public List<WsCe.Task> formatQueue(DbSession dbSession, List<CeQueueDto> dtos) { @@ -81,6 +84,11 @@ public class TaskFormatter { if (dto.getStartedAt() != null) { builder.setStartedAt(DateUtils.formatDateTime(new Date(dto.getStartedAt()))); } + // + Long executionTimeMs = computeExecutionTimeMs(dto); + if (executionTimeMs != null) { + builder.setExecutionTimeMs(executionTimeMs); + } return builder.build(); } @@ -152,4 +160,16 @@ public class TaskFormatter { return dto; } } + + /** + * now - startedAt + */ + @CheckForNull + Long computeExecutionTimeMs(CeQueueDto dto) { + Long startedAt = dto.getStartedAt(); + if (startedAt == null) { + return null; + } + return system2.now() - startedAt; + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActivityWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActivityWsActionTest.java index 8f981cb856e..c1b54d02df7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActivityWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActivityWsActionTest.java @@ -68,7 +68,7 @@ public class ActivityWsActionTest { ComponentDbTester componentDb = new ComponentDbTester(dbTester); CeLogging ceLogging = mock(CeLogging.class); - TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging); + TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging, System2.INSTANCE); ActivityWsAction underTest = new ActivityWsAction(userSession, dbTester.getDbClient(), formatter); WsActionTester tester = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComponentWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComponentWsActionTest.java index 8d2908d1460..9dd41cbe196 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComponentWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComponentWsActionTest.java @@ -53,7 +53,7 @@ public class ComponentWsActionTest { public DbTester dbTester = DbTester.create(System2.INSTANCE); CeLogging ceLogging = mock(CeLogging.class); - TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging); + TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging, System2.INSTANCE); ComponentWsAction underTest = new ComponentWsAction(userSession, dbTester.getDbClient(), formatter); WsActionTester tester = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java index 5709e7acda0..c8baf5778b6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java @@ -53,7 +53,7 @@ public class QueueWsActionTest { public DbTester dbTester = DbTester.create(System2.INSTANCE); CeLogging ceLogging = mock(CeLogging.class); - TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging); + TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging, System2.INSTANCE); QueueWsAction underTest = new QueueWsAction(userSession, dbTester.getDbClient(), formatter); WsActionTester tester = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskFormatterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskFormatterTest.java index 452ce1bb6f3..cca32b55a95 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskFormatterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskFormatterTest.java @@ -30,6 +30,7 @@ import org.mockito.Mockito; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.TestSystem2; import org.sonar.db.DbTester; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; @@ -53,8 +54,9 @@ public class TaskFormatterTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); + System2 system2 = mock(System2.class); CeLogging ceLogging = mock(CeLogging.class, Mockito.RETURNS_DEEP_STUBS); - TaskFormatter underTest = new TaskFormatter(db.getDbClient(), ceLogging); + TaskFormatter underTest = new TaskFormatter(db.getDbClient(), ceLogging, system2); @Test public void formatQueue_without_component() { @@ -78,6 +80,8 @@ public class TaskFormatterTest { assertThat(wsTask.hasComponentKey()).isFalse(); assertThat(wsTask.hasComponentName()).isFalse(); assertThat(wsTask.hasExecutedAt()).isFalse(); + assertThat(wsTask.hasStartedAt()).isFalse(); + assertThat(wsTask.hasExecutionTimeMs()).isFalse(); } @Test @@ -106,8 +110,7 @@ public class TaskFormatterTest { assertThat(wsTask.getStatus()).isEqualTo(WsCe.TaskStatus.IN_PROGRESS); assertThat(wsTask.getLogs()).isTrue(); assertThat(wsTask.getSubmitterLogin()).isEqualTo("rob"); - - assertThat(wsTask.hasExecutionTimeMs()).isFalse(); + assertThat(wsTask.hasExecutionTimeMs()).isTrue(); assertThat(wsTask.hasExecutedAt()).isFalse(); } @@ -128,6 +131,23 @@ public class TaskFormatterTest { } @Test + public void formatQueue_compute_execute_time_if_in_progress() { + long startedAt = 1_450_000_001_000L; + long now = 1_450_000_003_000L; + CeQueueDto dto = new CeQueueDto(); + dto.setUuid("UUID"); + dto.setTaskType("TYPE"); + dto.setStatus(CeQueueDto.Status.IN_PROGRESS); + dto.setCreatedAt(1_450_000_000_000L); + dto.setStartedAt(startedAt); + when(system2.now()).thenReturn(now); + + WsCe.Task wsTask = underTest.formatQueue(db.getSession(), dto); + + assertThat(wsTask.getExecutionTimeMs()).isEqualTo(now-startedAt); + } + + @Test public void formatQueues() throws Exception { CeQueueDto dto1 = new CeQueueDto(); dto1.setUuid("UUID1"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskWsActionTest.java index c43a24096ee..5eca667d865 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/TaskWsActionTest.java @@ -57,7 +57,7 @@ public class TaskWsActionTest { public DbTester dbTester = DbTester.create(System2.INSTANCE); CeLogging ceLogging = mock(CeLogging.class); - TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging); + TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging, System2.INSTANCE); TaskWsAction underTest = new TaskWsAction(dbTester.getDbClient(), formatter, userSession); WsActionTester tester = new WsActionTester(underTest); |