Browse Source

Minor code cleanup

pull/11873/head
ViacheslavKlimov 2 years ago
parent
commit
8b539a2255
  1. 4
      application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java
  2. 80
      application/src/main/java/org/thingsboard/server/service/install/update/ResourcesUpdater.java
  3. 6
      application/src/test/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncServiceTest.java
  4. 6
      dao/src/main/java/org/thingsboard/server/dao/resource/BaseImageService.java
  5. 8
      dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java

4
application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java

@ -512,10 +512,6 @@ public class InstallScripts {
public void updateResourcesUsage() {
resourcesUpdater.updateDashboardsResources();
resourcesUpdater.updateWidgetsResources();
/* TODO TMP remove */
// loadSystemResources(Path.of("/home/viacheslav/Desktop/thingsboard-ce/application/src/main/data/resources/js_modules"), ResourceType.JS_MODULE);
// resourcesUpdater.updateFiles();
}
private void loadSystemResources(Path dir, ResourceType resourceType) {

80
application/src/main/java/org/thingsboard/server/service/install/update/ResourcesUpdater.java

@ -15,22 +15,15 @@
*/
package org.thingsboard.server.service.install.update;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Iterators;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.HasImage;
import org.thingsboard.server.common.data.TbResourceInfo;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TbResourceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.WidgetTypeId;
import org.thingsboard.server.common.data.page.PageData;
@ -49,12 +42,6 @@ import org.thingsboard.server.dao.widget.WidgetTypeDao;
import org.thingsboard.server.dao.widget.WidgetTypeService;
import org.thingsboard.server.dao.widget.WidgetsBundleDao;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -141,73 +128,6 @@ public class ResourcesUpdater {
log.info("Updated {} widgets", updatedCount);
}
// TODO: remove after updating PE
@SneakyThrows
public void updateFiles() {
Path widgetsDirectory = Path.of("/home/viacheslav/Desktop/thingsboard-ce/application/src/main/data/json/system/widget_types");
Path dashboardsDirectory = Path.of("/home/viacheslav/Desktop/thingsboard-ce/application/src/main/data/json/demo/dashboards");
Files.list(widgetsDirectory).forEach(path -> {
WidgetTypeDetails widgetTypeDetails = JacksonUtil.readValue(path.toFile(), WidgetTypeDetails.class);
widgetTypeDetails.setTenantId(TenantId.SYS_TENANT_ID);
imageService.updateImagesUsage(widgetTypeDetails);
resourceService.updateResourcesUsage(widgetTypeDetails);
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>();
for (TbResourceInfo imageInfo : imageService.getUsedImages(widgetTypeDetails)) {
resources.putIfAbsent(imageInfo.getId(), imageInfo);
}
for (TbResourceInfo resourceInfo : resourceService.getUsedResources(widgetTypeDetails)) {
resources.putIfAbsent(resourceInfo.getId(), resourceInfo);
}
widgetTypeDetails.setResources(resourceService.exportResources(TenantId.SYS_TENANT_ID, resources.values()));
ObjectNode json = (ObjectNode) JacksonUtil.valueToTree(widgetTypeDetails);
Iterators.removeIf(json.fields(), field -> field.getValue() == null
|| field.getValue().isNull());
json.remove(List.of("id", "createdTime", "tenantId", "externalId", "version", "scada"));
JsonNode resourcesNode = json.remove("resources");
json.set("resources", resourcesNode);
try {
Files.writeString(path, json.toPrettyString());
// log.info("UPDATED {}: {} resources used", path, resources.size());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Files.list(dashboardsDirectory).forEach(path -> {
Dashboard dashboard = JacksonUtil.readValue(path.toFile(), Dashboard.class);
dashboard.setTenantId(TenantId.SYS_TENANT_ID);
imageService.updateImagesUsage(dashboard);
resourceService.updateResourcesUsage(dashboard);
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>();
for (TbResourceInfo imageInfo : imageService.getUsedImages(dashboard)) {
resources.putIfAbsent(imageInfo.getId(), imageInfo);
}
for (TbResourceInfo resourceInfo : resourceService.getUsedResources(dashboard)) {
resources.putIfAbsent(resourceInfo.getId(), resourceInfo);
}
dashboard.setResources(resourceService.exportResources(TenantId.SYS_TENANT_ID, resources.values()));
ObjectNode json = (ObjectNode) JacksonUtil.valueToTree(dashboard);
json.remove(List.of("id", "createdTime", "tenantId", "externalId", "version", "scada"));
JsonNode resourcesNode = json.remove("resources");
json.set("resources", resourcesNode);
try {
Files.writeString(path, json.toPrettyString());
// log.info("UPDATED {}: {} resources used", path, resources.size());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public void updateDeviceProfilesImages() {
log.info("Updating device profiles images...");
var deviceProfiles = new PageDataIterable<>(deviceProfileDao::findAllWithImages, 256);

6
application/src/test/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncServiceTest.java

@ -16,7 +16,6 @@
package org.thingsboard.server.service.entitiy.dashboard;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletResponse;
@ -55,7 +54,6 @@ public class DashboardSyncServiceTest extends AbstractControllerTest {
resourceRepository.deleteAll();
}
@Ignore
@Test
public void testGatewaysDashboardSync() throws Exception {
loginTenantAdmin();
@ -64,13 +62,9 @@ public class DashboardSyncServiceTest extends AbstractControllerTest {
.andExpect(status().isOk())
.andReturn().getResponse();
String dashboardJson = response.getContentAsString();
assertThat(dashboardJson).contains("tb-resource;/api/resource/js_module/system/gateway-management-extension.js"); // checking that resource link is used
assertThat(dashboardJson).doesNotContain("${RESOURCE");
Dashboard dashboard = JacksonUtil.fromString(dashboardJson, Dashboard.class);
assertThat(dashboard).isNotNull();
assertThat(dashboard.getTitle()).containsIgnoringCase("gateway");
assertThat(dashboard.getImage()).startsWith("tb-image;/api/images/system/gateway");
assertThat(response.getHeader("ETag")).isNotBlank();
});
}

6
dao/src/main/java/org/thingsboard/server/dao/resource/BaseImageService.java

@ -256,7 +256,7 @@ public class BaseImageService extends BaseResourceService implements ImageServic
String etag = calculateImageEtag(data);
TbResourceInfo existingImage = findSystemOrTenantImageByEtag(tenantId, etag);
if (existingImage != null) {
log.info("[{}] Using existing image {}", tenantId, existingImage.getLink());
log.debug("[{}] Using existing image {}", tenantId, existingImage.getLink());
return new TbResource(existingImage);
}
}
@ -282,7 +282,7 @@ public class BaseImageService extends BaseResourceService implements ImageServic
descriptor.setMediaType(imageData.getMediaType());
image.setDescriptorValue(descriptor);
image.setData(data);
log.info("[{}] Creating image {}", tenantId, image.getFileName());
log.debug("[{}] Creating image {}", tenantId, image.getFileName());
return image;
}
@ -626,7 +626,6 @@ public class BaseImageService extends BaseResourceService implements ImageServic
ImageCacheKey key = getKeyFromUrl(tenantId, imageUrl);
if (key != null) {
var imageInfo = getImageInfoByTenantIdAndKey(key.getTenantId(), key.getResourceKey());
// TODO: maybe export scada too?
if (imageInfo == null || (TenantId.SYS_TENANT_ID.equals(imageInfo.getTenantId()) && ResourceSubType.SCADA_SYMBOL.equals(imageInfo.getResourceSubType()))) {
return imageUrl;
} else {
@ -685,7 +684,6 @@ public class BaseImageService extends BaseResourceService implements ImageServic
private static class UpdateResult {
private final boolean updated;
private final String value;
}
}

8
dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java

@ -265,9 +265,9 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
if (update && !etag.equals(resource.getEtag())) {
resource.setData(data);
resource.setTitle(exportData.getTitle());
log.info("[{}] Updating existing resource {}", tenantId, existingResource.getLink());
log.debug("[{}] Updating existing resource {}", tenantId, existingResource.getLink());
} else {
log.info("[{}] Using existing resource {}", tenantId, existingResource.getLink());
log.debug("[{}] Using existing resource {}", tenantId, existingResource.getLink());
}
return resource;
}
@ -284,7 +284,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
resource.setResourceType(exportData.getType());
resource.setResourceKey(exportData.getResourceKey());
resource.setData(data);
log.info("[{}] Creating resource {}", tenantId, resource.getResourceKey());
log.debug("[{}] Creating resource {}", tenantId, resource.getResourceKey());
return resource;
}
@ -538,7 +538,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
JsonNode newValue = new TextNode(value);
if (!newValue.toString().equals(urlNode.toString())) {
updated.set(true);
log.info("Replaced '{}' with '{}'", urlNode, newValue);
log.trace("Replaced '{}' with '{}'", urlNode, newValue);
}
return newValue;
});

Loading…
Cancel
Save