Added the method getOutuput

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/Common/workspace-task-executor-library@167910 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-06-07 09:32:36 +00:00
parent e6c5b43c34
commit 321ecc49d1
5 changed files with 312 additions and 25 deletions

View File

@ -3,14 +3,22 @@
*/
package org.gcube.common.workspacetaskexecutor.dataminer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gcube.common.workspacetaskexecutor.shared.TaskOperator;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameter;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskComputation;
import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.FileResource;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.ImageResource;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource;
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.TableResource;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ColumnListParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ColumnParameter;
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.DateParameter;
@ -249,4 +257,89 @@ public class DMConverter {
return null;
}
}
/**
* Append output.
*
* @param builder the builder
* @param key the key
* @param res the res
*/
public static void appendOutput(StringBuilder builder, String key, Resource res){
switch (res.getResourceType()) {
case FILE:
FileResource fileResource = (FileResource) res;
String fileName=retrieveFileName(fileResource.getUrl());
logger.debug("Entry: " + key + " = "+ fileResource+", FileName="+fileName);
builder.append("Entry: " + key + " = "+fileResource+", FileName="+fileName);
break;
case IMAGE:
ImageResource imageResource = (ImageResource) res;
String imageName=retrieveFileName(imageResource.getLink());
logger.debug("Entry: " + key + " = "+ imageResource+", ImageName="+imageName);
builder.append("Entry: " + key + " = "+ imageResource+", ImageName="+imageName);
break;
case MAP:
logger.debug("Entry: " + key + " = "+ res);
builder.append("Entry: " + key + " = "+ res);
break;
case OBJECT:
logger.debug("Entry: " + key + " = "+ res);
builder.append("Entry: " + key + " = "+ res);
break;
case TABULAR:
TableResource tableResource = (TableResource) res;
String tableName=retrieveFileName(tableResource.getResourceId());
logger.debug("Entry: " + key + " = "+ tableResource+", TableName="+tableName);
builder.append("Entry: " + key + " = "+tableResource+", TableName="+tableName);
break;
default:
logger.debug("Entry: " + key + " = "+ res);
builder.append("Entry: " + key + " = "+ res);
break;
}
}
/**
* Retrieve file name.
* provided by Giancarlo
* @param url the url
* @return the string
*/
private static String retrieveFileName(String url) {
String fileName = "output";
try {
URL urlObj;
urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj
.openConnection();
connection.setRequestMethod("GET");
String contentDisposition = connection
.getHeaderField("Content-Disposition");
Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")");
Matcher regexMatcher = regex.matcher(contentDisposition);
if (regexMatcher.find()) {
fileName = regexMatcher.group();
}
if (fileName == null || fileName.isEmpty()) {
fileName = "output";
}
return fileName;
} catch (Throwable e) {
logger.error(
"Error retrieving file name: " + e.getLocalizedMessage(), e);
return fileName;
}
}
}

View File

