Merge pull request #5656 from desoliture1/device_search_improvements

[3.3.3] Device search improvements
This commit is contained in:
Andrew Shvayka 2021-12-28 18:36:50 +02:00 committed by GitHub
commit 6867744a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 193 additions and 32 deletions

View File

@ -19,7 +19,6 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.dao.model.sql.DeviceEntity; import org.thingsboard.server.dao.model.sql.DeviceEntity;
@ -83,7 +82,10 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID> {
"LEFT JOIN CustomerEntity c on c.id = d.customerId " + "LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " + "WHERE d.tenantId = :tenantId " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(p.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))")
Page<DeviceInfoEntity> findDeviceInfosByTenantId(@Param("tenantId") UUID tenantId, Page<DeviceInfoEntity> findDeviceInfosByTenantId(@Param("tenantId") UUID tenantId,
@Param("textSearch") String textSearch, @Param("textSearch") String textSearch,
Pageable pageable); Pageable pageable);
@ -132,7 +134,9 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID> {
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " + "WHERE d.tenantId = :tenantId " +
"AND d.type = :type " + "AND d.type = :type " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))")
Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndType(@Param("tenantId") UUID tenantId, Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndType(@Param("tenantId") UUID tenantId,
@Param("type") String type, @Param("type") String type,
@Param("textSearch") String textSearch, @Param("textSearch") String textSearch,
@ -144,7 +148,9 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID> {
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " + "LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " + "WHERE d.tenantId = :tenantId " +
"AND d.deviceProfileId = :deviceProfileId " + "AND d.deviceProfileId = :deviceProfileId " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))") "AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))")
Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndDeviceProfileId(@Param("tenantId") UUID tenantId, Page<DeviceInfoEntity> findDeviceInfosByTenantIdAndDeviceProfileId(@Param("tenantId") UUID tenantId,
@Param("deviceProfileId") UUID deviceProfileId, @Param("deviceProfileId") UUID deviceProfileId,
@Param("textSearch") String textSearch, @Param("textSearch") String textSearch,

View File

@ -352,7 +352,9 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
Assert.assertEquals("typeB", deviceTypes.get(1).getType()); Assert.assertEquals("typeB", deviceTypes.get(1).getType());
Assert.assertEquals("typeC", deviceTypes.get(2).getType()); Assert.assertEquals("typeC", deviceTypes.get(2).getType());
} finally { } finally {
devices.forEach((device) -> { deviceService.deleteDevice(tenantId, device.getId()); }); devices.forEach((device) -> {
deviceService.deleteDevice(tenantId, device.getId());
});
} }
} }
@ -818,4 +820,157 @@ public abstract class BaseDeviceServiceTest extends AbstractServiceTest {
Assert.assertNull("Can't find device by name in cache if it was renamed", renamedDevice); Assert.assertNull("Can't find device by name in cache if it was renamed", renamedDevice);
deviceService.deleteDevice(tenantId, savedDevice.getId()); deviceService.deleteDevice(tenantId, savedDevice.getId());
} }
@Test
public void testFindDeviceInfoByTenantId() {
Customer customer = new Customer();
customer.setTitle("Customer X");
customer.setTenantId(tenantId);
Customer savedCustomer = customerService.saveCustomer(customer);
Device device = new Device();
device.setTenantId(tenantId);
device.setName("default");
device.setType("default");
device.setLabel("label");
device.setCustomerId(savedCustomer.getId());
Device savedDevice = deviceService.saveDevice(device);
PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
List<DeviceInfo> deviceInfosWithLabel = deviceService
.findDeviceInfosByTenantId(tenantId, pageLinkWithLabel).getData();
Assert.assertFalse(deviceInfosWithLabel.isEmpty());
Assert.assertTrue(
deviceInfosWithLabel.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getLabel().equals(savedDevice.getLabel())
)
);
PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
List<DeviceInfo> deviceInfosWithCustomer = deviceService
.findDeviceInfosByTenantId(tenantId, pageLinkWithCustomer).getData();
Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
Assert.assertTrue(
deviceInfosWithCustomer.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getCustomerId().equals(savedCustomer.getId())
&& d.getCustomerTitle().equals(savedCustomer.getTitle())
)
);
PageLink pageLinkWithType = new PageLink(100, 0, device.getType());
List<DeviceInfo> deviceInfosWithType = deviceService
.findDeviceInfosByTenantId(tenantId, pageLinkWithType).getData();
Assert.assertFalse(deviceInfosWithType.isEmpty());
Assert.assertTrue(
deviceInfosWithType.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getType().equals(device.getType())
)
);
}
@Test
public void testFindDeviceInfoByTenantIdAndType() {
Customer customer = new Customer();
customer.setTitle("Customer X");
customer.setTenantId(tenantId);
Customer savedCustomer = customerService.saveCustomer(customer);
Device device = new Device();
device.setTenantId(tenantId);
device.setName("default");
device.setType("default");
device.setLabel("label");
device.setCustomerId(savedCustomer.getId());
Device savedDevice = deviceService.saveDevice(device);
PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
List<DeviceInfo> deviceInfosWithLabel = deviceService
.findDeviceInfosByTenantIdAndType(tenantId, device.getType(), pageLinkWithLabel).getData();
Assert.assertFalse(deviceInfosWithLabel.isEmpty());
Assert.assertTrue(
deviceInfosWithLabel.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getDeviceProfileName().equals(savedDevice.getType())
&& d.getLabel().equals(savedDevice.getLabel())
)
);
PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
List<DeviceInfo> deviceInfosWithCustomer = deviceService
.findDeviceInfosByTenantIdAndType(tenantId, device.getType(), pageLinkWithCustomer).getData();
Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
Assert.assertTrue(
deviceInfosWithCustomer.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getDeviceProfileName().equals(savedDevice.getType())
&& d.getCustomerId().equals(savedCustomer.getId())
&& d.getCustomerTitle().equals(savedCustomer.getTitle())
)
);
}
@Test
public void testFindDeviceInfoByTenantIdAndDeviceProfileId() {
Customer customer = new Customer();
customer.setTitle("Customer X");
customer.setTenantId(tenantId);
Customer savedCustomer = customerService.saveCustomer(customer);
Device device = new Device();
device.setTenantId(tenantId);
device.setName("default");
device.setLabel("label");
device.setCustomerId(savedCustomer.getId());
Device savedDevice = deviceService.saveDevice(device);
PageLink pageLinkWithLabel = new PageLink(100, 0, "label");
List<DeviceInfo> deviceInfosWithLabel = deviceService
.findDeviceInfosByTenantIdAndDeviceProfileId(tenantId, savedDevice.getDeviceProfileId(), pageLinkWithLabel).getData();
Assert.assertFalse(deviceInfosWithLabel.isEmpty());
Assert.assertTrue(
deviceInfosWithLabel.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getDeviceProfileId().equals(savedDevice.getDeviceProfileId())
&& d.getLabel().equals(savedDevice.getLabel())
)
);
PageLink pageLinkWithCustomer = new PageLink(100, 0, savedCustomer.getSearchText());
List<DeviceInfo> deviceInfosWithCustomer = deviceService
.findDeviceInfosByTenantIdAndDeviceProfileId(tenantId, savedDevice.getDeviceProfileId(), pageLinkWithCustomer).getData();
Assert.assertFalse(deviceInfosWithCustomer.isEmpty());
Assert.assertTrue(
deviceInfosWithCustomer.stream()
.anyMatch(
d -> d.getId().equals(savedDevice.getId())
&& d.getTenantId().equals(tenantId)
&& d.getDeviceProfileId().equals(savedDevice.getDeviceProfileId())
&& d.getCustomerId().equals(savedCustomer.getId())
&& d.getCustomerTitle().equals(savedCustomer.getTitle())
)
);
}
} }