use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class RrdStatisticAttributeVisitorTest method testVisitTwice.
public void testVisitTwice() throws Exception {
final RrdStatisticAttributeVisitor attributeVisitor1 = new RrdStatisticAttributeVisitor();
attributeVisitor1.setFetchStrategy(m_fetchStrategy);
attributeVisitor1.setConsolidationFunction("AVERAGE");
attributeVisitor1.setStartTime(m_startTime);
attributeVisitor1.setEndTime(m_endTime);
attributeVisitor1.setStatisticVisitor(m_statisticVisitor);
attributeVisitor1.afterPropertiesSet();
final RrdStatisticAttributeVisitor attributeVisitor2 = new RrdStatisticAttributeVisitor();
attributeVisitor2.setFetchStrategy(m_fetchStrategy);
attributeVisitor2.setConsolidationFunction("AVERAGE");
attributeVisitor2.setStartTime(m_startTime);
attributeVisitor2.setEndTime(m_endTime);
attributeVisitor2.setStatisticVisitor(m_statisticVisitor);
attributeVisitor2.afterPropertiesSet();
MockResourceType resourceType = new MockResourceType();
resourceType.setName("interfaceSnmp");
OnmsAttribute attribute = new RrdGraphAttribute("ifInOctets", "something", "something else");
attribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(attribute), ResourcePath.get("foo")));
Source source = new Source();
source.setLabel("result");
source.setResourceId(attribute.getResource().getId().toString());
source.setAttribute(attribute.getName());
source.setAggregation(attributeVisitor1.getConsolidationFunction().toUpperCase());
expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(new FetchResults(new long[] { m_startTime }, Collections.singletonMap("result", new double[] { 1.0 }), m_endTime - m_startTime, Collections.emptyMap()));
m_statisticVisitor.visit(attribute, 1.0);
expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(new FetchResults(new long[] { m_startTime }, Collections.singletonMap("result", new double[] { 2.0 }), m_endTime - m_startTime, Collections.emptyMap()));
m_statisticVisitor.visit(attribute, 2.0);
m_mocks.replayAll();
attributeVisitor1.visit(attribute);
attributeVisitor2.visit(attribute);
m_mocks.verifyAll();
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class RrdStatisticAttributeVisitor method visit.
/**
* {@inheritDoc}
*/
@Override
public void visit(OnmsAttribute attribute) {
if (!RrdGraphAttribute.class.isAssignableFrom(attribute.getClass())) {
// Nothing to do if we can't cast to an RrdGraphAttribute
return;
}
final Source source = new Source();
source.setLabel("result");
source.setResourceId(attribute.getResource().getId().toString());
source.setAttribute(attribute.getName());
source.setAggregation(m_consolidationFunction.toUpperCase());
final FetchResults results;
try {
results = m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false);
} catch (final Exception e) {
LOG.warn("Failed to fetch statistic: {}", source, e);
return;
}
if (results == null) {
LOG.warn("No statistic found: {}", source);
return;
}
final double[] statistics = results.getColumns().get(source.getLabel());
if (statistics == null || statistics.length == 0) {
LOG.warn("Statistic is empty: {}", source);
return;
}
final Aggregator aggregator = Aggregators.valueOf(m_consolidationFunction.toUpperCase()).get();
Arrays.stream(statistics).filter(v -> (!Double.isNaN(v))).forEach(v -> {
LOG.debug("Aggregating: {}", v);
aggregator.aggregate(v);
});
double statistic = aggregator.getValue();
LOG.debug("The value of {} is {}", attribute, statistic);
/*
* We don't want to do anything with NaN data, since
* it means there is no data. We especially want to
* stay away from it, because it will be sorted as
* being higher than any numeric value, which will
* leave our TopN report with most, if not all NaN
* values at the top.
*/
if (Double.isNaN(statistic)) {
return;
}
m_statisticVisitor.visit(attribute, statistic);
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class JrobinFetchStrategy method fetchMeasurements.
/**
* {@inheritDoc}
*/
@Override
protected FetchResults fetchMeasurements(long start, long end, long step, int maxrows, Map<Source, String> rrdsBySource, Map<String, Object> constants) throws RrdException {
final long startInSeconds = (long) Math.floor(start / 1000d);
final long endInSeconds = (long) Math.floor(end / 1000d);
long stepInSeconds = (long) Math.floor(step / 1000d);
// The step must be strictly positive
if (stepInSeconds <= 0) {
stepInSeconds = 1;
}
final DataProcessor dproc = new DataProcessor(startInSeconds, endInSeconds);
if (maxrows > 0) {
dproc.setPixelCount(maxrows);
}
dproc.setFetchRequestResolution(stepInSeconds);
for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
final Source source = entry.getKey();
final String rrdFile = entry.getValue();
dproc.addDatasource(source.getLabel(), rrdFile, source.getEffectiveDataSource(), source.getAggregation());
}
try {
dproc.processData();
} catch (IOException e) {
throw new RrdException("JRB processing failed.", e);
}
final long[] timestamps = dproc.getTimestamps();
// Convert the timestamps to milliseconds
for (int i = 0; i < timestamps.length; i++) {
timestamps[i] *= 1000;
}
final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(rrdsBySource.keySet().size());
for (Source source : rrdsBySource.keySet()) {
columns.put(source.getLabel(), dproc.getValues(source.getLabel()));
}
return new FetchResults(timestamps, columns, dproc.getStep() * 1000, constants);
}
use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.
the class ReportDefinitionTest method testUnfilteredResourceAttributeFilteringWithMatch.
public void testUnfilteredResourceAttributeFilteringWithMatch() throws Exception {
OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
Set<OnmsAttribute> attributes = new HashSet<>();
attributes.add(rrdAttribute);
attributes.add(externalValueAttribute);
MockResourceType resourceType = new MockResourceType();
resourceType.setName("interfaceSnmp");
OnmsResource resource = new OnmsResource("1", "Node One", resourceType, attributes, ResourcePath.get("foo"));
EasyMock.expect(m_resourceDao.findTopLevelResources()).andReturn(Collections.singletonList(resource));
ReportDefinition def = createReportDefinition();
def.setResourceAttributeKey(externalValueAttribute.getName());
def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
rrdAttribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(rrdAttribute), ResourcePath.get("foo")));
Source source = new Source();
source.setLabel("result");
source.setResourceId(rrdAttribute.getResource().getId().toString());
source.setAttribute(rrdAttribute.getName());
source.setAggregation("AVERAGE");
FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
m_mocks.replayAll();
report.walk();
m_mocks.verifyAll();
assertEquals("results size", 1, report.getResults().size());
m_mocks.replayAll();
}
Aggregations