diff --git a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverter.java b/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverter.java index 17a3bba54..a30057a2c 100644 --- a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverter.java +++ b/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverter.java @@ -1,6 +1,5 @@ package com.nitorcreations.nflow.rest.v1.converter; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -8,9 +7,9 @@ import com.nitorcreations.nflow.engine.workflow.definition.WorkflowDefinitionStatistics; import com.nitorcreations.nflow.engine.workflow.statistics.Statistics; -import com.nitorcreations.nflow.rest.v1.msg.DefinitionStatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.StatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse; +import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse.StateStatistics; @Component public class StatisticsConverter { @@ -30,14 +29,37 @@ public StatisticsResponse convert(Statistics stats) { public WorkflowDefinitionStatisticsResponse convert(Map> stats) { WorkflowDefinitionStatisticsResponse resp = new WorkflowDefinitionStatisticsResponse(); for (Entry> entry : stats.entrySet()) { - LinkedHashMap statusStats = new LinkedHashMap<>(); - resp.stateStatistics.put(entry.getKey(), statusStats); + StateStatistics stateStats = new StateStatistics(); + resp.stateStatistics.put(entry.getKey(), stateStats); for (Entry statusEntry : entry.getValue().entrySet()) { - DefinitionStatisticsResponse statsResponse = new DefinitionStatisticsResponse(); - WorkflowDefinitionStatistics statistics = statusEntry.getValue(); - statsResponse.allInstances = statistics.allInstances; - statsResponse.queuedInstances = statistics.queuedInstances; - statusStats.put(statusEntry.getKey(), statsResponse); + WorkflowDefinitionStatistics value = statusEntry.getValue(); + switch (statusEntry.getKey()) { + case "created": + stateStats.created.allInstances = value.allInstances; + stateStats.created.queuedInstances = value.queuedInstances; + break; + case "inProgress": + stateStats.inProgress.allInstances = value.allInstances; + stateStats.inProgress.queuedInstances = value.queuedInstances; + break; + case "executing": + stateStats.executing.allInstances = value.allInstances; + break; + case "paused": + stateStats.paused.allInstances = value.allInstances; + break; + case "stopped": + stateStats.stopped.allInstances = value.allInstances; + break; + case "manual": + stateStats.manual.allInstances = value.allInstances; + break; + case "finished": + stateStats.finished.allInstances = value.allInstances; + break; + default: + // ignored + } } } return resp; diff --git a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/DefinitionStatisticsResponse.java b/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/DefinitionStatisticsResponse.java deleted file mode 100644 index 38b2aebc2..000000000 --- a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/DefinitionStatisticsResponse.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.nitorcreations.nflow.rest.v1.msg; - -import com.wordnik.swagger.annotations.ApiModel; -import com.wordnik.swagger.annotations.ApiModelProperty; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -@ApiModel(value = "Statistics for workflow instance state and status") -@SuppressFBWarnings(value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", justification="jackson reads dto fields") -public class DefinitionStatisticsResponse { - - @ApiModelProperty(value = "The number of all instances of the workflow in given state and status") - public long allInstances; - - @ApiModelProperty(value = "The number of the instances of the workflow in given state and status that have next activation time in past") - public long queuedInstances; -} diff --git a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/WorkflowDefinitionStatisticsResponse.java b/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/WorkflowDefinitionStatisticsResponse.java index ce9394c79..7aee08d9d 100644 --- a/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/WorkflowDefinitionStatisticsResponse.java +++ b/nflow-rest-api/src/main/java/com/nitorcreations/nflow/rest/v1/msg/WorkflowDefinitionStatisticsResponse.java @@ -13,5 +13,23 @@ public class WorkflowDefinitionStatisticsResponse { @ApiModelProperty(value = "Statistics per state", required=true) - public Map> stateStatistics = new LinkedHashMap<>(); + public Map stateStatistics = new LinkedHashMap<>(); + + public static class StateStatistics { + public AllAndQueued created = new AllAndQueued(); + public AllAndQueued inProgress = new AllAndQueued(); + public All executing = new All(); + public All paused = new All(); + public All stopped = new All(); + public All manual = new All(); + public All finished = new All(); + + public static class AllAndQueued extends All { + public long queuedInstances; + } + + public static class All { + public long allInstances; + } + } } diff --git a/nflow-rest-api/src/test/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverterTest.java b/nflow-rest-api/src/test/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverterTest.java index ed8d41c88..3a79c1a15 100644 --- a/nflow-rest-api/src/test/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverterTest.java +++ b/nflow-rest-api/src/test/java/com/nitorcreations/nflow/rest/v1/converter/StatisticsConverterTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; +import java.util.HashMap; import java.util.Map; import org.junit.Test; @@ -12,9 +13,9 @@ import com.nitorcreations.nflow.engine.workflow.definition.WorkflowDefinitionStatistics; import com.nitorcreations.nflow.engine.workflow.statistics.Statistics; import com.nitorcreations.nflow.engine.workflow.statistics.Statistics.QueueStatistics; -import com.nitorcreations.nflow.rest.v1.msg.DefinitionStatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.StatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse; +import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse.StateStatistics; public class StatisticsConverterTest { @@ -40,16 +41,44 @@ public void queueStatisticsConvertWorks() { @Test public void workflowDefinitionStatisticsConverterWorks() { - WorkflowDefinitionStatistics statistics = new WorkflowDefinitionStatistics(); - statistics.allInstances = 1; - statistics.queuedInstances = 2; - Map> stats = singletonMap("state", singletonMap("status", statistics)); + Map stateStats = new HashMap<>(); + WorkflowDefinitionStatistics created = new WorkflowDefinitionStatistics(); + created.allInstances = 1; + created.queuedInstances = 2; + stateStats.put("created", created); + WorkflowDefinitionStatistics inProgress = new WorkflowDefinitionStatistics(); + inProgress.allInstances = 3; + inProgress.queuedInstances = 4; + stateStats.put("inProgress", inProgress); + WorkflowDefinitionStatistics executing = new WorkflowDefinitionStatistics(); + executing.allInstances = 5; + stateStats.put("executing", executing); + WorkflowDefinitionStatistics paused = new WorkflowDefinitionStatistics(); + paused.allInstances = 6; + stateStats.put("paused", paused); + WorkflowDefinitionStatistics stopped = new WorkflowDefinitionStatistics(); + stopped.allInstances = 7; + stateStats.put("stopped", stopped); + WorkflowDefinitionStatistics manual = new WorkflowDefinitionStatistics(); + manual.allInstances = 8; + stateStats.put("manual", manual); + WorkflowDefinitionStatistics finished = new WorkflowDefinitionStatistics(); + finished.allInstances = 9; + stateStats.put("finished", finished); + stateStats.put("unknown", new WorkflowDefinitionStatistics()); + Map> stats = singletonMap("state", stateStats); WorkflowDefinitionStatisticsResponse response = converter.convert(stats); - Map stateStatistics = response.stateStatistics.get("state"); - DefinitionStatisticsResponse statusStatistics = stateStatistics.get("status"); - assertThat(statusStatistics.allInstances, is(1L)); - assertThat(statusStatistics.queuedInstances, is(2L)); + StateStatistics stateStatistics = response.stateStatistics.get("state"); + assertThat(stateStatistics.created.allInstances, is(1L)); + assertThat(stateStatistics.created.queuedInstances, is(2L)); + assertThat(stateStatistics.inProgress.allInstances, is(3L)); + assertThat(stateStatistics.inProgress.queuedInstances, is(4L)); + assertThat(stateStatistics.executing.allInstances, is(5L)); + assertThat(stateStatistics.paused.allInstances, is(6L)); + assertThat(stateStatistics.stopped.allInstances, is(7L)); + assertThat(stateStatistics.manual.allInstances, is(8L)); + assertThat(stateStatistics.finished.allInstances, is(9L)); } } diff --git a/nflow-tests/src/test/java/com/nitorcreations/nflow/tests/StatisticsTest.java b/nflow-tests/src/test/java/com/nitorcreations/nflow/tests/StatisticsTest.java index 212d4e3c3..f1d6d4198 100644 --- a/nflow-tests/src/test/java/com/nitorcreations/nflow/tests/StatisticsTest.java +++ b/nflow-tests/src/test/java/com/nitorcreations/nflow/tests/StatisticsTest.java @@ -11,8 +11,6 @@ import static org.junit.Assert.assertThat; import static org.junit.runners.MethodSorters.NAME_ASCENDING; -import java.util.Map; - import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status.Family; @@ -24,10 +22,10 @@ import com.nitorcreations.nflow.rest.v1.msg.CreateWorkflowInstanceRequest; import com.nitorcreations.nflow.rest.v1.msg.CreateWorkflowInstanceResponse; -import com.nitorcreations.nflow.rest.v1.msg.DefinitionStatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.StatisticsResponse; import com.nitorcreations.nflow.rest.v1.msg.UpdateWorkflowInstanceRequest; import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse; +import com.nitorcreations.nflow.rest.v1.msg.WorkflowDefinitionStatisticsResponse.StateStatistics; import com.nitorcreations.nflow.tests.DemoWorkflowTest.DemoConfiguration; import com.nitorcreations.nflow.tests.runner.NflowServerRule; @@ -76,9 +74,9 @@ public void t02_queryStatistics() { public void t03_queryDefinitionStatistics() { WorkflowDefinitionStatisticsResponse statistics = getDefinitionStatistics(DEMO_WORKFLOW_TYPE); assertThat(statistics.stateStatistics, is(notNullValue())); - Map map = statistics.stateStatistics.get("begin"); - assertThat(map.get("created").allInstances, is(1L)); - assertThat(map.get("created").queuedInstances, is(0L)); + StateStatistics stats = statistics.stateStatistics.get("begin"); + assertThat(stats.created.allInstances, is(1L)); + assertThat(stats.created.queuedInstances, is(0L)); } @Test @@ -113,8 +111,8 @@ public void t07_queryStatistics() { public void t08_queryDefinitionStatistics() { WorkflowDefinitionStatisticsResponse statistics = getDefinitionStatistics(DEMO_WORKFLOW_TYPE); assertThat(statistics.stateStatistics, is(notNullValue())); - Map map = statistics.stateStatistics.get("begin"); - assertThat(map.get("created").allInstances, is(1L)); - assertThat(map.get("created").queuedInstances, is(1L)); + StateStatistics stats = statistics.stateStatistics.get("begin"); + assertThat(stats.created.allInstances, is(1L)); + assertThat(stats.created.queuedInstances, is(1L)); } }