use of org.opennms.newts.api.Measurement in project newts by OpenNMS.
the class ResultSerializationTest method testMeasurements.
@Test
public void testMeasurements() throws JsonProcessingException {
Results<Measurement> data = new Results<>();
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifInOctets", 5000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifOutOctets", 6000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifInOctets", 6000));
data.addElement(new Measurement(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifOutOctets", 7000));
String json = "[" + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000000000," + " \"value\":6000.0" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000000000," + " \"value\":5000.0" + " }" + " ]," + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000300000," + " \"value\":7000.0" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000300000," + " \"value\":6000.0" + " }" + " ]" + "]";
assertThat(new ObjectMapper().writeValueAsString(Transform.measurementDTOs(data)), is(normalize(json)));
}
use of org.opennms.newts.api.Measurement in project newts by OpenNMS.
the class Selecter method run.
@Override
public void run() {
Meter meter = m_metricRegistry.meter(MetricRegistry.name(Selector.class, "queries"));
Query query;
try {
while (true) {
if ((query = m_queue.poll()) == null) {
if (isShutdown()) {
break;
} else {
Thread.sleep(250);
continue;
}
}
LOG.debug("Selecting from {} to {} for resource {} at resolution {}", query.getStart(), query.getEnd(), query.getResource(), query.getResolution());
Results<Measurement> results = m_repository.select(Context.DEFAULT_CONTEXT, query.getResource(), query.getStart(), query.getEnd(), m_rDescriptor, query.getResolution());
int numRows = results.getRows().size();
LOG.debug("Select returned {} rows.", numRows);
meter.mark(numRows);
}
} catch (InterruptedException e) {
LOG.warn("Interrupted!");
}
}
use of org.opennms.newts.api.Measurement in project newts by OpenNMS.
the class InsertSelectMeasurementsITCase method testWithCounter.
@Test
public void testWithCounter() {
Iterator<Row<Sample>> testSamples = new SampleRowsBuilder(new Resource("localhost"), MetricType.COUNTER).row(900000000).element("mCounter", // Thu Jul 9 11:00:00 CDT 1998
0).row(900000300).element("mCounter", 300).row(900000600).element("mCounter", 600).row(900000900).element("mCounter", 900).row(900001200).element("mCounter", 1200).row(900001500).element("mCounter", 1500).row(900001800).element("mCounter", 1800, mapFor("a", "1")).row(900002100).element("mCounter", 2100).row(900002400).element("mCounter", 2400, mapFor("b", "2")).row(900002700).element("mCounter", 2700).row(900003000).element("mCounter", 3000).row(900003300).element("mCounter", 3300).row(900003600).element("mCounter", 3600).row(900003900).element("mCounter", 3900).row(900004200).element("mCounter", 4200).row(900004500).element("mCounter", 4500).row(900004800).element("mCounter", 4800).row(900005100).element("mCounter", 5100, mapFor("c", "3")).row(900005400).element("mCounter", 5400).row(900005700).element("mCounter", 5700, mapFor("d", "4")).row(900006000).element("mCounter", 6000).row(900006300).element("mCounter", 6300).row(900006600).element("mCounter", 6600).row(900006900).element("mCounter", 6900).row(900007200).element("mCounter", // Thu Jul 9 13:00:00 CDT 1998
7200).build();
ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("mCounter-avg", "mCounter", Duration.seconds(600), AVERAGE).export("mCounter-avg");
Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("mCounter-avg", 1).row(900007200).element("mCounter-avg", 1).build();
writeSamples(testSamples);
Results<Measurement> results = getRepository().select(Context.DEFAULT_CONTEXT, new Resource("localhost"), Optional.of(Timestamp.fromEpochSeconds(900003600)), Optional.of(Timestamp.fromEpochSeconds(900007200)), rDescriptor, Optional.of(Duration.minutes(60)));
// Validate results
assertRowsEqual(expected, results.iterator());
// Validate merged attributes
Iterator<Row<Measurement>> rows = results.iterator();
assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("a", "1", "b", "2"));
assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("c", "3", "d", "4"));
}
use of org.opennms.newts.api.Measurement in project newts by OpenNMS.
the class Utils method assertRowsEqual.
/**
* Assert that two sets of {@link Row} results are equal.
*
* @param expectedRows
* expected value
* @param actualRows
* actual value
*/
static void assertRowsEqual(Iterator<Row<Measurement>> expectedRows, Iterator<Row<Measurement>> actualRows) {
while (actualRows.hasNext()) {
Row<Measurement> actual = actualRows.next();
assertTrue("Extraneous result row(s)", expectedRows.hasNext());
Row<Measurement> expected = expectedRows.next();
assertEquals("Unexpected row resource", expected.getResource(), actual.getResource());
assertEquals("Unexpected row timestamp", expected.getTimestamp(), actual.getTimestamp());
assertEquals("Measurement count mismatch", expected.getElements().size(), actual.getElements().size());
for (Measurement m : actual.getElements()) {
assertNotNull("Extraneous result measurement(s)", expected.getElement(m.getName()));
assertSamplesEqual(expected.getElement(m.getName()), m);
}
}
assertFalse("Missing result rows(s)", expectedRows.hasNext());
}
use of org.opennms.newts.api.Measurement in project opennms by OpenNMS.
the class NewtsFetchStrategyTest method createMockResource.
public Source createMockResource(final String label, final String attr, final String node, boolean expect) {
OnmsResourceType type = EasyMock.createNiceMock(OnmsResourceType.class);
final int nodeId = node.hashCode();
final String newtsResourceId = "response:" + node + ":" + attr;
final ResourceId resourceId = ResourceId.get("nodeSource", "NODES:" + nodeId).resolve("responseTime", node);
OnmsResource resource = m_resources.get(resourceId);
if (resource == null) {
resource = new OnmsResource(attr, label, type, Sets.newHashSet(), ResourcePath.get("foo"));
m_resources.put(resourceId, resource);
}
Set<OnmsAttribute> attributes = resource.getAttributes();
attributes.add(new RrdGraphAttribute(attr, "", newtsResourceId));
Results<Measurement> results = new Results<>();
Resource res = new Resource(newtsResourceId);
Row<Measurement> row = new Row<Measurement>(Timestamp.fromEpochSeconds(0), res);
Measurement measurement = new Measurement(Timestamp.fromEpochSeconds(0), res, label, 0.0d);
row.addElement(measurement);
results.addRow(row);
if (expect) {
EasyMock.expect(m_sampleRepository.select(EasyMock.eq(m_context), EasyMock.eq(res), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject())).andReturn(results);
}
final Source source = new Source();
source.setAggregation("AVERAGE");
source.setAttribute(attr);
source.setLabel(label);
source.setResourceId(resourceId.toString());
source.setTransient(false);
return source;
}
Aggregations