|
| 1 | +package org.phoebus.alarm.logging.rest; |
| 2 | + |
| 3 | +import co.elastic.clients.elasticsearch.ElasticsearchClient; |
| 4 | +import co.elastic.clients.elasticsearch._types.FieldSort; |
| 5 | +import co.elastic.clients.elasticsearch._types.SortOptions; |
| 6 | +import co.elastic.clients.elasticsearch._types.SortOrder; |
| 7 | +import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery; |
| 8 | +import co.elastic.clients.elasticsearch._types.query_dsl.Query; |
| 9 | +import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery; |
| 10 | +import co.elastic.clients.elasticsearch._types.query_dsl.WildcardQuery; |
| 11 | +import co.elastic.clients.elasticsearch.core.SearchRequest; |
| 12 | +import co.elastic.clients.elasticsearch.core.SearchResponse; |
| 13 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 14 | +import com.fasterxml.jackson.databind.JsonNode; |
| 15 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 16 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 17 | +import org.phoebus.alarm.logging.AlarmLoggingService; |
| 18 | +import org.phoebus.framework.preferences.PreferencesReader; |
| 19 | +import org.phoebus.util.time.TimeParser; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.time.Instant; |
| 23 | +import java.time.ZoneId; |
| 24 | +import java.time.format.DateTimeFormatter; |
| 25 | +import java.time.temporal.ChronoUnit; |
| 26 | +import java.time.temporal.TemporalAmount; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.logging.Level; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +import static org.phoebus.alarm.logging.rest.SearchController.logger; |
| 34 | + |
| 35 | +/** |
| 36 | + * A Job to search for alarm messages logged by the alarm logging service |
| 37 | + * @author Kunal Shroff |
| 38 | + */ |
| 39 | +public class AlarmLogSearchUtil { |
| 40 | + |
| 41 | + private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneId.of("UTC")); |
| 42 | + |
| 43 | + private static final PreferencesReader prefs = new PreferencesReader(AlarmLoggingService.class, "/application.properties"); |
| 44 | + |
| 45 | + private static ObjectMapper mapper; |
| 46 | + static { |
| 47 | + mapper = new ObjectMapper(); |
| 48 | + mapper.registerModule(new JavaTimeModule()); |
| 49 | + } |
| 50 | + |
| 51 | + private static final String PV = "pv"; |
| 52 | + private static final String SEVERITY = "severity"; |
| 53 | + private static final String MESSAGE = "message"; |
| 54 | + private static final String CURRENTSEVERITY = "current_severity"; |
| 55 | + private static final String CURRENTMESSAGE = "current_message"; |
| 56 | + private static final String USER = "user"; |
| 57 | + private static final String HOST = "host"; |
| 58 | + private static final String COMMAND = "command"; |
| 59 | + private static final String STARTTIME = "start"; |
| 60 | + private static final String ENDTIME = "end"; |
| 61 | + |
| 62 | + public static List<AlarmLogMessage> search(ElasticsearchClient client, |
| 63 | + String pattern, |
| 64 | + Map<String, String> searchParameters) { |
| 65 | + logger.info("searching for alarm log entires : " + pattern); |
| 66 | + |
| 67 | + String from = formatter.format(Instant.now().minus(7, ChronoUnit.DAYS)); |
| 68 | + String to = formatter.format(Instant.now()); |
| 69 | + String searchPattern = "*".concat(pattern).concat("*"); |
| 70 | + int size = prefs.getInt("es_max_size"); |
| 71 | + Boolean configSet = false; |
| 72 | + |
| 73 | + BoolQuery.Builder boolQuery = new BoolQuery.Builder(); |
| 74 | + |
| 75 | + for (Map.Entry<String, String> entry : searchParameters.entrySet()) { |
| 76 | + String key = entry.getKey(); |
| 77 | + String value = entry.getValue(); |
| 78 | + if (key.equals("start")) { |
| 79 | + Object time = TimeParser.parseInstantOrTemporalAmount(value); |
| 80 | + if (time instanceof Instant) { |
| 81 | + from = formatter.format((Instant)time); |
| 82 | + } else if (time instanceof TemporalAmount) { |
| 83 | + from = formatter.format(Instant.now().minus((TemporalAmount)time)); |
| 84 | + } |
| 85 | + continue; |
| 86 | + } |
| 87 | + if (key.equals("end")) { |
| 88 | + Object time = TimeParser.parseInstantOrTemporalAmount(value); |
| 89 | + if (time instanceof Instant) { |
| 90 | + to = formatter.format((Instant)time); |
| 91 | + } else if (time instanceof TemporalAmount) { |
| 92 | + to = formatter.format(Instant.now().minus((TemporalAmount)time)); |
| 93 | + } |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (key.equals("size")) { |
| 97 | + size = Math.min(size, Integer.parseInt(value)); |
| 98 | + continue; |
| 99 | + } |
| 100 | + if (!value.equals("*")) { |
| 101 | + if (key.equals("command")) { |
| 102 | + if (value.equalsIgnoreCase("Enabled")) { |
| 103 | + key = "enabled"; |
| 104 | + value = "true"; |
| 105 | + } else if (value.equalsIgnoreCase("Disabled")) { |
| 106 | + key = "enabled"; |
| 107 | + value = "false"; |
| 108 | + } |
| 109 | + } |
| 110 | + if (key.equals("pv")) { |
| 111 | + value = "*".concat(value).concat("*"); |
| 112 | + String finalValue = value; //Effectively final |
| 113 | + boolQuery.must(Query.of(q -> q |
| 114 | + .wildcard(WildcardQuery.of(w -> w |
| 115 | + .field("config") |
| 116 | + .value(finalValue) |
| 117 | + ) |
| 118 | + ) |
| 119 | + ) |
| 120 | + ); |
| 121 | + configSet = true; |
| 122 | + continue; |
| 123 | + } |
| 124 | + //Effectively final |
| 125 | + String finalVal2 = value; |
| 126 | + String finalKey2 = key; |
| 127 | + // |
| 128 | + boolQuery.must(Query.of(q->q |
| 129 | + .wildcard(WildcardQuery.of(w->w |
| 130 | + .field(finalKey2) |
| 131 | + .value(finalVal2) |
| 132 | + ) |
| 133 | + ) |
| 134 | + ) |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + if (!configSet) { |
| 139 | + boolQuery.must(Query.of(q->q |
| 140 | + .wildcard(WildcardQuery.of(w->w |
| 141 | + .field("config") |
| 142 | + .value(searchPattern) |
| 143 | + ) |
| 144 | + ) |
| 145 | + ) |
| 146 | + ); |
| 147 | + } |
| 148 | + //Effectively final |
| 149 | + String finalFrom = from; |
| 150 | + String finalTo = to; |
| 151 | + // |
| 152 | + boolQuery.must( |
| 153 | + Query.of(q->q |
| 154 | + .range(RangeQuery.of(r->r |
| 155 | + .field("message_time") |
| 156 | + .from(finalFrom) |
| 157 | + .to(finalTo) |
| 158 | + ) |
| 159 | + ) |
| 160 | + ) |
| 161 | + ); |
| 162 | + int finalSize = size; //Effectively final |
| 163 | + SearchRequest searchRequest = SearchRequest.of(r->r |
| 164 | + .query(Query.of(q->q |
| 165 | + .bool(boolQuery.build()) |
| 166 | + ) |
| 167 | + ) |
| 168 | + .size(finalSize) |
| 169 | + .sort(SortOptions.of(o->o |
| 170 | + .field(FieldSort.of(f->f |
| 171 | + .field("message_time") |
| 172 | + .order(SortOrder.Desc) |
| 173 | + ) |
| 174 | + ) |
| 175 | + ) |
| 176 | + ) |
| 177 | + ); |
| 178 | + final List<AlarmLogMessage> result = new ArrayList<>(); |
| 179 | + try { |
| 180 | + SearchResponse<JsonNode> strResponse = client.search(searchRequest, JsonNode.class); |
| 181 | + return strResponse.hits().hits().stream().map(hit -> { |
| 182 | + JsonNode jsonNode = hit.source(); |
| 183 | + try { |
| 184 | + return mapper.treeToValue(jsonNode, AlarmLogMessage.class); |
| 185 | + } catch (JsonProcessingException e) { |
| 186 | + logger.log(Level.SEVERE, "Failed to parse the searched alarm log messages. " + hit, e); |
| 187 | + } |
| 188 | + return null; |
| 189 | + }).collect(Collectors.toList()); |
| 190 | + } catch (IOException e) { |
| 191 | + logger.log(Level.SEVERE, "Failed to search for alarm logs ", e); |
| 192 | + } |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments