|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.insights.core.reader; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | +import org.joda.time.DateTime; |
| 16 | +import org.joda.time.DateTimeZone; |
| 17 | +import org.joda.time.format.DateTimeFormatter; |
| 18 | +import org.opensearch.action.search.SearchRequest; |
| 19 | +import org.opensearch.action.search.SearchResponse; |
| 20 | +import org.opensearch.client.Client; |
| 21 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 22 | +import org.opensearch.index.IndexNotFoundException; |
| 23 | +import org.opensearch.index.query.MatchQueryBuilder; |
| 24 | +import org.opensearch.index.query.QueryBuilder; |
| 25 | +import org.opensearch.index.query.QueryBuilders; |
| 26 | +import org.opensearch.index.query.RangeQueryBuilder; |
| 27 | +import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; |
| 28 | +import org.opensearch.search.SearchHit; |
| 29 | +import org.opensearch.search.builder.SearchSourceBuilder; |
| 30 | + |
| 31 | +/** |
| 32 | + * Local index reader for reading query insights data from local OpenSearch indices. |
| 33 | + */ |
| 34 | +public final class LocalIndexReader implements QueryInsightsReader { |
| 35 | + /** |
| 36 | + * Logger of the local index reader |
| 37 | + */ |
| 38 | + private final Logger logger = LogManager.getLogger(); |
| 39 | + private final Client client; |
| 40 | + private DateTimeFormatter indexPattern; |
| 41 | + private final NamedXContentRegistry namedXContentRegistry; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructor of LocalIndexReader |
| 45 | + * |
| 46 | + * @param client OS client |
| 47 | + * @param indexPattern the pattern of index to read from |
| 48 | + * @param namedXContentRegistry for parsing purposes |
| 49 | + */ |
| 50 | + public LocalIndexReader(final Client client, final DateTimeFormatter indexPattern, final NamedXContentRegistry namedXContentRegistry) { |
| 51 | + this.indexPattern = indexPattern; |
| 52 | + this.client = client; |
| 53 | + this.namedXContentRegistry = namedXContentRegistry; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Getter of indexPattern |
| 58 | + * |
| 59 | + * @return indexPattern |
| 60 | + */ |
| 61 | + public DateTimeFormatter getIndexPattern() { |
| 62 | + return indexPattern; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Setter of indexPattern |
| 67 | + * |
| 68 | + * @param indexPattern index pattern |
| 69 | + * @return the current LocalIndexReader |
| 70 | + */ |
| 71 | + public LocalIndexReader setIndexPattern(DateTimeFormatter indexPattern) { |
| 72 | + this.indexPattern = indexPattern; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Export a list of SearchQueryRecord from local index |
| 78 | + * |
| 79 | + * @param from start timestamp |
| 80 | + * @param to end timestamp |
| 81 | + * @return list of SearchQueryRecords whose timestamps fall between from and to |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public List<SearchQueryRecord> read(final String from, final String to) { |
| 85 | + List<SearchQueryRecord> records = new ArrayList<>(); |
| 86 | + if (from == null || to == null) { |
| 87 | + return records; |
| 88 | + } |
| 89 | + final DateTime start = DateTime.parse(from); |
| 90 | + DateTime end = DateTime.parse(to); |
| 91 | + if (end.compareTo(DateTime.now(DateTimeZone.UTC)) > 0) { |
| 92 | + end = DateTime.now(DateTimeZone.UTC); |
| 93 | + } |
| 94 | + DateTime curr = start; |
| 95 | + while (curr.compareTo(end.plusDays(1).withTimeAtStartOfDay()) < 0) { |
| 96 | + String index = getDateTimeFromFormat(curr); |
| 97 | + SearchRequest searchRequest = new SearchRequest(index); |
| 98 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 99 | + MatchQueryBuilder excludeQuery = QueryBuilders.matchQuery("indices", "top_queries*"); |
| 100 | + RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timestamp").from(start.getMillis()).to(end.getMillis()); |
| 101 | + QueryBuilder query = QueryBuilders.boolQuery().must(rangeQuery).mustNot(excludeQuery); |
| 102 | + searchSourceBuilder.query(query); |
| 103 | + searchRequest.source(searchSourceBuilder); |
| 104 | + try { |
| 105 | + SearchResponse searchResponse = client.search(searchRequest).actionGet(); |
| 106 | + for (SearchHit hit : searchResponse.getHits()) { |
| 107 | + SearchQueryRecord record = SearchQueryRecord.getRecord(hit, namedXContentRegistry); |
| 108 | + records.add(record); |
| 109 | + } |
| 110 | + } catch (IndexNotFoundException ignored) {} catch (Exception e) { |
| 111 | + logger.error("Unable to parse search hit: ", e); |
| 112 | + } |
| 113 | + curr = curr.plusDays(1); |
| 114 | + |
| 115 | + } |
| 116 | + return records; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Close the reader sink |
| 121 | + */ |
| 122 | + @Override |
| 123 | + public void close() { |
| 124 | + logger.debug("Closing the LocalIndexReader.."); |
| 125 | + } |
| 126 | + |
| 127 | + private String getDateTimeFromFormat(DateTime current) { |
| 128 | + return indexPattern.print(current); |
| 129 | + } |
| 130 | +} |
0 commit comments