247 lines
7.7 KiB
Java
247 lines
7.7 KiB
Java
|
/**
|
||
|
*
|
||
|
*/
|
||
|
package org.gcube.portlets.user.dataminermanager.client.experimentArea;
|
||
|
|
||
|
import java.util.Date;
|
||
|
|
||
|
import org.gcube.portlets.user.dataminermanager.client.DataMinerManager;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus.Status;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.bean.Operator;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.common.EventBusProvider;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.events.SessionExpiredEvent;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.util.UtilsGXT3;
|
||
|
import org.gcube.portlets.user.dataminermanager.client.widgets.ComputationOutputPanel;
|
||
|
import org.gcube.portlets.user.dataminermanager.shared.Constants;
|
||
|
import org.gcube.portlets.user.dataminermanager.shared.data.ComputationId;
|
||
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ExpiredSessionServiceException;
|
||
|
|
||
|
import com.allen_sauer.gwt.log.client.Log;
|
||
|
import com.google.gwt.dom.client.Style.Unit;
|
||
|
import com.google.gwt.i18n.client.NumberFormat;
|
||
|
import com.google.gwt.user.client.Timer;
|
||
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
||
|
import com.google.gwt.user.client.ui.HTML;
|
||
|
import com.sencha.gxt.widget.core.client.ProgressBar;
|
||
|
import com.sencha.gxt.widget.core.client.button.TextButton;
|
||
|
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
|
||
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
|
||
|
import com.sencha.gxt.widget.core.client.event.SelectEvent;
|
||
|
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
|
||
|
import com.sencha.gxt.widget.core.client.info.Info;
|
||
|
|
||
|
/**
|
||
|
* @author ceras
|
||
|
*
|
||
|
*/
|
||
|
public class ComputationStatusPanel extends SimpleContainer {
|
||
|
|
||
|
|
||
|
|
||
|
private ProgressBar progressBar = new ProgressBar();
|
||
|
private ComputationId computationId;
|
||
|
private Operator operator;
|
||
|
private boolean terminated=false;
|
||
|
private ComputationTimer timer = new ComputationTimer();
|
||
|
private TextButton cancelComputationButton;
|
||
|
private VerticalLayoutContainer vert;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public ComputationStatusPanel(Operator operator) {
|
||
|
super();
|
||
|
this.operator=operator;
|
||
|
init();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
private void init(){
|
||
|
vert=new VerticalLayoutContainer();
|
||
|
HTML title = new HTML("<div class='computationTitle'>Computation of <b>" + operator.getName() + "</b></div>");
|
||
|
HTML date = new HTML("<div class='computationDate'>" + new Date().toString() + "</div>");
|
||
|
vert.add(title);
|
||
|
vert.add(date);
|
||
|
add(vert);
|
||
|
forceLayout();
|
||
|
}
|
||
|
|
||
|
/* (non-Javadoc)
|
||
|
* @see com.extjs.gxt.ui.client.widget.Container#onAttach()
|
||
|
*/
|
||
|
@Override
|
||
|
protected void onAttach() {
|
||
|
super.onAttach();
|
||
|
progressBar.redraw();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param computationId the computationId to set
|
||
|
*/
|
||
|
public void computationStarted(ComputationId computationId) {
|
||
|
this.computationId = computationId;
|
||
|
vert.add(new HTML("Created, the id is "+computationId.getId()+" [<a href='"+computationId.getUrlId()+"' >link</a>]"));
|
||
|
progressBar.getElement().getStyle().setMargin(20, Unit.PX);
|
||
|
progressBar.updateProgress(0, "Starting...");
|
||
|
vert.add(progressBar);
|
||
|
|
||
|
cancelComputationButton = new TextButton("Cancel");
|
||
|
|
||
|
cancelComputationButton.addSelectHandler(new SelectHandler() {
|
||
|
|
||
|
@Override
|
||
|
public void onSelect(SelectEvent event) {
|
||
|
cancelComputationButton.disable();
|
||
|
cancelComputation();
|
||
|
|
||
|
}
|
||
|
});
|
||
|
vert.add(cancelComputationButton);
|
||
|
forceLayout();
|
||
|
timer.scheduleRepeating(Constants.TIME_UPDATE_COMPUTATION_STATUS_PANEL);
|
||
|
}
|
||
|
|
||
|
|
||
|
private void cancelComputation(){
|
||
|
DataMinerManager.getService().cancelComputation(computationId, new AsyncCallback<String>() {
|
||
|
|
||
|
@Override
|
||
|
public void onFailure(Throwable caught) {
|
||
|
if (caught instanceof ExpiredSessionServiceException) {
|
||
|
UtilsGXT3.alert("Error", "Expired Session");
|
||
|
EventBusProvider.INSTANCE.fireEvent(new SessionExpiredEvent());
|
||
|
} else {
|
||
|
UtilsGXT3.alert(
|
||
|
"Error",
|
||
|
"Error in cancel computation: ComputationId="+computationId.getId()+", Error="
|
||
|
+ caught.getLocalizedMessage());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSuccess(String result) {
|
||
|
Log.debug(result);
|
||
|
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param computationId
|
||
|
* @param operator
|
||
|
*
|
||
|
*/
|
||
|
protected void computationTerminated(ComputationId computationId, Operator operator, ComputationStatus computationStatus) {
|
||
|
Log.debug("Computation Terminated");
|
||
|
Log.debug("Computation Status:"+computationStatus);
|
||
|
if (terminated == false) {
|
||
|
terminated = true;
|
||
|
cancelComputationButton.setVisible(false);
|
||
|
if (computationStatus.isComplete()) {
|
||
|
Log.debug("Computation is Complete");
|
||
|
Info.display("Terminated", "The computation "+computationId.getId()+" of "+operator.getName()+" is terminated correctly.");
|
||
|
progressBar.updateProgress(1, "Computation Complete");
|
||
|
progressBar.addStyleName("progressBar-complete");
|
||
|
|
||
|
showOutput();
|
||
|
|
||
|
} else if (computationStatus.isFailed()) {
|
||
|
Log.debug("Computation is Failed");
|
||
|
// Info.display("Failed", "The computation of "+operator.getName()+" has failed.");
|
||
|
// MessageBox.alert("Failed", "The computation "+computationId+" of "+operator.getName()+" has failed.</br>"
|
||
|
// +"Message: "+computationStatus.getMessage(), null);
|
||
|
String errorMessage;
|
||
|
if(computationStatus.getErrResource()==null||computationStatus.getErrResource().getDescription()==null ){
|
||
|
errorMessage=new String("Computation Failed!");
|
||
|
} else {
|
||
|
errorMessage=computationStatus.getErrResource().getDescription();
|
||
|
}
|
||
|
|
||
|
UtilsGXT3.alert("Failed", "The computation "+computationId.getId()+" of "+operator.getName()+" has failed.</br>"
|
||
|
+"Message: "+errorMessage, null);
|
||
|
progressBar.updateProgress(1, "Computation Fail");
|
||
|
progressBar.addStyleName("progressBar-failed");
|
||
|
|
||
|
//
|
||
|
}
|
||
|
|
||
|
}
|
||
|
forceLayout();
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
private void showOutput() {
|
||
|
vert.add(new HTML(" The computation "+operator.getName()+" finished."));
|
||
|
ComputationOutputPanel computationOutputPanel = new ComputationOutputPanel(computationId);
|
||
|
vert.add(computationOutputPanel);
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param computationStatus
|
||
|
*/
|
||
|
protected void updateStatus(ComputationStatus computationStatus) {
|
||
|
Log.debug("Conputation Status Panel ::Update Status ");
|
||
|
if (computationStatus.getStatus() == Status.PENDING)
|
||
|
progressBar.updateText("Pending...");
|
||
|
else {
|
||
|
double percentage = computationStatus.getPercentage();
|
||
|
progressBar.updateProgress(percentage/100,
|
||
|
"Running, " + NumberFormat.getFormat("0.00").format(percentage) + "% Complete");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
private class ComputationTimer extends Timer {
|
||
|
|
||
|
/* (non-Javadoc)
|
||
|
* @see com.google.gwt.user.client.Timer#run()
|
||
|
*/
|
||
|
@Override
|
||
|
public void run() {
|
||
|
Log.debug("Timer run .....");
|
||
|
// final ComputationStatusPanel cp = ComputationStatusPanel.this;
|
||
|
DataMinerManager.getService().getComputationStatus(computationId, new AsyncCallback<ComputationStatus>() {
|
||
|
|
||
|
@Override
|
||
|
public void onFailure(Throwable caught) {
|
||
|
progressBar.updateProgress(1, "Failed to get the status");
|
||
|
progressBar.addStyleName("progressBar-failed");
|
||
|
// MessageBox.alert("Error", ""+caught.getLocalizedMessage()+"\nCause: "+caught.getCause()+"\nStackTrace: "+caught.getStackTrace().toString(), null);
|
||
|
// computationTerminated(computationId, operator, TerminationMode.FAILED2);
|
||
|
// ComputationTimer.this.cancel();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSuccess(ComputationStatus computationStatus) {
|
||
|
if (computationStatus.isTerminated()) {
|
||
|
ComputationTimer.this.cancel();
|
||
|
computationTerminated(computationId, operator, computationStatus);
|
||
|
} else
|
||
|
updateStatus(computationStatus);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public void stopTimer() {
|
||
|
try {
|
||
|
timer.cancel();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
vert.clear();
|
||
|
|
||
|
}
|
||
|
}
|