From 3d16789b2f34c6d73d19a4b21aa1a609c104ae5c Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Fri, 14 Aug 2020 10:05:54 -0500 Subject: [PATCH] consolidate scenario transfer types --- .../resources/ScenarioExecutorEndpoint.java | 46 ++++++--------- .../rest/transfertypes/ProgressView.java | 36 ++++++++++++ .../engine/rest/transfertypes/ResultInfo.java | 50 ---------------- .../rest/transfertypes/ScenarioInfo.java | 58 ++++++++++++++----- 4 files changed, 95 insertions(+), 95 deletions(-) create mode 100644 engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ProgressView.java delete mode 100644 engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ResultInfo.java diff --git a/engine-rest/src/main/java/io/nosqlbench/engine/rest/resources/ScenarioExecutorEndpoint.java b/engine-rest/src/main/java/io/nosqlbench/engine/rest/resources/ScenarioExecutorEndpoint.java index 3de1594a3..5afb4c265 100644 --- a/engine-rest/src/main/java/io/nosqlbench/engine/rest/resources/ScenarioExecutorEndpoint.java +++ b/engine-rest/src/main/java/io/nosqlbench/engine/rest/resources/ScenarioExecutorEndpoint.java @@ -8,16 +8,14 @@ import io.nosqlbench.engine.cli.ScriptBuffer; import io.nosqlbench.engine.core.ScenarioResult; import io.nosqlbench.engine.core.script.Scenario; import io.nosqlbench.engine.core.script.ScenariosExecutor; +import io.nosqlbench.engine.rest.services.WorkspaceService; import io.nosqlbench.engine.rest.transfertypes.RunScenarioRequest; import io.nosqlbench.engine.rest.transfertypes.ScenarioInfo; -import io.nosqlbench.engine.rest.transfertypes.ResultInfo; import io.nosqlbench.nb.annotations.Service; import javax.inject.Singleton; import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.*; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -32,6 +30,10 @@ public class ScenarioExecutorEndpoint implements WebServiceObject { private ScenariosExecutor executor = new ScenariosExecutor("executor-service", 1); + @Context + private Configuration config; + + @POST @Path("cli") @Consumes(MediaType.APPLICATION_JSON) @@ -92,12 +94,13 @@ public class ScenarioExecutorEndpoint implements WebServiceObject { private void storeFiles(RunScenarioRequest rq) { Map filemap = rq.getFilemap(); + WorkspaceService workspaces = new WorkspaceService(config); + for (String filename : filemap.keySet()) { try { - Paths.get(rq.getBasedir(),rq.getScenarioName()); Files.createDirectories( - Paths.get(rq.getBasedir(),rq.getScenarioName()), + workspaces.getWorkspace(rq.getWorkspace()).getWorkspacePath(), PosixFilePermissions.asFileAttribute( PosixFilePermissions.fromString("rwxr-x---") )); @@ -167,8 +170,10 @@ public class ScenarioExecutorEndpoint implements WebServiceObject { @Produces(MediaType.APPLICATION_JSON) public synchronized ScenarioInfo getScenario(@PathParam("scenarioName") String scenarioName) { Optional pendingScenario = executor.getPendingScenario(scenarioName); + if (pendingScenario.isPresent()) { - return new ScenarioInfo(pendingScenario.get()); + Optional pendingResult = executor.getPendingResult(scenarioName); + return new ScenarioInfo(pendingScenario.get(),pendingResult.orElse(null)); } else { throw new RuntimeException("Scenario name '" + scenarioName + "' not found."); } @@ -178,33 +183,16 @@ public class ScenarioExecutorEndpoint implements WebServiceObject { @Path("scenarios") @Produces(MediaType.APPLICATION_JSON) public synchronized List getScenarios() { + List scenarioInfos = new ArrayList<>(); List pendingScenarios = executor.getPendingScenarios(); - for (String pendingName : pendingScenarios) { - Optional pendingScenario = executor.getPendingScenario(pendingName); - pendingScenario.ifPresent(scenario -> scenarioInfos.add(new ScenarioInfo(scenario))); + + for (String pendingScenario : pendingScenarios) { + ScenarioInfo scenarioInfo = getScenario(pendingScenario); + scenarioInfos.add(scenarioInfo); } return scenarioInfos; } - @GET - @Path("result/{scenarioName}") - @Produces(MediaType.APPLICATION_JSON) - public synchronized ResultInfo getResult(@PathParam("scenarioName") String scenarioName) { - return new ResultInfo(scenarioName, executor.getPendingResult(scenarioName).orElse(null)); - } - - @GET - @Path("results") - @Produces(MediaType.APPLICATION_JSON) - public synchronized List getResults() { - List results = new ArrayList<>(); - List pendingScenarios = executor.getPendingScenarios(); - for (String pendingScenario : pendingScenarios) { - Optional pendingResult = executor.getPendingResult(pendingScenario); - results.add(new ResultInfo(pendingScenario, pendingResult.orElse(null))); - } - return results; - } } diff --git a/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ProgressView.java b/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ProgressView.java new file mode 100644 index 000000000..9f620c49d --- /dev/null +++ b/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ProgressView.java @@ -0,0 +1,36 @@ +package io.nosqlbench.engine.rest.transfertypes; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.nosqlbench.engine.api.activityapi.core.ProgressMeter; + +public class ProgressView { + + private final ProgressMeter progressMeter; + + public ProgressView(ProgressMeter progressMeter) { + this.progressMeter = progressMeter; + + } + + @JsonProperty("details") + public String getProgressDetails() { + return progressMeter.getProgressDetails(); + } + + @JsonProperty("name") + public String getName() { + return progressMeter.getProgressName(); + } + + @JsonProperty("state") + public String getState() { + return progressMeter.getProgressState().toString(); + } + + @JsonProperty("completed") + public double getProgress() { + return progressMeter.getProgress(); + } + + +} diff --git a/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ResultInfo.java b/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ResultInfo.java deleted file mode 100644 index 1d6837b0d..000000000 --- a/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ResultInfo.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.nosqlbench.engine.rest.transfertypes; - -import io.nosqlbench.engine.core.ScenarioResult; - -/** - * TODO: Combine scenario status and pending state to one view - *
{@code
- *  {
- *      "scenarioName": "myscenarioname",
- *      "isComplete": (true|false),
- *      "isErrored": (true|false),
- *      "ioLog": "IOLOGLine1\n...\n"
- *
- *      [same progress data as for the pending scenario view]
- *
- *      " whole scenario "
- *      [constructed link to grafana dashboard for current duration, with selected update interval]
- *
- *      [create snapshot in grafana from the time range of the scenario once complete]
- *      [link to grafana snapshot]
- *
- *  }
- * }
- */ -public class ResultInfo { - private final String scenarioName; - private final ScenarioResult result; - - public ResultInfo(String scenarioName, ScenarioResult result) { - this.scenarioName = scenarioName; - this.result = result; - } - - public String getScenarioName() { - return scenarioName; - } - - public boolean isComplete() { - return result != null; - } - - public boolean isErrored() { - return (result != null && result.getException().isPresent()); - } - - public String getIOLog() { - return result.getIOLog(); - } - -} diff --git a/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ScenarioInfo.java b/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ScenarioInfo.java index 31c274ca0..c08a7119b 100644 --- a/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ScenarioInfo.java +++ b/engine-rest/src/main/java/io/nosqlbench/engine/rest/transfertypes/ScenarioInfo.java @@ -1,35 +1,61 @@ package io.nosqlbench.engine.rest.transfertypes; -import io.nosqlbench.engine.api.activityapi.core.ProgressMeter; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.nosqlbench.engine.core.ActivityExecutor; +import io.nosqlbench.engine.core.ScenarioResult; import io.nosqlbench.engine.core.script.Scenario; -import java.util.Collection; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class ScenarioInfo { - private final Scenario scenario; - public ScenarioInfo(Scenario scenario) { + private final Scenario scenario; + private final ScenarioResult result; + + public ScenarioInfo(Scenario scenario, ScenarioResult result) { this.scenario = scenario; + this.result = result; } + @JsonProperty("scenario_name") public String getScenarioName() { return scenario.getScenarioName(); } - public Map getProgress() { - Map progress = new HashMap<>(); + @JsonProperty("started_at") + public long getStartMillis() { + return scenario.getStartedAtMillis(); + } - Collection progressMeters = - scenario.getScenarioController().getProgressMeters(); - for (ProgressMeter meter : progressMeters) { - String activityName = meter.getProgressName(); - String activityProgress = meter.getProgressDetails(); - if (activityName!=null && activityProgress!=null) { - progress.put(activityName, activityProgress); - } + @JsonProperty("ended_at") + public long getEndMillis() { + return scenario.getEndedAtMillis(); + + } + + @JsonProperty("progress") + public List getProgress() { + List progress = new ArrayList<>(); + + return scenario.getScenarioController().getProgressMeters() + .stream().map(ProgressView::new).collect(Collectors.toList()); + } + + @JsonProperty("activity_states") + public List> getActivityStates() { + List> states = new ArrayList<>(); + for (ActivityExecutor ae : scenario.getScenarioController().getActivityExecutorMap().values()) { + states.add( + Map.of( + "name", ae.getProgressName(), + "completion", String.valueOf(ae.getProgress()), + "state", ae.getProgressState().toString() + ) + ); } - return progress; + return states; } }