@ -18,15 +18,11 @@ package org.thingsboard.server.service.install.update;
import lombok.RequiredArgsConstructor ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Component ;
import org.thingsboard.server.common.data.Dashboard ;
import org.thingsboard.server.common.data.DeviceProfile ;
import org.thingsboard.server.common.data.asset.AssetProfile ;
import org.thingsboard.server.common.data.id.DashboardId ;
import org.thingsboard.server.common.data.HasImage ;
import org.thingsboard.server.common.data.id.EntityId ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.common.data.id.WidgetTypeId ;
import org.thingsboard.server.common.data.page.PageDataIterable ;
import org.thingsboard.server.common.data.widget.WidgetTypeDetails ;
import org.thingsboard.server.common.data.widget.WidgetsBundle ;
import org.thingsboard.server.dao.Dao ;
import org.thingsboard.server.dao.asset.AssetProfileDao ;
import org.thingsboard.server.dao.dashboard.DashboardDao ;
import org.thingsboard.server.dao.device.DeviceProfileDao ;
@ -34,6 +30,9 @@ import org.thingsboard.server.dao.resource.ImageService;
import org.thingsboard.server.dao.widget.WidgetTypeDao ;
import org.thingsboard.server.dao.widget.WidgetsBundleDao ;
import java.util.function.BiFunction ;
import java.util.function.Function ;
@Component
@RequiredArgsConstructor
@Slf4j
@ -48,108 +47,78 @@ public class ImagesUpdater {
public void updateWidgetsBundlesImages ( ) {
log . info ( "Updating widgets bundles images..." ) ;
var widgetsBundles = new PageDataIterable < > ( widgetsBundleDao : : findAllWidgetsBundles , 128 ) ;
int updatedCount = 0 ;
int totalCount = 0 ;
for ( WidgetsBundle widgetsBundle : widgetsBundles ) {
totalCount + + ;
try {
boolean updated = imageService . replaceBase64WithImageUrl ( widgetsBundle , "bundle" ) ;
if ( updated ) {
widgetsBundleDao . save ( widgetsBundle . getTenantId ( ) , widgetsBundle ) ;
log . debug ( "[{}][{}][{}] Updated widgets bundle images" , widgetsBundle . getTenantId ( ) , widgetsBundle . getId ( ) , widgetsBundle . getTitle ( ) ) ;
updatedCount + + ;
}
} catch ( Exception e ) {
log . error ( "[{}][{}][{}] Failed to update widgets bundle images" , widgetsBundle . getTenantId ( ) , widgetsBundle . getId ( ) , widgetsBundle . getTitle ( ) , e ) ;
}
}
log . info ( "Updated {} widgets bundles out of {}" , updatedCount , totalCount ) ;
updateImages ( widgetsBundles , "bundle" , imageService : : replaceBase64WithImageUrl , widgetsBundleDao ) ;
}
public void updateWidgetTypesImages ( ) {
log . info ( "Updating widget types images..." ) ;
var widgetTypes = new PageDataIterable < > ( widgetTypeDao : : findAllWidgetTypesIds , 1024 ) ;
int updatedCount = 0 ;
int totalCount = 0 ;
for ( WidgetTypeId widgetTypeId : widgetTypes ) {
totalCount + + ;
WidgetTypeDetails widgetTypeDetails = widgetTypeDao . findById ( TenantId . SYS_TENANT_ID , widgetTypeId . getId ( ) ) ;
try {
boolean updated = imageService . replaceBase64WithImageUrl ( widgetTypeDetails ) ;
if ( updated ) {
widgetTypeDao . save ( widgetTypeDetails . getTenantId ( ) , widgetTypeDetails ) ;
log . debug ( "[{}][{}][{}] Updated widget type images" , widgetTypeDetails . getTenantId ( ) , widgetTypeDetails . getId ( ) , widgetTypeDetails . getName ( ) ) ;
updatedCount + + ;
}
} catch ( Exception e ) {
log . error ( "[{}][{}][{}] Failed to update widget type images" , widgetTypeDetails . getTenantId ( ) , widgetTypeDetails . getId ( ) , widgetTypeDetails . getName ( ) , e ) ;
}
}
log . info ( "Updated {} widget types out of {}" , updatedCount , totalCount ) ;
var widgetTypesIds = new PageDataIterable < > ( widgetTypeDao : : findAllWidgetTypesIds , 1024 ) ;
updateImages ( widgetTypesIds , "widget type" , imageService : : replaceBase64WithImageUrl , widgetTypeDao ) ;
}
public void updateDashboardsImages ( ) {
log . info ( "Updating dashboards images..." ) ;
var dashboards = new PageDataIterable < > ( dashboardDao : : findAllIds , 1024 ) ;
int updatedCount = 0 ;
int totalCount = 0 ;
for ( DashboardId dashboardId : dashboards ) {
totalCount + + ;
Dashboard dashboard = dashboardDao . findById ( TenantId . SYS_TENANT_ID , dashboardId . getId ( ) ) ;
try {
boolean updated = imageService . replaceBase64WithImageUrl ( dashboard ) ;
if ( updated ) {
dashboardDao . save ( dashboard . getTenantId ( ) , dashboard ) ;
log . info ( "[{}][{}][{}] Updated dashboard images" , dashboard . getTenantId ( ) , dashboardId , dashboard . getTitle ( ) ) ;
updatedCount + + ;
}
} catch ( Exception e ) {
log . error ( "[{}][{}][{}] Failed to update dashboard images" , dashboard . getTenantId ( ) , dashboardId , dashboard . getTitle ( ) , e ) ;
}
}
log . info ( "Updated {} dashboards out of {}" , updatedCount , totalCount ) ;
var dashboardsIds = new PageDataIterable < > ( dashboardDao : : findAllIds , 1024 ) ;
updateImages ( dashboardsIds , "dashboard" , imageService : : replaceBase64WithImageUrl , dashboardDao ) ;
}
public void updateDeviceProfilesImages ( ) {
log . info ( "Updating device profiles images..." ) ;
var deviceProfiles = new PageDataIterable < > ( deviceProfileDao : : findAll , 256 ) ;
updateImages ( deviceProfiles , "device profile" , imageService : : replaceBase64WithImageUrl , deviceProfileDao ) ;
}
public void updateAssetProfilesImages ( ) {
log . info ( "Updating asset profiles images..." ) ;
var assetProfiles = new PageDataIterable < > ( assetProfileDao : : findAll , 256 ) ;
updateImages ( assetProfiles , "asset profile" , imageService : : replaceBase64WithImageUrl , assetProfileDao ) ;
}
private < E extends HasImage > void updateImages ( Iterable < E > entities , String type ,
BiFunction < E , String , Boolean > updater , Dao < E > dao ) {
int updatedCount = 0 ;
int totalCount = 0 ;
for ( DeviceProfile deviceProfile : deviceProfiles ) {
for ( E entity : entiti es) {
totalCount + + ;
try {
boolean updated = imageService . replaceBase64WithImageUrl ( deviceProfile , "device profile" ) ;
boolean updated = updater . apply ( entity , type ) ;
if ( updated ) {
deviceProfileDao . save ( deviceProfile . getTenantId ( ) , deviceProfile ) ;
log . debug ( "[{}][{}][{}] Updated device profile images" , deviceProfile . getTenantId ( ) , deviceProfile . getId ( ) , deviceProfile . getName ( ) ) ;
dao . save ( entity . getTenantId ( ) , entity ) ;
log . debug ( "[{}][{}] Updated {} images" , entity . getTenantId ( ) , entity . getName ( ) , type ) ;
updatedCount + + ;
}
} catch ( Exception e ) {
log . error ( "[{}][{}][{}] Failed to update device profile images" , deviceProfile . getTenantId ( ) , deviceProfile . getId ( ) , deviceProfile . getName ( ) , e ) ;
log . error ( "[{}][{}] Failed to update {} images" , entity . getTenantId ( ) , entity . getName ( ) , type , e ) ;
}
if ( totalCount % 100 = = 0 ) {
log . info ( "Processed {} {}s so far" , totalCount , type ) ;
}
}
log . info ( "Updated {} device profiles out of {}" , updatedCount , totalCount ) ;
log . info ( "Updated {} {} s out of {}" , updatedCount , type , totalCount ) ;
}
public void updateAssetProfilesImages ( ) {
log . info ( "Updating asset profiles images..." ) ;
var assetProfiles = new PageDataIterable < > ( assetProfileDao : : findAll , 256 ) ;
private < E extends HasImage > void updateImages ( Iterable < ? extends EntityId > entitiesIds , String type ,
Function < E , Boolean > updater , Dao < E > dao ) {
int updatedCount = 0 ;
int totalCount = 0 ;
for ( AssetProfile assetProfile : assetProfile s) {
for ( EntityId id : entitiesId s) {
totalCount + + ;
E entity = dao . findById ( TenantId . SYS_TENANT_ID , id . getId ( ) ) ;
try {
boolean updated = imageService . replaceBase64WithImageUrl ( assetProfile , "asset profile" ) ;
boolean updated = updater . apply ( entity ) ;
if ( updated ) {
assetProfileD ao. save ( assetProfile . getTenantId ( ) , assetProfile ) ;
log . debug ( "[{}][{}][{}] Updated asset profile images" , assetProfile . getTenantId ( ) , assetProfile . getId ( ) , assetProfile . getName ( ) ) ;
d ao. save ( entity . getTenantId ( ) , entity ) ;
log . debug ( "[{}][{}] Updated {} images" , entity . getTenantId ( ) , entity . getName ( ) , type ) ;
updatedCount + + ;
}
} catch ( Exception e ) {
log . error ( "[{}][{}][{}] Failed to update asset profile images" , assetProfile . getTenantId ( ) , assetProfile . getId ( ) , assetProfile . getName ( ) , e ) ;
log . error ( "[{}][{}] Failed to update {} images" , entity . getTenantId ( ) , entity . getName ( ) , type , e ) ;
}
if ( totalCount % 100 = = 0 ) {
log . info ( "Processed {} {}s so far" , totalCount , type ) ;
}
}
log . info ( "Updated {} asset profiles out of {}" , updatedCount , totalCount ) ;
log . info ( "Updated {} {} s out of {}" , updatedCount , type , totalCount ) ;
}
}