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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations