Skip to content

Commit 75cccb2

Browse files
committed
Adding a REST end point for service info
1 parent b64527a commit 75cccb2

File tree

4 files changed

+115
-71
lines changed

4 files changed

+115
-71
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PropertiesHelper {
1414
* load the default properties from the properties file packaged with the jar
1515
*/
1616
static {
17-
String filename = "alarm_logger.properties";
17+
String filename = "application.properties";
1818
try (InputStream input = PropertiesHelper.class.getClassLoader().getResourceAsStream(filename);) {
1919
if (input != null) {
2020
// load a properties file from class path, inside static method

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

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,95 @@
11
package org.phoebus.alarm.logging.rest;
22

3-
import java.io.IOException;
4-
import java.util.*;
5-
import java.util.logging.Level;
6-
import java.util.logging.Logger;
7-
83
import co.elastic.clients.elasticsearch.ElasticsearchClient;
4+
import co.elastic.clients.elasticsearch._types.ElasticsearchVersionInfo;
95
import co.elastic.clients.elasticsearch._types.FieldSort;
106
import co.elastic.clients.elasticsearch._types.SortOptions;
117
import co.elastic.clients.elasticsearch._types.SortOrder;
128
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
139
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
10+
import co.elastic.clients.elasticsearch.core.InfoResponse;
1411
import co.elastic.clients.elasticsearch.core.SearchRequest;
1512
import co.elastic.clients.elasticsearch.core.SearchResponse;
13+
import com.fasterxml.jackson.core.JsonProcessingException;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
1615
import org.phoebus.alarm.logging.AlarmLoggingService;
1716
import org.phoebus.alarm.logging.ElasticClientHelper;
1817
import org.phoebus.applications.alarm.messages.AlarmStateMessage;
1918
import org.phoebus.framework.preferences.PreferencesReader;
19+
import org.springframework.web.bind.annotation.GetMapping;
2020
import org.springframework.web.bind.annotation.PathVariable;
2121
import org.springframework.web.bind.annotation.RequestMapping;
2222
import org.springframework.web.bind.annotation.RequestMethod;
2323
import org.springframework.web.bind.annotation.RequestParam;
2424
import org.springframework.web.bind.annotation.RestController;
2525

26-
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.LinkedHashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
34+
2735
/**
2836
* A REST service for quering the alarm message history
29-
* @author Kunal Shroff
3037
*
38+
* @author Kunal Shroff
3139
*/
3240
@RestController
3341
@RequestMapping("/alarm-history")
3442
public class SearchController {
3543

3644
private static final Logger logger = Logger.getLogger(SearchController.class.getName());
37-
private final PreferencesReader prefs = new PreferencesReader(AlarmLoggingService.class, "/alarm_logging_service.properties");
45+
private final PreferencesReader prefs = new PreferencesReader(AlarmLoggingService.class, "/application.properties");
46+
47+
private static final ObjectMapper objectMapper = new ObjectMapper();
48+
/**
49+
*
50+
* @return Information about the alarm logging service
51+
*/
52+
@GetMapping
53+
public String info() {
54+
55+
Map<String, Object> alarmLoggingServiceInfo = new LinkedHashMap<String, Object>();
56+
alarmLoggingServiceInfo.put("name", "Alarm logging Service");
57+
//alarmLoggingServiceInfo.put("version", version);
58+
59+
Map<String, String> elasticInfo = new LinkedHashMap<String, String>();
60+
try {
61+
ElasticsearchClient client = ElasticClientHelper.getInstance().getClient();
62+
InfoResponse response = client.info();
63+
64+
elasticInfo.put("status", "Connected");
65+
elasticInfo.put("clusterName", response.clusterName());
66+
elasticInfo.put("clusterUuid", response.clusterUuid());
67+
ElasticsearchVersionInfo version = response.version();
68+
elasticInfo.put("version", version.toString());
69+
} catch (IOException e) {
70+
AlarmLoggingService.logger.log(Level.WARNING, "Failed to create Alarm Logging service info resource.", e);
71+
elasticInfo.put("status", "Failed to connect to elastic " + e.getLocalizedMessage());
72+
}
73+
alarmLoggingServiceInfo.put("elastic", elasticInfo);
74+
try {
75+
return objectMapper.writeValueAsString(alarmLoggingServiceInfo);
76+
} catch (JsonProcessingException e) {
77+
AlarmLoggingService.logger.log(Level.WARNING, "Failed to create Alarm Logging service info resource.", e);
78+
return "Failed to gather Alarm Logging service info";
79+
}
80+
}
3881

3982
@RequestMapping(value = "/search/alarm", method = RequestMethod.GET)
4083
public List<AlarmStateMessage> search(@RequestParam Map<String, String> allRequestParams) {
4184
BoolQuery.Builder boolQuery = new BoolQuery.Builder();
4285
List<Query> queries = new ArrayList<>();
4386
allRequestParams.forEach((key, value) -> queries.add(
44-
Query.of( q->q
45-
.wildcard(w->w
46-
.field(key)
47-
.value(value)
87+
Query.of(q -> q
88+
.wildcard(w -> w
89+
.field(key)
90+
.value(value)
91+
)
4892
)
49-
)
5093
)
5194
);
5295
boolQuery.must(queries);
@@ -55,28 +98,28 @@ public List<AlarmStateMessage> search(@RequestParam Map<String, String> allReque
5598

5699
@RequestMapping(value = "/search/alarm/{pv}", method = RequestMethod.GET)
57100
public List<AlarmStateMessage> searchPv(@PathVariable String pv) {
58-
return esAlarmSearch(BoolQuery.of( b->b
59-
.must(
60-
Query.of( q->q
61-
.wildcard(w->w
62-
.field("pv")
63-
.value(pv)
64-
)
101+
return esAlarmSearch(BoolQuery.of(b -> b
102+
.must(
103+
Query.of(q -> q
104+
.wildcard(w -> w
105+
.field("pv")
106+
.value(pv)
107+
)
108+
)
65109
)
66-
)
67110
)
68-
);
111+
);
69112
}
70113

71114
@RequestMapping(value = "/search/alarm/{config}", method = RequestMethod.GET)
72115
public List<AlarmStateMessage> searchConfig(@PathVariable String config) {
73-
return esAlarmSearch(BoolQuery.of( b->b
74-
.must(
75-
Query.of( q->q
76-
.wildcard(w->w
77-
.field("config")
78-
.value(config)
79-
)
116+
return esAlarmSearch(BoolQuery.of(b -> b
117+
.must(
118+
Query.of(q -> q
119+
.wildcard(w -> w
120+
.field("config")
121+
.value(config)
122+
)
80123
)
81124
)
82125
)
@@ -88,24 +131,24 @@ private List<AlarmStateMessage> esAlarmSearch(BoolQuery query) {
88131
List<AlarmStateMessage> result = new ArrayList<>();
89132
try {
90133
SearchResponse<AlarmStateMessage> response = client.search(
91-
SearchRequest.of( req->req
134+
SearchRequest.of(req -> req
92135
.size(prefs.getInt("es_max_size"))
93136
.query(Query.of(
94-
q->q.bool(query)
95-
)
137+
q -> q.bool(query)
138+
)
96139
)
97-
.sort(SortOptions.of(s->s
98-
.field(FieldSort.of(f->f
99-
.field("time")
100-
.order(SortOrder.Desc)
101-
)
140+
.sort(SortOptions.of(s -> s
141+
.field(FieldSort.of(f -> f
142+
.field("time")
143+
.order(SortOrder.Desc)
144+
)
145+
)
102146
)
103-
)
104147
)
105148
),
106149
AlarmStateMessage.class
107150
);
108-
response.hits().hits().forEach(hit->result.add(hit.source()));
151+
response.hits().hits().forEach(hit -> result.add(hit.source()));
109152
return result;
110153
} catch (IOException e) {
111154
logger.log(Level.SEVERE, "Failed to search for alarm logs ", e);

services/alarm-logger/src/main/resources/alarm_logger.properties

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
# The server port for the rest service
2-
#server.port=9000
2+
server.port=9000
33

44
# Disable the spring banner
55
spring.main.banner-mode=off
66

77
# Suppress the logging from spring boot during debugging this should be set to DEBUG
8-
logging.level.root=WARN
8+
logging.level.root=WARN
9+
10+
# Alarm topics to be logged, they can be defined as a comma separated list
11+
alarm_topics=Accelerator
12+
13+
# location of elastic node/s
14+
es_host=localhost
15+
es_port=9228
16+
# max default size for es queries
17+
es_max_size=1000
18+
# set to 'true' if sniffing to be enabled to discover other cluster nodes
19+
es_sniff=false
20+
21+
# when set to true, the service will automatically create the index templates needed
22+
es_create_templates=true
23+
24+
# Kafka server location
25+
bootstrap.servers=localhost:19092
26+
27+
# Kafka client properties file
28+
kafka_properties=
29+
30+
# The units of the indices date span: Days (D), Weeks(W), Months(M), Years(Y).
31+
date_span_units=M
32+
33+
# The value of the indices date span.
34+
# An integer value to be combined with the units. For example, if months were selected and the value set to 2, each index would span 2 months.
35+
date_span_value=1
36+
37+
# Size of the thread pool for message and command loggers. Two threads per topic/configuration are required
38+
thread_pool_size=4

0 commit comments

Comments
 (0)