Search in sources :

Example 6 with SearchResults

use of org.opennms.newts.api.search.SearchResults 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 7 with SearchResults

use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.

the class NewtsResourceStorageDao method getAttributes.

@Override
public Set<OnmsAttribute> getAttributes(ResourcePath path) {
    Set<OnmsAttribute> attributes = Sets.newHashSet();
    // Fetch the resource-level attributes in parallel
    Future<Map<String, String>> stringAttributes = ForkJoinPool.commonPool().submit(getResourceAttributesCallable(path));
    // Gather the list of metrics available under the resource path
    SearchResults results = searchFor(path, 0, true);
    for (Result result : results) {
        final String resourceId = result.getResource().getId();
        final ResourcePath resultPath = toResourcePath(resourceId);
        if (!path.equals(resultPath)) {
            // This shouldn't happen
            LOG.warn("Encountered non-child resource {} when searching for {} with depth {}. Ignoring resource.", result.getResource(), path, 0);
            continue;
        }
        for (String metric : result.getMetrics()) {
            // Use the metric name as the dsName
            // Store the resource id in the rrdFile field
            attributes.add(new RrdGraphAttribute(metric, "", resourceId));
        }
    }
    // Add the resource level attributes to the result set
    try {
        stringAttributes.get().entrySet().stream().map(e -> new StringPropertyAttribute(e.getKey(), e.getValue())).forEach(attr -> attributes.add(attr));
    } catch (InterruptedException | ExecutionException e) {
        throw Throwables.propagate(e);
    }
    return attributes;
}
Also used : NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) IntStream(java.util.stream.IntStream) Context(org.opennms.newts.api.Context) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) CassandraIndexer(org.opennms.newts.cassandra.search.CassandraIndexer) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils(org.opennms.netmgt.newts.support.NewtsUtils) NewtsUtils.toResourceId(org.opennms.netmgt.newts.support.NewtsUtils.toResourceId) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Map(java.util.Map) Sample(org.opennms.newts.api.Sample) NewtsWriter(org.opennms.netmgt.newts.NewtsWriter) Path(java.nio.file.Path) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Query(org.opennms.newts.api.search.Query) CassandraSampleRepository(org.opennms.newts.persistence.cassandra.CassandraSampleRepository) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Resource(org.opennms.newts.api.Resource) Set(java.util.Set) Throwables(com.google.common.base.Throwables) CassandraSearcher(org.opennms.newts.cassandra.search.CassandraSearcher) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) Result(org.opennms.newts.api.search.SearchResults.Result) SearchableResourceMetadataCache(org.opennms.netmgt.newts.support.SearchableResourceMetadataCache) Sets(com.google.common.collect.Sets) ExecutionException(java.util.concurrent.ExecutionException) NewtsUtils.findResourcesWithMetricsAtDepth(org.opennms.netmgt.newts.support.NewtsUtils.findResourcesWithMetricsAtDepth) List(java.util.List) SearchResults(org.opennms.newts.api.search.SearchResults) ForkJoinPool(java.util.concurrent.ForkJoinPool) ResourcePath(org.opennms.netmgt.model.ResourcePath) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) SearchResults(org.opennms.newts.api.search.SearchResults) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Result(org.opennms.newts.api.search.SearchResults.Result) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with SearchResults

use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.

the class NewtsResourceStorageDao method searchFor.

private SearchResults searchFor(ResourcePath path, int depth, boolean fetchMetrics) {
    final Query q = findResourcesWithMetricsAtDepth(path, depth);
    LOG.trace("Searching for '{}'.", q);
    final SearchResults results = m_searcher.search(m_context, q, fetchMetrics);
    LOG.trace("Found {} results.", results.size());
    return results;
}
Also used : Query(org.opennms.newts.api.search.Query) SearchResults(org.opennms.newts.api.search.SearchResults)

Example 9 with SearchResults

use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.

the class NewtsResourceStorageDao method delete.

@Override
public boolean delete(ResourcePath path) {
    final SearchResults results = searchFor(path, 0, true);
    if (results.isEmpty()) {
        return false;
    }
    for (final Result result : results) {
        m_sampleRepository.delete(m_context, result.getResource());
        m_indexer.delete(m_context, result.getResource());
    }
    return true;
}
Also used : SearchResults(org.opennms.newts.api.search.SearchResults) Result(org.opennms.newts.api.search.SearchResults.Result)

Aggregations

SearchResults (org.opennms.newts.api.search.SearchResults)9 Resource (org.opennms.newts.api.Resource)5 Result (org.opennms.newts.api.search.SearchResults.Result)5 Query (org.opennms.newts.api.search.Query)4 ResourcePath (org.opennms.netmgt.model.ResourcePath)3 Set (java.util.Set)2 ExecutionException (java.util.concurrent.ExecutionException)2 Test (org.junit.Test)2 NewtsUtils.toResourcePath (org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath)2 Context (org.opennms.newts.api.Context)2 Sample (org.opennms.newts.api.Sample)2 BooleanQuery (org.opennms.newts.api.search.BooleanQuery)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Timer (com.codahale.metrics.Timer)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1