Search in sources :

Example 1 with FetchException

use of org.opennms.netmgt.measurements.api.exceptions.FetchException 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;
}
Also used : QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) MeasurementException(org.opennms.netmgt.measurements.api.exceptions.MeasurementException) ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) Source(org.opennms.netmgt.measurements.model.Source)

Example 2 with FetchException

use of org.opennms.netmgt.measurements.api.exceptions.FetchException 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;
}
Also used : ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) FilterException(org.opennms.netmgt.measurements.api.exceptions.FilterException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ExpressionException(org.opennms.netmgt.measurements.api.exceptions.ExpressionException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) FilterException(org.opennms.netmgt.measurements.api.exceptions.FilterException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) ExpressionException(org.opennms.netmgt.measurements.api.exceptions.ExpressionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

FetchException (org.opennms.netmgt.measurements.api.exceptions.FetchException)2 ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)2 ValidationException (org.opennms.netmgt.measurements.api.exceptions.ValidationException)2 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)2 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ExpressionException (org.opennms.netmgt.measurements.api.exceptions.ExpressionException)1 FilterException (org.opennms.netmgt.measurements.api.exceptions.FilterException)1 MeasurementException (org.opennms.netmgt.measurements.api.exceptions.MeasurementException)1 Source (org.opennms.netmgt.measurements.model.Source)1 Transactional (org.springframework.transaction.annotation.Transactional)1