Skip to content

Commit 543d6be

Browse files
authored
Merge pull request #3391 from ControlSystemStudio/CSSTUDIO-3229
Update logbook clients to use server side managed "levels"
2 parents 286eee1 + 031c578 commit 543d6be

File tree

14 files changed

+275
-113
lines changed

14 files changed

+275
-113
lines changed

app/logbook/inmemory/src/main/java/org/phoebus/applications/logbook/InMemoryLogClient.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package org.phoebus.applications.logbook;
22

3+
import org.phoebus.logbook.Attachment;
4+
import org.phoebus.logbook.AttachmentImpl;
5+
import org.phoebus.logbook.LogClient;
6+
import org.phoebus.logbook.LogEntry;
7+
import org.phoebus.logbook.LogEntryImpl;
8+
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
9+
import org.phoebus.logbook.LogEntryLevel;
10+
import org.phoebus.logbook.Logbook;
11+
import org.phoebus.logbook.LogbookException;
12+
import org.phoebus.logbook.LogbookImpl;
13+
import org.phoebus.logbook.Property;
14+
import org.phoebus.logbook.PropertyImpl;
15+
import org.phoebus.logbook.SearchResult;
16+
import org.phoebus.logbook.Tag;
17+
import org.phoebus.logbook.TagImpl;
18+
319
import java.io.File;
420
import java.io.FileInputStream;
521
import java.io.FileNotFoundException;
622
import java.io.IOException;
723
import java.io.InputStream;
824
import java.net.URLConnection;
9-
import java.nio.file.CopyOption;
1025
import java.nio.file.Files;
11-
import java.nio.file.StandardCopyOption;
1226
import java.time.Instant;
1327
import java.util.Arrays;
1428
import java.util.Collection;
@@ -23,21 +37,6 @@
2337
import java.util.stream.Collectors;
2438
import java.util.stream.Stream;
2539

26-
import org.phoebus.logbook.Attachment;
27-
import org.phoebus.logbook.AttachmentImpl;
28-
import org.phoebus.logbook.LogClient;
29-
import org.phoebus.logbook.LogEntry;
30-
import org.phoebus.logbook.LogEntryImpl;
31-
import org.phoebus.logbook.LogEntryImpl.LogEntryBuilder;
32-
import org.phoebus.logbook.Logbook;
33-
import org.phoebus.logbook.LogbookException;
34-
import org.phoebus.logbook.LogbookImpl;
35-
import org.phoebus.logbook.Property;
36-
import org.phoebus.logbook.PropertyImpl;
37-
import org.phoebus.logbook.SearchResult;
38-
import org.phoebus.logbook.Tag;
39-
import org.phoebus.logbook.TagImpl;
40-
4140
/**
4241
* A logbook which maintains logentries in memory. It is mainly for testing and debugging purpose.
4342
*/
@@ -47,16 +46,16 @@ public class InMemoryLogClient implements LogClient {
4746
private final Map<Long, LogEntry> logEntries;
4847

4948
private final Collection<Logbook> logbooks = Arrays.asList(LogbookImpl.of("Controls"),
50-
LogbookImpl.of("Commissioning"),
51-
LogbookImpl.of("Scratch Pad"));
49+
LogbookImpl.of("Commissioning"),
50+
LogbookImpl.of("Scratch Pad"));
5251
private final Collection<Tag> tags = Arrays.asList(TagImpl.of("Operations"),
53-
TagImpl.of("Alarm"),
54-
TagImpl.of("Example"));
52+
TagImpl.of("Alarm"),
53+
TagImpl.of("Example"));
5554
private final List<String> levels = Arrays.asList("Urgent", "Suggestion", "Info", "Request", "Problem");
5655

