Skip to content

Commit

Permalink
Add service operation to retrieve results (sopeco#116)
Browse files Browse the repository at this point in the history
Currently only the main data can be fetched, but a download option to
get all the raw data will be offered later. The results are not fetched
within the UI yet.

The SpotterResult's message field had to be renamed to comply with JSON.

Added the report text as a string inside ResultsContainer to have all
data in one spot. However, the report text file is written normally like
before. Changes in Spotter were only to set the report in the container
object.

Change-Id: I13b546924b0c0b7c832ee20f6c59f3f5f9244a50
Signed-off-by: Denis Knoepfle <[email protected]>
  • Loading branch information
DenisKnoepfle committed Oct 10, 2014
1 parent c057a02 commit 23318e3
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.spotter.shared.configuration.JobDescription;
import org.spotter.shared.configuration.SpotterExtensionType;
import org.spotter.shared.hierarchy.model.XPerformanceProblem;
import org.spotter.shared.result.model.ResultsContainer;
import org.spotter.shared.service.SpotterServiceResponse;
import org.spotter.shared.status.SpotterProgress;

Expand Down Expand Up @@ -97,6 +98,29 @@ public long startDiagnosis(JobDescription jobDescription) {

}

/**
* Requests the results of a the run with the given job id.
*
* @param jobId
* the job id of the diagnosis run
* @return the retrieved results container or <code>null</code> if none
*/
public ResultsContainer requestResults(String jobId) {
SpotterServiceResponse<ResultsContainer> response = webResource.path(ConfigKeys.SPOTTER_REST_BASE)
.path(ConfigKeys.SPOTTER_REST_REQU_RESULTS).path(jobId).accept(MediaType.APPLICATION_JSON)
.get(new GenericType<SpotterServiceResponse<ResultsContainer>>() {
});
switch (response.getStatus()) {
case OK:
return response.getPayload();
case SERVER_ERROR:
throw new RuntimeException("Server error: " + response.getErrorMessage());
case INVALID_STATE:
default:
throw new IllegalStateException("Illegal response state!");
}
}

/**
* @return true if Spotter Diagnostics is currently running
*/
Expand Down Expand Up @@ -172,8 +196,8 @@ public Set<String> getAvailableExtensions(SpotterExtensionType extType) {
*/
public Set<ConfigParameterDescription> getExtensionConfigParamters(String extName) {
SpotterServiceResponse<Set<ConfigParameterDescription>> response = webResource
.path(ConfigKeys.SPOTTER_REST_BASE).path(ConfigKeys.SPOTTER_REST_EXTENSION_PARAMETERS)
.path(extName.toString()).accept(MediaType.APPLICATION_JSON)
.path(ConfigKeys.SPOTTER_REST_BASE).path(ConfigKeys.SPOTTER_REST_EXTENSION_PARAMETERS).path(extName)
.accept(MediaType.APPLICATION_JSON)
.get(new GenericType<SpotterServiceResponse<Set<ConfigParameterDescription>>>() {
});
switch (response.getStatus()) {
Expand Down
9 changes: 6 additions & 3 deletions org.spotter.core/src/org/spotter/core/Spotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public synchronized void startDiagnosis(String configurationFile, long timestamp
long durationMillis = ((System.currentTimeMillis() - timestamp));

resultsContainer.setResultsMap(ResultBlackboard.getInstance().getResults());
printResults(durationMillis);
String report = printResults(durationMillis);
resultsContainer.setReport(report);
serializeResults(resultsContainer);
ResultBlackboard.getInstance().reset();
}
Expand Down Expand Up @@ -178,12 +179,13 @@ private PerformanceProblem retrieveRootPerformanceProblem(ResultsContainer resul
}

/**
* Writes the Spotter report.
* Writes the Spotter report and returns the text that was printed.
*
* @param durationMillis
* time in milli seconds the diagnostics took
* @return the printed text
*/
private void printResults(long durationMillis) {
private String printResults(long durationMillis) {
StringBuilder builder = new StringBuilder();
builder.append("PPD analysis took ");
builder.append(LpeNumericUtils.formatTimeMillis(durationMillis));
Expand All @@ -205,6 +207,7 @@ private void printResults(long durationMillis) {
}

LOGGER.info("Spotter analysis finished! Report is written to the following file: {}", outputFile);
return resultString;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.spotter.eclipse.ui;

import java.net.ConnectException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -35,6 +36,7 @@
import org.spotter.shared.configuration.SpotterExtensionType;
import org.spotter.shared.hierarchy.model.RawHierarchyFactory;
import org.spotter.shared.hierarchy.model.XPerformanceProblem;
import org.spotter.shared.result.model.ResultsContainer;
import org.spotter.shared.status.SpotterProgress;

import com.sun.jersey.api.client.ClientHandlerException;
Expand Down Expand Up @@ -70,6 +72,7 @@ public class ServiceClientWrapper {
private static final String MSG_NO_ACTION = "Action cannot be performed without connection to DynamicSpotter Service. "
+ "Please check connection settings and try again.";
private static final String MSG_START_DIAGNOSIS = "Could not start diagnosis!";
private static final String MSG_REQU_RESULTS = "Could not retrieve diagnosis results!";
private static final String MSG_NO_STATUS = "Could not retrieve status.";
private static final String MSG_NO_CONFIG_PARAMS = "Could not retrieve configuration parameters.";
private static final String MSG_NO_EXTENSIONS = "Could not retrieve list of extensions.";
Expand Down Expand Up @@ -240,6 +243,23 @@ public Long startDiagnosis(final JobDescription jobDescription) {
return null;
}

/**
* Requests the results of a the run with the given job id.
*
* @param jobId
* the job id of the diagnosis run
* @return the retrieved results container or <code>null</code> if none
*/
public ResultsContainer requestResults(final String jobId) {
lastException = null;
try {
return client.requestResults(jobId);
} catch (Exception e) {
handleException("requestResults", MSG_REQU_RESULTS, e, false, false);
}
return null;
}

/**
* Returns <code>true</code> if DynamicSpotter diagnostics is currently
* running, otherwise <code>false</code>.
Expand Down Expand Up @@ -506,7 +526,12 @@ public Exception getLastException() {
* otherwise
*/
public boolean isConnectionIssue() {
return lastException instanceof ClientHandlerException;
if (lastException instanceof ClientHandlerException) {
if (lastException != null && lastException.getCause() instanceof ConnectException) {
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -577,6 +602,7 @@ public static void showConnectionProblemMessage(String cause, String host, Strin
private void handleException(String requestName, String requestErrorMsg, Exception exception, boolean silent,
boolean warning) {
lastException = exception;
exception.printStackTrace();
if (warning) {
LOGGER.warn("{} request failed! Cause: {}", requestName, exception.getMessage());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {

IProject project = selectedProjects.iterator().next();
ServiceClientWrapper client = activator.getClient(project.getName());

String spotterFileName = FileManager.SPOTTER_CONFIG_FILENAME;
IFile spotterFile = project.getFile(spotterFileName);
String spotterFilePath = spotterFile.getLocation().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class ResultsView extends ViewPart implements ISelectionListener {
private static final String RESULTS_EMPTY_CONTENT_DESC = "None selected.";
private static final String EMPTY_RESULTS = "No results selected.";
private static final String ERR_MSG_IO_ERROR = "An I/O error occured while reading the file '%s'.";
private static final String ERR_MSG_MISSING_REPORT = "Could not find the spotter report file.";
private static final String ERR_MSG_MISSING_REPORT = "Either file is missing or report is not set.";
private static final String ERR_MSG_MISSING_SER_FILE = "Could not find the spotter serialization file.";

private static final String LABEL_NONE_SELECTED = "<none selected>";
Expand Down Expand Up @@ -587,6 +587,7 @@ private void updateTabs() {
String contentDescription = String.format(RESULTS_CONTENT_DESC_TEMPLATE, runResultItem.getText(),
runResultItem.getProject().getName());
setContentDescription(contentDescription);
updateResultsContainer();
updateHierarchy();
updateReport();
}
Expand Down Expand Up @@ -614,25 +615,22 @@ private void resetReport() {
textReport.setText(EMPTY_RESULTS);
}

private void updateHierarchy() {
private void updateResultsContainer() {
String filename = FileManager.DEFAULT_RESULTS_DIR_NAME + File.separator + runResultItem.getText()
+ File.separator + ResultsLocationConstants.RESULTS_SERIALIZATION_FILE_NAME;
IFile file = runResultItem.getProject().getFile(filename);
ExtensionItem input = null;
resultsContainer = null;
try {
if (!file.isSynchronized(IResource.DEPTH_ZERO)) {
file.refreshLocal(IResource.DEPTH_ZERO, null);
}
BufferedInputStream bufferedInStream = new BufferedInputStream(file.getContents());
ObjectInputStream objectIn = new ObjectInputStream(bufferedInStream);

resultsContainer = (ResultsContainer) objectIn.readObject();

objectIn.close();
bufferedInStream.close();

XPerformanceProblem root = resultsContainer.getRootProblem();
if (root != null) {
input = HierarchyEditor.createPerformanceProblemHierarchy(runResultItem.getProject().getName(), root);
}
} catch (CoreException e) {
resultsContainer = null;
String text = ERR_MSG_MISSING_SER_FILE + " (" + filename + ")";
Expand All @@ -643,48 +641,37 @@ private void updateHierarchy() {
String text = String.format(ERR_MSG_IO_ERROR, filename);
LOGGER.error(text + (e.getMessage() != null ? " (" + e.getMessage() + ")" : ""));
DialogUtils.openWarning(RESULTS_VIEW_TITLE, text);
} finally {
imageProvider.setResultsContainer(resultsContainer);
if (input == null) {
input = new ExtensionItem();
}
}

private void updateHierarchy() {
ExtensionItem input = null;

if (resultsContainer != null) {
XPerformanceProblem root = resultsContainer.getRootProblem();
if (root != null) {
input = HierarchyEditor.createPerformanceProblemHierarchy(runResultItem.getProject().getName(), root);
}
hierarchyTreeViewer.setInput(input);
hierarchyTreeViewer.expandAll();
}

imageProvider.setResultsContainer(resultsContainer);
if (input == null) {
input = new ExtensionItem();
}
hierarchyTreeViewer.setInput(input);
hierarchyTreeViewer.expandAll();
}

private void updateReport() {
String filename = FileManager.DEFAULT_RESULTS_DIR_NAME + File.separator + runResultItem.getText()
+ File.separator + ResultsLocationConstants.TXT_REPORT_FILE_NAME;
IFile file = runResultItem.getProject().getFile(filename);
StringBuilder sb = new StringBuilder();
try {
if (!file.isSynchronized(IResource.DEPTH_ZERO)) {
file.refreshLocal(IResource.DEPTH_ZERO, null);
}
BufferedInputStream bufferedInStream = new BufferedInputStream(file.getContents());
int readByte;
while ((readByte = bufferedInStream.read()) != -1) {
sb.append((char) readByte);
}
bufferedInStream.close();
textReport.setText(sb.toString());
} catch (CoreException e) {
String text = ERR_MSG_MISSING_REPORT + " (" + filename + ")";
LOGGER.error(text + (e.getMessage() != null ? " (" + e.getMessage() + ")" : ""));
textReport.setText(text);
DialogUtils.openWarning(RESULTS_VIEW_TITLE, text);
} catch (IOException e) {
String text = String.format(ERR_MSG_IO_ERROR, filename);
LOGGER.error(text + (e.getMessage() != null ? " (" + e.getMessage() + ")" : ""));
textReport.setText(text);
DialogUtils.openWarning(RESULTS_VIEW_TITLE, text);
if (resultsContainer != null && resultsContainer.getReport() != null) {
textReport.setText(resultsContainer.getReport());
} else {
textReport.setText(ERR_MSG_MISSING_REPORT);
}
}

private String getCurrentResourceFolder() {
String projectRelativeRunPath = FileManager.DEFAULT_RESULTS_DIR_NAME + File.separator
+ runResultItem.getText();
String projectRelativeRunPath = FileManager.DEFAULT_RESULTS_DIR_NAME + File.separator + runResultItem.getText();
IFolder folder = runResultItem.getProject().getFolder(projectRelativeRunPath);
String currentRunFolder = folder.getLocation().toString() + "/";
String subDirPath = getSubDirPathForProblem(currentSelectedProblem);
Expand Down
Loading

0 comments on commit 23318e3

Please sign in to comment.