Skip to content

Commit 55fbcbe

Browse files
committed
Adding optimization based on time index
1 parent 6cfde8a commit 55fbcbe

File tree

1 file changed

+63
-20
lines changed

1 file changed

+63
-20
lines changed

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,35 +219,31 @@ public static List<AlarmLogMessage> search(ElasticsearchClient client,
219219
);
220220

221221
try {
222-
IndexNameHelper indexNameHelper = new IndexNameHelper("*", useDatedIndexNames, indexDateSpanUnits);
223-
String fromIndex = indexNameHelper.getIndexName(fromInstant);
224-
String toIndex = indexNameHelper.getIndexName(toInstant);
225-
if(fromIndex.equalsIgnoreCase(toIndex)) {
226-
indexList.add(fromIndex);
227-
}
222+
indexList = findIndexNames("*", fromInstant, toInstant, indexDateSpanUnits);
228223
} catch (Exception e) {
229-
e.printStackTrace();
224+
logger.log(Level.SEVERE,
225+
"Failed to search for alarm logs:" + e.getMessage(), e);
230226
}
231-
232-
233227
}
234228

235229
int finalSize = maxSize; //Effectively final
236-
SearchRequest searchRequest = SearchRequest.of(r -> r
237-
.query(Query.of(q -> q
238-
.bool(boolQuery.build())
239-
)
240-
)
241-
.size(finalSize)
242-
.sort(SortOptions.of(o -> o
243-
.field(FieldSort.of(f -> f
244-
.field("message_time")
245-
.order(SortOrder.Desc)
246-
)
230+
SearchRequest.Builder searchRequestBuilder = new SearchRequest.Builder();
231+
searchRequestBuilder.query(Query.of(q -> q
232+
.bool(boolQuery.build())
233+
));
234+
searchRequestBuilder.size(finalSize);
235+
searchRequestBuilder.sort(SortOptions.of(o -> o
236+
.field(FieldSort.of(f -> f
237+
.field("message_time")
238+
.order(SortOrder.Desc)
247239
)
248240
)
249241
)
250242
);
243+
if (!indexList.isEmpty()) {
244+
searchRequestBuilder.index(indexList);
245+
}
246+
SearchRequest searchRequest = searchRequestBuilder.build();
251247
try {
252248
SearchResponse<JsonNode> strResponse = client.search(searchRequest, JsonNode.class);
253249
return strResponse.hits().hits().stream().map(hit -> {
@@ -307,4 +303,51 @@ public static List<AlarmLogMessage> searchConfig(ElasticsearchClient client, Str
307303
}
308304
return Collections.emptyList();
309305
}
306+
307+
/**
308+
* return a list of index names between the from and to instant
309+
* @param fromInstant
310+
* @param toInstant
311+
* @param indexDateSpanUnits
312+
* @throws Exception
313+
* @return List of index names
314+
*/
315+
public static List<String> findIndexNames(String baseIndexName, Instant fromInstant, Instant toInstant, String indexDateSpanUnits) throws Exception {
316+
317+
IndexNameHelper fromIndexNameHelper = new IndexNameHelper(baseIndexName, true, indexDateSpanUnits);
318+
IndexNameHelper toIndexNameHelper = new IndexNameHelper(baseIndexName, true, indexDateSpanUnits);
319+
320+
String fromIndex = fromIndexNameHelper.getIndexName(fromInstant);
321+
String toIndex = toIndexNameHelper.getIndexName(toInstant);
322+
323+
List<String> indexList = new ArrayList<>();
324+
if (fromInstant.isBefore(toInstant)) {
325+
if (fromIndex.equalsIgnoreCase(toIndex)) {
326+
indexList.add(fromIndex);
327+
} else {
328+
int indexDateSpanDayValue = -1;
329+
switch (indexDateSpanUnits){
330+
case "Y":
331+
indexDateSpanDayValue = 365;
332+
break;
333+
case "M":
334+
indexDateSpanDayValue = 30;
335+
break;
336+
case "W":
337+
indexDateSpanDayValue = 7;
338+
break;
339+
case "D":
340+
indexDateSpanDayValue = 1;
341+
break;
342+
}
343+
indexList.add(fromIndex);
344+
while (!fromIndex.equalsIgnoreCase(toIndex)) {
345+
fromInstant = fromInstant.plus(indexDateSpanDayValue, ChronoUnit.DAYS);
346+
fromIndex = fromIndexNameHelper.getIndexName(fromInstant);
347+
indexList.add(fromIndex);
348+
}
349+
}
350+
}
351+
return indexList;
352+
}
310353
}

0 commit comments

Comments
 (0)