Search in sources :

Example 1 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method testFindByQueryAscOrder.

@Test
public void testFindByQueryAscOrder() throws Exception {
    DeviceId deviceId = new DeviceId(Uuids.timeBased());
    saveEntries(deviceId, TS - 3);
    saveEntries(deviceId, TS - 2);
    saveEntries(deviceId, TS - 1);
    List<ReadTsKvQuery> queries = new ArrayList<>();
    queries.add(new BaseReadTsKvQuery(STRING_KEY, TS - 3, TS, 0, 1000, Aggregation.NONE, "ASC"));
    List<TsKvEntry> entries = tsService.findAll(tenantId, deviceId, queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(2));
    EntityView entityView = saveAndCreateEntityView(deviceId, Arrays.asList(STRING_KEY));
    entries = tsService.findAll(tenantId, entityView.getId(), queries).get(MAX_TIMEOUT, TimeUnit.SECONDS);
    Assert.assertEquals(3, entries.size());
    Assert.assertEquals(toTsEntry(TS - 3, stringKvEntry), entries.get(0));
    Assert.assertEquals(toTsEntry(TS - 2, stringKvEntry), entries.get(1));
    Assert.assertEquals(toTsEntry(TS - 1, stringKvEntry), entries.get(2));
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 2 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testFindEntityViewById.

@Test
public void testFindEntityViewById() throws Exception {
    EntityView savedView = getNewSavedEntityView("Test entity view");
    EntityView foundView = doGet("/api/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    Assert.assertNotNull(foundView);
    assertEquals(savedView, foundView);
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Test(org.junit.Test)

Example 3 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testGetTenantEntityViewsByName.

@Test
public void testGetTenantEntityViewsByName() throws Exception {
    String name1 = "Entity view name1";
    List<EntityView> namesOfView1 = fillListOf(143, name1);
    List<EntityView> loadedNamesOfView1 = loadListOf(new PageLink(15, 0, name1), "/api/tenant/entityViews?");
    Collections.sort(namesOfView1, idComparator);
    Collections.sort(loadedNamesOfView1, idComparator);
    assertEquals(namesOfView1, loadedNamesOfView1);
    String name2 = "Entity view name2";
    List<EntityView> NamesOfView2 = fillListOf(75, name2);
    List<EntityView> loadedNamesOfView2 = loadListOf(new PageLink(4, 0, name2), "/api/tenant/entityViews?");
    Collections.sort(NamesOfView2, idComparator);
    Collections.sort(loadedNamesOfView2, idComparator);
    assertEquals(NamesOfView2, loadedNamesOfView2);
    for (EntityView view : loadedNamesOfView1) {
        doDelete("/api/entityView/" + view.getId().getId().toString()).andExpect(status().isOk());
    }
    PageData<EntityView> pageData = doGetTypedWithPageLink("/api/tenant/entityViews?", new TypeReference<PageData<EntityView>>() {
    }, new PageLink(4, 0, name1));
    Assert.assertFalse(pageData.hasNext());
    assertEquals(0, pageData.getData().size());
    for (EntityView view : loadedNamesOfView2) {
        doDelete("/api/entityView/" + view.getId().getId().toString()).andExpect(status().isOk());
    }
    pageData = doGetTypedWithPageLink("/api/tenant/entityViews?", new TypeReference<PageData<EntityView>>() {
    }, new PageLink(4, 0, name2));
    Assert.assertFalse(pageData.hasNext());
    assertEquals(0, pageData.getData().size());
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) PageLink(org.thingsboard.server.common.data.page.PageLink) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 4 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testSaveEntityViewWithEmptyName.

@Test
public void testSaveEntityViewWithEmptyName() throws Exception {
    EntityView entityView = new EntityView();
    entityView.setType("default");
    doPost("/api/entityView", entityView).andExpect(status().isBadRequest()).andExpect(statusReason(containsString("Entity view name should be specified!")));
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Test(org.junit.Test)

Example 5 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class DefaultDataUpdateService method updateEntityViewLatestTelemetry.

private ListenableFuture<List<Void>> updateEntityViewLatestTelemetry(EntityView entityView) {
    EntityViewId entityId = entityView.getId();
    List<String> keys = entityView.getKeys() != null && entityView.getKeys().getTimeseries() != null ? entityView.getKeys().getTimeseries() : Collections.emptyList();
    long startTs = entityView.getStartTimeMs();
    long endTs = entityView.getEndTimeMs() == 0 ? Long.MAX_VALUE : entityView.getEndTimeMs();
    ListenableFuture<List<String>> keysFuture;
    if (keys.isEmpty()) {
        keysFuture = Futures.transform(tsService.findAllLatest(TenantId.SYS_TENANT_ID, entityView.getEntityId()), latest -> latest.stream().map(TsKvEntry::getKey).collect(Collectors.toList()), MoreExecutors.directExecutor());
    } else {
        keysFuture = Futures.immediateFuture(keys);
    }
    ListenableFuture<List<TsKvEntry>> latestFuture = Futures.transformAsync(keysFuture, fetchKeys -> {
        List<ReadTsKvQuery> queries = fetchKeys.stream().filter(key -> !isBlank(key)).map(key -> new BaseReadTsKvQuery(key, startTs, endTs, 1, "DESC")).collect(Collectors.toList());
        if (!queries.isEmpty()) {
            return tsService.findAll(TenantId.SYS_TENANT_ID, entityView.getEntityId(), queries);
        } else {
            return Futures.immediateFuture(null);
        }
    }, MoreExecutors.directExecutor());
    return Futures.transformAsync(latestFuture, latestValues -> {
        if (latestValues != null && !latestValues.isEmpty()) {
            ListenableFuture<List<Void>> saveFuture = tsService.saveLatest(TenantId.SYS_TENANT_ID, entityId, latestValues);
            return saveFuture;
        }
        return Futures.immediateFuture(null);
    }, MoreExecutors.directExecutor());
}
Also used : TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TenantService(org.thingsboard.server.dao.tenant.TenantService) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) OAuth2Service(org.thingsboard.server.dao.oauth2.OAuth2Service) EntityService(org.thingsboard.server.dao.entity.EntityService) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceProfileRepository(org.thingsboard.server.dao.sql.device.DeviceProfileRepository) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) PageLink(org.thingsboard.server.common.data.page.PageLink) RelationEntity(org.thingsboard.server.dao.model.sql.RelationEntity) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) Collectors(java.util.stream.Collectors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmDao(org.thingsboard.server.dao.alarm.AlarmDao) EntityView(org.thingsboard.server.common.data.EntityView) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) TbDeviceProfileNodeConfiguration(org.thingsboard.rule.engine.profile.TbDeviceProfileNodeConfiguration) List(java.util.List) DeviceProfileEntity(org.thingsboard.server.dao.model.sql.DeviceProfileEntity) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) TbDeviceProfileNode(org.thingsboard.rule.engine.profile.TbDeviceProfileNode) AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) DynamicValue(org.thingsboard.server.common.data.query.DynamicValue) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) JacksonUtil(org.thingsboard.common.util.JacksonUtil) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode) Tenant(org.thingsboard.server.common.data.Tenant) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) ArrayList(java.util.ArrayList) RelationService(org.thingsboard.server.dao.relation.RelationService) Service(org.springframework.stereotype.Service) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) InstallScripts(org.thingsboard.server.service.install.InstallScripts) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Profile(org.springframework.context.annotation.Profile) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Futures(com.google.common.util.concurrent.Futures) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) DaoUtil(org.thingsboard.server.dao.DaoUtil) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

EntityView (org.thingsboard.server.common.data.EntityView)51 TelemetryEntityView (org.thingsboard.server.common.data.objects.TelemetryEntityView)21 Test (org.junit.Test)18 AttributesEntityView (org.thingsboard.server.common.data.objects.AttributesEntityView)17 ExecutionException (java.util.concurrent.ExecutionException)16 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)15 ArrayList (java.util.ArrayList)13 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Customer (org.thingsboard.server.common.data.Customer)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ApiOperation (io.swagger.annotations.ApiOperation)9 List (java.util.List)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 Edge (org.thingsboard.server.common.data.edge.Edge)8 PageLink (org.thingsboard.server.common.data.page.PageLink)7 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6