Skip to content

Commit

Permalink
Merge pull request #2356 from ControlSystemStudio/SaveRestoreLogFix
Browse files Browse the repository at this point in the history
Bug fix save&restore logging
  • Loading branch information
shroffk authored Aug 19, 2022
2 parents 2b9ea45 + 7a0db88 commit 2e5ba3e
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.selection.Selection;
import org.phoebus.framework.selection.SelectionService;
import org.phoebus.logbook.*;
import org.phoebus.logbook.olog.ui.HelpViewer;
import org.phoebus.logbook.olog.ui.LogbookUIPreferences;
Expand Down Expand Up @@ -376,10 +378,14 @@ public void onChanged(Change<? extends String> change) {
}

/**
* Handler for Cancel button
* Handler for Cancel button. Note that any selections in the {@link SelectionService} are
* cleared to prevent next launch of {@link org.phoebus.logbook.olog.ui.menu.SendToLogBookApp}
* to pick them up.
*/
@FXML
public void cancel() {
// Need to clear selections.
SelectionService.getInstance().clearSelection("");
((LogEntryEditorStage) cancelButton.getScene().getWindow()).handleCloseEditor(isDirty, root);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,6 @@ public void restore() {
LOGGER.log(Level.WARNING,
"Not all PVs could be restored for {0}: {1}. The following errors occured:\n{2}",
new Object[] { s.getSnapshot().get().getName(), s.getSnapshot().get(), sb.toString() });

Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(Messages.restoreErrorTitle);
alert.setContentText(sb.toString());
alert.setHeaderText(Messages.restoreErrorContent);
DialogHelper.positionDialog(alert, snapshotTab.getTabPane(), -150, -150);
alert.showAndWait();
});
}
logSnapshotRestored(s.getSnapshot().get(), restoreFailed);
}).start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2020 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

package org.phoebus.applications.saveandrestore.logging;

import org.phoebus.logbook.LogEntry;
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
import org.phoebus.logbook.PropertyImpl;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.phoebus.logbook.LogEntryImpl.LogEntryBuilder.log;

