2525import java .time .temporal .ChronoUnit ;
2626import java .time .temporal .TemporalAmount ;
2727import java .util .ArrayList ;
28+ import java .util .Collections ;
2829import java .util .List ;
2930import java .util .Map ;
3031import 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}
0 commit comments