Search in sources :

Example 21 with EntityKey

use of org.thingsboard.server.common.data.query.EntityKey in project thingsboard by thingsboard.

the class BaseEntityServiceTest method createDeviceSearchQuery.

private EntityDataQuery createDeviceSearchQuery(String deviceField, StringOperation operation, String searchQuery) {
    DeviceTypeFilter deviceTypeFilter = new DeviceTypeFilter();
    deviceTypeFilter.setDeviceType("default");
    deviceTypeFilter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(1000, 0, null, sortOrder);
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "label"));
    List<KeyFilter> keyFilters = createStringKeyFilters(deviceField, EntityKeyType.ENTITY_FIELD, operation, searchQuery);
    return new EntityDataQuery(deviceTypeFilter, pageLink, entityFields, null, keyFilters);
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink)

Example 22 with EntityKey

use of org.thingsboard.server.common.data.query.EntityKey in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleGetTsCmd.

private ListenableFuture<TbEntityDataSubCtx> handleGetTsCmd(TbEntityDataSubCtx ctx, GetTsCmd cmd, boolean subscribe) {
    List<String> keys = cmd.getKeys();
    List<ReadTsKvQuery> finalTsKvQueryList;
    List<ReadTsKvQuery> tsKvQueryList = cmd.getKeys().stream().map(key -> new BaseReadTsKvQuery(key, cmd.getStartTs(), cmd.getEndTs(), cmd.getInterval(), getLimit(cmd.getLimit()), cmd.getAgg())).collect(Collectors.toList());
    if (cmd.isFetchLatestPreviousPoint()) {
        finalTsKvQueryList = new ArrayList<>(tsKvQueryList);
        finalTsKvQueryList.addAll(cmd.getKeys().stream().map(key -> new BaseReadTsKvQuery(key, cmd.getStartTs() - TimeUnit.DAYS.toMillis(365), cmd.getStartTs(), cmd.getInterval(), 1, cmd.getAgg())).collect(Collectors.toList()));
    } else {
        finalTsKvQueryList = tsKvQueryList;
    }
    Map<EntityData, ListenableFuture<List<TsKvEntry>>> fetchResultMap = new HashMap<>();
    ctx.getData().getData().forEach(entityData -> fetchResultMap.put(entityData, tsService.findAll(ctx.getTenantId(), entityData.getEntityId(), finalTsKvQueryList)));
    return Futures.transform(Futures.allAsList(fetchResultMap.values()), f -> {
        fetchResultMap.forEach((entityData, future) -> {
            Map<String, List<TsValue>> keyData = new LinkedHashMap<>();
            cmd.getKeys().forEach(key -> keyData.put(key, new ArrayList<>()));
            try {
                List<TsKvEntry> entityTsData = future.get();
                if (entityTsData != null) {
                    entityTsData.forEach(entry -> keyData.get(entry.getKey()).add(new TsValue(entry.getTs(), entry.getValueAsString())));
                }
                keyData.forEach((k, v) -> entityData.getTimeseries().put(k, v.toArray(new TsValue[v.size()])));
                if (cmd.isFetchLatestPreviousPoint()) {
                    entityData.getTimeseries().values().forEach(dataArray -> {
                        Arrays.sort(dataArray, (o1, o2) -> Long.compare(o2.getTs(), o1.getTs()));
                    });
                }
            } catch (InterruptedException | ExecutionException e) {
                log.warn("[{}][{}][{}] Failed to fetch historical data", ctx.getSessionId(), ctx.getCmdId(), entityData.getEntityId(), e);
                wsService.sendWsMsg(ctx.getSessionId(), new EntityDataUpdate(ctx.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR.getCode(), "Failed to fetch historical data!"));
            }
        });
        EntityDataUpdate update;
        if (!ctx.isInitialDataSent()) {
            update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
            ctx.setInitialDataSent(true);
        } else {
            update = new EntityDataUpdate(ctx.getCmdId(), null, ctx.getData().getData(), ctx.getMaxEntitiesPerDataSubscription());
        }
        wsService.sendWsMsg(ctx.getSessionId(), update);
        if (subscribe) {
            ctx.createTimeseriesSubscriptions(keys.stream().map(key -> new EntityKey(EntityKeyType.TIME_SERIES, key)).collect(Collectors.toList()), cmd.getStartTs(), cmd.getEndTs());
        }
        ctx.getData().getData().forEach(ed -> ed.getTimeseries().clear());
        return ctx;
    }, wsCallBackExecutor);
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) TsValue(org.thingsboard.server.common.data.query.TsValue) Autowired(org.springframework.beans.factory.annotation.Autowired) CloseStatus(org.springframework.web.socket.CloseStatus) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Map(java.util.Map) EntityService(org.thingsboard.server.dao.entity.EntityService) TbServiceInfoProvider(org.thingsboard.server.queue.discovery.TbServiceInfoProvider) ThreadFactory(java.util.concurrent.ThreadFactory) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Set(java.util.Set) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) Collectors(java.util.stream.Collectors) GetTsCmd(org.thingsboard.server.service.telemetry.cmd.v2.GetTsCmd) Executors(java.util.concurrent.Executors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) EntityHistoryCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityHistoryCmd) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) UnsubscribeCmd(org.thingsboard.server.service.telemetry.cmd.v2.UnsubscribeCmd) PostConstruct(javax.annotation.PostConstruct) Lazy(org.springframework.context.annotation.Lazy) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Getter(lombok.Getter) AlarmDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd) LatestValueCmd(org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) LinkedHashMap(java.util.LinkedHashMap) EntityDataCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeSeriesCmd(org.thingsboard.server.service.telemetry.cmd.v2.TimeSeriesCmd) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) EntityData(org.thingsboard.server.common.data.query.EntityData) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) FutureCallback(com.google.common.util.concurrent.FutureCallback) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) SubscriptionErrorCode(org.thingsboard.server.service.telemetry.sub.SubscriptionErrorCode) PageData(org.thingsboard.server.common.data.page.PageData) EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TsValue(org.thingsboard.server.common.data.query.TsValue) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) LinkedHashMap(java.util.LinkedHashMap) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with EntityKey

