Commit 872a6fb45dff91a2b6c24977b83edf11a035e907
Committed by
Andrew Shvayka
1 parent
a50368a1
added checkAndTruncateDebugEvent
Showing
1 changed file
with
17 additions
and
0 deletions
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.event; |
17 | 17 | |
18 | +import com.fasterxml.jackson.databind.node.ObjectNode; | |
18 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
19 | 20 | import lombok.extern.slf4j.Slf4j; |
20 | 21 | import org.apache.commons.lang3.StringUtils; |
... | ... | @@ -28,6 +29,7 @@ import org.thingsboard.server.common.data.page.TimePageLink; |
28 | 29 | import org.thingsboard.server.dao.exception.DataValidationException; |
29 | 30 | import org.thingsboard.server.dao.service.DataValidator; |
30 | 31 | |
32 | +import java.nio.charset.StandardCharsets; | |
31 | 33 | import java.util.List; |
32 | 34 | import java.util.Optional; |
33 | 35 | |
... | ... | @@ -35,6 +37,8 @@ import java.util.Optional; |
35 | 37 | @Slf4j |
36 | 38 | public class BaseEventService implements EventService { |
37 | 39 | |
40 | + private static final int MAX_DEBUG_EVENT_IN_BYTES = 10 * 1024; | |
41 | + | |
38 | 42 | @Autowired |
39 | 43 | public EventDao eventDao; |
40 | 44 | |
... | ... | @@ -47,6 +51,7 @@ public class BaseEventService implements EventService { |
47 | 51 | @Override |
48 | 52 | public ListenableFuture<Event> saveAsync(Event event) { |
49 | 53 | eventValidator.validate(event, Event::getTenantId); |
54 | + checkAndTruncateDebugEvent(event); | |
50 | 55 | return eventDao.saveAsync(event); |
51 | 56 | } |
52 | 57 | |
... | ... | @@ -56,9 +61,21 @@ public class BaseEventService implements EventService { |
56 | 61 | if (StringUtils.isEmpty(event.getUid())) { |
57 | 62 | throw new DataValidationException("Event uid should be specified!."); |
58 | 63 | } |
64 | + checkAndTruncateDebugEvent(event); | |
59 | 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 | 79 | @Override |
63 | 80 | public Optional<Event> findEvent(TenantId tenantId, EntityId entityId, String eventType, String eventUid) { |
64 | 81 | if (tenantId == null) { | ... | ... |