Search in sources :

Example 41 with Resource

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

the class ResultProcessorTest method test.

@Test
public void test() {
    Iterator<Row<Sample>> testData = new SampleRowsBuilder(new Resource("localhost"), MetricType.GAUGE).row(900000000).element("m0", // Thu Jul  9 11:00:00 CDT 1998
    1).row(900000300).element("m0", 1).row(900000600).element("m0", 1).row(900000900).element("m0", 1).row(900001200).element("m0", 1).row(900001500).element("m0", 1).row(900001800).element("m0", 1).row(900002100).element("m0", 3).row(900002400).element("m0", 3).row(900002700).element("m0", 3).row(900003000).element("m0", 3).row(900003300).element("m0", 3).row(900003600).element("m0", 3).row(900003900).element("m0", 1).row(900004200).element("m0", 1).row(900004500).element("m0", 1).row(900004800).element("m0", 1).row(900005100).element("m0", 1).row(900005400).element("m0", 1).row(900005700).element("m0", 3).row(900006000).element("m0", 3).row(900006300).element("m0", 3).row(900006600).element("m0", 3).row(900006900).element("m0", 3).row(900007200).element("m0", // Thu Jul  9 13:00:00 CDT 1998
    3).build();
    ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("m0-avg", "m0", Duration.seconds(600), AVERAGE).export("m0-avg");
    Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("m0-avg", 2.0).row(900007200).element("m0-avg", 2.0).build();
    ResultProcessor processor = new ResultProcessor(new Resource("localhost"), Timestamp.fromEpochSeconds(900003600), Timestamp.fromEpochSeconds(900007200), rDescriptor, Duration.minutes(60));
    assertRowsEqual(expected, processor.process(testData).iterator());
}
Also used : MeasurementRowsBuilder(org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) SampleRowsBuilder(org.opennms.newts.aggregate.Utils.SampleRowsBuilder) Test(org.junit.Test)

Example 42 with Resource

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

the class ResultProcessorTest method testCounterRate.

@Test
public void testCounterRate() {
    Iterator<Row<Sample>> testData = new SampleRowsBuilder(new Resource("localhost"), MetricType.COUNTER).row(900000000).element("m0", // Thu Jul  9 11:00:00 CDT 1998
    3000).row(900000300).element("m0", 6000).row(900000600).element("m0", 9000).row(900000900).element("m0", 12000).row(900001200).element("m0", 15000).row(900001500).element("m0", 18000).row(900001800).element("m0", 21000).row(900002100).element("m0", 24000).row(900002400).element("m0", 27000).row(900002700).element("m0", 30000).row(900003000).element("m0", 33000).row(900003300).element("m0", 36000).row(900003600).element("m0", 39000).row(900003900).element("m0", 42000).row(900004200).element("m0", 45000).row(900004500).element("m0", 48000).row(900004800).element("m0", 51000).row(900005100).element("m0", 54000).row(900005400).element("m0", 57000).row(900005700).element("m0", 60000).row(900006000).element("m0", 63000).row(900006300).element("m0", 66000).row(900006600).element("m0", 69000).row(900006900).element("m0", 72000).row(900007200).element("m0", // Thu Jul  9 13:00:00 CDT 1998
    75000).build();
    ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("m0", AVERAGE).export("m0");
    Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("m0", 10.0).row(900007200).element("m0", 10.0).build();
    ResultProcessor processor = new ResultProcessor(new Resource("localhost"), Timestamp.fromEpochSeconds(900003600), Timestamp.fromEpochSeconds(900007200), rDescriptor, Duration.minutes(60));
    assertRowsEqual(expected, processor.process(testData).iterator());
}
Also used : MeasurementRowsBuilder(org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) SampleRowsBuilder(org.opennms.newts.aggregate.Utils.SampleRowsBuilder) Test(org.junit.Test)

Example 43 with Resource

use of org.opennms.newts.api.Resource 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();
    }
}
Also used : Context(org.opennms.newts.api.Context) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Sample(org.opennms.newts.api.Sample) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Resource(org.opennms.newts.api.Resource) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator) Timer(com.codahale.metrics.Timer) Map(java.util.Map)

Example 44 with Resource

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

the class CassandraSearcher method search.

