throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED,
"Method not allowed", "The HTTP method [" + query.method().getName() +
"] is not permitted for this endpoint");
}
final TSQuery data_query;
if (query.method() == HttpMethod.POST) {
switch (query.apiVersion()) {
case 0:
case 1:
data_query = query.serializer().parseQueryV1();
break;
default:
throw new BadRequestException(HttpResponseStatus.NOT_IMPLEMENTED,
"Requested API version not implemented", "Version " +
query.apiVersion() + " is not implemented");
}
} else {
data_query = this.parseQuery(tsdb, query);
}
// validate and then compile the queries
try {
LOG.debug(data_query.toString());
data_query.validateAndSetQuery();
} catch (Exception e) {
throw new BadRequestException(HttpResponseStatus.BAD_REQUEST,
e.getMessage(), data_query.toString(), e);
}
Query[] tsdbqueries = data_query.buildQueries(tsdb);
final int nqueries = tsdbqueries.length;
final ArrayList<DataPoints[]> results =
new ArrayList<DataPoints[]>(nqueries);
final ArrayList<Deferred<DataPoints[]>> deferreds =
new ArrayList<Deferred<DataPoints[]>>(nqueries);
for (int i = 0; i < nqueries; i++) {
deferreds.add(tsdbqueries[i].runAsync());
}
/**
* After all of the queries have run, we get the results in the order given
* and add dump the results in an array
*/
class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {
public Object call(final ArrayList<DataPoints[]> query_results)
throws Exception {
results.addAll(query_results);
return null;
}
}
// if the user wants global annotations, we need to scan and fetch
// TODO(cl) need to async this at some point. It's not super straight
// forward as we can't just add it to the "deferreds" queue since the types
// are different.
List<Annotation> globals = null;
if (!data_query.getNoAnnotations() && data_query.getGlobalAnnotations()) {
try {
globals = Annotation.getGlobalAnnotations(tsdb,
data_query.startTime() / 1000, data_query.endTime() / 1000)
.joinUninterruptibly();
} catch (Exception e) {
throw new RuntimeException("Shouldn't be here", e);
}
}