Search in sources :

Example 16 with EntityData

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

the class DefaultTbEntityDataSubscriptionService method handleLatestCmd.

private void handleLatestCmd(TbEntityDataSubCtx ctx, LatestValueCmd latestCmd) {
    log.trace("[{}][{}] Going to process latest command: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
    // Fetch the latest values for telemetry keys (in case they are not copied from NoSQL to SQL DB in hybrid mode.
    if (!tsInSqlDB) {
        log.trace("[{}][{}] Going to fetch missing latest values: {}", ctx.getSessionId(), ctx.getCmdId(), latestCmd);
        List<String> allTsKeys = latestCmd.getKeys().stream().filter(key -> key.getType().equals(EntityKeyType.TIME_SERIES)).map(EntityKey::getKey).collect(Collectors.toList());
        Map<EntityData, ListenableFuture<Map<String, TsValue>>> missingTelemetryFutures = new HashMap<>();
        for (EntityData entityData : ctx.getData().getData()) {
            Map<EntityKeyType, Map<String, TsValue>> latestEntityData = entityData.getLatest();
            Map<String, TsValue> tsEntityData = latestEntityData.get(EntityKeyType.TIME_SERIES);
            Set<String> missingTsKeys = new LinkedHashSet<>(allTsKeys);
            if (tsEntityData != null) {
                missingTsKeys.removeAll(tsEntityData.keySet());
            } else {
                tsEntityData = new HashMap<>();
                latestEntityData.put(EntityKeyType.TIME_SERIES, tsEntityData);
            }
            ListenableFuture<List<TsKvEntry>> missingTsData = tsService.findLatest(ctx.getTenantId(), entityData.getEntityId(), missingTsKeys);
            missingTelemetryFutures.put(entityData, Futures.transform(missingTsData, this::toTsValue, MoreExecutors.directExecutor()));
        }
        Futures.addCallback(Futures.allAsList(missingTelemetryFutures.values()), new FutureCallback<List<Map<String, TsValue>>>() {

            @Override
            public void onSuccess(@Nullable List<Map<String, TsValue>> result) {
                missingTelemetryFutures.forEach((key, value) -> {
                    try {
                        key.getLatest().get(EntityKeyType.TIME_SERIES).putAll(value.get());
                    } catch (InterruptedException | ExecutionException e) {
                        log.warn("[{}][{}] Failed to lookup latest telemetry: {}:{}", ctx.getSessionId(), ctx.getCmdId(), key.getEntityId(), allTsKeys, e);
                    }
                });
                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);
                ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
            }

            @Override
            public void onFailure(Throwable t) {
                log.warn("[{}][{}] Failed to process websocket command: {}:{}", ctx.getSessionId(), ctx.getCmdId(), ctx.getQuery(), latestCmd, t);
                wsService.sendWsMsg(ctx.getSessionId(), new EntityDataUpdate(ctx.getCmdId(), SubscriptionErrorCode.INTERNAL_ERROR.getCode(), "Failed to process websocket command!"));
            }
        }, wsCallBackExecutor);
    } else {
        if (!ctx.isInitialDataSent()) {
            EntityDataUpdate update = new EntityDataUpdate(ctx.getCmdId(), ctx.getData(), null, ctx.getMaxEntitiesPerDataSubscription());
            wsService.sendWsMsg(ctx.getSessionId(), update);
            ctx.setInitialDataSent(true);
        }
        ctx.createLatestValuesSubscriptions(latestCmd.getKeys());
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) LinkedHashSet(java.util.LinkedHashSet) 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) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with EntityData

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

the class TbAlarmDataSubCtx method doUpdate.

@Override
protected synchronized void doUpdate(Map<EntityId, EntityData> newDataMap) {
    resetInvocationCounter();
    entitiesMap.clear();
    tooManyEntities = data.hasNext();
    for (EntityData entityData : data.getData()) {
        entitiesMap.put(entityData.getEntityId(), entityData);
    }
    fetchAlarms();
    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()) {
        List<EntityKey> keys = query.getLatestValues();
        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));
            });
        }
        long startTs = System.currentTimeMillis() - query.getPageLink().getTimeWindow();
        newSubsList.forEach(entity -> createAlarmSubscriptionForEntity(query.getPageLink(), startTs, entity));
    }
    subIdsToCancel.forEach(subId -> localSubscriptionService.cancelSubscription(getSessionId(), subId));
    subsToAdd.forEach(localSubscriptionService::addSubscription);
}
Also used : EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) EntityData(org.thingsboard.server.common.data.query.EntityData) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 18 with EntityData

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

the class TbAlarmDataSubCtx method setAndMergeAlarmsData.

public PageData<AlarmData> setAndMergeAlarmsData(PageData<AlarmData> alarms) {
    this.alarms = alarms;
    for (AlarmData alarmData : alarms.getData()) {
        EntityId entityId = alarmData.getEntityId();
        if (entityId != null) {
            EntityData entityData = entitiesMap.get(entityId);
            if (entityData != null) {
                alarmData.getLatest().putAll(entityData.getLatest());
            }
        }
    }
    alarmsMap.clear();
    alarmsMap.putAll(alarms.getData().stream().collect(Collectors.toMap(AlarmData::getId, Function.identity(), (a, b) -> a)));
    return this.alarms;
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EntityData(org.thingsboard.server.common.data.query.EntityData) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 19 with EntityData

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

the class TbAlarmDataSubCtx method createAlarmSubscriptions.

public void createAlarmSubscriptions() {
    AlarmDataPageLink pageLink = query.getPageLink();
    long startTs = System.currentTimeMillis() - pageLink.getTimeWindow();
    for (EntityData entityData : entitiesMap.values()) {
        createAlarmSubscriptionForEntity(pageLink, startTs, entityData);
    }
}
Also used : EntityData(org.thingsboard.server.common.data.query.EntityData) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink)

Example 20 with EntityData

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

the class TbAlarmDataSubCtx method fetchData.

public void fetchData() {
    resetInvocationCounter();
    log.trace("[{}] Fetching data: {}", cmdId, alarmInvocationAttempts);
    super.fetchData();
    entitiesMap.clear();
    tooManyEntities = data.hasNext();
    for (EntityData entityData : data.getData()) {
        entitiesMap.put(entityData.getEntityId(), entityData);
    }
}
Also used : EntityData(org.thingsboard.server.common.data.query.EntityData)

Aggregations

EntityData (org.thingsboard.server.common.data.query.EntityData)43 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)34 Device (org.thingsboard.server.common.data.Device)28 Test (org.junit.Test)27 EntityKey (org.thingsboard.server.common.data.query.EntityKey)25 EntityDataPageLink (org.thingsboard.server.common.data.query.EntityDataPageLink)23 EntityKeyType (org.thingsboard.server.common.data.query.EntityKeyType)22 ArrayList (java.util.ArrayList)21 List (java.util.List)21 EntityId (org.thingsboard.server.common.data.id.EntityId)20 DeviceTypeFilter (org.thingsboard.server.common.data.query.DeviceTypeFilter)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Collectors (java.util.stream.Collectors)18 PageData (org.thingsboard.server.common.data.page.PageData)18 LongDataEntry (org.thingsboard.server.common.data.kv.LongDataEntry)16 Collections (java.util.Collections)15 Slf4j (lombok.extern.slf4j.Slf4j)15 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)15 EntityType (org.thingsboard.server.common.data.EntityType)14