EDQS: optimize serialization, fix api usage fields

This commit is contained in:
ViacheslavKlimov 2025-02-28 12:45:01 +02:00
parent 0115ac231b
commit 506ec363eb
6 changed files with 25 additions and 7 deletions

View File

@ -43,8 +43,9 @@ public class ApiUsageStateFields extends AbstractEntityFields {
public ApiUsageStateFields(UUID id, long createdTime, UUID tenantId, UUID entityId, String entityType, ApiUsageStateValue transportState, ApiUsageStateValue dbStorageState, public ApiUsageStateFields(UUID id, long createdTime, UUID tenantId, UUID entityId, String entityType, ApiUsageStateValue transportState, ApiUsageStateValue dbStorageState,
ApiUsageStateValue reExecState, ApiUsageStateValue jsExecState, ApiUsageStateValue tbelExecState, ApiUsageStateValue reExecState, ApiUsageStateValue jsExecState, ApiUsageStateValue tbelExecState,
ApiUsageStateValue emailExecState, ApiUsageStateValue smsExecState, ApiUsageStateValue alarmExecState) { ApiUsageStateValue emailExecState, ApiUsageStateValue smsExecState, ApiUsageStateValue alarmExecState,
super(id, createdTime, tenantId); Long version) {
super(id, createdTime, tenantId, null, null, version);
this.entityId = (entityType != null && entityId != null) ? EntityIdFactory.getByTypeAndUuid(entityType, entityId) : null; this.entityId = (entityType != null && entityId != null) ? EntityIdFactory.getByTypeAndUuid(entityType, entityId) : null;
this.transportState = transportState; this.transportState = transportState;
this.dbStorageState = dbStorageState; this.dbStorageState = dbStorageState;

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.common.data.edqs.fields; package org.thingsboard.server.common.data.edqs.fields;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
@ -23,6 +24,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@JsonInclude(JsonInclude.Include.NON_NULL)
public interface EntityFields { public interface EntityFields {
Logger log = LoggerFactory.getLogger(EntityFields.class); Logger log = LoggerFactory.getLogger(EntityFields.class);

View File

@ -284,6 +284,7 @@ public class FieldsUtil {
.emailExecState(entity.getEmailExecState()) .emailExecState(entity.getEmailExecState())
.smsExecState(entity.getSmsExecState()) .smsExecState(entity.getSmsExecState())
.alarmExecState(entity.getAlarmExecState()) .alarmExecState(entity.getAlarmExecState())
.version(entity.getVersion())
.build(); .build();
} }

View File

@ -219,7 +219,9 @@ public class TenantRepo {
EntityType entityType = entity.getType(); EntityType entityType = entity.getType();
EntityData<?> removed = getEntityMap(entityType).remove(entityId); EntityData<?> removed = getEntityMap(entityType).remove(entityId);
if (removed != null) { if (removed != null) {
getEntitySet(entityType).remove(removed); if (removed.getFields() != null) {
getEntitySet(entityType).remove(removed);
}
edqsStatsService.ifPresent(statService -> statService.reportEvent(tenantId, ObjectType.fromEntityType(entityType), EdqsEventType.DELETED)); edqsStatsService.ifPresent(statService -> statService.reportEvent(tenantId, ObjectType.fromEntityType(entityType), EdqsEventType.DELETED));
UUID customerId = removed.getCustomerId(); UUID customerId = removed.getCustomerId();
if (customerId != null) { if (customerId != null) {

View File

@ -15,12 +15,15 @@
*/ */
package org.thingsboard.server.edqs.util; package org.thingsboard.server.edqs.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.AttributeScope; import org.thingsboard.server.common.data.AttributeScope;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.ObjectType; import org.thingsboard.server.common.data.ObjectType;
@ -217,16 +220,24 @@ public class EdqsConverter {
@RequiredArgsConstructor @RequiredArgsConstructor
private static class JsonConverter<T> implements Converter<T> { private static class JsonConverter<T> implements Converter<T> {
private static final ObjectMapper mapper = JsonMapper.builder()
.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
.visibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
.build();
private final Class<T> type; private final Class<T> type;
@SneakyThrows
@Override @Override
public byte[] serialize(ObjectType objectType, T value) { public byte[] serialize(ObjectType objectType, T value) {
return JacksonUtil.writeValueAsBytes(value); return mapper.writeValueAsBytes(value);
} }
@SneakyThrows
@Override @Override
public T deserialize(ObjectType objectType, byte[] bytes) { public T deserialize(ObjectType objectType, byte[] bytes) {
return JacksonUtil.fromBytes(bytes, this.type); return mapper.readValue(bytes, this.type);
} }
} }

View File

@ -54,6 +54,7 @@ public interface ApiUsageStateRepository extends JpaRepository<ApiUsageStateEnti
@Query("SELECT new org.thingsboard.server.common.data.edqs.fields.ApiUsageStateFields(a.id, a.createdTime, a.tenantId," + @Query("SELECT new org.thingsboard.server.common.data.edqs.fields.ApiUsageStateFields(a.id, a.createdTime, a.tenantId," +
"a.entityId, a.entityType, a.transportState, a.dbStorageState, a.reExecState, a.jsExecState, a.tbelExecState, " + "a.entityId, a.entityType, a.transportState, a.dbStorageState, a.reExecState, a.jsExecState, a.tbelExecState, " +
"a.emailExecState, a.smsExecState, a.alarmExecState) FROM ApiUsageStateEntity a WHERE a.id > :id ORDER BY a.id") "a.emailExecState, a.smsExecState, a.alarmExecState, a.version) FROM ApiUsageStateEntity a WHERE a.id > :id ORDER BY a.id")
List<ApiUsageStateFields> findNextBatch(@Param("id") UUID id, Limit limit); List<ApiUsageStateFields> findNextBatch(@Param("id") UUID id, Limit limit);
} }