From 1d23caa08309a7b9e2becde9e9fb41147774b3d4 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 25 Nov 2015 16:38:00 +0100 Subject: [PATCH] SONAR-7080 Web service must provide execution time of the background task in progress --- .../server/computation/ws/TaskFormatter.java | 22 +++++++++++++++- .../computation/ws/ActivityWsActionTest.java | 2 +- .../computation/ws/ComponentWsActionTest.java | 2 +- .../computation/ws/QueueWsActionTest.java | 2 +- .../computation/ws/TaskFormatterTest.java | 26 ++++++++++++++++--- .../computation/ws/TaskWsActionTest.java | 2 +- 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 formatQueue(DbSession dbSession, List 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(); } @@ -127,6 +130,23 @@ public class TaskFormatterTest { assertThat(wsTask.hasComponentName()).isFalse(); } + @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(); 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); -- 2.39.5