Refactor events clearing

This commit is contained in:
Viacheslav Klimov 2021-10-29 16:52:19 +03:00
parent b9e6c2b75e
commit 144da0367a
3 changed files with 25 additions and 27 deletions

View File

@ -265,7 +265,7 @@ public class EventController extends BaseController {
EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId); EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
checkEntityId(entityId, Operation.DELETE); checkEntityId(entityId, Operation.DELETE);
eventService.removeEventsByTypeInPeriod(tenantId, entityId, eventType, startTime, endTime); eventService.removeEvents(tenantId, entityId, eventType, startTime, endTime);
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }

View File

@ -46,7 +46,8 @@ public interface EventService {
void removeEvents(TenantId tenantId, EntityId entityId); void removeEvents(TenantId tenantId, EntityId entityId);
void removeEvents(TenantId tenantId, EntityId entityId, String eventType, Long startTime, Long endTime);
void cleanupEvents(long ttl, long debugTtl); void cleanupEvents(long ttl, long debugTtl);
void removeEventsByTypeInPeriod(TenantId tenantId, EntityId entityId, String eventType, Long startTime, Long endTime);
} }

View File

@ -32,7 +32,6 @@ 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.sql.Time;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -122,8 +121,28 @@ public class BaseEventService implements EventService {
@Override @Override
public void removeEvents(TenantId tenantId, EntityId entityId) { public void removeEvents(TenantId tenantId, EntityId entityId) {
TimePageLink eventPageLink = new TimePageLink(1000); removeEvents(tenantId, entityId, null, null, null);
removeEventsByTypeAndPageLink(tenantId, entityId, null, eventPageLink); }
@Override
public void removeEvents(TenantId tenantId, EntityId entityId, String eventType, Long startTime, Long endTime) {
TimePageLink eventsPageLink = new TimePageLink(1000, 0, null, null, startTime, endTime);
PageData<Event> eventsPageData;
do {
if (eventType == null) {
eventsPageData = findEvents(tenantId, entityId, eventsPageLink);
} else {
eventsPageData = findEvents(tenantId, entityId, eventType, eventsPageLink);
}
eventDao.removeAllByIds(eventsPageData.getData().stream()
.map(IdBased::getUuidId)
.collect(Collectors.toList()));
if (eventsPageData.hasNext()) {
eventsPageLink = eventsPageLink.nextPageLink();
}
} while (eventsPageData.hasNext());
} }
@Override @Override
@ -131,28 +150,6 @@ public class BaseEventService implements EventService {
eventDao.cleanupEvents(ttl, debugTtl); eventDao.cleanupEvents(ttl, debugTtl);
} }
@Override
public void removeEventsByTypeInPeriod(TenantId tenantId, EntityId entityId, String eventType, Long startTime, Long endTime) {
TimePageLink eventPageLink =
new TimePageLink(1000, 0, null, null, startTime, endTime);
removeEventsByTypeAndPageLink(tenantId, entityId, eventType, eventPageLink);
}
private void removeEventsByTypeAndPageLink(TenantId tenantId, EntityId entityId, String eventType, TimePageLink eventPageLink) {
PageData<Event> eventPageData;
do {
if (eventType == null)
eventPageData = findEvents(tenantId, entityId, eventPageLink);
else
eventPageData = findEvents(tenantId, entityId, eventType, eventPageLink);
List<UUID> eventsIds = eventPageData.getData().stream().map(IdBased::getUuidId).collect(Collectors.toList());
eventDao.removeAllByIds(eventsIds);
if (eventPageData.hasNext()) {
eventPageLink = eventPageLink.nextPageLink();
}
} while (eventPageData.hasNext());
}
private DataValidator<Event> eventValidator = private DataValidator<Event> eventValidator =
new DataValidator<Event>() { new DataValidator<Event>() {
@Override @Override