use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEnrichedEntities.
@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (isNull(tenantId)) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
for (EnrichedEntity entity : request.getEntitiesList()) {
if (StringUtils.isEmpty(entity.getEntityType())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
return;
}
if (StringUtils.isEmpty(entity.getEntityId())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
}
Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityChangeEventGeneratorImpl method sendChangeNotification.
@Override
public void sendChangeNotification(RequestContext requestContext, Collection<Entity> existingEntities, Collection<Entity> updatedEntities) {
Map<String, Entity> existingEntityMap = existingEntities.stream().collect(Collectors.toMap(Entity::getEntityId, identity()));
Map<String, Entity> upsertedEntityMap = updatedEntities.stream().collect(Collectors.toMap(Entity::getEntityId, identity()));
MapDifference<String, Entity> mapDifference = Maps.difference(existingEntityMap, upsertedEntityMap);
mapDifference.entriesOnlyOnRight().entrySet().forEach(entry -> {
sendCreateNotification(requestContext, entry.getValue());
});
mapDifference.entriesDiffering().entrySet().forEach(entry -> {
MapDifference.ValueDifference<Entity> valueDifference = entry.getValue();
Entity prevEntity = valueDifference.leftValue();
Entity currEntity = valueDifference.rightValue();
sendUpdateNotification(requestContext, prevEntity, currEntity);
});
mapDifference.entriesOnlyOnLeft().entrySet().forEach(entry -> {
sendDeleteNotification(requestContext, entry.getValue());
});
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityChangeEventGeneratorImplTest method sendCreateNotification.
@Test
void sendCreateNotification() {
List<Entity> entities = createEntities(2);
changeEventGenerator.sendCreateNotification(requestContext, entities);
InOrder inOrderVerifier = inOrder(eventProducer);
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(0)), EntityChangeEventValue.newBuilder().setCreateEvent(EntityCreateEvent.newBuilder().setCreatedEntity(entities.get(0)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(1)), EntityChangeEventValue.newBuilder().setCreateEvent(EntityCreateEvent.newBuilder().setCreatedEntity(entities.get(1)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EntityChangeEventGeneratorImplTest method sendDeleteNotification.
@Test
void sendDeleteNotification() {
List<Entity> entities = createEntities(2);
changeEventGenerator.sendDeleteNotification(requestContext, entities);
InOrder inOrderVerifier = inOrder(eventProducer);
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(0)), EntityChangeEventValue.newBuilder().setDeleteEvent(EntityDeleteEvent.newBuilder().setDeletedEntity(entities.get(0)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
inOrderVerifier.verify(eventProducer).send(KeyUtil.getKey(entities.get(1)), EntityChangeEventValue.newBuilder().setDeleteEvent(EntityDeleteEvent.newBuilder().setDeletedEntity(entities.get(1)).build()).setEventTimeMillis(CURRENT_TIME_MILLIS).build());
}
use of com.google.cloud.videointelligence.v1p3beta1.Entity in project entity-service by hypertrace.
the class EdsCacheClient method initCache.
private void initCache(EntityServiceClientCacheConfig cacheConfig, Executor executor) {
this.enrichedEntityCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEnrichedEntityCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEnrichedEntityCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEnrichedEntityMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {
public EnrichedEntity load(@Nonnull EdsCacheKey key) throws Exception {
EnrichedEntity enrichedEntity = client.getEnrichedEntityById(key.tenantId, key.entityId);
if (enrichedEntity == null) {
throw new NotFoundException("Enriched entity not found");
}
return enrichedEntity;
}
}, executor));
this.entityCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEntityCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEntityCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEntityMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {
public Entity load(@Nonnull EdsCacheKey key) throws Exception {
Entity entity = client.getById(key.tenantId, key.entityId);
if (entity == null) {
throw new NotFoundException("Entity not found");
}
return entity;
}
}, executor));
this.entityIdsCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEntityIdsCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEntityIdsCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEntityIdsMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {
public String load(@Nonnull EdsTypeAndIdAttributesCacheKey key) throws Exception {
Entity entity = client.getByTypeAndIdentifyingAttributes(key.tenantId, key.byTypeAndIdentifyingAttributes);
if (entity == null) {
throw new NotFoundException("Entity not found!!");
}
entityCache.put(new EdsCacheKey(entity.getTenantId(), entity.getEntityId()), entity);
return entity.getEntityId();
}
}, executor));
PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".enrichedEntityCache", enrichedEntityCache, Collections.emptyMap());
PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".entityCache", entityCache, Collections.emptyMap());
PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".entityIdsCache", entityIdsCache, Collections.emptyMap());
}
Aggregations