use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class PrimaryData method next.
@Override
public Row<Measurement> next() {
if (!hasNext())
throw new NoSuchElementException();
Timestamp intervalCeiling = m_timestamps.next();
Row<Measurement> output = new Row<>(intervalCeiling, m_resource);
for (Datasource ds : m_resultDescriptor.getDatasources().values()) {
Accumulation accumulation = getOrCreateAccumulation(ds.getSource());
accumulation.reset();
int lastSampleIdx = 0;
if (m_lastSampleIndex.containsKey(ds.getSource())) {
lastSampleIdx = m_lastSampleIndex.get(ds.getSource());
}
Sample last = null;
for (int sampleIdx = lastSampleIdx; sampleIdx < m_samples.size(); sampleIdx++) {
Row<Sample> row = m_samples.get(sampleIdx);
Sample current;
current = row.getElement(ds.getSource());
// Skip the row if it does not contain a sample for the current datasource
if (current == null) {
continue;
}
if (last == null) {
last = current;
lastSampleIdx = sampleIdx;
continue;
}
// Accumulate nothing when samples are beyond this interval
if (intervalCeiling.lt(last.getTimestamp())) {
break;
}
Timestamp lowerBound = last.getTimestamp();
if (lastIntervalCeiling != null && lastIntervalCeiling.gt(lowerBound)) {
lowerBound = lastIntervalCeiling;
}
Timestamp upperBound = current.getTimestamp();
if (intervalCeiling.lt(upperBound)) {
upperBound = intervalCeiling;
}
if (lowerBound.gt(upperBound)) {
lowerBound = upperBound;
}
Duration elapsedWithinInterval = upperBound.minus(lowerBound);
Duration elapsedBetweenSamples = current.getTimestamp().minus(last.getTimestamp());
m_lastSampleIndex.put(ds.getSource(), lastSampleIdx);
accumulation.accumulateValue(elapsedWithinInterval, elapsedBetweenSamples, ds.getHeartbeat(), current.getValue()).accumlateAttrs(current.getAttributes());
last = current;
lastSampleIdx = sampleIdx;
}
// Add sample with accumulated value to output row
output.addElement(new Measurement(output.getTimestamp(), output.getResource(), ds.getSource(), accumulation.getAverage(), accumulation.getAttributes()));
}
lastIntervalCeiling = intervalCeiling;
return output;
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class Rate method getRate.
private Sample getRate(Sample sample) {
ValueType<?> value = NAN;
Sample previous = m_prevSamples.get(sample.getName());
if (previous != null) {
long elapsed = sample.getTimestamp().asSeconds() - previous.getTimestamp().asSeconds();
value = new Gauge(sample.getValue().delta(previous.getValue()).doubleValue() / elapsed);
}
return new Sample(sample.getTimestamp(), sample.getResource(), sample.getName(), GAUGE, value, sample.getAttributes());
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class RateTest method testMissing.
@Test
public void testMissing() {
Results<Sample> input = new Results<>();
Timestamp start = Timestamp.fromEpochMillis(1000);
Duration step = Duration.seconds(1);
// row_1
input.addElement(new Sample(start, m_resource, m_metrics[0], COUNTER, new Counter(0)));
input.addElement(new Sample(start, m_resource, m_metrics[1], COUNTER, new Counter(0)));
// row_2
input.addElement(new Sample(start.plus(step), m_resource, m_metrics[0], COUNTER, new Counter(100)));
input.addElement(new Sample(start.plus(step), m_resource, m_metrics[1], COUNTER, new Counter(100)));
// row_3 (sample for m_metrics[0] missing)
input.addElement(new Sample(start.plus(step.times(2)), m_resource, m_metrics[1], COUNTER, new Counter(200)));
// row_4
input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[0], COUNTER, new Counter(300)));
input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[1], COUNTER, new Counter(300)));
Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(2)).iterator();
// result_1 is always null
assertTrue(output.hasNext());
assertEquals(new Gauge(Double.NaN), output.next().getElement(m_metrics[0]).getValue());
// result_2, rate 100
assertTrue(output.hasNext());
assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
// result_3, missing because sample in row_3 is missing
assertTrue(output.hasNext());
assertNull(output.next().getElement(m_metrics[0]));
// result_4, rate of 100 calculated between row_4 and row_2
assertTrue(output.hasNext());
assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class CassandraIndexerITCase method testDelete.
@Test
public void testDelete() {
ResourceMetadataCache cache = mock(ResourceMetadataCache.class);
when(cache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
MetricRegistry registry = new MetricRegistry();
ContextConfigurations contextConfigurations = new ContextConfigurations();
CassandraSession session = newtsInstance.getCassandraSession();
CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
Indexer indexer = new CassandraIndexer(session, 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
Map<String, String> base = map("meat", "people", "bread", "beer");
List<Sample> samples = Lists.newArrayList();
samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
indexer.update(samples);
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aac:aaa", Optional.of(base)));
assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(0));
}
use of org.opennms.newts.api.Sample in project newts by OpenNMS.
the class CassandraIndexerTest method insertStatementsAreDeduplicatedWhenIndexingManySamples.
@Test
public void insertStatementsAreDeduplicatedWhenIndexingManySamples() {
CassandraSession session = mock(CassandraSession.class);
ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class);
when(session.executeAsync(statementCaptor.capture())).thenReturn(mock(ResultSetFuture.class));
PreparedStatement statement = mock(PreparedStatement.class);
BoundStatement boundStatement = mock(BoundStatement.class);
when(session.prepare(any(RegularStatement.class))).thenReturn(statement);
when(statement.bind()).thenReturn(boundStatement);
when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).withMaxBatchSize(1).build();
MetricRegistry registry = new MetricRegistry();
GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(2048, registry);
CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, registry, options, new EscapableResourceIdSplitter(), new ContextConfigurations());
Resource r = new Resource("snmp:1589:vmware5Cpu:2:vmware5Cpu");
List<Sample> samples = Lists.newArrayList();
samples.add(new Sample(Timestamp.now(), r, "CpuCostopSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuIdleSum", MetricType.GAUGE, new Gauge(19299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuMaxLdSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuOverlapSum", MetricType.GAUGE, new Gauge(5.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRdySum", MetricType.GAUGE, new Gauge(41.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRunSum", MetricType.GAUGE, new Gauge(619.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSpwaitSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSystemSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsagemhzAvg", MetricType.GAUGE, new Gauge(32.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsedSum", MetricType.GAUGE, new Gauge(299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuWaitSum", MetricType.GAUGE, new Gauge(19343)));
// Index the collection of samples
indexer.update(samples);
// Verify the number of exectuteAsync calls
verify(session, times(20)).executeAsync(any(Statement.class));
}
Aggregations