Fix ResourceService.getUsedResources; improve dashboard fields ordering

This commit is contained in:
ViacheslavKlimov 2024-11-19 11:56:29 +02:00
parent 3124ad360d
commit e79e5c8f8e
4 changed files with 17 additions and 14 deletions

View File

@ -606,7 +606,8 @@ public class DashboardControllerTest extends AbstractControllerTest {
{"xxx":
{"config":{"actions":{"elementClick":[
{"customResources":[{"url":{"entityType":"TB_RESOURCE","id":
"tb-resource;/api/resource/js_module/tenant/gateway-management-extension.js"},"isModule":true}]}]}}}}
"tb-resource;/api/resource/js_module/tenant/gateway-management-extension.js"},"isModule":true},
{"url":"tb-resource;/api/resource/js_module/tenant/gateway-management-extension.js","isModule":true}]}]}}}}
""")));
dashboard = doPost("/api/dashboard", dashboard, Dashboard.class);

View File

@ -84,9 +84,9 @@ public interface ResourceService extends EntityDaoService {
boolean updateResourcesUsage(WidgetTypeDetails widgetTypeDetails);
List<TbResourceInfo> getUsedResources(Dashboard dashboard);
Collection<TbResourceInfo> getUsedResources(Dashboard dashboard);
List<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails);
Collection<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails);
TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, byte[] data);

View File

@ -16,6 +16,7 @@
package org.thingsboard.server.common.data;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams;
@ -32,6 +33,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
@EqualsAndHashCode(callSuper = true)
@JsonPropertyOrder({"title", "image", "mobileHide", "mobileOrder", "configuration", "name", "resources"})
public class Dashboard extends DashboardInfo implements ExportableEntity<DashboardId> {
private static final long serialVersionUID = 872682138346187503L;
@ -68,7 +70,7 @@ public class Dashboard extends DashboardInfo implements ExportableEntity<Dashboa
@Schema(description = "JSON object with main configuration of the dashboard: layouts, widgets, aliases, etc. " +
"The JSON structure of the dashboard configuration is quite complex. " +
"The easiest way to learn it is to export existing dashboard to JSON."
,implementation = com.fasterxml.jackson.databind.JsonNode.class)
, implementation = com.fasterxml.jackson.databind.JsonNode.class)
public JsonNode getConfiguration() {
return configuration;
}
@ -111,4 +113,5 @@ public class Dashboard extends DashboardInfo implements ExportableEntity<Dashboa
builder.append("]");
return builder.toString();
}
}

View File

@ -57,7 +57,6 @@ import org.thingsboard.server.dao.service.Validator;
import org.thingsboard.server.dao.service.validator.ResourceDataValidator;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
@ -461,22 +460,22 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
}
@Override
public List<TbResourceInfo> getUsedResources(Dashboard dashboard) {
return getUsedResources(dashboard.getTenantId(), dashboard.getConfiguration(), DASHBOARD_RESOURCES_MAPPING);
public Collection<TbResourceInfo> getUsedResources(Dashboard dashboard) {
return getUsedResources(dashboard.getTenantId(), dashboard.getConfiguration(), DASHBOARD_RESOURCES_MAPPING).values();
}
@Override
public List<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails) {
List<TbResourceInfo> resources = getUsedResources(widgetTypeDetails.getTenantId(), widgetTypeDetails.getDescriptor(), WIDGET_RESOURCES_MAPPING);
public Collection<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails) {
Map<TbResourceId, TbResourceInfo> resources = getUsedResources(widgetTypeDetails.getTenantId(), widgetTypeDetails.getDescriptor(), WIDGET_RESOURCES_MAPPING);
JsonNode defaultConfig = widgetTypeDetails.getDefaultConfig();
if (defaultConfig != null) {
resources.addAll(getUsedResources(widgetTypeDetails.getTenantId(), defaultConfig, WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING));
resources.putAll(getUsedResources(widgetTypeDetails.getTenantId(), defaultConfig, WIDGET_DEFAULT_CONFIG_RESOURCES_MAPPING));
}
return resources;
return resources.values();
}
private List<TbResourceInfo> getUsedResources(TenantId tenantId, JsonNode jsonNode, Map<String, String> mapping) {
List<TbResourceInfo> resources = new ArrayList<>();
private Map<TbResourceId, TbResourceInfo> getUsedResources(TenantId tenantId, JsonNode jsonNode, Map<String, String> mapping) {
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>();
processResources(jsonNode, mapping, value -> {
String link = getResourceLink(value);
if (link == null) {
@ -499,7 +498,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
TbResourceInfo resourceInfo = findResourceInfoByTenantIdAndKey(resourceTenantId, resourceType, resourceKey);
if (resourceInfo != null) {
resources.add(resourceInfo);
resources.putIfAbsent(resourceInfo.getId(), resourceInfo);
} else {
log.warn("[{}] Unknown resource referenced with '{}'", tenantId, value);
}