@ -11,6 +11,7 @@ import java.util.Map;
import org.gcube.common.workspacetaskexecutor.shared.BaseTaskConfiguration;
import org.gcube.common.workspacetaskexecutor.shared.TaskOperator;
import org.gcube.common.workspacetaskexecutor.shared.TaskOutput;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameter;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType;
import org.gcube.common.workspacetaskexecutor.shared.TaskStatus;
@ -183,16 +184,17 @@ public class DataMinerAccessPoint {
TaskExecutionStatus tes = loadTaskExecutionStatus(taskConfiguration, taskComputation);
switch (tes.getStatus()) {
case COMPLETED:
case CANCELLED:
case FAILED:
logger.info("Removing "+tes+ "from memory");
removeTaskFromMemory(tes.getTaskConfiguration());
break;
default:
break;
}
//MOVED INTO METHOD getOutput
// switch (tes.getStatus()) {
// case COMPLETED:
// case CANCELLED:
// case FAILED:
// logger.info("Removing "+tes+ "from memory");
// removeTaskFromMemory(tes.getTaskConfiguration());
// break;
// default:
// break;
// }
return tes;
}
@ -297,43 +299,83 @@ public class DataMinerAccessPoint {
}
/**
* Gets the output.
*
* @param taskConfiguration the task configuration
* @param taskComputation the task computation
* @return the output
* @throws TaskErrorException the task error exception
* @throws TaskNotExecutableException the task not executable exception
*/
public TaskOutput getOutput(TaskConfiguration taskConfiguration, TaskComputation taskComputation) throws TaskErrorException, TaskNotExecutableException {
TaskExecutionStatus tes = monitorStatus(taskConfiguration, taskComputation);
SClient sClient;
ComputationId computationId = DMConverter.toComputationId(taskComputation);
try {
String token = EncrypterUtil.decryptString(taskConfiguration.getMaskedToken());
sClient = dataMinerService.getClient(token);
}
catch (Exception e) {
logger.error("Error on get DM client", e);
throw new TaskErrorException("Error on getting DataMiner client, Try later");
}
String outputMessage = retrieveOutput(computationId, sClient);
//REMOVING THE TASK FROM MEMORY
switch (tes.getStatus()) {
case COMPLETED:
case CANCELLED:
case FAILED:
logger.info("Removing "+tes+ "from memory");
removeTaskFromMemory(tes.getTaskConfiguration());
break;
default:
break;
}
return new TaskOutput(tes, outputMessage);
}
/**
* Retrieve output.
* provided by Giancarlo
*
* @param computationId the computation id
* @param sClient the s client
* @return the string
* @throws TaskErrorException the task error exception
*/
private String retrieveOutput(ComputationId computationId, SClient sClient) {
StringBuilder builder = new StringBuilder();
private String retrieveOutput(ComputationId computationId, SClient sClient) throws TaskErrorException {
try {
StringBuilder builder = new StringBuilder();
OutputData output = sClient.getOutputDataByComputationId(computationId);
logger.debug("Output: " + output);
builder.append("Output: " + output);
Resource resource = output.getResource();
if (resource.isMap()) {
MapResource mapResource = (MapResource) resource;
for (String key : mapResource.getMap().keySet()) {
logger.debug("Entry: " + key + " = "+ mapResource.getMap().get(key));
builder.append("Entry: " + key + " = "+ mapResource.getMap().get(key));
Resource res = mapResource.getMap().get(key);
DMConverter.appendOutput(builder, key, res);
}
} else {
}else{
DMConverter.appendOutput(builder, "", resource);
}
return builder.toString();
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
e.printStackTrace();
logger.error("Error on retrieve the output for computationId: "+computationId, e);
throw new TaskErrorException("Error on retrieve the output for computationId: "+computationId);
}
return builder.toString();
}
/**
* Adds the parameters to operator.
*
@ -422,4 +464,5 @@ public class DataMinerAccessPoint {
}
}

View File

@ -8,6 +8,7 @@ import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.workspacetaskexecutor.shared.ExecutableItem;
import org.gcube.common.workspacetaskexecutor.shared.ExecutableTask;
import org.gcube.common.workspacetaskexecutor.shared.TaskOperator;
import org.gcube.common.workspacetaskexecutor.shared.TaskOutput;
import org.gcube.common.workspacetaskexecutor.shared.TaskParameterType;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskComputation;
import org.gcube.common.workspacetaskexecutor.shared.dataminer.TaskConfiguration;
@ -401,4 +402,21 @@ public class WorkspaceDataMinerTaskExecutor implements ExecutableTask<TaskConfig
return dap.getListOperators();
}
/**
* Gets the task output.
*
* @param taskConfiguration the task configuration
* @param taskComputation the task computation
* @return the task output
* @throws TaskErrorException the task error exception
* @throws Exception the exception
*/
public TaskOutput getTaskOutput(TaskConfiguration taskConfiguration, TaskComputation taskComputation) throws TaskErrorException, Exception {
ValidateTaskConfiguration(taskConfiguration);
DataMinerAccessPoint dap = getDataMinerAccessPoint();
return dap.getOutput(taskConfiguration, taskComputation);
}
}

View File

@ -0,0 +1,32 @@
/**
*
*/
package org.gcube.common.workspacetaskexecutor.shared;
/**
* The Interface BaseTaskOutput.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Jun 7, 2018
*/
public interface BaseTaskOutput {
/**
* Gets the task execution status.
*
* @return the task execution status
*/
public BaseTaskExecutionStatus getTaskExecutionStatus();
/**
* Gets the output message.
*
* @return the outputMessage
*/
public String getOutputMessage();
}

View File

@ -0,0 +1,101 @@
/**
*
*/
package org.gcube.common.workspacetaskexecutor.shared;
import java.io.Serializable;
/**
* The Class TaskOutput.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Jun 7, 2018
*/
public class TaskOutput implements BaseTaskOutput, Serializable{
/**
*
*/
private static final long serialVersionUID = 4243040464402882775L;
private BaseTaskExecutionStatus taskExecutionStatus;
private String outputMessage;
/**
* Instantiates a new task parameter.
*/
public TaskOutput() {
}
/**
* Instantiates a new task output.
*
* @param taskExecutionstatus the task executionstatus
* @param outputMessage the output message
*/
public TaskOutput(BaseTaskExecutionStatus taskExecutionstatus, String outputMessage) {
this.taskExecutionStatus = taskExecutionstatus;
this.outputMessage = outputMessage;
}
/**
* @return the taskExecutionStatus
*/
public BaseTaskExecutionStatus getTaskExecutionStatus() {
return taskExecutionStatus;
}
/**
* @return the outputMessage
*/
public String getOutputMessage() {
return outputMessage;
}
/**
* @param taskExecutionStatus the taskExecutionStatus to set
*/
public void setTaskExecutionStatus(BaseTaskExecutionStatus taskExecutionStatus) {
this.taskExecutionStatus = taskExecutionStatus;
}
/**
* @param outputMessage the outputMessage to set
*/
public void setOutputMessage(String outputMessage) {
this.outputMessage = outputMessage;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("TaskOutput [taskExecutionStatus=");
builder.append(taskExecutionStatus);
builder.append(", outputMessage=");
builder.append(outputMessage);
builder.append("]");
return builder.toString();
}
}