@Produces(MediaType.APPLICATION_JSON)
public String list(
@ApiParam(name = "range", value = "Relative timeframe to search in. See method description.", required = true)
@QueryParam("range")
final int range) {
TermsResult sources;
try {
sources = cache.get(CACHE_KEY + range, new Callable<TermsResult>() {
@Override
public TermsResult call() throws Exception {
try {
return searches.terms("source", 5000, "*", new RelativeRange(range));
} catch (IndexHelper.InvalidRangeFormatException e) {
throw new ExecutionException(e);
} catch (InvalidRangeParametersException e) {
throw new ExecutionException(e);
}
}
});
} catch (ExecutionException e) {
if (e.getCause() instanceof InvalidRangeParametersException) {
LOG.error("Invalid relative time range value.", e);
throw new BadRequestException("Invalid time range " + range);
} else {
LOG.error("Could not calculate list of sources.", e);
throw new WebApplicationException(500);
}
}
Map<String, Object> result = Maps.newHashMap();
result.put("total", sources.getTerms().size());
result.put("sources", sources.getTerms());
result.put("took_ms", sources.took().millis());
result.put("range", range);
return json(result);
}