5756
private static List<Property> inMemoryProperties() {
5857
Map<String, String> tracAttributes = new HashMap<>();
59-
Property track = PropertyImpl.of("Track",tracAttributes);
58+
Property track = PropertyImpl.of("Track", tracAttributes);
6059
Map<String, String> experimentAttributes = new HashMap<>();
6160
Property experimentProperty = PropertyImpl.of("Experiment", experimentAttributes);
6261
Map<String, String> resourceAttributes = new HashMap<>();
@@ -80,8 +79,8 @@ public InMemoryLogClient() {
8079
}
8180

8281
@Override
83-
public Collection<String> listLevels() {
84-
return levels;
82+
public Collection<LogEntryLevel> listLevels() {
83+
return levels.stream().map(l -> new LogEntryLevel(l, false)).toList();
8584
}
8685

8786
@Override
@@ -110,6 +109,7 @@ public LogEntry getLog(Long logId) {
110109
}
111110

112111
String prefix = "phoebus_tmp_file";
112+
113113
@Override
114114
public LogEntry set(LogEntry log) {
115115
long id = logIdCounter.incrementAndGet();
@@ -155,12 +155,12 @@ public SearchResult search(Map<String, String> map) {
155155
@Override
156156
public List<LogEntry> findLogs(Map<String, String> map) {
157157
Stream<LogEntry> searchStream = logEntries.values().stream();
158-
if(map.containsKey("start")) {
158+
if (map.containsKey("start")) {
159159
searchStream = searchStream.filter(log -> {
160160
return log.getCreatedDate().isAfter(Instant.ofEpochSecond(Long.valueOf(map.get("start"))));
161161
});
162162
}
163-
if(map.containsKey("end")) {
163+
if (map.containsKey("end")) {
164164
searchStream = searchStream.filter(log -> {
165165
return log.getCreatedDate().isBefore(Instant.ofEpochSecond(Long.valueOf(map.get("end"))));
166166
});
@@ -169,7 +169,7 @@ public List<LogEntry> findLogs(Map<String, String> map) {
169169
final String searchString = map.get("search").replaceAll("\\*", "");
170170
if (!searchString.isEmpty()) {
171171
searchStream = searchStream.filter(log -> {
172-
return log.getDescription().contains(searchString)||log.getTitle().contains(searchString);
172+
return log.getDescription().contains(searchString) || log.getTitle().contains(searchString);
173173
});
174174
}
175175
}

app/logbook/olog/client-es/src/main/java/org/phoebus/olog/es/api/OlogHttpClient.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.phoebus.logbook.LogClient;
1414
import org.phoebus.logbook.LogEntry;
1515
import org.phoebus.logbook.LogEntryChangeHandler;
16+
import org.phoebus.logbook.LogEntryLevel;
1617
import org.phoebus.logbook.LogTemplate;
1718
import org.phoebus.logbook.Logbook;
1819
import org.phoebus.logbook.LogbookException;
@@ -49,6 +50,7 @@
4950
import java.util.List;
5051
import java.util.Map;
5152
import java.util.ServiceLoader;
53+
import java.util.Set;
5254
import java.util.logging.Level;
5355
import java.util.logging.Logger;
5456

@@ -548,4 +550,22 @@ public LogTemplate saveTemplate(LogTemplate template) throws LogbookException {
548550
throw new LogbookException(e);
549551
}
550552
}
553+
554+
@Override
555+
public Collection<LogEntryLevel> listLevels(){
556+
HttpRequest request = HttpRequest.newBuilder()
557+
.uri(URI.create(Preferences.olog_url + "/levels"))
558+
.GET()
559+
.build();
560+
561+
try {
562+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
563+
return OlogObjectMappers.logEntryDeserializer.readValue(
564+
response.body(), new TypeReference<Set<LogEntryLevel>>() {
565+
});
566+
} catch (Exception e) {
567+
LOGGER.log(Level.WARNING, "Unable to get templates from service", e);
568+
return Collections.emptySet();
569+
}
570+
}
551571
}

app/logbook/olog/client/src/main/java/org/phoebus/olog/api/OlogClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.phoebus.logbook.Attachment;
77
import org.phoebus.logbook.LogClient;
88
import org.phoebus.logbook.LogEntry;
9+
import org.phoebus.logbook.LogEntryLevel;
910
import org.phoebus.logbook.Logbook;
1011
import org.phoebus.logbook.LogbookException;
1112
import org.phoebus.logbook.Messages;
@@ -197,13 +198,12 @@ private OlogClient(HttpClient httpClient, String userName, String password) {
197198

198199
}
199200

200-
201201
// A predefined set of levels supported by olog
202202
private final List<String> levels = Arrays.asList("Urgent", "Suggestion", "Info", "Request", "Problem");
203203

204204
@Override
205-
public Collection<String> listLevels() {
206-
return levels;
205+
public Collection<LogEntryLevel> listLevels() {
206+
return levels.stream().map(l -> new LogEntryLevel(l, false)).toList();
207207
}
208208

209209
@Override

0 commit comments

Comments
 (0)