11package 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-
83import co .elastic .clients .elasticsearch .ElasticsearchClient ;
4+ import co .elastic .clients .elasticsearch ._types .ElasticsearchVersionInfo ;
95import co .elastic .clients .elasticsearch ._types .FieldSort ;
106import co .elastic .clients .elasticsearch ._types .SortOptions ;
117import co .elastic .clients .elasticsearch ._types .SortOrder ;
128import co .elastic .clients .elasticsearch ._types .query_dsl .BoolQuery ;
139import co .elastic .clients .elasticsearch ._types .query_dsl .Query ;
10+ import co .elastic .clients .elasticsearch .core .InfoResponse ;
1411import co .elastic .clients .elasticsearch .core .SearchRequest ;
1512import co .elastic .clients .elasticsearch .core .SearchResponse ;
13+ import com .fasterxml .jackson .core .JsonProcessingException ;
14+ import com .fasterxml .jackson .databind .ObjectMapper ;
1615import org .phoebus .alarm .logging .AlarmLoggingService ;
1716import org .phoebus .alarm .logging .ElasticClientHelper ;
1817import org .phoebus .applications .alarm .messages .AlarmStateMessage ;
1918import org .phoebus .framework .preferences .PreferencesReader ;
19+ import org .springframework .web .bind .annotation .GetMapping ;
2020import org .springframework .web .bind .annotation .PathVariable ;
2121import org .springframework .web .bind .annotation .RequestMapping ;
2222import org .springframework .web .bind .annotation .RequestMethod ;
2323import org .springframework .web .bind .annotation .RequestParam ;
2424import 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" )
3442public 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 );
0 commit comments