Search in sources :

Example 1 with AlarmData

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

the class AlarmDataAdapter method toEntityData.

private static AlarmData toEntityData(Map<String, Object> row, Map<UUID, EntityId> entityIdMap) {
    Alarm alarm = new Alarm();
    alarm.setId(new AlarmId((UUID) row.get(ModelConstants.ID_PROPERTY)));
    alarm.setCreatedTime((long) row.get(ModelConstants.CREATED_TIME_PROPERTY));
    alarm.setAckTs((long) row.get(ModelConstants.ALARM_ACK_TS_PROPERTY));
    alarm.setClearTs((long) row.get(ModelConstants.ALARM_CLEAR_TS_PROPERTY));
    alarm.setStartTs((long) row.get(ModelConstants.ALARM_START_TS_PROPERTY));
    alarm.setEndTs((long) row.get(ModelConstants.ALARM_END_TS_PROPERTY));
    Object additionalInfo = row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY);
    if (additionalInfo != null) {
        try {
            alarm.setDetails(mapper.readTree(additionalInfo.toString()));
        } catch (JsonProcessingException e) {
            log.warn("Failed to parse json: {}", row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY), e);
        }
    }
    EntityType originatorType = EntityType.values()[(int) row.get(ModelConstants.ALARM_ORIGINATOR_TYPE_PROPERTY)];
    UUID originatorId = (UUID) row.get(ModelConstants.ALARM_ORIGINATOR_ID_PROPERTY);
    alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));
    alarm.setPropagate((boolean) row.get(ModelConstants.ALARM_PROPAGATE_PROPERTY));
    alarm.setPropagateToOwner((boolean) row.get(ModelConstants.ALARM_PROPAGATE_TO_OWNER_PROPERTY));
    alarm.setPropagateToTenant((boolean) row.get(ModelConstants.ALARM_PROPAGATE_TO_TENANT_PROPERTY));
    alarm.setType(row.get(ModelConstants.ALARM_TYPE_PROPERTY).toString());
    alarm.setSeverity(AlarmSeverity.valueOf(row.get(ModelConstants.ALARM_SEVERITY_PROPERTY).toString()));
    alarm.setStatus(AlarmStatus.valueOf(row.get(ModelConstants.ALARM_STATUS_PROPERTY).toString()));
    alarm.setTenantId(TenantId.fromUUID((UUID) row.get(ModelConstants.TENANT_ID_PROPERTY)));
    Object customerIdObj = row.get(ModelConstants.CUSTOMER_ID_PROPERTY);
    CustomerId customerId = customerIdObj != null ? new CustomerId((UUID) customerIdObj) : null;
    alarm.setCustomerId(customerId);
    if (row.get(ModelConstants.ALARM_PROPAGATE_RELATION_TYPES) != null) {
        String propagateRelationTypes = row.get(ModelConstants.ALARM_PROPAGATE_RELATION_TYPES).toString();
        if (!StringUtils.isEmpty(propagateRelationTypes)) {
            alarm.setPropagateRelationTypes(Arrays.asList(propagateRelationTypes.split(",")));
        } else {
            alarm.setPropagateRelationTypes(Collections.emptyList());
        }
    } else {
        alarm.setPropagateRelationTypes(Collections.emptyList());
    }
    UUID entityUuid = (UUID) row.get(ModelConstants.ENTITY_ID_COLUMN);
    EntityId entityId = entityIdMap.get(entityUuid);
    Object originatorNameObj = row.get(ModelConstants.ALARM_ORIGINATOR_NAME_PROPERTY);
    String originatorName = originatorNameObj != null ? originatorNameObj.toString() : null;
    return new AlarmData(alarm, originatorName, entityId);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EntityType(org.thingsboard.server.common.data.EntityType) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 2 with AlarmData

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

the class BaseAlarmServiceTest method testFindPropagatedToOwnerAndTenantAlarm.

@Test
public void testFindPropagatedToOwnerAndTenantAlarm() {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setName("TestTenantDevice");
    device.setType("default");
    device.setTenantId(tenantId);
    device.setCustomerId(customer.getId());
    device = deviceService.saveDevice(device);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated To Tenant").propagateToTenant(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated to Customer").propagate(false).propagateToOwner(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = 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(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> tenantAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(tenantId));
    Assert.assertEquals(1, tenantAlarms.getData().size());
    Assert.assertEquals(tenantAlarm, tenantAlarms.getData().get(0));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customer.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) 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)

Example 3 with AlarmData

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

the class BaseAlarmServiceTest method testFindPropagatedCustomerAssetAlarm.

@Test
public void testFindPropagatedCustomerAssetAlarm() throws ExecutionException, InterruptedException {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device tenantDevice = new Device();
    tenantDevice.setName("TestTenantDevice");
    tenantDevice.setType("default");
    tenantDevice.setTenantId(tenantId);
    tenantDevice = deviceService.saveDevice(tenantDevice);
    Asset customerAsset = new Asset();
    customerAsset.setName("TestCustomerDevice");
    customerAsset.setType("default");
    customerAsset.setTenantId(tenantId);
    customerAsset.setCustomerId(customer.getId());
    customerAsset = assetService.saveAsset(customerAsset);
    EntityRelation relation = new EntityRelation();
    relation.setFrom(customerAsset.getId());
    relation.setTo(tenantDevice.getId());
    relation.setAdditionalInfo(JacksonUtil.newObjectNode());
    relation.setType("Contains");
    relation.setTypeGroup(RelationTypeGroup.COMMON);
    relationService.saveRelation(tenantId, relation);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Not Propagated").propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Propagated").propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = 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(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customerAsset.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) 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)

Example 4 with AlarmData

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

the class TbAlarmDataSubCtx method cleanupOldAlarms.

public void cleanupOldAlarms() {
    long expTime = System.currentTimeMillis() - query.getPageLink().getTimeWindow();
    boolean shouldRefresh = false;
    for (AlarmData alarmData : alarms.getData()) {
        if (alarmData.getCreatedTime() < expTime) {
            shouldRefresh = true;
            break;
        }
    }
    if (shouldRefresh) {
        doFetchAlarms();
    }
}
Also used : AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 5 with AlarmData

use of org.thingsboard.server.common.data.query.AlarmData 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)

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