use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.
the class EntityViewServiceImpl method findEntityViewsByQuery.
@Override
public ListenableFuture<List<EntityView>> findEntityViewsByQuery(TenantId tenantId, EntityViewSearchQuery query) {
ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery());
ListenableFuture<List<EntityView>> entityViews = Futures.transformAsync(relations, r -> {
EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
List<ListenableFuture<EntityView>> futures = new ArrayList<>();
for (EntityRelation relation : r) {
EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
if (entityId.getEntityType() == EntityType.ENTITY_VIEW) {
futures.add(findEntityViewByIdAsync(tenantId, new EntityViewId(entityId.getId())));
}
}
return Futures.successfulAsList(futures);
}, MoreExecutors.directExecutor());
entityViews = Futures.transform(entityViews, new Function<List<EntityView>, List<EntityView>>() {
@Nullable
@Override
public List<EntityView> apply(@Nullable List<EntityView> entityViewList) {
return entityViewList == null ? Collections.emptyList() : entityViewList.stream().filter(entityView -> query.getEntityViewTypes().contains(entityView.getType())).collect(Collectors.toList());
}
}, MoreExecutors.directExecutor());
return entityViews;
}
use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.
the class DeviceServiceImpl method assignDeviceToTenant.
@Transactional
@Override
public Device assignDeviceToTenant(TenantId tenantId, Device device) {
log.trace("Executing assignDeviceToTenant [{}][{}]", tenantId, device);
try {
List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), device.getId()).get();
if (!CollectionUtils.isEmpty(entityViews)) {
throw new DataValidationException("Can't assign device that has entity views to another tenant!");
}
} catch (ExecutionException | InterruptedException e) {
log.error("Exception while finding entity views for deviceId [{}]", device.getId(), e);
throw new RuntimeException("Exception while finding entity views for deviceId [" + device.getId() + "]", e);
}
eventService.removeEvents(device.getTenantId(), device.getId());
relationService.removeRelations(device.getTenantId(), device.getId());
TenantId oldTenantId = device.getTenantId();
device.setTenantId(tenantId);
device.setCustomerId(null);
Device savedDevice = doSaveDevice(device, null, true);
// explicitly remove device with previous tenant id from cache
// result device object will have different tenant id and will not remove entity from cache
cacheManager.removeDeviceFromCacheByName(oldTenantId, device.getName());
cacheManager.removeDeviceFromCacheById(oldTenantId, device.getId());
return savedDevice;
}
use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.
the class AbstractEntityViewEntity method toEntityView.
protected EntityView toEntityView() {
EntityView entityView = new EntityView(new EntityViewId(getUuid()));
entityView.setCreatedTime(createdTime);
if (entityId != null) {
entityView.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType.name(), entityId));
}
if (tenantId != null) {
entityView.setTenantId(TenantId.fromUUID(tenantId));
}
if (customerId != null) {
entityView.setCustomerId(new CustomerId(customerId));
}
entityView.setType(type);
entityView.setName(name);
try {
entityView.setKeys(mapper.readValue(keys, TelemetryEntityView.class));
} catch (IOException e) {
log.error("Unable to read entity view keys!", e);
}
entityView.setStartTimeMs(startTs);
entityView.setEndTimeMs(endTs);
entityView.setAdditionalInfo(additionalInfo);
return entityView;
}
use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.
the class BaseTimeseriesServiceTest method saveAndCreateEntityView.
private EntityView saveAndCreateEntityView(DeviceId deviceId, List<String> timeseries) {
EntityView entityView = new EntityView();
entityView.setName("entity_view_name");
entityView.setType("default");
entityView.setTenantId(tenantId);
TelemetryEntityView keys = new TelemetryEntityView();
keys.setTimeseries(timeseries);
entityView.setKeys(keys);
entityView.setEntityId(deviceId);
return entityViewService.saveEntityView(entityView);
}
use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.
the class BaseTimeseriesServiceTest method testFindByQueryDescOrder.
@Test
public void testFindByQueryDescOrder() throws Exception {
DeviceId deviceId = new DeviceId(Uuids.timeBased());
saveEntries(deviceId, TS - 3);
saveEntries(deviceId, TS - 2);
saveEntries(deviceId, TS - 1);
List<ReadTsKvQuery> queries = new ArrayList<>();
queries.add(new BaseReadTsKvQuery(STRING_KEY, TS - 3, TS, 0, 1000, Aggregation.NONE, "DESC"));
List<TsKvEntry> entries = tsService.findAll(tenantId, deviceId, queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
Assert.assertEquals(3, entries.size());
Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
EntityView entityView = saveAndCreateEntityView(deviceId, Arrays.asList(STRING_KEY));
entries = tsService.findAll(tenantId, entityView.getId(), queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
Assert.assertEquals(3, entries.size());
Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(0));
Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(2));
}
Aggregations