/**
* Adapts save/restore action information to a log entry.
*/
public class RestoreSnapshotActionAdapterFactory extends SaveSnapshotActionAdapterFactory {

@Override
public Class getAdaptableObject() {
return RestoreSnapshotActionInfo.class;
}

@Override
public List<? extends Class> getAdapterList() {
return Arrays.asList(LogEntry.class);
}

@Override
public <T> Optional<T> adapt(Object adaptableObject, Class<T> adapterType) {
Map<String, String> map = new HashMap<>();
map.put("file", "file:/" + ((RestoreSnapshotActionInfo)adaptableObject).getSnapshotUniqueId() + "?app=saveandrestore");
map.put("name", ((RestoreSnapshotActionInfo)adaptableObject).getSnapshotName());

RestoreSnapshotActionInfo restoreSnapshotActionInfo = (RestoreSnapshotActionInfo)adaptableObject;
String title = restoreSnapshotActionInfo.isGolden() ?
MessageFormat.format(Messages.GoldenSnapshotRestored, restoreSnapshotActionInfo.getSnapshotName()) :
MessageFormat.format(Messages.SnapshotRestored, restoreSnapshotActionInfo.getSnapshotName());
LogEntryBuilder log = log()
.title(title)
.appendDescription(getBody(restoreSnapshotActionInfo))
.appendProperty(PropertyImpl.of("resource", map));
return Optional.of(adapterType.cast(log.build()));
}

protected String getBody(RestoreSnapshotActionInfo restoreSnapshotActionInfo){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Messages.RestoreSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder);
stringBuilder.append("| Golden | ").append(restoreSnapshotActionInfo.isGolden() ? "yes" : "no").append(" |\n");
stringBuilder.append("| Restored by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");

if(restoreSnapshotActionInfo.getFailedPVs() != null && !restoreSnapshotActionInfo.getFailedPVs().isEmpty()){
stringBuilder.append("\n").append(Messages.FailedPVs).append("\n\n");
// This is needed!
stringBuilder.append("| |\n");
// This is needed!
stringBuilder.append("|-|\n");
restoreSnapshotActionInfo.getFailedPVs().forEach(p -> stringBuilder.append("| ").append(p).append(" |\n"));
stringBuilder.append("\n");
}

return stringBuilder.toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,4 @@ public void snapshotRestored(Node node, List<String> failedPVs, Consumer<String>
SelectionService.getInstance().setSelection("SaveAndRestoreLogging", List.of(restoreSnapshotActionInfo));
Platform.runLater(() -> ApplicationService.createInstance("logbook"));
}

protected String getSnapshotInfoTable(Node node) {
StringBuilder stringBuilder = new StringBuilder();
// This is needed!
stringBuilder.append("| | |\n");
// This is needed!
stringBuilder.append("|-|-|\n");
stringBuilder.append("| Snapshot name | ").append(node.getName()).append(" |\n");
stringBuilder.append("| Comment | ").append(node.getProperty("comment")).append(" |\n");
stringBuilder.append("| Created | ").append(node.getCreated()).append(" |\n");
String isGolden = node.getProperty("golden");
stringBuilder.append("| Golden | ").append("true".equals(isGolden) ? "yes" : "no").append(" |\n");
stringBuilder.append("| User id | ").append(node.getUserName()).append(" |\n\n");

return stringBuilder.toString();
}

protected String getFailedPVsTable(List<String> pvs) {
StringBuilder stringBuilder = new StringBuilder();
// This is needed!
stringBuilder.append("| |\n");
// This is needed!
stringBuilder.append("|-|\n");
pvs.forEach(p -> stringBuilder.append("| ").append(p).append(" |\n"));

return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2020 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

package org.phoebus.applications.saveandrestore.logging;

import org.phoebus.framework.adapter.AdapterFactory;
import org.phoebus.logbook.LogEntry;
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
import org.phoebus.logbook.PropertyImpl;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.phoebus.logbook.LogEntryImpl.LogEntryBuilder.log;

/**
* Adapts save snapshot action information to a log entry.
*/
public class SaveSnapshotActionAdapterFactory implements AdapterFactory {

@Override
public Class getAdaptableObject() {
return SaveSnapshotActionInfo.class;
}

@Override
public List<? extends Class> getAdapterList() {
return Arrays.asList(LogEntry.class);
}

@Override
public <T> Optional<T> adapt(Object adaptableObject, Class<T> adapterType) {
Map<String, String> map = new HashMap<>();
map.put("file", "file:/" + ((SaveSnapshotActionInfo) adaptableObject).getSnapshotUniqueId() + "?app=saveandrestore");
map.put("name", ((SaveSnapshotActionInfo) adaptableObject).getSnapshotName());

SaveSnapshotActionInfo saveSnapshotActionInfo = (SaveSnapshotActionInfo) adaptableObject;
LogEntryBuilder log = log()
.title(MessageFormat.format(Messages.SnapshotCreated, saveSnapshotActionInfo.getSnapshotName()))
.appendDescription(getBody(saveSnapshotActionInfo))
.appendProperty(PropertyImpl.of("resource", map));
return Optional.of(adapterType.cast(log.build()));
}

private String getBody(SaveSnapshotActionInfo saveSnapshotActionInfo) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Messages.SaveSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
getCommonSnapshotInfo(saveSnapshotActionInfo, stringBuilder);
stringBuilder.append("| Saved by | ").append(saveSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");

return stringBuilder.toString();
}

protected void getCommonSnapshotInfo(SaveSnapshotActionInfo saveSnapshotActionInfo, StringBuilder stringBuilder) {
// This is needed!
stringBuilder.append("| | |\n");
// This is needed!
stringBuilder.append("|-|-|\n");
stringBuilder.append("| Snapshot name | ").append(saveSnapshotActionInfo.getSnapshotName()).append(" |\n");
stringBuilder.append("| Comment | ").append(saveSnapshotActionInfo.getComment()).append(" |\n");
stringBuilder.append("| Created | ").append(saveSnapshotActionInfo.getSnapshotCreatedDate()).append(" |\n");
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.phoebus.applications.saveandrestore.logging.SaveAndRestoreActionAdapterFactory
org.phoebus.applications.saveandrestore.logging.SaveSnapshotActionAdapterFactory
org.phoebus.applications.saveandrestore.logging.RestoreSnapshotActionAdapterFactory
Loading

0 comments on commit 2e5ba3e

Please sign in to comment.