Browse Source

Merge pull request #5814 from ViacheslavKlimov/feature/events-clearing

[3.3.x] Events clearing
pull/5822/head
Andrew Shvayka 5 years ago
committed by GitHub
parent
commit
fe38da0b34
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      application/src/main/java/org/thingsboard/server/controller/EventController.java
  2. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/event/EventService.java
  3. 29
      dao/src/main/java/org/thingsboard/server/dao/event/BaseEventService.java
  4. 9
      ui-ngx/src/app/core/http/event.service.ts
  5. 13
      ui-ngx/src/app/modules/home/components/event/event-table-config.ts
  6. 3
      ui-ngx/src/app/modules/home/models/entity/entities-table-config.models.ts
  7. 1
      ui-ngx/src/assets/locale/locale.constant-en_US.json

28
application/src/main/java/org/thingsboard/server/controller/EventController.java

@ -18,6 +18,7 @@ package org.thingsboard.server.controller;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable; 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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.Event; import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.event.EventFilter; import org.thingsboard.server.common.data.event.EventFilter;
@ -238,4 +240,30 @@ public class EventController extends BaseController {
} }
} }
@ApiOperation(value = "Clear Events (clearEvents)", notes = "Clears events by filter for specified entity.")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/events/{entityType}/{entityId}/clear", method = RequestMethod.POST)
@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 = EVENT_START_TIME_DESCRIPTION)
@RequestParam(required = false) Long startTime,
@ApiParam(value = EVENT_END_TIME_DESCRIPTION)
@RequestParam(required = false) Long endTime,
@ApiParam(value = EVENT_FILTER_DEFINITION)
@RequestBody EventFilter eventFilter) throws ThingsboardException {
checkParameter("EntityId", strEntityId);
checkParameter("EntityType", strEntityType);
try {
EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
checkEntityId(entityId, Operation.WRITE);
eventService.removeEvents(getTenantId(), entityId, eventFilter, startTime, endTime);
} catch (Exception e) {
throw handleException(e);
}
}
} }

2
common/dao-api/src/main/java/org/thingsboard/server/dao/event/EventService.java

@ -46,6 +46,8 @@ public interface EventService {
void removeEvents(TenantId tenantId, EntityId entityId); void removeEvents(TenantId tenantId, EntityId entityId);
void removeEvents(TenantId tenantId, EntityId entityId, EventFilter eventFilter, Long startTime, Long endTime);
void cleanupEvents(long ttl, long debugTtl); void cleanupEvents(long ttl, long debugTtl);
} }

29
dao/src/main/java/org/thingsboard/server/dao/event/BaseEventService.java

