Skip to content

Commit 36d227b

Browse files
committed
Add a separate utility function for quering for configuration info
1 parent a530a7c commit 36d227b

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/rest/AlarmLogMessage.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.phoebus.alarm.logging.rest;
22

3-
import com.fasterxml.jackson.annotation.JsonFormat;
4-
import com.fasterxml.jackson.annotation.JsonIgnore;
53
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
64
import com.fasterxml.jackson.annotation.JsonInclude;
75
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -15,8 +13,6 @@
1513
import java.time.Instant;
1614
import java.time.ZoneId;
1715
import java.time.format.DateTimeFormatter;
18-
import java.time.temporal.TemporalAccessor;
19-
import java.util.TimeZone;
2016

2117
@JsonIgnoreProperties(ignoreUnknown = true)
2218
@JsonInclude(Include.NON_NULL)
@@ -39,7 +35,7 @@ public class AlarmLogMessage {
3935
private String host;
4036
private String command;
4137
private boolean enabled;
42-
38+
4339
public String getConfig() {
4440
return config;
4541
}
@@ -63,7 +59,7 @@ public String getSeverity() {
6359
public void setSeverity(String severity) {
6460
this.severity = severity;
6561
}
66-
62+
6763
public String getMessage() {
6864
return message;
6965
}
@@ -151,7 +147,7 @@ public String getCommand() {
151147
public void setCommand(String command) {
152148
this.command = command;
153149
}
154-
150+
155151
public boolean isEnabled() {
156152
return enabled;
157153
}

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/rest/AlarmLogSearchUtil.java

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.time.temporal.ChronoUnit;
2626
import java.time.temporal.TemporalAmount;
2727
import java.util.ArrayList;
28+
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.logging.Level;
@@ -61,6 +62,13 @@ public class AlarmLogSearchUtil {
6162
private static final String STARTTIME = "start";
6263
private static final String ENDTIME = "end";
6364

65+
/**
66+
* Find all the log (state and config) messages which match the search criteria
67+
*
68+
* @param client elastic client
69+
* @param searchParameters search parameters
70+
* @return list of alarm state and config messages
71+
*/
6472
public static List<AlarmLogMessage> search(ElasticsearchClient client,
6573
Map<String, String> searchParameters) {
6674
logger.info("searching for alarm log entires : " +
@@ -118,40 +126,46 @@ public static List<AlarmLogMessage> search(ElasticsearchClient client,
118126
configSet = true;
119127
break;
120128
case SEVERITY:
121-
boolQuery.must(WildcardQuery.of(w -> w
122-
.field(SEVERITY)
123-
.value(parameter.getValue().strip().toUpperCase()))._toQuery()
124-
);
129+
if (!parameter.getValue().equalsIgnoreCase("*"))
130+
boolQuery.must(WildcardQuery.of(w -> w
131+
.field(SEVERITY)
132+
.value(parameter.getValue().strip().toUpperCase()))._toQuery()
133+
);
125134
break;
126135
case CURRENTSEVERITY:
127-
boolQuery.must(WildcardQuery.of(w -> w
128-
.field(CURRENTSEVERITY)
129-
.value(parameter.getValue().strip().toUpperCase()))._toQuery()
130-
);
136+
if (!parameter.getValue().equalsIgnoreCase("*"))
137+
boolQuery.must(WildcardQuery.of(w -> w
138+
.field(CURRENTSEVERITY)
139+
.value(parameter.getValue().strip().toUpperCase()))._toQuery()
140+
);
131141
break;
132142
case MESSAGE:
133-
boolQuery.must(WildcardQuery.of(w -> w
134-
.field(MESSAGE)
135-
.value(parameter.getValue().strip()))._toQuery()
136-
);
143+
if (!parameter.getValue().equalsIgnoreCase("*"))
144+
boolQuery.must(WildcardQuery.of(w -> w
145+
.field(MESSAGE)
146+
.value(parameter.getValue().strip()))._toQuery()
147+
);
137148
break;
138149
case CURRENTMESSAGE:
139-
boolQuery.must(WildcardQuery.of(w -> w
140-
.field(CURRENTMESSAGE)
141-
.value(parameter.getValue().strip()))._toQuery()
142-
);
150+
if (!parameter.getValue().equalsIgnoreCase("*"))
151+
boolQuery.must(WildcardQuery.of(w -> w
152+
.field(CURRENTMESSAGE)
153+
.value(parameter.getValue().strip()))._toQuery()
154+
);
143155
break;
144156
case USER:
145-
boolQuery.must(WildcardQuery.of(w -> w
146-
.field(USER)
147-
.value(parameter.getValue().strip()))._toQuery()
148-
);
157+
if (!parameter.getValue().equalsIgnoreCase("*"))
158+
boolQuery.must(WildcardQuery.of(w -> w
159+
.field(USER)
160+
.value(parameter.getValue().strip()))._toQuery()
161+
);
149162
break;
150163
case HOST:
151-
boolQuery.must(WildcardQuery.of(w -> w
152-
.field(HOST)
153-
.value(parameter.getValue().strip()))._toQuery()
154-
);
164+
if (!parameter.getValue().equalsIgnoreCase("*"))
165+
boolQuery.must(WildcardQuery.of(w -> w
166+
.field(HOST)
167+
.value(parameter.getValue().strip()))._toQuery()
168+
);
155169
break;
156170
default:
157171
// Unsupported search parameters are ignored
@@ -204,7 +218,6 @@ public static List<AlarmLogMessage> search(ElasticsearchClient client,
204218
)
205219
)
206220
);
207-
final List<AlarmLogMessage> result = new ArrayList<>();
208221
try {
209222
SearchResponse<JsonNode> strResponse = client.search(searchRequest, JsonNode.class);
210223
return strResponse.hits().hits().stream().map(hit -> {
@@ -219,7 +232,49 @@ public static List<AlarmLogMessage> search(ElasticsearchClient client,
219232
} catch (IOException e) {
220233
logger.log(Level.SEVERE, "Failed to search for alarm logs ", e);
221234
}
222-
return result;
235+
return Collections.emptyList();
223236
}
224237

238+
/**
239+
* Return the latest alarm config message associated with 'config'
240+
*
241+
* @param client elastic client
242+
* @param configPattern the wildcard pattern which matches the 'config'
243+
* @return last alarm config message for the given 'config'
244+
*/
245+
public static List<AlarmLogMessage> searchConfig(ElasticsearchClient client, String configPattern) {
246+
String searchPattern = "*".concat(configPattern).concat("*");
247+
int size = 1;
248+
249+
SearchRequest searchRequest = SearchRequest.of(r -> r
250+
.query(Query.of(q -> q.wildcard(WildcardQuery.of(w -> w.field("config").value(searchPattern)))
251+
)
252+
)
253+
.size(size)
254+
.sort(SortOptions.of(o -> o
255+
.field(FieldSort.of(f -> f
256+
.field("message_time")
257+
.order(SortOrder.Desc)
258+
)
259+
)
260+
)
261+
)
262+
);
263+
264+
try {
265+
SearchResponse<JsonNode> strResponse = client.search(searchRequest, JsonNode.class);
266+
return strResponse.hits().hits().stream().map(hit -> {
267+
JsonNode jsonNode = hit.source();
268+
try {
269+
return mapper.treeToValue(jsonNode, AlarmLogMessage.class);
270+
} catch (JsonProcessingException e) {
271+
logger.log(Level.SEVERE, "Failed to parse the searched alarm config messages. " + hit, e);
272+
}
273+
return null;
274+
}).collect(Collectors.toList());
275+
} catch (IOException e) {
276+
logger.log(Level.SEVERE, "Failed to search for alarm config logs ", e);
277+
}
278+
return Collections.emptyList();
279+
}
225280
}

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/rest/SearchController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,17 @@ public List<AlarmLogMessage> search(@RequestParam Map<String, String> allRequest
8686
return result;
8787
}
8888

89-
@RequestMapping(value = "/search/alarm/{pv}", method = RequestMethod.GET)
89+
@RequestMapping(value = "/search/alarm/pv/{pv}", method = RequestMethod.GET)
9090
public List<AlarmLogMessage> searchPv(@PathVariable String pv) {
9191
Map<String, String> searchParameters = new HashMap<>();
9292
searchParameters.put("pv", pv);
9393
List<AlarmLogMessage> result = AlarmLogSearchUtil.search(ElasticClientHelper.getInstance().getClient(), searchParameters);
9494
return result;
9595
}
9696

97-
@RequestMapping(value = "/search/alarm/{config}", method = RequestMethod.GET)
97+
@RequestMapping(value = "/search/alarm/config/{config}", method = RequestMethod.GET)
9898
public List<AlarmLogMessage> searchConfig(@PathVariable String config) {
99-
Map<String, String> searchParameters = new HashMap<>();
100-
searchParameters.put("config", config);
101-
List<AlarmLogMessage> result = AlarmLogSearchUtil.search(ElasticClientHelper.getInstance().getClient(), searchParameters);
99+
List<AlarmLogMessage> result = AlarmLogSearchUtil.searchConfig(ElasticClientHelper.getInstance().getClient(), config);
102100
return result;
103101
}
104102

0 commit comments

Comments
 (0)