Event clearing
This commit is contained in:
		
							parent
							
								
									530765487c
								
							
						
					
					
						commit
						b9e6c2b75e
					
				@ -18,6 +18,7 @@ package org.thingsboard.server.controller;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import io.swagger.annotations.ApiParam;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.http.MediaType;
 | 
			
		||||
import org.springframework.security.access.prepost.PreAuthorize;
 | 
			
		||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
			
		||||
@ -26,6 +27,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMethod;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestParam;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseBody;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseStatus;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
import org.thingsboard.server.common.data.Event;
 | 
			
		||||
import org.thingsboard.server.common.data.event.EventFilter;
 | 
			
		||||
@ -238,4 +240,35 @@ public class EventController extends BaseController {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "Clear Events (clearEvents)", notes = "Clears events for specified entity.")
 | 
			
		||||
    @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
 | 
			
		||||
    @RequestMapping(value = "/events/{entityType}/{entityId}/{eventType}/clear", method = RequestMethod.DELETE)
 | 
			
		||||
    @ResponseStatus(HttpStatus.OK)
 | 
			
		||||
    public void clearEvents(
 | 
			
		||||
            @ApiParam(value = ENTITY_TYPE_PARAM_DESCRIPTION, required = true)
 | 
			
		||||
            @PathVariable(ENTITY_TYPE) String strEntityType,
 | 
			
		||||
            @ApiParam(value = ENTITY_ID_PARAM_DESCRIPTION, required = true)
 | 
			
		||||
            @PathVariable(ENTITY_ID) String strEntityId,
 | 
			
		||||
            @ApiParam(value = TENANT_ID_PARAM_DESCRIPTION, required = true)
 | 
			
		||||
            @RequestParam("tenantId") String strTenantId,
 | 
			
		||||
            @ApiParam(value = "A string value representing event type", example = "STATS", required = true)
 | 
			
		||||
            @PathVariable("eventType") String eventType,
 | 
			
		||||
            @ApiParam(value = EVENT_START_TIME_DESCRIPTION)
 | 
			
		||||
            @RequestParam(required = false) Long startTime,
 | 
			
		||||
            @ApiParam(value = EVENT_END_TIME_DESCRIPTION)
 | 
			
		||||
            @RequestParam(required = false) Long endTime) throws ThingsboardException {
 | 
			
		||||
        checkParameter("EntityId", strEntityId);
 | 
			
		||||
        checkParameter("EntityType", strEntityType);
 | 
			
		||||
        try {
 | 
			
		||||
            TenantId tenantId = new TenantId(toUUID(strTenantId));
 | 
			
		||||
 | 
			
		||||
            EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
 | 
			
		||||
            checkEntityId(entityId, Operation.DELETE);
 | 
			
		||||
 | 
			
		||||
            eventService.removeEventsByTypeInPeriod(tenantId, entityId, eventType, startTime, endTime);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -48,4 +48,5 @@ public interface EventService {
 | 
			
		||||
 | 
			
		||||
    void cleanupEvents(long ttl, long debugTtl);
 | 
			
		||||
 | 
			
		||||
    void removeEventsByTypeInPeriod(TenantId tenantId, EntityId entityId, String eventType, Long startTime, Long endTime);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -25,14 +25,18 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.server.common.data.Event;
 | 
			
		||||
import org.thingsboard.server.common.data.event.EventFilter;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.IdBased;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.PageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.service.DataValidator;
 | 
			
		||||
 | 
			
		||||
import java.sql.Time;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@Slf4j
 | 
			
		||||
@ -118,17 +122,8 @@ public class BaseEventService implements EventService {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void removeEvents(TenantId tenantId, EntityId entityId) {
 | 
			
		||||
        PageData<Event> eventPageData;
 | 
			
		||||
        TimePageLink eventPageLink = new TimePageLink(1000);
 | 
			
		||||
        do {
 | 
			
		||||
            eventPageData = findEvents(tenantId, entityId, eventPageLink);
 | 
			
		||||
            for (Event event : eventPageData.getData()) {
 | 
			
		||||
                eventDao.removeById(tenantId, event.getUuidId());
 | 
			
		||||
            }
 | 
			
		||||
            if (eventPageData.hasNext()) {
 | 
			
		||||
                eventPageLink = eventPageLink.nextPageLink();
 | 
			
		||||
            }
 | 
			
		||||
        } while (eventPageData.hasNext());
 | 
			
		||||
        removeEventsByTypeAndPageLink(tenantId, entityId, null, eventPageLink);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -136,6 +131,28 @@ public class BaseEventService implements EventService {
 | 
			
		||||
        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 =
 | 
			
		||||
            new DataValidator<Event>() {
 | 
			
		||||
                @Override
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user