Search in sources :

Example 6 with QueryRequest

use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method canRetrieveAttributeWhenFallbackAttributeIsSet.

@Test
public void canRetrieveAttributeWhenFallbackAttributeIsSet() {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1417046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    // Average
    Source ifInOctetsAvg = new Source();
    ifInOctetsAvg.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsAvg.setAttribute("ifInOctets");
    ifInOctetsAvg.setFallbackAttribute("willNotBeFound");
    ifInOctetsAvg.setAggregation("AVERAGE");
    ifInOctetsAvg.setLabel("ifInOctetsAvg");
    request.setSources(Lists.newArrayList(ifInOctetsAvg));
    // Perform the query
    QueryResponse response = m_svc.query(request);
    // Validate the results
    long[] timestamps = response.getTimestamps();
    final Map<String, double[]> columns = response.columnsWithLabels();
    assertEquals(3600000L, response.getStep());
    assertEquals(680, timestamps.length);
    // Verify the values at an arbitrary index
    final int idx = 8;
    assertEquals(1414630800000L, timestamps[idx]);
    assertEquals(270.66140826873385, columns.get("ifInOctetsAvg")[idx], 0.0001);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Example 7 with QueryRequest

use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method badRequestOnMissingResourceId.

@Test
public void badRequestOnMissingResourceId() {
    final QueryRequest request = buildRequest();
    request.getSources().get(0).setResourceId(null);
    exception.expect(exceptionWithResponseCode(400));
    m_svc.query(request);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Test(org.junit.Test)

Example 8 with QueryRequest

use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method badRequestOnMissingAttribute.

@Test
public void badRequestOnMissingAttribute() {
    final QueryRequest request = buildRequest();
    request.getSources().get(0).setResourceId(null);
    exception.expect(exceptionWithResponseCode(400));
    m_svc.query(request);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Test(org.junit.Test)

Example 9 with QueryRequest

use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.

the class MeasurementsRestService method simpleQuery.

/**
     * Retrieves the measurements for a single attribute.
     */
@GET
@Path("{resourceId}/{attribute}")
@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 simpleQuery(@PathParam("resourceId") final String resourceId, @PathParam("attribute") final String attribute, @DefaultValue("-14400000") @QueryParam("start") final long start, @DefaultValue("0") @QueryParam("end") final long end, @DefaultValue("300000") @QueryParam("step") final long step, @DefaultValue("0") @QueryParam("maxrows") final int maxrows, @DefaultValue("") @QueryParam("fallback-attribute") final String fallbackAttribute, @DefaultValue("AVERAGE") @QueryParam("aggregation") final String aggregation, @DefaultValue("false") @QueryParam("relaxed") final boolean relaxed) {
    QueryRequest request = new QueryRequest();
    // If end is not strictly positive, use the current timestamp
    request.setEnd(end > 0 ? end : new Date().getTime());
    // If start is negative, subtract it from the end
    request.setStart(start >= 0 ? start : request.getEnd() + start);
    // Make sure the resulting start time is not negative
    if (request.getStart() < 0) {
        request.setStart(0);
    }
    request.setStep(step);
    request.setMaxRows(maxrows);
    request.setRelaxed(relaxed);
    // Use the attribute name as the datasource and label
    Source source = new Source(attribute, resourceId, attribute, attribute, false);
    source.setFallbackAttribute(fallbackAttribute);
    source.setAggregation(aggregation);
    request.setSources(Lists.newArrayList(source));
    return query(request);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Date(java.util.Date) Source(org.opennms.netmgt.measurements.model.Source) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with QueryRequest

use of org.opennms.netmgt.measurements.model.QueryRequest 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);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) JRException(net.sf.jasperreports.engine.JRException) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Aggregations

QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)21 Test (org.junit.Test)13 Source (org.opennms.netmgt.measurements.model.Source)13 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)7 Expression (org.opennms.netmgt.measurements.model.Expression)3 JRException (net.sf.jasperreports.engine.JRException)2 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)2 MeasurementDataSource (org.opennms.netmgt.jasper.measurement.MeasurementDataSource)2 Request (com.github.tomakehurst.wiremock.http.Request)1 LoggedRequest (com.github.tomakehurst.wiremock.verification.LoggedRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)1