added checkAndTruncateDebugEvent
This commit is contained in:
parent
a50368a1ae
commit
872a6fb45d
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.dao.event;
|
package org.thingsboard.server.dao.event;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -28,6 +29,7 @@ import org.thingsboard.server.common.data.page.TimePageLink;
|
|||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
import org.thingsboard.server.dao.service.DataValidator;
|
import org.thingsboard.server.dao.service.DataValidator;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ -35,6 +37,8 @@ import java.util.Optional;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class BaseEventService implements EventService {
|
public class BaseEventService implements EventService {
|
||||||
|
|
||||||
|
private static final int MAX_DEBUG_EVENT_IN_BYTES = 10 * 1024;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public EventDao eventDao;
|
public EventDao eventDao;
|
||||||
|
|
||||||
@ -47,6 +51,7 @@ public class BaseEventService implements EventService {
|
|||||||
@Override
|
@Override
|
||||||
public ListenableFuture<Event> saveAsync(Event event) {
|
public ListenableFuture<Event> saveAsync(Event event) {
|
||||||
eventValidator.validate(event, Event::getTenantId);
|
eventValidator.validate(event, Event::getTenantId);
|
||||||
|
checkAndTruncateDebugEvent(event);
|
||||||
return eventDao.saveAsync(event);
|
return eventDao.saveAsync(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,9 +61,21 @@ public class BaseEventService implements EventService {
|
|||||||
if (StringUtils.isEmpty(event.getUid())) {
|
if (StringUtils.isEmpty(event.getUid())) {
|
||||||
throw new DataValidationException("Event uid should be specified!.");
|
throw new DataValidationException("Event uid should be specified!.");
|
||||||
}
|
}
|
||||||
|
checkAndTruncateDebugEvent(event);
|
||||||
return eventDao.saveIfNotExists(event);
|
return eventDao.saveIfNotExists(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkAndTruncateDebugEvent(Event event) {
|
||||||
|
if (event.getType().startsWith("DEBUG")) {
|
||||||
|
String dataStr = event.getBody().get("data").asText();
|
||||||
|
int dataSize = dataStr.getBytes(StandardCharsets.UTF_8).length;
|
||||||
|
if (dataSize > MAX_DEBUG_EVENT_IN_BYTES) {
|
||||||
|
((ObjectNode) event.getBody()).put("data", dataStr.substring(0, 1024));
|
||||||
|
log.trace("[{}] Event was truncated.", event.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Event> findEvent(TenantId tenantId, EntityId entityId, String eventType, String eventUid) {
|
public Optional<Event> findEvent(TenantId tenantId, EntityId entityId, String eventType, String eventUid) {
|
||||||
if (tenantId == null) {
|
if (tenantId == null) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user