Commit 872a6fb45dff91a2b6c24977b83edf11a035e907

Authored by YevhenBondarenko
Committed by Andrew Shvayka
1 parent a50368a1

added checkAndTruncateDebugEvent

@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 */ 15 */
16 package org.thingsboard.server.dao.event; 16 package org.thingsboard.server.dao.event;
17 17
  18 +import com.fasterxml.jackson.databind.node.ObjectNode;
18 import com.google.common.util.concurrent.ListenableFuture; 19 import com.google.common.util.concurrent.ListenableFuture;
19 import lombok.extern.slf4j.Slf4j; 20 import lombok.extern.slf4j.Slf4j;
20 import org.apache.commons.lang3.StringUtils; 21 import org.apache.commons.lang3.StringUtils;
@@ -28,6 +29,7 @@ import org.thingsboard.server.common.data.page.TimePageLink; @@ -28,6 +29,7 @@ import org.thingsboard.server.common.data.page.TimePageLink;
28 import org.thingsboard.server.dao.exception.DataValidationException; 29 import org.thingsboard.server.dao.exception.DataValidationException;
29 import org.thingsboard.server.dao.service.DataValidator; 30 import org.thingsboard.server.dao.service.DataValidator;
30 31
  32 +import java.nio.charset.StandardCharsets;
31 import java.util.List; 33 import java.util.List;
32 import java.util.Optional; 34 import java.util.Optional;
33 35
@@ -35,6 +37,8 @@ import java.util.Optional; @@ -35,6 +37,8 @@ import java.util.Optional;
35 @Slf4j 37 @Slf4j
36 public class BaseEventService implements EventService { 38 public class BaseEventService implements EventService {
37 39
  40 + private static final int MAX_DEBUG_EVENT_IN_BYTES = 10 * 1024;
  41 +
38 @Autowired 42 @Autowired
39 public EventDao eventDao; 43 public EventDao eventDao;
40 44
@@ -47,6 +51,7 @@ public class BaseEventService implements EventService { @@ -47,6 +51,7 @@ public class BaseEventService implements EventService {
47 @Override 51 @Override
48 public ListenableFuture<Event> saveAsync(Event event) { 52 public ListenableFuture<Event> saveAsync(Event event) {
49 eventValidator.validate(event, Event::getTenantId); 53 eventValidator.validate(event, Event::getTenantId);
  54 + checkAndTruncateDebugEvent(event);
50 return eventDao.saveAsync(event); 55 return eventDao.saveAsync(event);
51 } 56 }
52 57
@@ -56,9 +61,21 @@ public class BaseEventService implements EventService { @@ -56,9 +61,21 @@ public class BaseEventService implements EventService {
56 if (StringUtils.isEmpty(event.getUid())) { 61 if (StringUtils.isEmpty(event.getUid())) {
57 throw new DataValidationException("Event uid should be specified!."); 62 throw new DataValidationException("Event uid should be specified!.");
58 } 63 }
  64 + checkAndTruncateDebugEvent(event);
59 return eventDao.saveIfNotExists(event); 65 return eventDao.saveIfNotExists(event);
60 } 66 }
61 67
  68 + private void checkAndTruncateDebugEvent(Event event) {
  69 + if (event.getType().startsWith("DEBUG")) {
  70 + String dataStr = event.getBody().get("data").asText();
  71 + int dataSize = dataStr.getBytes(StandardCharsets.UTF_8).length;
  72 + if (dataSize > MAX_DEBUG_EVENT_IN_BYTES) {
  73 + ((ObjectNode) event.getBody()).put("data", dataStr.substring(0, 1024));
  74 + log.trace("[{}] Event was truncated.", event.getId());
  75 + }
  76 + }
  77 + }
  78 +
62 @Override 79 @Override
63 public Optional<Event> findEvent(TenantId tenantId, EntityId entityId, String eventType, String eventUid) { 80 public Optional<Event> findEvent(TenantId tenantId, EntityId entityId, String eventType, String eventUid) {
64 if (tenantId == null) { 81 if (tenantId == null) {