Browse Source

IoT Hub installed items: one query for both lists, a failed load is not 'up to date', one ICU plural (PROD-9326 review)

pull/16657/head
dpinkevych 3 weeks ago
parent
commit
b5d1210cae
  1. 33
      dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java
  2. 26
      dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java
  3. 9
      dao/src/test/java/org/thingsboard/server/dao/service/IotHubInstalledItemServiceTest.java
  4. 11
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html
  5. 4
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.ts
  6. 2
      ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html
  7. 4
      ui-ngx/src/assets/locale/locale.constant-en_US.json

33
dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/IotHubInstalledItemRepository.java

@ -33,38 +33,29 @@ interface IotHubInstalledItemRepository extends JpaRepository<IotHubInstalledIte
List<UUID> findInstalledItemIdsByTenantIdAndItemIdIn(@Param("tenantId") UUID tenantId,
@Param("itemIds") Collection<UUID> 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<IotHubInstalledItemEntity> findByTenantId(@Param("tenantId") UUID tenantId, @Param("itemTypes") List<String> 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<IotHubInstalledItemEntity> findByTenantIdWithUpdates(@Param("tenantId") UUID tenantId, @Param("itemTypes") List<String> itemTypes,
@Param("textSearch") String textSearch,
@Param("publishedItemIds") Collection<UUID> publishedItemIds,
@Param("publishedVersionIds") Collection<UUID> publishedVersionIds,
Pageable pageable);
Page<IotHubInstalledItemEntity> findByTenantId(@Param("tenantId") UUID tenantId, @Param("itemTypes") List<String> itemTypes,
@Param("itemId") UUID itemId, @Param("textSearch") String textSearch,
@Param("updatesOnly") boolean updatesOnly,
@Param("publishedItemIds") Collection<UUID> publishedItemIds,
@Param("publishedVersionIds") Collection<UUID> publishedVersionIds,
Pageable pageable);
@Query("""
SELECT COUNT(item) FROM IotHubInstalledItemEntity item

26
dao/src/main/java/org/thingsboard/server/dao/sql/iot_hub/JpaIotHubInstalledItemDao.java

@ -48,24 +48,30 @@ class JpaIotHubInstalledItemDao extends JpaAbstractDao<IotHubInstalledItemEntity
@Override
public PageData<IotHubInstalledItem> findByTenantId(TenantId tenantId, List<String> 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<IotHubInstalledItem> findByTenantIdWithUpdates(TenantId tenantId, List<String> itemTypes,
Map<UUID, UUID> 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<UUID> 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<IotHubInstalledItem> find(TenantId tenantId, List<String> itemTypes, UUID itemId,
Map<UUID, UUID> 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))
);
}

9
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();

11
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-installed-items-table.component.html

@ -125,7 +125,16 @@
<tr mat-row *matRowDef="let _row; columns: displayedColumns;"></tr>
</table>
@if (!isLoading && dataSource.length === 0 && onlyUpdates && !hasActiveFilters()) {
@if (!isLoading && loadFailed && onlyUpdates) {
<!-- The list of items with an update didn't load: not the same as none having one -->
<div class="tb-iot-hub-empty-state flex flex-1 flex-col items-center justify-center text-center">
<div class="tb-no-data-bg"></div>
<h3>{{ 'iot-hub.installed-items-load-failed' | translate }}</h3>
<button mat-flat-button color="primary" (click)="showOnlyUpdates(true)">
{{ 'iot-hub.check-for-updates-try-again' | translate }}
</button>
</div>
} @else if (!isLoading && dataSource.length === 0 && onlyUpdates && !hasActiveFilters()) {
<!-- Only the items with an update, and none has one -->
<div class="tb-iot-hub-empty-state flex flex-1 flex-col items-center justify-center text-center">
<div class="tb-no-data-bg"></div>

4
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;
}
});

2
ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-installed-items.component.html

@ -39,7 +39,7 @@
<span class="tb-installed-updates-note">
@if (updatesCount > 0) {
<mat-icon class="tb-installed-updates-available">arrow_circle_up</mat-icon>
{{ (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 {
<mat-icon class="tb-installed-updates-none">check_circle</mat-icon>
{{ 'iot-hub.all-items-up-to-date' | translate }}

4
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",

Loading…
Cancel
Save