Skip to content

Commit 994b652

Browse files
committed
Retake of save&restore logging
1 parent 7051802 commit 994b652

File tree

14 files changed

+368
-304
lines changed

14 files changed

+368
-304
lines changed

app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/menu/SendLogbookAction.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.phoebus.logbook.olog.ui.menu;
99

1010
import javafx.application.Platform;
11-
import javafx.scene.Node;
1211
import javafx.scene.control.MenuItem;
1312
import javafx.scene.image.Image;
1413
import org.phoebus.framework.jobs.JobManager;
@@ -43,19 +42,6 @@ public class SendLogbookAction extends MenuItem {
4342
/**
4443
* Constructor.
4544
*
46-
* @param parent JavaFX parent that context menu is called from.
47-
* @param title Initial title or <code>null</code>
48-
* @param body Initial body text or <code>null</code>
49-
* @param get_image Supplier for image to attach, or <code>null</code>
50-
*/
51-
public SendLogbookAction(final String title, final String body, final Supplier<Image> get_image) {
52-
this(title, body == null ? null : () -> body, get_image);
53-
}
54-
55-
/**
56-
* Constructor.
57-
*
58-
* @param parent JavaFX parent that context menu is called from.
5945
* @param title Initial title or <code>null</code>
6046
* @param get_body Supplier for initial body text or <code>null</code>
6147
* @param get_image Supplier for image to attach, or <code>null</code>

app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/menu/SendToLogBookApp.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
package org.phoebus.logbook.olog.ui.menu;
99

1010
import javafx.scene.image.Image;
11+
import org.phoebus.framework.adapter.AdapterService;
12+
import org.phoebus.framework.selection.Selection;
13+
import org.phoebus.framework.selection.SelectionService;
1114
import org.phoebus.framework.spi.AppDescriptor;
1215
import org.phoebus.framework.spi.AppInstance;
13-
import org.phoebus.logbook.olog.ui.LogbookAvailabilityChecker;
16+
import org.phoebus.logbook.LogEntry;
1417
import org.phoebus.logbook.olog.ui.write.LogEntryEditorStage;
1518
import org.phoebus.olog.es.api.model.OlogLog;
1619
import org.phoebus.ui.javafx.ImageCache;
@@ -37,10 +40,15 @@ public String getName() {
3740

3841
@Override
3942
public AppInstance create() {
40-
if (!LogbookAvailabilityChecker.isLogbookAvailable()) {
41-
return null;
43+
Selection selection = SelectionService.getInstance().getSelection();
44+
LogEntry ologLog;
45+
if(selection.getSelections().isEmpty()){
46+
ologLog = new OlogLog();
4247
}
43-
new LogEntryEditorStage(new OlogLog()).show();
48+
else{
49+
ologLog = AdapterService.adapt(selection.getSelections().get(0), LogEntry.class).get();
50+
}
51+
new LogEntryEditorStage(ologLog).show();
4452
return null;
4553
}
4654

app/save-and-restore/app/doc/index.rst

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,10 @@ Moving nodes on the other hand is lightweight as only references in the tree str
6868
Logging
6969
-------
7070

71-
The optional logging module can be configured to call any registered electronic logbook implementations to automatically create a log
72-
entry when a new snapshot has been saved, or when a snapshot has been restored (even if the restore operation fails to
73-
write values to PVs in the save set).
74-
75-
As neither the save-and-restore application
76-
nor the logging module will prompt for logbook credentials, users should use the Credentials Management app to make
77-
sure valid credentials are available. Such credentials are also available if user has successfully submitted a log entry,
78-
and if the electronic logbook module has been configured to save these credentials.
79-
80-
Note save-and-restore operation is independent of the log request. This means that if credentials are missing or invalid,
81-
only logging will fail.
71+
If a logbook implementation is available in the application, the optional logging module can be used to launch a log entry
72+
editor for the purpose of logging when a new snapshot has been saved, or when a snapshot has been restored.
73+
Properties of the snapshot (name, date etc) are automatically set on the log entry rendered by the editor. If
74+
a restore action has failed to write one or multiple PVs, a list of these PVs is also added to the log entry.
8275

8376
Script Support
8477
--------------

app/save-and-restore/logging/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@
2121
</dependency>
2222
<dependency>
2323
<groupId>org.phoebus</groupId>
24-
<artifactId>core-security</artifactId>
24+
<artifactId>core-logbook</artifactId>
2525
<version>4.6.10-SNAPSHOT</version>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.openjfx</groupId>
29+
<artifactId>javafx-graphics</artifactId>
30+
<version>${openjfx.version}</version>
31+
</dependency>
2732
<dependency>
2833
<groupId>org.phoebus</groupId>
29-
<artifactId>core-logbook</artifactId>
34+
<artifactId>core-security</artifactId>
3035
<version>4.6.10-SNAPSHOT</version>
3136
</dependency>
32-
37+
3338
<!-- https://mvnrepository.com/artifact/junit/junit -->
3439
<dependency>
3540
<groupId>junit</groupId>

app/save-and-restore/logging/src/main/java/org/phoebus/applications/saveandrestore/logging/Messages.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222

2323
public class Messages {
2424

25-
public static String LogbookCredentialsMissing;
2625
public static String SnapshotCreated;
2726
public static String SnapshotRestored;
27+
public static String SaveSnapshotTemplateMessage;
28+
public static String GoldenSnapshotRestored;
29+
30+
public static String RestoreSnapshotTemplateMessage;
31+
32+
public static String FailedPVs;
2833

2934
static
3035
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2020 European Spallation Source ERIC.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
*/
19+
20+
package org.phoebus.applications.saveandrestore.logging;
21+
22+
import java.util.List;
23+
24+
/**
25+
* Object wrapping information about a restore action.
26+
*/
27+
public class RestoreSnapshotActionInfo extends SaveSnapshotActionInfo{
28+
29+
private boolean isGolden;
30+
private List<String> failedPVs;
31+
32+
public boolean isGolden() {
33+
return isGolden;
34+
}
35+
36+
public void setGolden(boolean golden) {
37+
isGolden = golden;
38+
}
39+
40+
public List<String> getFailedPVs() {
41+
return failedPVs;
42+
}
43+
44+
public void setFailedPVs(List<String> failedPVs) {
45+
this.failedPVs = failedPVs;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (C) 2020 European Spallation Source ERIC.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
*/
19+
20+
package org.phoebus.applications.saveandrestore.logging;
21+
22+
import org.phoebus.framework.adapter.AdapterFactory;
23+
import org.phoebus.logbook.LogEntry;
24+
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
25+
import org.phoebus.logbook.PropertyImpl;
26+
27+
import java.text.MessageFormat;
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.Optional;
33+
34+
import static org.phoebus.logbook.LogEntryImpl.LogEntryBuilder.log;
35+
36+
/**
37+
* Adapts save/restore action information to a log entry.
38+
*/
39+
public class SaveAndRestoreActionAdapterFactory implements AdapterFactory {
40+
41+
@Override
42+
public Class getAdaptableObject() {
43+
return SaveSnapshotActionInfo.class;
44+
}
45+
46+
@Override
47+
public List<? extends Class> getAdapterList() {
48+
return Arrays.asList(LogEntry.class);
49+
}
50+
51+
@Override
52+
public <T> Optional<T> adapt(Object adaptableObject, Class<T> adapterType) {
53+
Map<String, String> map = new HashMap<>();
54+
map.put("file", "file:/" + ((SaveSnapshotActionInfo)adaptableObject).getSnapshotUniqueId() + "?app=saveandrestore");
55+
map.put("name", ((SaveSnapshotActionInfo)adaptableObject).getSnapshotName());
56+
if(adaptableObject instanceof RestoreSnapshotActionInfo){
57+
RestoreSnapshotActionInfo restoreSnapshotActionInfo = (RestoreSnapshotActionInfo)adaptableObject;
58+
String title = restoreSnapshotActionInfo.isGolden() ?
59+
MessageFormat.format(Messages.GoldenSnapshotRestored, restoreSnapshotActionInfo.getSnapshotName()) :
60+
MessageFormat.format(Messages.SnapshotRestored, restoreSnapshotActionInfo.getSnapshotName());
61+
LogEntryBuilder log = log()
62+
.title(title)
63+
.appendDescription(getBody(restoreSnapshotActionInfo))
64+
.appendProperty(PropertyImpl.of("resource", map));
65+
return Optional.of(adapterType.cast(log.build()));
66+
}
67+
else {
68+
SaveSnapshotActionInfo saveSnapshotActionInfo = (SaveSnapshotActionInfo)adaptableObject;
69+
LogEntryBuilder log = log()
70+
.title(MessageFormat.format(Messages.SnapshotCreated, saveSnapshotActionInfo.getSnapshotName()))
71+
.appendDescription(getBody(saveSnapshotActionInfo))
72+
.appendProperty(PropertyImpl.of("resource", map));
73+
return Optional.of(adapterType.cast(log.build()));
74+
}
75+
}
76+
77+
private String getBody(SaveSnapshotActionInfo restoreSnapshotActionInfo){
78+
StringBuilder stringBuilder = new StringBuilder();
79+
stringBuilder.append(Messages.RestoreSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
80+
getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder);
81+
stringBuilder.append("| Saved by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");
82+
83+
return stringBuilder.toString();
84+
}
85+
86+
private String getBody(RestoreSnapshotActionInfo restoreSnapshotActionInfo){
87+
StringBuilder stringBuilder = new StringBuilder();
88+
stringBuilder.append(Messages.SaveSnapshotTemplateMessage).append(System.lineSeparator()).append(System.lineSeparator());
89+
getCommonSnapshotInfo(restoreSnapshotActionInfo, stringBuilder);
90+
stringBuilder.append("| Golden | ").append(restoreSnapshotActionInfo.isGolden() ? "yes" : "no").append(" |\n");
91+
stringBuilder.append("| Restored by | ").append(restoreSnapshotActionInfo.getActionPerformedBy()).append(" |\n\n");
92+
93+
if(restoreSnapshotActionInfo.getFailedPVs() != null && !restoreSnapshotActionInfo.getFailedPVs().isEmpty()){
94+
stringBuilder.append(Messages.FailedPVs).append("\n\n");
95+
// This is needed!
96+
stringBuilder.append("| |\n");
97+
// This is needed!
98+
stringBuilder.append("|-|\n");
99+
restoreSnapshotActionInfo.getFailedPVs().forEach(p -> stringBuilder.append("| ").append(p).append(" |\n\n"));
100+
}
101+
102+
return stringBuilder.toString();
103+
}
104+
105+
private void getCommonSnapshotInfo(SaveSnapshotActionInfo saveSnapshotActionInfo, StringBuilder stringBuilder){
106+
// This is needed!
107+
stringBuilder.append("| | |\n");
108+
// This is needed!
109+
stringBuilder.append("|-|-|\n");
110+
stringBuilder.append("| Snapshot name | ").append(saveSnapshotActionInfo.getSnapshotName()).append(" |\n");
111+
stringBuilder.append("| Comment | ").append(saveSnapshotActionInfo.getComment()).append(" |\n");
112+
stringBuilder.append("| Created | ").append(saveSnapshotActionInfo.getSnapshotCreatedDate()).append(" |\n");
113+
}
114+
}

0 commit comments

Comments
 (0)