Search in sources :

Example 1 with AlarmDataPageLink

use of org.thingsboard.server.common.data.query.AlarmDataPageLink 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 2 with AlarmDataPageLink

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

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

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

the class TbAlarmDataSubCtx method filter.

private boolean filter(Alarm alarm) {
    AlarmDataPageLink filter = query.getPageLink();
    long startTs = System.currentTimeMillis() - filter.getTimeWindow();
    if (alarm.getCreatedTime() < startTs) {
        // Skip update that does not match time window.
        return false;
    }
    if (filter.getTypeList() != null && !filter.getTypeList().isEmpty() && !filter.getTypeList().contains(alarm.getType())) {
        return false;
    }
    if (filter.getSeverityList() != null && !filter.getSeverityList().isEmpty()) {
        if (!filter.getSeverityList().contains(alarm.getSeverity())) {
            return false;
        }
    }
    if (filter.getStatusList() != null && !filter.getStatusList().isEmpty()) {
        boolean matches = false;
        for (AlarmSearchStatus status : filter.getStatusList()) {
            if (status.getStatuses().contains(alarm.getStatus())) {
                matches = true;
                break;
            }
        }
        if (!matches) {
            return false;
        }
    }
    return true;
}
Also used : AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink)

Example 5 with AlarmDataPageLink

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

AlarmDataPageLink (org.thingsboard.server.common.data.query.AlarmDataPageLink)7 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)5 Test (org.junit.Test)4 Alarm (org.thingsboard.server.common.data.alarm.Alarm)4 AlarmData (org.thingsboard.server.common.data.query.AlarmData)4 EntityKey (org.thingsboard.server.common.data.query.EntityKey)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 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 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AlarmSearchStatus (org.thingsboard.server.common.data.alarm.AlarmSearchStatus)1 AlarmSeverity (org.thingsboard.server.common.data.alarm.AlarmSeverity)1 AlarmStatus (org.thingsboard.server.common.data.alarm.AlarmStatus)1 Asset (org.thingsboard.server.common.data.asset.Asset)1 AssetId (org.thingsboard.server.common.data.id.AssetId)1