use of org.thingsboard.server.common.data.query.EntityKey in project thingsboard by thingsboard.

the class TbAlarmDataSubCtx method buildEntityDataQuery.

@Override
protected EntityDataQuery buildEntityDataQuery() {
    EntityDataSortOrder sortOrder = query.getPageLink().getSortOrder();
    EntityDataSortOrder entitiesSortOrder;
    if (sortOrder == null || sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) {
        entitiesSortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, ModelConstants.CREATED_TIME_PROPERTY));
    } else {
        entitiesSortOrder = sortOrder;
    }
    EntityDataPageLink edpl = new EntityDataPageLink(maxEntitiesPerAlarmSubscription, 0, null, entitiesSortOrder);
    return new EntityDataQuery(query.getEntityFilter(), edpl, query.getEntityFields(), query.getLatestValues(), query.getKeyFilters());
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink)

Example 24 with EntityKey

use of org.thingsboard.server.common.data.query.EntityKey in project thingsboard by thingsboard.

the class TbEntityDataSubCtx method doUpdate.

@Override
public synchronized void doUpdate(Map<EntityId, EntityData> newDataMap) {
    List<Integer> subIdsToCancel = new ArrayList<>();
    List<TbSubscription> subsToAdd = new ArrayList<>();
    Set<EntityId> currentSubs = new HashSet<>();
    subToEntityIdMap.forEach((subId, entityId) -> {
        if (!newDataMap.containsKey(entityId)) {
            subIdsToCancel.add(subId);
        } else {
            currentSubs.add(entityId);
        }
    });
    log.trace("[{}][{}] Subscriptions that are invalid: {}", sessionRef.getSessionId(), cmdId, subIdsToCancel);
    subIdsToCancel.forEach(subToEntityIdMap::remove);
    List<EntityData> newSubsList = newDataMap.entrySet().stream().filter(entry -> !currentSubs.contains(entry.getKey())).map(Map.Entry::getValue).collect(Collectors.toList());
    if (!newSubsList.isEmpty()) {
        // NOTE: We ignore the TS subscriptions for new entities here, because widgets will re-init it's content and will create new subscriptions.
        if (curTsCmd == null && latestValueCmd != null) {
            List<EntityKey> keys = latestValueCmd.getKeys();
            if (keys != null && !keys.isEmpty()) {
                Map<EntityKeyType, List<EntityKey>> keysByType = getEntityKeyByTypeMap(keys);
                newSubsList.forEach(entity -> {
                    log.trace("[{}][{}] Found new subscription for entity: {}", sessionRef.getSessionId(), cmdId, entity.getEntityId());
                    subsToAdd.addAll(addSubscriptions(entity, keysByType, true, 0, 0));
                });
            }
        }
    }
    wsService.sendWsMsg(sessionRef.getSessionId(), new EntityDataUpdate(cmdId, data, null, maxEntitiesPerDataSubscription));
    subIdsToCancel.forEach(subId -> localSubscriptionService.cancelSubscription(getSessionId(), subId));
    subsToAdd.forEach(localSubscriptionService::addSubscription);
}
Also used : EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) EntityKey(org.thingsboard.server.common.data.query.EntityKey) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 25 with EntityKey

use of org.thingsboard.server.common.data.query.EntityKey in project thingsboard by thingsboard.

the class BaseWebsocketApiTest method testEntityCountWsCmd.

