package rocks.inspectit.ui.rcp.handlers;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.progress.IProgressConstants;
import rocks.inspectit.shared.all.cmr.model.PlatformIdent;
import rocks.inspectit.shared.all.communication.data.cmr.CmrStatusData;
import rocks.inspectit.shared.cs.storage.recording.RecordingProperties;
import rocks.inspectit.ui.rcp.InspectIT;
import rocks.inspectit.ui.rcp.InspectITImages;
import rocks.inspectit.ui.rcp.formatter.NumberFormatter;
import rocks.inspectit.ui.rcp.provider.ICmrRepositoryAndAgentProvider;
import rocks.inspectit.ui.rcp.provider.ICmrRepositoryProvider;
import rocks.inspectit.ui.rcp.repository.CmrRepositoryDefinition;
import rocks.inspectit.ui.rcp.view.impl.RepositoryManagerView;
import rocks.inspectit.ui.rcp.view.impl.StorageManagerView;
import rocks.inspectit.ui.rcp.wizard.StartRecordingWizard;
/**
* Starts recording.
*
* @author Ivan Senic
*
*/
public class StartRecordingHandler extends AbstractHandler implements IHandler {
/**
* {@inheritDoc}
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// try to get the CMR where recording should start.
CmrRepositoryDefinition cmrRepositoryDefinition = null;
Collection<PlatformIdent> autoSelectedAgents = Collections.emptyList();
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof StructuredSelection) {
Object selectedObject = ((StructuredSelection) selection).getFirstElement();
if (selectedObject instanceof ICmrRepositoryProvider) {
cmrRepositoryDefinition = ((ICmrRepositoryProvider) selectedObject).getCmrRepositoryDefinition();
} else if (selectedObject instanceof ICmrRepositoryAndAgentProvider) {
cmrRepositoryDefinition = ((ICmrRepositoryAndAgentProvider) selectedObject).getCmrRepositoryDefinition();
autoSelectedAgents = Collections.singletonList(((ICmrRepositoryAndAgentProvider) selectedObject).getPlatformIdent());
}
}
// check if the writing state is OK
if (null != cmrRepositoryDefinition) {
try {
CmrStatusData cmrStatusData = cmrRepositoryDefinition.getCmrManagementService().getCmrStatusData();
if (cmrStatusData.isWarnSpaceLeftActive()) {
String leftSpace = NumberFormatter.humanReadableByteCount(cmrStatusData.getStorageDataSpaceLeft());
if (!MessageDialog.openQuestion(HandlerUtil.getActiveShell(event), "Confirm", "For selected CMR there is an active warning about insufficient storage space left. Only " + leftSpace
+ " are left on the target server, are you sure you want to continue?")) {
return null;
}
}
} catch (Exception e) { // NOPMD NOCHK
// ignore because if we can not get the info. we will still respond to user action
}
}
// open wizard
StartRecordingWizard startRecordingWizard = new StartRecordingWizard(cmrRepositoryDefinition, autoSelectedAgents);
WizardDialog wizardDialog = new WizardDialog(HandlerUtil.getActiveShell(event), startRecordingWizard);
wizardDialog.open();
// if recording has been started refresh the repository and storage manager view
if (wizardDialog.getReturnCode() == Window.OK) {
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart repositoryManagerView = activePage.findView(RepositoryManagerView.VIEW_ID);
if (repositoryManagerView instanceof RepositoryManagerView) {
((RepositoryManagerView) repositoryManagerView).refresh();
}
IViewPart storageManagerView = activePage.findView(StorageManagerView.VIEW_ID);
if (storageManagerView instanceof StorageManagerView) {
if (null != cmrRepositoryDefinition) {
((StorageManagerView) storageManagerView).refresh(cmrRepositoryDefinition);
} else {
((StorageManagerView) storageManagerView).refresh();
}
}
// auto-refresh on recording stop if there is recording duration specified
RecordingProperties recordingProperties = startRecordingWizard.getRecordingProperties();
if ((null != recordingProperties) && (recordingProperties.getRecordDuration() > 0)) {
Job refreshStorageManagerJob = new Job("Recording Auto-Stop Updates") {
@Override
protected IStatus run(IProgressMonitor monitor) {
IViewPart storageManagerView = activePage.findView(StorageManagerView.VIEW_ID);
if (storageManagerView instanceof StorageManagerView) {
((StorageManagerView) storageManagerView).refresh();
}
return Status.OK_STATUS;
}
};
refreshStorageManagerJob.setUser(false);
refreshStorageManagerJob.setProperty(IProgressConstants.ICON_PROPERTY, InspectIT.getDefault().getImage(InspectITImages.IMG_RECORD_STOP));
// add 5 seconds to be sure all is done
long delay = 5000 + recordingProperties.getRecordDuration() + recordingProperties.getStartDelay();
refreshStorageManagerJob.schedule(delay);
}
}
return null;
}
}