use of org.opennms.newts.api.Gauge 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.Gauge 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();
try {
value = new Gauge(sample.getValue().delta(previous.getValue()).doubleValue() / elapsed);
} catch (ArithmeticException e) {
value = NAN;
}
}
return new Sample(sample.getTimestamp(), sample.getResource(), sample.getName(), GAUGE, value, sample.getAttributes());
}
use of org.opennms.newts.api.Gauge 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));
}
use of org.opennms.newts.api.Gauge in project newts by OpenNMS.
the class InsertSelectSamplesITCase method test.
@Test
public void test() {
List<Sample> samples = Lists.newArrayList();
int rows = 10, cols = 3;
Resource resource = new Resource("r");
for (int i = 1; i <= rows; i++) {
Timestamp ts = Timestamp.fromEpochMillis(i * 1000);
for (int j = 1; j <= cols; j++) {
samples.add(new Sample(ts, resource, "m" + j, GAUGE, new Gauge((i + 1) * j)));
}
}
// Override the shard period to ensure we test query concurrency
m_contextConfigurations.addContextConfig(Context.DEFAULT_CONTEXT, Duration.seconds(1), ConsistencyLevel.ALL, ConsistencyLevel.ALL);
getRepository().insert(samples);
Timestamp start = Timestamp.fromEpochMillis(0), end = Timestamp.fromEpochMillis(rows * 1000);
Iterator<Row<Sample>> results = getRepository().select(Context.DEFAULT_CONTEXT, resource, Optional.of(start), Optional.of(end)).iterator();
for (int i = 1; i <= rows; i++) {
assertTrue("Insufficient number of results", results.hasNext());
Timestamp timestamp = Timestamp.fromEpochMillis(i * 1000);
Row<Sample> row = results.next();
assertEquals("Unexpected timestamp for row " + i, timestamp, row.getTimestamp());
assertEquals("Unexpected resource name", resource, row.getResource());
for (int j = 1; j <= cols; j++) {
assertNotNull("Missing sample: m" + j, row.getElement("m" + j));
Sample sample = row.getElement("m" + j);
assertEquals("Unexpected timestamp for metric m" + j, timestamp, sample.getTimestamp());
assertEquals("Unexpected resource name", resource, sample.getResource());
assertEquals("Unexpected metric name", "m" + j, sample.getName());
assertEquals("Unexpected metric type", GAUGE, sample.getType());
assertEquals((double) ((i + 1) * j), sample.getValue().doubleValue(), 0.0d);
}
}
}
use of org.opennms.newts.api.Gauge in project newts by OpenNMS.
the class LineParser method parseLine.
List<Sample> parseLine(String line) throws ParseException {
LOG.trace("Parsing {}", line);
List<Sample> samples = Lists.newArrayList();
Resource station = new Resource(stringAt(line, 0));
String wban = stringAt(line, 7);
String dateYMD = stringAt(line, 14);
Date date = getDateFormat().parse(dateYMD);
Timestamp ts = new Timestamp(date.getTime(), TimeUnit.MILLISECONDS);
double meanTemp = doubleAt(line, 24);
samples.add(new Sample(ts, station, "meanTemperature", GAUGE, LineParser.valueFor(meanTemp, 9999.9)));
double dewpoint = doubleAt(line, 35);
samples.add(new Sample(ts, station, "dewPoint", GAUGE, LineParser.valueFor(dewpoint, 9999.9)));
double slp = doubleAt(line, 46);
Gauge seaLevelPressure = LineParser.valueFor(slp, 9999.9);
samples.add(new Sample(ts, station, "seaLevelPressure", GAUGE, seaLevelPressure));
double stp = doubleAt(line, 57);
Gauge stationPressure = LineParser.valueFor(stp, 9999.9);
samples.add(new Sample(ts, station, "stationPressure", GAUGE, stationPressure));
double vis = doubleAt(line, 68);
Gauge visibility = LineParser.valueFor(vis, 999.9);
samples.add(new Sample(ts, station, "visibility", GAUGE, visibility));
double speed = doubleAt(line, 78);
Gauge meanWindSpeed = new Gauge(speed);
samples.add(new Sample(ts, station, "meanWindSpeed", GAUGE, meanWindSpeed));
double maxSpeed = doubleAt(line, 88);
Gauge maxWindSpeed = LineParser.valueFor(maxSpeed, 999.9);
samples.add(new Sample(ts, station, "maxWindSpeed", GAUGE, maxWindSpeed));
double maxGust = doubleAt(line, 95);
Gauge maxWindGust = LineParser.valueFor(maxGust, 999.9);
samples.add(new Sample(ts, station, "maxWindGust", GAUGE, maxWindGust));
double maxTemp = doubleAt(line, 102);
Gauge maxTemperature = LineParser.valueFor(maxTemp, 9999.9);
samples.add(new Sample(ts, station, "maxTemperature", GAUGE, maxTemperature));
double minTemp = doubleAt(line, 110);
Gauge minTemperature = LineParser.valueFor(minTemp, 9999.9);
samples.add(new Sample(ts, station, "minTemperature", GAUGE, minTemperature));
LOG.trace("Station number {}, WBAN {}, date {}, Max Temp {}...", station, wban, dateYMD, maxTemperature);
return samples;
}
Aggregations