use of org.opennms.newts.api.Context in project opennms by OpenNMS.
the class RedisResourceMetadataCache method getResourceIdsWithPrefix.
@Override
public List<String> getResourceIdsWithPrefix(Context context, String resourceIdPrefix) {
try (Jedis jedis = m_pool.getResource()) {
final List<String> elements = Lists.newArrayList(SEARCH_PREFIX, context.getId());
elements.addAll(m_resourceIdSplitter.splitIdIntoElements(resourceIdPrefix));
return jedis.lrange(m_resourceIdSplitter.joinElementsToId(elements).getBytes(), 0, -1).stream().map(bytes -> resourceId(METADATA_PREFIX, context.getId(), bytes)).collect(Collectors.toList());
}
}
use of org.opennms.newts.api.Context in project opennms by OpenNMS.
the class RedisResourceMetadataCacheIT method canGetEntriesWithPrefix.
@Test
public void canGetEntriesWithPrefix() {
Context ctx = Context.DEFAULT_CONTEXT;
RedisResourceMetadataCache cache = new RedisResourceMetadataCache(REDIS_HOSTNAME, REDIS_PORT, 8, m_registry, new EscapableResourceIdSplitter());
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").isEmpty());
Resource resource = new Resource("a:b:c");
ResourceMetadata resourceMetadata = new ResourceMetadata();
cache.merge(ctx, resource, resourceMetadata);
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c").contains("a:b:c"));
assertTrue(cache.getResourceIdsWithPrefix(ctx, "a:b:c:d").isEmpty());
}
use of org.opennms.newts.api.Context in project newts by OpenNMS.
the class SearchResource method search.
@GET
@Timed
public Collection<SearchResultDTO> search(@QueryParam("q") Optional<String> query, @QueryParam("context") Optional<String> contextId) {
checkArgument(query.isPresent(), "missing required query parameter (q=<argument>)");
QueryParser qp = new QueryParser();
Query parsedQuery;
try {
parsedQuery = qp.parse(query.get());
} catch (ParseException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).entity("Invalid query " + query.get()).build());
}
Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
return Transform.searchResultDTOs(m_searcher.search(context, parsedQuery));
}
use of org.opennms.newts.api.Context in project newts by OpenNMS.
the class CassandraCachePrimer method prime.
public void prime(ResourceMetadataCache cache, Context context) {
BoundStatement bindStatement = m_selectAllMetricsStatement.bind();
bindStatement.setFetchSize(m_fetchSize);
ResultSet rs = m_session.execute(bindStatement);
for (Row row : rs) {
// of waiting until all rows have been processed
if (rs.getAvailableWithoutFetching() == m_fetchMoreThreshold && !rs.isFullyFetched()) {
// this is asynchronous
rs.fetchMoreResults();
}
final Context rowContext = new Context(row.getString(Constants.Schema.C_METRICS_CONTEXT));
if (context != null && !context.equals(rowContext)) {
// Skip this entry, it's not in the context we're interested in
continue;
}
final Resource resource = new Resource(row.getString(Constants.Schema.C_METRICS_RESOURCE));
final ResourceMetadata resourceMetadata = new ResourceMetadata();
// As cassandra is unable to provide remaining TTL values for tables with partition/primary keys only, we
// hope that the cached metadata is merged with one from the `resource_attributes` table on which the TTL
// can be determined. As a fallback, we let the cache expire immediately avoiding objects in the cache with
// are in fact already timed out.
resourceMetadata.setExpires(null);
resourceMetadata.putMetric(row.getString(Constants.Schema.C_METRICS_NAME));
cache.merge(rowContext, resource, resourceMetadata);
}
bindStatement = m_selectAllAttributesStatement.bind();
bindStatement.setFetchSize(m_fetchSize);
for (Row row : m_session.execute(bindStatement)) {
// of waiting until all rows have been processed
if (rs.getAvailableWithoutFetching() == m_fetchMoreThreshold && !rs.isFullyFetched()) {
// this is asynchronous
rs.fetchMoreResults();
}
final Context rowContext = new Context(row.getString(Constants.Schema.C_ATTRS_CONTEXT));
if (context != null && !context.equals(rowContext)) {
// Skip this entry, it's not in the context we're interested in
continue;
}
final Resource resource = new Resource(row.getString(Constants.Schema.C_ATTRS_RESOURCE));
final ResourceMetadata resourceMetadata = new ResourceMetadata();
// Let the caches expire before the real TTL to avoid corner-cases and add some margin
// Cassandra supports fetching of TTL values only on rows where not all columns are primary keys. Therefore
// we assume that the TTL of entries in this table is similar to entries of other metadata tables. Setting
// the expiration time only once will will merge the value to all other cached entries for the same resource
resourceMetadata.setExpires(System.currentTimeMillis() + row.getInt("ttl") * 1000L * 3L / 4L);
resourceMetadata.putAttribute(row.getString(Constants.Schema.C_ATTRS_ATTR), row.getString(Constants.Schema.C_ATTRS_VALUE));
cache.merge(rowContext, resource, resourceMetadata);
}
}
use of org.opennms.newts.api.Context in project newts by OpenNMS.
the class MeasurementsResource method getMeasurements.
@POST
@Path("/{resource}")
@Timed
public Collection<Collection<MeasurementDTO>> getMeasurements(ResultDescriptorDTO descriptorDTO, @PathParam("resource") Resource resource, @QueryParam("start") Optional<TimestampParam> start, @QueryParam("end") Optional<TimestampParam> end, @QueryParam("resolution") Optional<DurationParam> resolution, @QueryParam("context") Optional<String> contextId) {
Optional<Timestamp> lower = Transform.toTimestamp(start);
Optional<Timestamp> upper = Transform.toTimestamp(end);
Optional<Duration> step = Transform.toDuration(resolution);
Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
LOG.debug("Retrieving measurements for resource {}, from {} to {} w/ resolution {} and w/ report {}", resource, lower, upper, step, descriptorDTO);
ResultDescriptor rDescriptor = Transform.resultDescriptor(descriptorDTO);
return Transform.measurementDTOs(m_repository.select(context, resource, lower, upper, rDescriptor, step));
}
Aggregations