@Test
public void testEntityCountWsCmd() throws Exception {
    Device device = new Device();
    device.setName("Device");
    device.setType("default");
    device.setLabel("testLabel" + (int) (Math.random() * 1000));
    device = doPost("/api/device", device, Device.class);
    AttributeKvEntry dataPoint1 = new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperature", 42L));
    sendAttributes(device, TbAttributeSubscriptionScope.SERVER_SCOPE, Collections.singletonList(dataPoint1));
    DeviceTypeFilter dtf1 = new DeviceTypeFilter();
    dtf1.setDeviceNameFilter("D");
    dtf1.setDeviceType("default");
    EntityCountQuery edq1 = new EntityCountQuery(dtf1, Collections.emptyList());
    EntityCountCmd cmd1 = new EntityCountCmd(1, edq1);
    TelemetryPluginCmdsWrapper wrapper1 = new TelemetryPluginCmdsWrapper();
    wrapper1.setEntityCountCmds(Collections.singletonList(cmd1));
    wsClient.send(mapper.writeValueAsString(wrapper1));
    String msg1 = wsClient.waitForReply();
    EntityCountUpdate update1 = mapper.readValue(msg1, EntityCountUpdate.class);
    Assert.assertEquals(1, update1.getCmdId());
    Assert.assertEquals(1, update1.getCount());
    DeviceTypeFilter dtf2 = new DeviceTypeFilter();
    dtf2.setDeviceNameFilter("D");
    dtf2.setDeviceType("non-existing-device-type");
    EntityCountQuery edq2 = new EntityCountQuery(dtf2, Collections.emptyList());
    EntityCountCmd cmd2 = new EntityCountCmd(2, edq2);
    TelemetryPluginCmdsWrapper wrapper2 = new TelemetryPluginCmdsWrapper();
    wrapper2.setEntityCountCmds(Collections.singletonList(cmd2));
    wsClient.send(mapper.writeValueAsString(wrapper2));
    String msg2 = wsClient.waitForReply();
    EntityCountUpdate update2 = mapper.readValue(msg2, EntityCountUpdate.class);
    Assert.assertEquals(2, update2.getCmdId());
    Assert.assertEquals(0, update2.getCount());
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(40));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    highTemperatureFilter.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf3 = new DeviceTypeFilter();
    dtf3.setDeviceNameFilter("D");
    dtf3.setDeviceType("default");
    EntityCountQuery edq3 = new EntityCountQuery(dtf3, Collections.singletonList(highTemperatureFilter));
    EntityCountCmd cmd3 = new EntityCountCmd(3, edq3);
    TelemetryPluginCmdsWrapper wrapper3 = new TelemetryPluginCmdsWrapper();
    wrapper3.setEntityCountCmds(Collections.singletonList(cmd3));
    wsClient.send(mapper.writeValueAsString(wrapper3));
    String msg3 = wsClient.waitForReply();
    EntityCountUpdate update3 = mapper.readValue(msg3, EntityCountUpdate.class);
    Assert.assertEquals(3, update3.getCmdId());
    Assert.assertEquals(1, update3.getCount());
    KeyFilter highTemperatureFilter2 = new KeyFilter();
    highTemperatureFilter2.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate2 = new NumericFilterPredicate();
    predicate2.setValue(FilterPredicateValue.fromDouble(50));
    predicate2.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter2.setPredicate(predicate2);
    highTemperatureFilter2.setValueType(EntityKeyValueType.NUMERIC);
    DeviceTypeFilter dtf4 = new DeviceTypeFilter();
    dtf4.setDeviceNameFilter("D");
    dtf4.setDeviceType("default");
    EntityCountQuery edq4 = new EntityCountQuery(dtf4, Collections.singletonList(highTemperatureFilter2));
    EntityCountCmd cmd4 = new EntityCountCmd(4, edq4);
    TelemetryPluginCmdsWrapper wrapper4 = new TelemetryPluginCmdsWrapper();
    wrapper4.setEntityCountCmds(Collections.singletonList(cmd4));
    wsClient.send(mapper.writeValueAsString(wrapper4));
    String msg4 = wsClient.waitForReply();
    EntityCountUpdate update4 = mapper.readValue(msg4, EntityCountUpdate.class);
    Assert.assertEquals(4, update4.getCmdId());
    Assert.assertEquals(0, update4.getCount());
}
Also used : EntityCountCmd(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountCmd) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityCountUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityCountUpdate) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) TelemetryPluginCmdsWrapper(org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper) Test(org.junit.Test)

Aggregations

EntityKey (org.thingsboard.server.common.data.query.EntityKey)36 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)24 EntityData (org.thingsboard.server.common.data.query.EntityData)23 Test (org.junit.Test)22 Device (org.thingsboard.server.common.data.Device)22 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)22 ArrayList (java.util.ArrayList)21 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)21 List (java.util.List)19 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)19 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)19 KeyFilter (org.thingsboard.server.common.data.query.KeyFilter)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)16 EntityId (org.thingsboard.server.common.data.id.EntityId)15 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)15 PageData (org.thingsboard.server.common.data.page.PageData)15 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)15 EntityType (org.thingsboard.server.common.data.EntityType)14