Search in sources :

Example 1 with MeasurementDataSource

use of org.opennms.netmgt.jasper.measurement.MeasurementDataSource 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)

Example 2 with MeasurementDataSource

use of org.opennms.netmgt.jasper.measurement.MeasurementDataSource in project opennms by OpenNMS.

the class RemoteMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    try {
        QueryRequest queryRequest = unmarshal(query);
        // enforce relaxed mode
        queryRequest.setRelaxed(true);
        query = marshal(queryRequest);
        Result result = connector.execute(useSsl, url, username, password, query);
        // therefore do not allow redirection at all.
        if (result.wasRedirection()) {
            throw new IOException("Request was redirected. This is not supported.");
        }
        // if there is no data
        if (404 == result.getResponseCode()) {
            LOG.warn("Got a 404 (Not Found) response. This might be due to a wrong url or the resource does not exist. Requested URL was: '{}'", url);
            return new EmptyJRDataSource();
        }
        // OK
        if (result.wasSuccessful() && result.getInputStream() != null) {
            return new MeasurementDataSource(result.getInputStream());
        }
        // Error
        ByteArrayOutputStream errorMessageStream = new ByteArrayOutputStream();
        if (result.getErrorStream() != null) {
            ByteStreams.copy(result.getErrorStream(), errorMessageStream);
        }
        throw new JRException("Invalid request. Response was : " + result.getResponseCode() + " (" + result.getResponseMessage() + ")\n" + errorMessageStream.toString());
    } catch (IOException ioException) {
        throw new JRException(ioException);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Example 3 with MeasurementDataSource

use of org.opennms.netmgt.jasper.measurement.MeasurementDataSource in project opennms by OpenNMS.

the class LocalMeasurementDataSourceWrapperTest method verifyContainsResultEvenIfResourceOrAttributeDoNotExist.

// See NMS-8337
@Test
public void verifyContainsResultEvenIfResourceOrAttributeDoNotExist() throws JRException {
    SpringHelper springHelper = MeasurementsHelper.getSpringHelper();
    MeasurementDataSource dataSource = (MeasurementDataSource) new LocalMeasurementDataSourceWrapper(springHelper.getMeasurementFetchStrategy(), springHelper.getExpressionEngine(), springHelper.getFilterEngine()).createDataSource(getQuery());
    Assert.assertEquals(2, dataSource.getRowCount());
    double[] data = new double[] { 13, 17 };
    while (dataSource.next()) {
        Assert.assertEquals(data[dataSource.getCurrentRow()], dataSource.getFieldValue("ifInErrors", dataSource.getCurrentRow()));
        Assert.assertEquals(Double.NaN, dataSource.getFieldValue("ifOutDiscards", dataSource.getCurrentRow()));
        Assert.assertEquals(Double.NaN, dataSource.getFieldValue("ifOutErrors", dataSource.getCurrentRow()));
    }
}
Also used : SpringHelper(org.opennms.netmgt.jasper.helper.SpringHelper) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) LocalMeasurementDataSourceWrapper(org.opennms.netmgt.jasper.measurement.local.LocalMeasurementDataSourceWrapper) Test(org.junit.Test)

Aggregations

MeasurementDataSource (org.opennms.netmgt.jasper.measurement.MeasurementDataSource)3 JRException (net.sf.jasperreports.engine.JRException)2 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)2 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 SpringHelper (org.opennms.netmgt.jasper.helper.SpringHelper)1 LocalMeasurementDataSourceWrapper (org.opennms.netmgt.jasper.measurement.local.LocalMeasurementDataSourceWrapper)1 ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)1 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)1