use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class DriverAdapter method next.
@Override
public Results.Row<Sample> next() {
if (!hasNext())
throw new NoSuchElementException();
Results.Row<Sample> nextNext = null;
while (m_results.hasNext()) {
Sample m = getNextSample();
if (m.getTimestamp().gt(m_next.getTimestamp())) {
nextNext = new Results.Row<>(m.getTimestamp(), m.getResource());
addSample(nextNext, m);
break;
}
addSample(m_next, m);
}
try {
return m_next;
} finally {
m_next = nextNext;
}
}
use of org.opennms.newts.api.Sample 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);
}
}
}
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class CassandraIndexer method update.
@Override
public void update(Collection<Sample> samples) {
Timer.Context ctx = m_updateTimer.time();
Set<StatementGenerator> generators = Sets.newHashSet();
Map<Context, Map<Resource, ResourceMetadata>> cacheQueue = Maps.newHashMap();
for (Sample sample : samples) {
maybeIndexResource(cacheQueue, generators, sample.getContext(), sample.getResource());
maybeIndexResourceAttributes(cacheQueue, generators, sample.getContext(), sample.getResource());
maybeAddMetricName(cacheQueue, generators, sample.getContext(), sample.getResource(), sample.getName());
}
try {
if (!generators.isEmpty()) {
synchronized (statementsInFlight) {
generators.removeAll(statementsInFlight);
statementsInFlight.addAll(generators);
}
m_inserts.mark(generators.size());
// Asynchronously execute the statements
List<ResultSetFuture> futures = Lists.newArrayList();
for (Statement statementToExecute : toStatements(generators)) {
futures.add(m_session.executeAsync(statementToExecute));
}
for (ResultSetFuture future : futures) {
future.getUninterruptibly();
}
}
// Order matters here; We want the cache updated only after a successful Cassandra write.
for (Context context : cacheQueue.keySet()) {
for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
m_cache.merge(context, entry.getKey(), entry.getValue());
}
}
} finally {
synchronized (statementsInFlight) {
statementsInFlight.removeAll(generators);
}
ctx.stop();
}
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class Rate method next.
@Override
public Row<Sample> next() {
if (!hasNext())
throw new NoSuchElementException();
Row<Sample> working = m_input.next();
Row<Sample> result = new Row<>(working.getTimestamp(), working.getResource());
for (String metricName : m_metrics) {
Sample sample = working.getElement(metricName);
if (sample == null) {
continue;
}
// Use rate as result if one of counter types, else pass through as-is.
result.addElement(COUNTERS.contains(sample.getType()) ? getRate(sample) : sample);
m_prevSamples.put(sample.getName(), sample);
}
return result;
}
use of org.opennms.newts.api.Sample in project opennms by OpenNMS.
the class NewtsConverter method toSample.
protected static Sample toSample(AbstractDS ds, Resource resource, Timestamp timestamp, double value) {
final String metric = ds.getName();
final MetricType type = ds.isCounter() ? MetricType.COUNTER : MetricType.GAUGE;
final ValueType<?> valueType = ds.isCounter() ? new Counter(UnsignedLong.valueOf(BigDecimal.valueOf(value).toBigInteger())) : new Gauge(value);
return new Sample(timestamp, resource, metric, type, valueType);
}
Aggregations