Skip to content

Commit

Permalink
Merge pull request #3225 from ControlSystemStudio/CSSTUDIO-2646
Browse files Browse the repository at this point in the history
Replace http client for alarm log app
  • Loading branch information
shroffk authored Jan 9, 2025
2 parents f12b02e + 8230771 commit ee828ea
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 120 deletions.
10 changes: 0 additions & 10 deletions app/alarm/logging-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
<version>4.7.4-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand All @@ -63,10 +58,5 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.phoebus.applications.alarm.logging.ui;

import com.sun.jersey.api.client.WebResource;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableValue;
Expand All @@ -21,6 +20,7 @@
import org.phoebus.util.time.TimeRelativeInterval;
import org.phoebus.util.time.TimestampFormats;

import java.net.http.HttpClient;
import java.util.logging.Logger;

import static org.phoebus.ui.time.TemporalAmountPane.Type.TEMPORAL_AMOUNTS_AND_NOW;
Expand Down Expand Up @@ -57,16 +57,17 @@ public class AdvancedSearchViewController {

PopOver timeSearchPopover;

private WebResource searchClient;
//private WebResource searchClient;
private HttpClient httpClient;

// Search parameters
ObservableMap<Keys, String> searchParameters;

@FXML
private AnchorPane advancedSearchPane;

public AdvancedSearchViewController(WebResource client){
this.searchClient = client;
public AdvancedSearchViewController(HttpClient httpClient) {
this.httpClient = httpClient;
}

@FXML
Expand Down Expand Up @@ -169,7 +170,7 @@ public void initialize() {
});
}

