Search in sources :

Example 21 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class NewtsReporter method reportG.

private void reportG(List<Sample> samples, Timestamp timestamp, String resource, String metricName, double val, Map<String, String> attrs) {
    Sample s = new Sample(timestamp, resourceFor(resource), metricName, MetricType.GAUGE, gauge(val), attrs);
    samples.add(s);
}
Also used : Sample(org.opennms.newts.api.Sample)

Example 22 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class FileImport method run.

@Override
public void run() {
    String line;
    try {
        // Throw away the first (header).
        m_reader.readLine();
        while ((line = m_reader.readLine()) != null) {
            try {
                List<Sample> samples = m_lineParser.parseLine(line);
                Context timerCtx = m_writeTimer.time();
                try {
                    m_repository.insert(samples);
                } finally {
                    timerCtx.stop();
                }
                m_numRows.inc();
                m_numSamples.inc(10);
            } catch (ParseException e) {
                LOG.error("Unable to parse date from line '{}'", line);
            }
        }
    } catch (IOException e) {
        LOG.error("Error reading GSOD data file: {]", e);
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) Sample(org.opennms.newts.api.Sample) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 23 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class ImportRunner method adjustTime.

private Func1<? super Sample, ? extends Sample> adjustTime() {
    return new Func1<Sample, Sample>() {

        @Override
        public Sample call(Sample s) {
            Timestamp oldTs = s.getTimestamp();
            Timestamp newTs = Timestamp.fromEpochMillis(m_timeoffset + Math.round(oldTs.asMillis() / m_timescaleFactor));
            return new Sample(newTs, s.getResource(), s.getName(), s.getType(), s.getValue());
        }
    };
}
Also used : Sample(org.opennms.newts.api.Sample) Func1(rx.functions.Func1) Timestamp(org.opennms.newts.api.Timestamp)

Example 24 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class CassandraIndexerITCase method test.

@Test
public void test() {
    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"));
    CassandraSession session = newtsInstance.getCassandraSession();
    ResourceMetadataCache mockCache = mock(ResourceMetadataCache.class);
    when(mockCache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
    Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    indexer.update(samples);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    // Match path components
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aac")).size(), equalTo(1));
    // Match attribute values
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("people")).size(), equalTo(3));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).size(), equalTo(1));
    // Match attribute key + value pairs
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("beverage", "beer")), Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(1));
    // Or'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal", "country")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("beer", "wine")).size(), equalTo(3));
    // And'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("metal", "country")).size(), equalTo(0));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("aaa", "aac")).size(), equalTo(1));
    // Groups queries
    // (beer AND metal) OR (aaa AND country)
    BooleanQuery subquery1 = new BooleanQuery();
    subquery1.add(new TermQuery(new Term("beer")), Operator.OR);
    subquery1.add(new TermQuery(new Term("metal")), Operator.AND);
    BooleanQuery subquery2 = new BooleanQuery();
    subquery2.add(new TermQuery(new Term("aaa")), Operator.OR);
    subquery2.add(new TermQuery(new Term("country")), Operator.AND);
    query = new BooleanQuery();
    query.add(subquery1, Operator.OR);
    query.add(subquery2, Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(2));
    // Attributes are retrieved
    Result r = searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).iterator().next();
    assertThat(r.getResource().getId(), is(equalTo("aab")));
    assertThat(r.getResource().getAttributes().isPresent(), is(true));
    assertThat(r.getResource().getAttributes().get(), equalTo(map(base, "music", "metal", "beverage", "beer")));
    // Metrics too
    assertThat(r.getMetrics().size(), equalTo(1));
    assertThat(r.getMetrics().iterator().next(), equalTo("m0"));
}
Also used : Context(org.opennms.newts.api.Context) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Term(org.opennms.newts.api.search.Term) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 25 with Sample

use of org.opennms.newts.api.Sample in project newts by OpenNMS.

the class CassandraIndexerITCase method testCache.

@Test
public void testCache() {
    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();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
    Indexer indexer = new CassandraIndexer(newtsInstance.getCassandraSession(), 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    Sample s = sampleFor(new Resource("aaa", Optional.of(map("beverage", "beer"))), "m0");
    indexer.update(Collections.singletonList(s));
    ResourceMetadata expected = new ResourceMetadata().putMetric("m0").putAttribute("beverage", "beer");
    verify(cache, atLeast(1)).get(any(Context.class), any(Resource.class));
    verify(cache).merge(any(Context.class), any(Resource.class), eq(expected));
}
Also used : Context(org.opennms.newts.api.Context) Indexer(org.opennms.newts.api.search.Indexer) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Aggregations

Sample (org.opennms.newts.api.Sample)37 Resource (org.opennms.newts.api.Resource)18 Test (org.junit.Test)14 Timestamp (org.opennms.newts.api.Timestamp)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 Gauge (org.opennms.newts.api.Gauge)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Timer (com.codahale.metrics.Timer)5 List (java.util.List)5 Counter (org.opennms.newts.api.Counter)5 Row (org.opennms.newts.api.Results.Row)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Map (java.util.Map)4 Context (org.opennms.newts.api.Context)4 Results (org.opennms.newts.api.Results)4 Meter (com.codahale.metrics.Meter)3 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3