@Override
public SearchResults search(Context context, Query query, boolean populateMetricsAndAttributes) {
    checkNotNull(context, "context argument");
    checkNotNull(query, "query argument");
    Timer.Context ctx = m_searchTimer.time();
    ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
    SearchResults searchResults = new SearchResults();
    try {
        Set<String> ids;
        Query q = query.rewrite();
        if (q instanceof BooleanQuery) {
            ids = searchForIds(context, (BooleanQuery) q, readConsistency);
        } else if (q instanceof TermQuery) {
            ids = searchForIds(context, (TermQuery) q, readConsistency);
        } else {
            throw new IllegalStateException("Unsupported query: " + q);
        }
        for (final String id : ids) {
            if (!populateMetricsAndAttributes) {
                Resource resource = new Resource(id);
                List<String> emptyList = Collections.emptyList();
                searchResults.addResult(resource, emptyList);
            } else {
                // Fetch the metric names and attributes concurrently
                ResultSetFuture attrsFuture = fetchResourceAttributes(context, id, readConsistency);
                ResultSetFuture metricsFuture = fetchMetricNames(context, id, readConsistency);
                try {
                    Map<String, String> attrs = getResourceAttributesFromResults(attrsFuture);
                    Collection<String> metrics = getMetricNamesFromResults(metricsFuture);
                    Resource resource = attrs.size() > 0 ? new Resource(id, Optional.of(attrs)) : new Resource(id);
                    searchResults.addResult(resource, metrics);
                } catch (ExecutionException | InterruptedException e) {
                    throw Throwables.propagate(e);
                }
            }
        }
        return searchResults;
    } finally {
        ctx.stop();
    }
}
Also used : BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Query(org.opennms.newts.api.search.Query) Resource(org.opennms.newts.api.Resource) SearchResults(org.opennms.newts.api.search.SearchResults) ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) Timer(com.codahale.metrics.Timer) ExecutionException(java.util.concurrent.ExecutionException)

Example 45 with Resource

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

the class AggregationAttributesTest method test.

@Test
public void test() {
    Iterator<Row<Measurement>> testData = new MeasurementRowsBuilder(new Resource("localhost")).row(1).element("m0", 1, mapFor("a", "1")).row(300).element("m0", 1).row(600).element("m0", 1).row(900).element("m0", 1).row(1200).element("m0", 1).row(1500).element("m0", 1).row(1800).element("m0", 3, mapFor("b", "1")).row(2100).element("m0", 3).row(2400).element("m0", 3, mapFor("a", "2")).row(2700).element("m0", 3).row(3000).element("m0", 3).row(3300).element("m0", 3).build();
    ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("m0-avg", "m0", Duration.seconds(600), AVERAGE).datasource("m0-min", "m0", Duration.seconds(600), MIN).datasource("m0-max", "m0", Duration.seconds(600), MAX);
    Aggregation aggregation = new Aggregation(new Resource("localhost"), Timestamp.fromEpochSeconds(1), Timestamp.fromEpochSeconds(3300), rDescriptor, Duration.minutes(60), testData);
    Row<Measurement> row = aggregation.next();
    assertThat(row.getTimestamp(), equalTo(fromEpochSeconds(0)));
    assertThat(row.getElement("m0-avg").getAttributes(), nullValue());
    row = aggregation.next();
    assertThat(row.getTimestamp(), equalTo(fromEpochSeconds(3600)));
    assertAttributes(row.getElement("m0-avg"), mapFor("a", "2", "b", "1"));
}
Also used : Measurement(org.opennms.newts.api.Measurement) MeasurementRowsBuilder(org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) Test(org.junit.Test)

Aggregations

Resource (org.opennms.newts.api.Resource)67 Test (org.junit.Test)50 Row (org.opennms.newts.api.Results.Row)35 ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)31 MeasurementRowsBuilder (org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder)20 SampleRowsBuilder (org.opennms.newts.aggregate.Utils.SampleRowsBuilder)20 Sample (org.opennms.newts.api.Sample)19 Measurement (org.opennms.newts.api.Measurement)14 Context (org.opennms.newts.api.Context)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 SearchResults (org.opennms.newts.api.search.SearchResults)8 Timestamp (org.opennms.newts.api.Timestamp)7 ResourcePath (org.opennms.netmgt.model.ResourcePath)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Map (java.util.Map)5 Results (org.opennms.newts.api.Results)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 List (java.util.List)4 Result (org.opennms.newts.api.search.SearchResults.Result)4