public void setSearchParameters(ObservableMap<Keys, String> params){
public void setSearchParameters(ObservableMap<Keys, String> params) {
searchParameters = params;
searchParameters.addListener((MapChangeListener<Keys, String>) change -> {
searchPV.setText(searchParameters.get(Keys.PV));
Expand All @@ -194,7 +195,7 @@ public void setSearchParameters(ObservableMap<Keys, String> params){
searchCommand.setText(searchParameters.get(Keys.COMMAND));
}

public AnchorPane getPane(){
public AnchorPane getPane() {
return advancedSearchPane;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.phoebus.applications.alarm.logging.ui;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.api.client.WebResource;
import org.phoebus.framework.jobs.Job;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.jobs.JobRunnableWithCancel;
import org.phoebus.util.http.QueryParamsHelper;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
Expand All @@ -23,28 +26,28 @@
* @author Kunal Shroff
*/
public class AlarmLogConfigSearchJob extends JobRunnableWithCancel {
private final WebResource client;
private final HttpClient httpClient;
private final String pattern;

private final Consumer<AlarmLogTableItem> alarmMessageHandler;
private final BiConsumer<String, Exception> errorHandler;

private final ObjectMapper objectMapper;

public static Job submit(WebResource client,
public static Job submit(HttpClient httpClient,
final String pattern,
final Consumer<AlarmLogTableItem> alarmMessageHandler,
final BiConsumer<String, Exception> errorHandler) {
return JobManager.schedule("searching alarm log messages for : " + pattern,
new AlarmLogConfigSearchJob(client, pattern, alarmMessageHandler, errorHandler));
new AlarmLogConfigSearchJob(httpClient, pattern, alarmMessageHandler, errorHandler));
}

private AlarmLogConfigSearchJob(WebResource client,
private AlarmLogConfigSearchJob(HttpClient httpClient,
String pattern,
Consumer<AlarmLogTableItem> alarmMessageHandler,
BiConsumer<String, Exception> errorHandler) {
super();
this.client = client;
this.httpClient = httpClient;
this.pattern = pattern;
this.alarmMessageHandler = alarmMessageHandler;
this.errorHandler = errorHandler;
Expand All @@ -66,16 +69,25 @@ public Runnable getRunnable() {
try {
MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
map.put("config", Arrays.asList(pattern));

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(Preferences.service_uri + "/search/alarm/config?" + QueryParamsHelper.mapToQueryParams(map)))
.header("Content-Type", MediaType.APPLICATION_JSON)
.GET()
.build();

HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

List<AlarmLogTableItem> result = objectMapper
.readValue(client.path("/search/alarm/config").queryParams(map).accept(MediaType.APPLICATION_JSON).get(String.class),
.readValue(response.body(),
new TypeReference<List<AlarmLogTableItem>>() {
});
if (result.size() >= 1) {
alarmMessageHandler.accept(result.get(0));
} else {
alarmMessageHandler.accept(null);
}
} catch (JsonProcessingException e) {
} catch (Exception e) {
errorHandler.accept("Failed to search for alarm logs ", e);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.api.client.WebResource;
import javafx.collections.ObservableMap;
import org.phoebus.applications.alarm.logging.ui.AlarmLogTableQueryUtil.Keys;
import org.phoebus.framework.jobs.Job;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.jobs.JobMonitor;
import org.phoebus.framework.jobs.JobRunnable;
import org.phoebus.framework.preferences.PreferencesReader;
import org.phoebus.util.http.QueryParamsHelper;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand All @@ -34,25 +40,26 @@ public class AlarmLogSearchJob implements JobRunnable {
private final BiConsumer<String, Exception> errorHandler;

private final ObjectMapper objectMapper;
private final WebResource client;
//private final WebResource client;
private final HttpClient httpClient;

private final PreferencesReader prefs = new PreferencesReader(AlarmLogTableApp.class,
"/alarm_logging_preferences.properties");

public static Job submit(WebResource client,
public static Job submit(HttpClient httpClient,
final String pattern,
Boolean isNodeTable,
ObservableMap<Keys, String> searchParameters,
final Consumer<List<AlarmLogTableItem>> alarmMessageHandler,
final BiConsumer<String, Exception> errorHandler) {
return JobManager.schedule("searching alarm log messages for : " + pattern,
new AlarmLogSearchJob(client, isNodeTable, searchParameters, alarmMessageHandler, errorHandler));
new AlarmLogSearchJob(httpClient, isNodeTable, searchParameters, alarmMessageHandler, errorHandler));
}

private AlarmLogSearchJob(WebResource client, Boolean isNodeTable, ObservableMap<Keys, String> searchParameters,
private AlarmLogSearchJob(HttpClient httpClient, Boolean isNodeTable, ObservableMap<Keys, String> searchParameters,
Consumer<List<AlarmLogTableItem>> alarmMessageHandler, BiConsumer<String, Exception> errorHandler) {
super();
this.client = client;
this.httpClient = httpClient;
this.searchParameters = searchParameters;
this.alarmMessageHandler = alarmMessageHandler;
this.errorHandler = errorHandler;
Expand All @@ -69,19 +76,26 @@ public void run(JobMonitor monitor) {
int size = prefs.getInt("results_max_size");

MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
searchParameters.forEach((key, value) -> { if (!value.equals("")) map.add(key.getName(), value); });
searchParameters.forEach((key, value) -> {
if (!value.equals("")) map.add(key.getName(), value);
});
map.putIfAbsent("size", List.of(String.valueOf(size)));

try {

long start = System.currentTimeMillis();
String resultStr = client.path("/search/alarm")
.queryParams(map)
.accept(MediaType.APPLICATION_JSON).get(String.class);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(Preferences.service_uri + "/search/alarm?" + QueryParamsHelper.mapToQueryParams(map)))
.header("Content-Type", MediaType.APPLICATION_JSON)
.GET()
.build();

HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

logger.log(Level.FINE, "String response = " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
List<AlarmLogTableItem> result = objectMapper.readValue(resultStr, new TypeReference<>() {
List<AlarmLogTableItem> result = objectMapper.readValue(response.body(), new TypeReference<>() {
});
logger.log(Level.FINE, "Object mapper response = " + (System.currentTimeMillis() - start));
alarmMessageHandler.accept(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.sun.jersey.api.client.WebResource;
import org.phoebus.framework.nls.NLS;
import org.phoebus.framework.spi.AppDescriptor;
import org.phoebus.framework.spi.AppInstance;
Expand All @@ -34,12 +34,12 @@ public class AlarmLogTable implements AppInstance {
loader.setControllerFactory(clazz -> {
try {
if(clazz.isAssignableFrom(AlarmLogTableController.class)){
return clazz.getConstructor(WebResource.class)
.newInstance(app.getClient());
return clazz.getConstructor(HttpClient.class)
.newInstance(app.httpClient());
}
else if(clazz.isAssignableFrom(AdvancedSearchViewController.class)){
return clazz.getConstructor(WebResource.class)
.newInstance(app.getClient());
return clazz.getConstructor(HttpClient.class)
.newInstance(app.httpClient());
}
} catch (Exception e) {
Logger.getLogger(AlarmLogTable.class.getName()).log(Level.SEVERE, "Failed to construct controller for Alarm Log Table View", e);
Expand Down
Loading

0 comments on commit ee828ea

Please sign in to comment.