diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java index c86f7edadf..c09f8c12f5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java @@ -33,38 +33,29 @@ interface IotHubInstalledItemRepository extends JpaRepository findInstalledItemIdsByTenantIdAndItemIdIn(@Param("tenantId") UUID tenantId, @Param("itemIds") Collection itemIds); - @Query(""" - SELECT item FROM IotHubInstalledItemEntity item - WHERE item.tenantId = :tenantId - AND (:itemTypes IS NULL OR item.itemType IN :itemTypes) - AND (:itemId IS NULL OR item.itemId = :itemId) - AND (:textSearch IS NULL - OR ilike(item.itemName, CONCAT('%', :textSearch, '%')) = true - OR ilike(item.itemType, CONCAT('%', :textSearch, '%')) = true - OR ilike(item.version, CONCAT('%', :textSearch, '%')) = true) - """) - Page findByTenantId(@Param("tenantId") UUID tenantId, @Param("itemTypes") List itemTypes, @Param("itemId") UUID itemId, @Param("textSearch") String textSearch, Pageable pageable); - /** - * As {@link #findByTenantId}, only the items with an update: an item of {@code publishedItemIds} whose installed version - * is not among {@code publishedVersionIds}. Version ids are unique, so this is "not the published version of its item". + * The tenant's installed items, by type, item and text. With {@code updatesOnly}, only the items with an update: an item + * of {@code publishedItemIds} whose installed version is not among {@code publishedVersionIds} (version ids are unique, + * so: not the published version of its item). A flag, as Hibernate can't type {@code :uuids IS NULL}. */ @Query(""" SELECT item FROM IotHubInstalledItemEntity item WHERE item.tenantId = :tenantId - AND item.itemId IN :publishedItemIds - AND item.itemVersionId NOT IN :publishedVersionIds AND (:itemTypes IS NULL OR item.itemType IN :itemTypes) + AND (:itemId IS NULL OR item.itemId = :itemId) + AND (:updatesOnly = false + OR (item.itemId IN :publishedItemIds AND item.itemVersionId NOT IN :publishedVersionIds)) AND (:textSearch IS NULL OR ilike(item.itemName, CONCAT('%', :textSearch, '%')) = true OR ilike(item.itemType, CONCAT('%', :textSearch, '%')) = true OR ilike(item.version, CONCAT('%', :textSearch, '%')) = true) """) - Page findByTenantIdWithUpdates(@Param("tenantId") UUID tenantId, @Param("itemTypes") List itemTypes, - @Param("textSearch") String textSearch, - @Param("publishedItemIds") Collection publishedItemIds, - @Param("publishedVersionIds") Collection publishedVersionIds, - Pageable pageable); + Page findByTenantId(@Param("tenantId") UUID tenantId, @Param("itemTypes") List itemTypes, + @Param("itemId") UUID itemId, @Param("textSearch") String textSearch, + @Param("updatesOnly") boolean updatesOnly, + @Param("publishedItemIds") Collection publishedItemIds, + @Param("publishedVersionIds") Collection publishedVersionIds, + Pageable pageable); @Query(""" SELECT COUNT(item) FROM IotHubInstalledItemEntity item diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java index 1d05081d99..4c91e5137b 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java @@ -48,24 +48,30 @@ class JpaIotHubInstalledItemDao extends JpaAbstractDao findByTenantId(TenantId tenantId, List itemTypes, UUID itemId, PageLink pageLink) { - return DaoUtil.toPageData(repository.findByTenantId( - tenantId.getId(), - itemTypes == null || itemTypes.isEmpty() ? null : itemTypes, - itemId, - StringUtils.defaultIfEmpty(pageLink.getTextSearch(), null), - toPageRequest(pageLink)) - ); + return find(tenantId, itemTypes, itemId, null, pageLink); } @Override public PageData findByTenantIdWithUpdates(TenantId tenantId, List itemTypes, Map publishedVersionIds, PageLink pageLink) { - return DaoUtil.toPageData(repository.findByTenantIdWithUpdates( + return find(tenantId, itemTypes, null, publishedVersionIds, pageLink); + } + + // Never matches: the query binds the published ids even when it doesn't filter by them + private static final Set NO_IDS = Set.of(new UUID(0, 0)); + + // One query for both lists, so the only-updates list filters and searches as the plain one does + private PageData find(TenantId tenantId, List itemTypes, UUID itemId, + Map publishedVersionIds, PageLink pageLink) { + boolean updatesOnly = publishedVersionIds != null; + return DaoUtil.toPageData(repository.findByTenantId( tenantId.getId(), itemTypes == null || itemTypes.isEmpty() ? null : itemTypes, + itemId, StringUtils.defaultIfEmpty(pageLink.getTextSearch(), null), - publishedVersionIds.keySet(), - publishedVersionIds.values(), + updatesOnly, + updatesOnly ? publishedVersionIds.keySet() : NO_IDS, + updatesOnly ? publishedVersionIds.values() : NO_IDS, toPageRequest(pageLink)) ); } diff --git a/dao/src/test/java/org/thingsboard/server/dao/service/IotHubInstalledItemServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/service/IotHubInstalledItemServiceTest.java index 560b3cf0d0..8d7698d2ce 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/service/IotHubInstalledItemServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/service/IotHubInstalledItemServiceTest.java @@ -99,6 +99,15 @@ public class IotHubInstalledItemServiceTest extends AbstractServiceTest { new PageLink(10)).getData()).isEmpty(); } + @Test + public void plainList_sameQuery_isNotFiltered() { + installFive(); + PageLink byName = new PageLink(10, 0, null, new SortOrder("itemName", SortOrder.Direction.ASC)); + + assertThat(names(installedItemService.findByTenantId(tenantId, null, null, byName))).containsExactly("a", "b", "c", "d", "e"); + assertThat(names(installedItemService.findByTenantId(tenantId, null, sensorItemId, byName))).containsExactly("a", "c", "d"); + } + @Test public void withUpdates_withoutPublishedVersions_isEmpty() { installFive(); diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html index 3af83f064f..c871f0492e 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html +++ b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html @@ -125,7 +125,16 @@ - @if (!isLoading && dataSource.length === 0 && onlyUpdates && !hasActiveFilters()) { + @if (!isLoading && loadFailed && onlyUpdates) { + +
+
+

{{ 'iot-hub.installed-items-load-failed' | translate }}

+ +
+ } @else if (!isLoading && dataSource.length === 0 && onlyUpdates && !hasActiveFilters()) {
diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.ts b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.ts index f1bf82d05e..073bbe8171 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.ts @@ -88,6 +88,8 @@ export class TbIotHubInstalledItemsTableComponent implements OnInit, OnChanges, onlyUpdates = false; // How many installed items have an update, and how many there are, of all types and with no search; null while not known updatesCount: number | null = null; + // The last load of the list failed: the list is empty, but not because nothing has an update + loadFailed = false; installedCount = 0; // A later load (another page, mode, sort or search) wins over an earlier one that answers last private loadRun = 0; @@ -420,6 +422,7 @@ export class TbIotHubInstalledItemsTableComponent implements OnInit, OnChanges, } this.dataSource = data.data; this.totalElements = data.totalElements; + this.loadFailed = false; this.isLoading = false; // In the list of items with an update their version update shows, not the converter's if (this.updatesChecked && !this.itemVersion && !this.onlyUpdates) { @@ -432,6 +435,7 @@ export class TbIotHubInstalledItemsTableComponent implements OnInit, OnChanges, } this.dataSource = []; this.totalElements = 0; + this.loadFailed = true; this.isLoading = false; } }); diff --git a/ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html b/ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html index f95bef69d7..456fcdbd1f 100644 --- a/ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html +++ b/ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html @@ -39,7 +39,7 @@ @if (updatesCount > 0) { arrow_circle_up - {{ (updatesCount === 1 ? 'iot-hub.item-with-update' : 'iot-hub.items-with-update') | translate:{ count: updatesCount, total: installedCount } }} + {{ 'iot-hub.items-with-update' | translate:{ count: updatesCount, total: installedCount } }} } @else { check_circle {{ 'iot-hub.all-items-up-to-date' | translate }} diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index c2c51b4632..44d86bf476 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -6496,8 +6496,8 @@ "check-for-updates": "Check for updates", "check-for-updates-failed": "Couldn't check for updates", "check-for-updates-try-again": "Try again", - "item-with-update": "1 of {{total}} items has an update", - "items-with-update": "{{count}} of {{total}} items have an update", + "items-with-update": "{ count, plural, =1 {1 of {total} items has an update} other {# of {total} items have an update} }", + "installed-items-load-failed": "Couldn't load the items with an update", "all-items-up-to-date": "All items are up to date", "all-items-up-to-date-text": "No installed item has a newer version in IoT Hub.", "show-all-items": "Show all items",