use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.
the class DefaultMeasurementsService method query.
@Override
public QueryResponse query(QueryRequest request) throws MeasurementException {
validate(request);
// Fetch the measurements
FetchResults results;
try {
results = fetchStrategy.fetch(request.getStart(), request.getEnd(), request.getStep(), request.getMaxRows(), request.getHeartbeat(), request.getInterval(), request.getSources(), request.isRelaxed());
} catch (Exception e) {
throw new FetchException(e, "Fetch failed: {}", e.getMessage());
}
if (results == null) {
throw new ResourceNotFoundException(request);
}
// Apply the expression to the fetch results
expressionEngine.applyExpressions(request, results);
// Apply the filters
if (!request.getFilters().isEmpty()) {
RowSortedTable<Long, String, Double> table = results.asRowSortedTable();
filterEngine.filter(request.getFilters(), table);
results = new FetchResults(table, results.getStep(), results.getConstants());
}
// Remove any transient values belonging to sources
final Map<String, double[]> columns = results.getColumns();
for (final Source source : request.getSources()) {
if (source.getTransient()) {
columns.remove(source.getLabel());
}
}
// Build the response
final QueryResponse response = new QueryResponse();
response.setStart(request.getStart());
response.setEnd(request.getEnd());
response.setStep(results.getStep());
response.setTimestamps(results.getTimestamps());
response.setColumns(results.getColumns());
response.setConstants(results.getConstants());
return response;
}
use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.
the class MeasurementsRestService method query.
/**
* Retrieves the measurements of many resources and performs
* arbitrary calculations on these.
*
* This a read-only query, however we use a POST instead of GET
* since the request parameters are difficult to express in a query string.
*/
@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional(readOnly = true)
public QueryResponse query(final QueryRequest request) {
Preconditions.checkState(service != null);
LOG.debug("Executing query with {}", request);
QueryResponse response = null;
try {
response = service.query(request);
} catch (ExpressionException e) {
throw getException(Status.BAD_REQUEST, e, "An error occurred while evaluating an expression: {}", e.getMessage());
} catch (FilterException | ValidationException e) {
throw getException(Status.BAD_REQUEST, e, e.getMessage());
} catch (ResourceNotFoundException e) {
throw getException(Status.NOT_FOUND, e, e.getMessage());
} catch (FetchException e) {
throw getException(Status.INTERNAL_SERVER_ERROR, e, e.getMessage());
} catch (Exception e) {
throw getException(Status.INTERNAL_SERVER_ERROR, e, "Query failed: {}", e.getMessage());
}
// Return a 204 if there are no columns
if (response.getColumns().length == 0) {
throw getException(Status.NO_CONTENT, "No content.");
}
return response;
}
use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.
the class LocalMeasurementDataSourceWrapper method createDataSource.
@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
Objects.requireNonNull(query);
QueryRequest queryRequest = unmarshal(query);
Objects.requireNonNull(queryRequest);
// Enforce relaxed mode
queryRequest.setRelaxed(true);
try {
QueryResponse response = fetchService.query(queryRequest);
return new MeasurementDataSource(response);
} catch (ResourceNotFoundException rnfe) {
LOG.warn("A attribute or resource was not found", rnfe);
return new EmptyJRDataSource();
} catch (Exception e) {
LOG.error("An error occurred while fetching the measurement results", e);
throw new JRException(e);
}
}
Aggregations