use of org.opennms.newts.api.Results in project newts by OpenNMS.
the class ResultSerializationTest method testSamples.
@Test
public void testSamples() throws JsonProcessingException {
// Use the optional attributes map at least once.
Map<String, String> attributes = Maps.newHashMap();
attributes.put("units", "bytes");
Results<Sample> data = new Results<>();
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(5000, COUNTER)));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(6000, COUNTER), attributes));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(6000, COUNTER)));
data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(7000, COUNTER)));
String json = "[" + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000000000," + " \"type\":\"COUNTER\"," + " \"value\":6000," + " \"attributes\":{\"units\":\"bytes\"}" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000000000," + " \"type\":\"COUNTER\"," + " \"value\":5000" + " }" + " ]," + " [" + " {" + " \"name\": \"ifOutOctets\"," + " \"timestamp\":900000300000," + " \"type\":\"COUNTER\"," + " \"value\":7000" + " }," + " {" + " \"name\": \"ifInOctets\"," + " \"timestamp\":900000300000," + " \"type\":\"COUNTER\"," + " \"value\":6000" + " }" + " ]" + "]";
assertThat(new ObjectMapper().writeValueAsString(Transform.sampleDTOs(data)), is(normalize(json)));
}
use of org.opennms.newts.api.Results in project newts by OpenNMS.
the class CassandraSampleRepository method select.
@Override
public Results<Sample> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end) {
Timer.Context timer = m_sampleSelectTimer.time();
validateSelect(start, end);
Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
LOG.debug("Querying database for resource {}, from {} to {}", resource, lower, upper);
Results<Sample> samples = new Results<>();
DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower, upper));
for (Row<Sample> row : driverAdapter) {
samples.addRow(row);
}
LOG.debug("{} results returned from database", driverAdapter.getResultCount());
m_samplesSelected.mark(driverAdapter.getResultCount());
try {
return samples;
} finally {
timer.stop();
}
}
use of org.opennms.newts.api.Results in project newts by OpenNMS.
the class RateTest method test.
@Test
public void test() {
Results<Sample> input = new Results<>();
int rows = 10, cols = 2, rate = 100;
for (int i = 1; i <= rows; i++) {
Timestamp t = Timestamp.fromEpochMillis(i * 1000);
for (int j = 0; j < cols; j++) {
input.addElement(new Sample(t, m_resource, m_metrics[j], COUNTER, new Counter((i + j) * rate)));
}
}
Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(cols)).iterator();
for (int i = 1; i <= rows; i++) {
assertTrue("Insufficient number of results", output.hasNext());
Results.Row<Sample> row = output.next();
assertEquals("Unexpected row timestamp", Timestamp.fromEpochMillis(i * 1000), row.getTimestamp());
assertEquals("Unexpected row resource", m_resource, row.getResource());
assertEquals("Unexpected number of columns", cols, row.getElements().size());
for (int j = 0; j < cols; j++) {
String name = m_metrics[j];
assertNotNull("Missing sample" + name, row.getElement(name));
assertEquals("Unexpected sample name", name, row.getElement(name).getName());
assertEquals("Unexpected sample type", GAUGE, row.getElement(name).getType());
// Samples in the first row are null, this is normal.
if (i != 1) {
assertEquals("Incorrect rate value", 100.0d, row.getElement(name).getValue().doubleValue(), 0.0d);
}
}
}
}
Aggregations