use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.
the class SearchExecutorTest method appliesSearchExecutionState.
@Test
public void appliesSearchExecutionState() {
final Search search = mockSearch();
final SearchUser searchUser = mock(SearchUser.class);
when(searchUser.canReadView(any())).thenReturn(true);
when(searchUser.canReadStream(any())).thenReturn(true);
when(searchUser.username()).thenReturn("frank-drebin");
final SearchJob searchJob = mock(SearchJob.class);
when(searchJobService.create(search, "frank-drebin")).thenReturn(searchJob);
when(searchJob.getResultFuture()).thenReturn(CompletableFuture.completedFuture(null));
when(queryEngine.execute(searchJob)).thenReturn(searchJob);
when(searchDomain.getForUser(eq("search1"), eq(searchUser))).thenReturn(Optional.of(search));
final ExecutionState executionState = ExecutionState.builder().addAdditionalParameter("foo", 42).build();
this.searchExecutor.execute("search1", searchUser, executionState);
verify(search, times(1)).applyExecutionState(any(), executionStateCaptor.capture());
assertThat(executionStateCaptor.getValue()).isEqualTo(executionState);
}
use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.
the class PivotAggregationSearch method getSearchJob.
private SearchJob getSearchJob(AggregationEventProcessorParameters parameters, String username, long searchWithinMs, long executeEveryMs) throws EventProcessorException {
Search search = Search.builder().queries(ImmutableSet.of(getAggregationQuery(parameters, searchWithinMs, executeEveryMs), getSourceStreamsQuery(parameters))).parameters(config.queryParameters()).build();
// This adds all streams if none were provided
// TODO: Once we introduce "EventProcessor owners" this should only load the permitted streams of the
// user who created this EventProcessor.
search = search.addStreamsToQueriesWithoutStreams(() -> permittedStreams.load((streamId) -> true));
final SearchJob searchJob = queryEngine.execute(searchJobService.create(search, username));
try {
Uninterruptibles.getUninterruptibly(searchJob.getResultFuture(), configurationProvider.get().eventsSearchTimeout(), TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw new EventProcessorException("Error executing search job: " + e.getMessage(), false, eventDefinition, e);
} catch (TimeoutException e) {
throw new EventProcessorException("Timeout while executing search job.", false, eventDefinition, e);
} catch (Exception e) {
throw new EventProcessorException("Unhandled exception in search job.", false, eventDefinition, e);
}
return searchJob;
}
use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.
the class SearchResource method executeSyncJobv2.
@POST
@ApiOperation(value = "Execute a new synchronous search", notes = "Executes a new search and waits for its result", response = SearchJobDTO.class)
@Path("sync")
@NoAuditEvent("Creating audit event manually in method body.")
@Consumes({ SEARCH_FORMAT_V2 })
@Produces({ SEARCH_FORMAT_V2 })
public Response executeSyncJobv2(@ApiParam @NotNull(message = "Search body is mandatory") SearchDTOv2 searchRequest, @ApiParam(name = "timeout", defaultValue = "60000") @QueryParam("timeout") @DefaultValue("60000") long timeout, @Context SearchUser searchUser) {
final Search search = searchRequest.toSearch();
final SearchJob searchJob = searchExecutor.execute(search, searchUser, ExecutionState.empty());
postAuditEvent(searchJob);
final SearchJobDTO searchJobDTO = SearchJobDTO.fromSearchJob(searchJob);
return Response.ok(searchJobDTO).build();
}
use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.
the class SearchResource method executeSyncJob.
@POST
@ApiOperation(value = "Execute a new synchronous search", notes = "Executes a new search and waits for its result", response = SearchJobDTO.class)
@Path("sync")
@NoAuditEvent("Creating audit event manually in method body.")
@Consumes({ MediaType.APPLICATION_JSON, SEARCH_FORMAT_V1 })
@Produces({ MediaType.APPLICATION_JSON, SEARCH_FORMAT_V1 })
public Response executeSyncJob(@ApiParam @NotNull(message = "Search body is mandatory") SearchDTO searchRequest, @ApiParam(name = "timeout", defaultValue = "60000") @QueryParam("timeout") @DefaultValue("60000") long timeout, @Context SearchUser searchUser) {
final Search search = searchRequest.toSearch();
final SearchJob searchJob = searchExecutor.execute(search, searchUser, ExecutionState.empty());
postAuditEvent(searchJob);
final SearchJobDTO searchJobDTO = SearchJobDTO.fromSearchJob(searchJob);
return Response.ok(searchJobDTO).build();
}
use of org.graylog.plugins.views.search.SearchJob in project graylog2-server by Graylog2.
the class SearchResource method extractSearchResponse.
protected SearchResponse extractSearchResponse(SearchJob searchJob, String query, boolean decorate, List<String> fieldList, TimeRange timeRange, Optional<String> streamId) {
final QueryResult queryResult = searchJob.results().values().stream().findFirst().orElseThrow(() -> new IllegalStateException("Missing query result"));
final MessageList.Result result = queryResult.searchTypes().values().stream().findFirst().map(searchTypeResult -> (MessageList.Result) searchTypeResult).orElseThrow(() -> new IllegalStateException("Missing search type result!"));
final long tookMs = queryResult.executionStats().duration();
return buildSearchResponse(query, result, fieldList, tookMs, timeRange, decorate, streamId);
}
Aggregations