private CacheQuery createRequestLogsQuery(CapedwarfLogQuery logQuery) { LogQuery query = logQuery.getQuery(); QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(CapedwarfRequestLogs.class).get(); List<Query> queries = new ArrayList<Query>(); if (query.getStartTimeUsec() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.END_TIME_USEC) .above(query.getStartTimeUsec()) .createQuery()); } if (query.getEndTimeUsec() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.END_TIME_USEC) .below(query.getEndTimeUsec()) .createQuery()); } if (query.getMinLogLevel() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.MAX_LOG_LEVEL) .above(query.getMinLogLevel().ordinal()) .createQuery()); } boolean onlyCompleteRequests = !Boolean.TRUE.equals(query.getIncludeIncomplete()); if (onlyCompleteRequests) { queries.add( queryBuilder .keyword() .onField(CapedwarfRequestLogs.FINISHED) .matching(Boolean.TRUE) .createQuery()); } Query luceneQuery = getQuery(queryBuilder, queries); CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, CapedwarfRequestLogs.class); cacheQuery.sort( new Sort(new SortField(CapedwarfRequestLogs.END_TIME_USEC, SortField.LONG, true))); cacheQuery.firstResult(logQuery.getOptions().getOffset()); if (logQuery.getOptions().getLimit() != null) { cacheQuery.maxResults(logQuery.getOptions().getLimit()); } return cacheQuery; }
@SuppressWarnings("unchecked") private CapedwarfLogQueryResult fetchCapedwarfRequestLogs(CapedwarfLogQuery logQuery) { List<CapedwarfRequestLogs> list; int resultCount; List<String> requestIds = logQuery.getQuery().getRequestIds(); if (requestIds.isEmpty()) { CacheQuery cacheQuery = createRequestLogsQuery(logQuery); list = (List<CapedwarfRequestLogs>) (List) cacheQuery.list(); resultCount = cacheQuery.getResultSize(); } else { list = new ArrayList<>(requestIds.size()); for (String requestId : requestIds) { CapedwarfRequestLogs requestLogs = (CapedwarfRequestLogs) store.get(requestId); if (requestLogs != null) { list.add(requestLogs); } } resultCount = list.size(); } for (int i = 0; i < list.size(); i++) { CapedwarfRequestLogs capedwarfRequestLogs = list.get(i); list.set(i, capedwarfRequestLogs.clone()); } return new CapedwarfLogQueryResult(list, resultCount); }
@Override public CapedwarfLogQueryResult fetch(CapedwarfLogQuery logQuery) { CapedwarfLogQueryResult result = fetchCapedwarfRequestLogs(logQuery); for (CapedwarfRequestLogs capedwarfRequestLogs : result.getCapedwarfRequestLogs()) { if (logQuery.getQuery().getIncludeAppLogs()) { fetchAppLogLines(capedwarfRequestLogs.getRequestLogs(), logQuery); } } return result; }