Search in sources :

Example 6 with AlarmData

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

the class TbAlarmDataSubCtx method sendWsMsg.

private void sendWsMsg(String sessionId, AlarmSubscriptionUpdate subscriptionUpdate) {
    Alarm alarm = subscriptionUpdate.getAlarm();
    AlarmId alarmId = alarm.getId();
    if (subscriptionUpdate.isAlarmDeleted()) {
        Alarm deleted = alarmsMap.remove(alarmId);
        if (deleted != null) {
            fetchAlarms();
        }
    } else {
        AlarmData current = alarmsMap.get(alarmId);
        boolean onCurrentPage = current != null;
        boolean matchesFilter = filter(alarm);
        if (onCurrentPage) {
            if (matchesFilter) {
                AlarmData updated = new AlarmData(alarm, current.getOriginatorName(), current.getEntityId());
                updated.getLatest().putAll(current.getLatest());
                alarmsMap.put(alarmId, updated);
                wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, Collections.singletonList(updated), maxEntitiesPerAlarmSubscription, data.getTotalElements()));
            } else {
                fetchAlarms();
            }
        } else if (matchesFilter && query.getPageLink().getPage() == 0) {
            fetchAlarms();
        }
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 7 with AlarmData

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

the class TbAlarmDataSubCtx method doFetchAlarms.

private void doFetchAlarms() {
    AlarmDataUpdate update;
    if (!entitiesMap.isEmpty()) {
        long start = System.currentTimeMillis();
        PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(getTenantId(), query, getOrderedEntityIds());
        long end = System.currentTimeMillis();
        stats.getAlarmQueryInvocationCnt().incrementAndGet();
        stats.getAlarmQueryTimeSpent().addAndGet(end - start);
        alarms = setAndMergeAlarmsData(alarms);
        update = new AlarmDataUpdate(cmdId, alarms, null, maxEntitiesPerAlarmSubscription, data.getTotalElements());
    } else {
        update = new AlarmDataUpdate(cmdId, new PageData<>(), null, maxEntitiesPerAlarmSubscription, data.getTotalElements());
    }
    wsService.sendWsMsg(getSessionId(), update);
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 8 with AlarmData

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

the class DefaultEntityQueryService method findAlarmDataByQuery.

@Override
public PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query) {
    EntityDataQuery entityDataQuery = this.buildEntityDataQuery(query);
    PageData<EntityData> entities = entityService.findEntityDataByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), entityDataQuery);
    if (entities.getTotalElements() > 0) {
        LinkedHashMap<EntityId, EntityData> entitiesMap = new LinkedHashMap<>();
        for (EntityData entityData : entities.getData()) {
            entitiesMap.put(entityData.getEntityId(), entityData);
        }
        PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(securityUser.getTenantId(), query, entitiesMap.keySet());
        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());
                }
            }
        }
        return alarms;
    } else {
        return new PageData<>();
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) PageData(org.thingsboard.server.common.data.page.PageData) EntityData(org.thingsboard.server.common.data.query.EntityData) LinkedHashMap(java.util.LinkedHashMap) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 9 with AlarmData

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

the class TbAlarmDataSubCtx method sendWsMsg.

@Override
void sendWsMsg(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType, boolean resultToLatestValues) {
    EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
    if (entityId != null) {
        Map<String, TsValue> latestUpdate = new HashMap<>();
        subscriptionUpdate.getData().forEach((k, v) -> {
            Object[] data = (Object[]) v.get(0);
            latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
        });
        EntityData entityData = entitiesMap.get(entityId);
        entityData.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
        log.trace("[{}][{}][{}][{}] Received subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
        List<AlarmData> update = alarmsMap.values().stream().filter(alarm -> entityId.equals(alarm.getEntityId())).map(alarm -> {
            alarm.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
            return alarm;
        }).collect(Collectors.toList());
        if (!update.isEmpty()) {
            wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, update, maxEntitiesPerAlarmSubscription, data.getTotalElements()));
        }
    } else {
        log.trace("[{}][{}][{}][{}] Received stale subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) Setter(lombok.Setter) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Getter(lombok.Getter) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) TsValue(org.thingsboard.server.common.data.query.TsValue) AlarmSubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) Map(java.util.Map) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityService(org.thingsboard.server.dao.entity.EntityService) EntityKey(org.thingsboard.server.common.data.query.EntityKey) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Collection(java.util.Collection) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) Set(java.util.Set) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) Collectors(java.util.stream.Collectors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AlarmData(org.thingsboard.server.common.data.query.AlarmData) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) AlarmId(org.thingsboard.server.common.data.id.AlarmId) PageData(org.thingsboard.server.common.data.page.PageData) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) Collections(java.util.Collections) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) EntityData(org.thingsboard.server.common.data.query.EntityData) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 10 with AlarmData

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

the class BaseAlarmServiceTest method testFindAlarmUsingAlarmDataQuery.

@Test
public void testFindAlarmUsingAlarmDataQuery() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(Uuids.timeBased());
    AssetId parentId2 = new AssetId(Uuids.timeBased());
    AssetId childId = new AssetId(Uuids.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    EntityRelation relation2 = new EntityRelation(parentId2, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(tenantId, relation).get());
    Assert.assertTrue(relationService.saveRelationAsync(tenantId, relation2).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(alarm);
    Alarm created = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(false);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(false);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, new Alarm(alarms.getData().get(0)));
    pageLink.setSearchPropagatedAlarms(true);
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, new Alarm(alarms.getData().get(0)));
    // Check child relation
    created.setPropagate(true);
    result = alarmService.createOrUpdateAlarm(created);
    created = result.getAlarm();
    // Check child relation
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(parentId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    PageData<AlarmInfo> alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    alarmsInfoData = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(parentId2).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarmsInfoData.getData());
    Assert.assertEquals(1, alarmsInfoData.getData().size());
    Assert.assertEquals(created, alarmsInfoData.getData().get(0));
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(parentId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    alarmService.ackAlarm(tenantId, created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(tenantId, created.getId()).get();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    alarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(childId));
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
}
Also used : SortOrder(org.thingsboard.server.common.data.page.SortOrder) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AssetId(org.thingsboard.server.common.data.id.AssetId) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Aggregations

AlarmData (org.thingsboard.server.common.data.query.AlarmData)11 Alarm (org.thingsboard.server.common.data.alarm.Alarm)7 AlarmDataPageLink (org.thingsboard.server.common.data.query.AlarmDataPageLink)5 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)5 EntityKey (org.thingsboard.server.common.data.query.EntityKey)5 Test (org.junit.Test)4 EntityId (org.thingsboard.server.common.data.id.EntityId)4 AlarmOperationResult (org.thingsboard.server.dao.alarm.AlarmOperationResult)4 Customer (org.thingsboard.server.common.data.Customer)3 Device (org.thingsboard.server.common.data.Device)3 AlarmId (org.thingsboard.server.common.data.id.AlarmId)3 PageData (org.thingsboard.server.common.data.page.PageData)3 EntityData (org.thingsboard.server.common.data.query.EntityData)3 LinkedHashMap (java.util.LinkedHashMap)2 AlarmInfo (org.thingsboard.server.common.data.alarm.AlarmInfo)2 SortOrder (org.thingsboard.server.common.data.page.SortOrder)2 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)2 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)2 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)2 AlarmDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate)2