@ -25,6 +25,7 @@ import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Event; import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.event.EventFilter; import org.thingsboard.server.common.data.event.EventFilter;
import org.thingsboard.server.common.data.id.EntityId; 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.id.TenantId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.TimePageLink; import org.thingsboard.server.common.data.page.TimePageLink;
@ -33,6 +34,7 @@ import org.thingsboard.server.dao.service.DataValidator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
@ -118,17 +120,28 @@ public class BaseEventService implements EventService {
@Override @Override
public void removeEvents(TenantId tenantId, EntityId entityId) { public void removeEvents(TenantId tenantId, EntityId entityId) {
PageData<Event> eventPageData; removeEvents(tenantId, entityId, null, null, null);
TimePageLink eventPageLink = new TimePageLink(1000); }
@Override
public void removeEvents(TenantId tenantId, EntityId entityId, EventFilter eventFilter, Long startTime, Long endTime) {
TimePageLink eventsPageLink = new TimePageLink(1000, 0, null, null, startTime, endTime);
PageData<Event> eventsPageData;
do { do {
eventPageData = findEvents(tenantId, entityId, eventPageLink); if (eventFilter == null) {
for (Event event : eventPageData.getData()) { eventsPageData = findEvents(tenantId, entityId, eventsPageLink);
eventDao.removeById(tenantId, event.getUuidId()); } else {
eventsPageData = findEventsByFilter(tenantId, entityId, eventFilter, eventsPageLink);
} }
if (eventPageData.hasNext()) {
eventPageLink = eventPageLink.nextPageLink(); eventDao.removeAllByIds(eventsPageData.getData().stream()
.map(IdBased::getUuidId)
.collect(Collectors.toList()));
if (eventsPageData.hasNext()) {
eventsPageLink = eventsPageLink.nextPageLink();
} }
} while (eventPageData.hasNext()); } while (eventsPageData.hasNext());
} }
@Override @Override

9
ui-ngx/src/app/core/http/event.service.ts

@ -44,5 +44,12 @@ export class EventService {
return this.http.post<PageData<Event>>(`/api/events/${entityId.entityType}/${entityId.id}` + return this.http.post<PageData<Event>>(`/api/events/${entityId.entityType}/${entityId.id}` +
`${pageLink.toQuery()}&tenantId=${tenantId}`, {...filters, eventType}, defaultHttpOptionsFromConfig(config)); `${pageLink.toQuery()}&tenantId=${tenantId}`, {...filters, eventType}, defaultHttpOptionsFromConfig(config));
} }
public clearEvents(entityId: EntityId, eventType: EventType | DebugEventType, filters: FilterEventBody, tenantId: string,
pageLink: TimePageLink, config?: RequestConfig) {
return this.http.post(`/api/events/${entityId.entityType}/${entityId.id}/clear?tenantId=${tenantId}` +
(pageLink.startTime ? `&startTime=${pageLink.startTime}` : ``) +
(pageLink.endTime ? `&endTime=${pageLink.endTime}` : ``), {...filters, eventType},
defaultHttpOptionsFromConfig(config));
}
} }

13
ui-ngx/src/app/modules/home/components/event/event-table-config.ts

@ -135,6 +135,19 @@ export class EventTableConfig extends EntityTableConfig<Event, TimePageLink> {
onAction: ($event) => { onAction: ($event) => {
this.editEventFilter($event); this.editEventFilter($event);
} }
},
{
name: this.translate.instant('event.clean-events'),
icon: 'delete',
isEnabled: () => true,
onAction: ($event) => {
this.eventService.clearEvents(this.entityId, this.eventType, this.filterParams, this.tenantId, this.table.pageLink as TimePageLink).subscribe(
() => {
this.table.paginator.pageIndex = 0;
this.table.updateData();
}
);
}
}); });
} }

3
ui-ngx/src/app/modules/home/models/entity/entities-table-config.models.ts

@ -25,7 +25,8 @@ import { EntityComponent } from '@home/components/entity/entity.component';
import { Type } from '@angular/core'; import { Type } from '@angular/core';
import { EntityAction } from './entity-component.models'; import { EntityAction } from './entity-component.models';
import { HasUUID } from '@shared/models/id/has-uuid'; import { HasUUID } from '@shared/models/id/has-uuid';
import { PageLink } from '@shared/models/page/page-link'; import { PageLink, TimePageLink } from '@shared/models/page/page-link';
import { EntitiesTableComponent } from '@home/components/entity/entities-table.component';
import { EntityTableHeaderComponent } from '@home/components/entity/entity-table-header.component'; import { EntityTableHeaderComponent } from '@home/components/entity/entity-table-header.component';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { EntityTabsComponent } from '../../components/entity/entity-tabs.component'; import { EntityTabsComponent } from '../../components/entity/entity-tabs.component';

1
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -1853,6 +1853,7 @@
"event": { "event": {
"event-type": "Event type", "event-type": "Event type",
"events-filter": "Events Filter", "events-filter": "Events Filter",
"clean-events": "Clear Events",
"type-error": "Error", "type-error": "Error",
"type-lc-event": "Lifecycle event", "type-lc-event": "Lifecycle event",
"type-stats": "Statistics", "type-stats": "Statistics",

Loading…
Cancel
Save