Search in sources :

Example 1 with Gauge

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);
}
Also used : Counter(org.opennms.newts.api.Counter) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Rate(org.opennms.newts.aggregate.Rate) Duration(org.opennms.newts.api.Duration) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Gauge(org.opennms.newts.api.Gauge) Test(org.junit.Test)

Example 2 with Gauge

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());
}
Also used : Sample(org.opennms.newts.api.Sample) Gauge(org.opennms.newts.api.Gauge)

Example 3 with Gauge

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));
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement) Gauge(org.opennms.newts.api.Gauge) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Example 4 with Gauge

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);
        }
    }
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Gauge(org.opennms.newts.api.Gauge) Test(org.junit.Test)

Example 5 with Gauge

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;
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) Timestamp(org.opennms.newts.api.Timestamp) Date(java.util.Date) Gauge(org.opennms.newts.api.Gauge)

Aggregations

Gauge (org.opennms.newts.api.Gauge)6 Sample (org.opennms.newts.api.Sample)6 Test (org.junit.Test)3 Resource (org.opennms.newts.api.Resource)3 Timestamp (org.opennms.newts.api.Timestamp)3 Counter (org.opennms.newts.api.Counter)2 Row (org.opennms.newts.api.Results.Row)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Statement (com.datastax.driver.core.Statement)1 Date (java.util.Date)1 Rate (org.opennms.newts.aggregate.Rate)1 Duration (org.opennms.newts.api.Duration)1 MetricType (org.opennms.newts.api.MetricType)1 Results (org.opennms.newts.api.Results)1 CassandraSession (org.opennms.newts.cassandra.CassandraSession)1 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)1