Merge branch 'feature/swagger-event-filter' of https://github.com/dmytro-landiak/thingsboard into feature/swagger

This commit is contained in:
Andrii Shvaika 2021-10-15 12:52:13 +03:00
commit c26d7d1662
13 changed files with 108 additions and 68 deletions

View File

@ -208,6 +208,14 @@ public abstract class BaseController {
protected final String EVENT_START_TIME_DESCRIPTION = "Timestamp. Events with creation time before it won't be queried.";
protected final String EVENT_END_TIME_DESCRIPTION = "Timestamp. Events with creation time after it won't be queried.";
protected final String EVENT_ERROR_FILTER_OBJ = "{ \"eventType\": \"ERROR\", \"server\": \"ip-172-31-24-152\", \"method\": \"onClusterEventMsg\", \"error\": \"Error Message\" }";
protected final String EVENT_LC_EVENT_FILTER_OBJ = "{ \"eventType\": \"LC_EVENT\", \"server\": \"ip-172-31-24-152\", \"event\": \"STARTED\", \"status\": \"Success\", \"error\": \"Error Message\" }";
protected final String EVENT_STATS_FILTER_OBJ = "{ \"eventType\": \"STATS\", \"server\": \"ip-172-31-24-152\", \"messagesProcessed\": 10, \"errorsOccurred\": 5 }";
protected final String DEBUG_FILTER_OBJ = "\"msgDirectionType\": \"IN\", \"server\": \"ip-172-31-24-152\", \"dataSearch\": \"humidity\", \"metadataSearch\": \"deviceName\", \"entityName\": \"DEVICE\", \"relationType\": \"Success\", \"entityId\": \"de9d54a0-2b7a-11ec-a3cc-23386423d98f\", \"msgType\": \"POST_TELEMETRY_REQUEST\", \"isError\": \"false\", \"error\": \"Error Message\" }";
protected final String EVENT_DEBUG_RULE_NODE_FILTER_OBJ = "{ \"eventType\": \"DEBUG_RULE_NODE\"," + DEBUG_FILTER_OBJ;
protected final String EVENT_DEBUG_RULE_CHAIN_FILTER_OBJ = "{ \"eventType\": \"DEBUG_RULE_CHAIN\"," + DEBUG_FILTER_OBJ;
protected static final String RELATION_TYPE_PARAM_DESCRIPTION = "A string value representing relation type between entities. For example, 'Contains', 'Manages'. It can be any string value.";
protected static final String RELATION_TYPE_GROUP_PARAM_DESCRIPTION = "A string value representing relation type group. For example, 'COMMON'";

View File

@ -59,17 +59,14 @@ public class EntityRelationController extends BaseController {
"If the user has the authority of 'Tenant Administrator', the server checks that 'from' and 'to' entities are owned by the same tenant. " +
"If the user has the authority of 'Customer User', the server checks that the 'from' and 'to' entities are assigned to the same customer.";
private static final String SECURITY_CHECKS_ENTITY_DESCRIPTION = "\n\nIf the user has the authority of 'System Administrator', the server checks that 'from' and 'to' entities are owned by the sysadmin. " +
private static final String SECURITY_CHECKS_ENTITY_DESCRIPTION = "\n\nIf the user has the authority of 'System Administrator', the server checks that the entity is owned by the sysadmin. " +
"If the user has the authority of 'Tenant Administrator', the server checks that the entity is owned by the same tenant. " +
"If the user has the authority of 'Customer User', the server checks that the entity is assigned to the same customer.";
@ApiOperation(value = "Create Relation (saveRelation)",
notes = "Creates or updates a relation between two entities in the platform. " +
"Relations unique key is a combination of from/to entity id and relation type group and relation type. " +
"\n\nIf the user has the authority of 'System Administrator', the server checks that 'from' and 'to' entities are owned by the sysadmin. " +
"If the user has the authority of 'Tenant Administrator', the server checks that 'from' and 'to' entities are owned by the same tenant. " +
"If the user has the authority of 'Customer User', the server checks that the 'from' and 'to' entities are assigned to the same customer.")
SECURITY_CHECKS_ENTITIES_DESCRIPTION)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
@ -163,7 +160,7 @@ public class EntityRelationController extends BaseController {
}
@ApiOperation(value = "Get Relation (getRelation)",
notes = "Returns relation object between two specified entities if present. Otherwise throws exception." + SECURITY_CHECKS_ENTITIES_DESCRIPTION,
notes = "Returns relation object between two specified entities if present. Otherwise throws exception. " + SECURITY_CHECKS_ENTITIES_DESCRIPTION,
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.GET, params = {FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE})
@ -216,7 +213,7 @@ public class EntityRelationController extends BaseController {
@ApiOperation(value = "Get List of Relation Infos (findInfoByFrom)",
notes = "Returns list of relation info objects for the specified entity by the 'from' direction. " +
SECURITY_CHECKS_ENTITY_DESCRIPTION +" " + RELATION_INFO_DESCRIPTION,
SECURITY_CHECKS_ENTITY_DESCRIPTION + " " + RELATION_INFO_DESCRIPTION,
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations/info", method = RequestMethod.GET, params = {FROM_ID, FROM_TYPE})

View File

@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.Event;
import org.thingsboard.server.common.data.event.EventFilter;
import org.thingsboard.server.common.data.event.BaseEventFilter;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory;
@ -45,11 +45,13 @@ import org.thingsboard.server.service.security.permission.Operation;
@RequestMapping("/api")
public class EventController extends BaseController {
private static final String NEW_LINE = "\n\n";
@Autowired
private EventService eventService;
@ApiOperation(value = "Get Events (getEvents)",
notes = "Returns a page of events for specified entity by specifying event type." +
@ApiOperation(value = "Get Events by type (getEvents)",
notes = "Returns a page of events for specified entity by specifying event type. " +
PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/events/{entityType}/{entityId}/{eventType}", method = RequestMethod.GET)
@ -92,7 +94,7 @@ public class EventController extends BaseController {
}
@ApiOperation(value = "Get Events (getEvents)",
notes = "Returns a page of events for specified entity." +
notes = "Returns a page of events for specified entity. " +
PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/events/{entityType}/{entityId}", method = RequestMethod.GET)
@ -134,9 +136,17 @@ public class EventController extends BaseController {
}
}
@ApiOperation(value = "Get Events (getEvents)",
notes = "Returns a page of events for specified entity by specifying event filter." +
PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get Events by event filter (getEvents)",
notes = "Returns a page of events for the chosen entity by specifying the event filter. " +
PAGE_DATA_PARAMETERS + NEW_LINE + "5 different eventFilter objects could be set for different event types. " +
"The eventType field is required. Others are optional. If some of them are set, the filtering will be applied according to them. " +
"See the examples below for all the fields used for each event type filtering. " + NEW_LINE +
EVENT_ERROR_FILTER_OBJ + NEW_LINE +
EVENT_LC_EVENT_FILTER_OBJ + NEW_LINE +
EVENT_STATS_FILTER_OBJ + NEW_LINE +
EVENT_DEBUG_RULE_NODE_FILTER_OBJ + NEW_LINE +
EVENT_DEBUG_RULE_CHAIN_FILTER_OBJ + NEW_LINE,
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/events/{entityType}/{entityId}", method = RequestMethod.POST)
@ResponseBody
@ -152,7 +162,7 @@ public class EventController extends BaseController {
@ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true)
@RequestParam int page,
@ApiParam(value = "A JSON value representing the event filter.", required = true)
@RequestBody EventFilter eventFilter,
@RequestBody BaseEventFilter eventFilter,
@ApiParam(value = EVENT_TEXT_SEARCH_DESCRIPTION)
@RequestParam(required = false) String textSearch,
@ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = EVENT_SORT_PROPERTY_ALLOWABLE_VALUES)

View File

@ -0,0 +1,57 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.event;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel
@Data
public abstract class BaseEventFilter implements EventFilter {
@ApiModelProperty(position = 1, value = "String value representing msg direction type (incoming to entity or outcoming from entity)", allowableValues = "IN, OUT")
protected String msgDirectionType;
@ApiModelProperty(position = 2, value = "String value representing the server name, identifier or ip address where the platform is running", example = "ip-172-31-24-152")
protected String server;
@ApiModelProperty(position = 3, value = "The case insensitive 'contains' filter based on data (key and value) for the message.", example = "humidity")
protected String dataSearch;
@ApiModelProperty(position = 4, value = "The case insensitive 'contains' filter based on metadata (key and value) for the message.", example = "deviceName")
protected String metadataSearch;
@ApiModelProperty(position = 5, value = "String value representing the entity type", allowableValues = "DEVICE")
protected String entityName;
@ApiModelProperty(position = 6, value = "String value representing the type of message routing", example = "Success")
protected String relationType;
@ApiModelProperty(position = 7, value = "String value representing the entity id in the event body (originator of the message)", example = "de9d54a0-2b7a-11ec-a3cc-23386423d98f")
protected String entityId;
@ApiModelProperty(position = 8, value = "String value representing the message type", example = "POST_TELEMETRY_REQUEST")
protected String msgType;
@ApiModelProperty(position = 9, value = "Boolean value to filter the errors", allowableValues = "false, true")
protected boolean isError;
@ApiModelProperty(position = 10, value = "The case insensitive 'contains' filter based on error message", example = "not present in the DB")
protected String errorStr;
@ApiModelProperty(position = 11, value = "String value representing the method name when the error happened", example = "onClusterEventMsg")
protected String method;
@ApiModelProperty(position = 12, value = "The minimum number of successfully processed messages", example = "25")
protected Integer messagesProcessed;
@ApiModelProperty(position = 13, value = "The minimum number of errors occurred during messages processing", example = "30")
protected Integer errorsOccurred;
@ApiModelProperty(position = 14, value = "String value representing the lifecycle event type", example = "STARTED")
protected String event;
@ApiModelProperty(position = 15, value = "String value representing status of the lifecycle event", allowableValues = "Success, Failure")
protected String status;
}

View File

@ -16,23 +16,10 @@
package org.thingsboard.server.common.data.event;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.thingsboard.server.common.data.StringUtils;
@Data
@ApiModel
public abstract class DebugEvent implements EventFilter {
private String msgDirectionType;
private String server;
private String dataSearch;
private String metadataSearch;
private String entityName;
private String relationType;
private String entityId;
private String msgType;
private boolean isError;
private String error;
public abstract class DebugEvent extends BaseEventFilter implements EventFilter {
public void setIsError(boolean isError) {
this.isError = isError;
@ -41,7 +28,7 @@ public abstract class DebugEvent implements EventFilter {
@Override
public boolean hasFilterForJsonBody() {
return !StringUtils.isEmpty(msgDirectionType) || !StringUtils.isEmpty(server) || !StringUtils.isEmpty(dataSearch) || !StringUtils.isEmpty(metadataSearch)
|| !StringUtils.isEmpty(entityName) || !StringUtils.isEmpty(relationType) || !StringUtils.isEmpty(entityId) || !StringUtils.isEmpty(msgType) || !StringUtils.isEmpty(error) || isError;
|| !StringUtils.isEmpty(entityName) || !StringUtils.isEmpty(relationType) || !StringUtils.isEmpty(entityId) || !StringUtils.isEmpty(msgType) || !StringUtils.isEmpty(errorStr) || isError;
}
}

View File

@ -16,15 +16,10 @@
package org.thingsboard.server.common.data.event;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.thingsboard.server.common.data.StringUtils;
@Data
@ApiModel
public class ErrorEventFilter implements EventFilter {
private String server;
private String method;
private String error;
public class ErrorEventFilter extends BaseEventFilter implements EventFilter {
@Override
public EventType getEventType() {
@ -33,6 +28,6 @@ public class ErrorEventFilter implements EventFilter {
@Override
public boolean hasFilterForJsonBody() {
return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(method) || !StringUtils.isEmpty(error);
return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(method) || !StringUtils.isEmpty(errorStr);
}
}

View File

@ -15,10 +15,10 @@
*/
package org.thingsboard.server.common.data.event;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel
@JsonTypeInfo(
@ -33,7 +33,8 @@ import io.swagger.annotations.ApiModel;
@JsonSubTypes.Type(value = StatisticsEventFilter.class, name = "STATS")
})
public interface EventFilter {
@JsonIgnore
@ApiModelProperty(position = 1, required = true, value = "String value representing the event type", example = "STATS")
EventType getEventType();
boolean hasFilterForJsonBody();

View File

@ -16,16 +16,10 @@
package org.thingsboard.server.common.data.event;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.thingsboard.server.common.data.StringUtils;
@Data
@ApiModel
public class LifeCycleEventFilter implements EventFilter {
private String server;
private String event;
private String status;
private String error;
public class LifeCycleEventFilter extends BaseEventFilter implements EventFilter {
@Override
public EventType getEventType() {
@ -34,6 +28,6 @@ public class LifeCycleEventFilter implements EventFilter {
@Override
public boolean hasFilterForJsonBody() {
return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(event) || !StringUtils.isEmpty(status) || !StringUtils.isEmpty(error);
return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(event) || !StringUtils.isEmpty(status) || !StringUtils.isEmpty(errorStr);
}
}

View File

@ -16,15 +16,10 @@
package org.thingsboard.server.common.data.event;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.thingsboard.server.common.data.StringUtils;
@Data
@ApiModel
public class StatisticsEventFilter implements EventFilter {
private String server;
private Integer messagesProcessed;
private Integer errorsOccurred;
public class StatisticsEventFilter extends BaseEventFilter implements EventFilter {
@Override
public EventType getEventType() {

View File

@ -39,10 +39,6 @@ import org.thingsboard.server.dao.event.EventDao;
import org.thingsboard.server.dao.model.sql.EventEntity;
import org.thingsboard.server.dao.sql.JpaAbstractDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@ -196,7 +192,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
eventFilter.getEntityId(),
eventFilter.getMsgType(),
eventFilter.isError(),
eventFilter.getError(),
eventFilter.getErrorStr(),
eventFilter.getDataSearch(),
eventFilter.getMetadataSearch(),
DaoUtil.toPageable(pageLink)));
@ -212,7 +208,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
notNull(pageLink.getEndTime()),
eventFilter.getServer(),
eventFilter.getMethod(),
eventFilter.getError(),
eventFilter.getErrorStr(),
DaoUtil.toPageable(pageLink))
);
}
@ -231,7 +227,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
eventFilter.getEvent(),
statusFilterEnabled,
statusFilter,
eventFilter.getError(),
eventFilter.getErrorStr(),
DaoUtil.toPageable(pageLink))
);
}

View File

@ -44,10 +44,10 @@
{{ 'event.has-error' | translate }}
</tb-checkbox>
</ng-template>
<ng-template [ngSwitchCase]="'error'">
<ng-template [ngSwitchCase]="'errorStr'">
<mat-form-field fxHide [fxShow]="showErrorMsgFields()">
<mat-label>{{ column.title | translate}}</mat-label>
<input matInput type="text" name="errorSearchText" formControlName="error">
<input matInput type="text" name="errorSearchText" formControlName="errorStr">
</mat-form-field>
</ng-template>
<ng-container *ngSwitchDefault>

View File

@ -292,14 +292,14 @@ export class EventTableConfig extends EntityTableConfig<Event, TimePageLink> {
case EventType.ERROR:
this.filterColumns.push(
{key: 'method', title: 'event.method'},
{key: 'error', title: 'event.error'}
{key: 'errorStr', title: 'event.error'}
);
break;
case EventType.LC_EVENT:
this.filterColumns.push(
{key: 'event', title: 'event.event'},
{key: 'status', title: 'event.status'},
{key: 'error', title: 'event.error'}
{key: 'errorStr', title: 'event.error'}
);
break;
case EventType.STATS:
@ -319,7 +319,7 @@ export class EventTableConfig extends EntityTableConfig<Event, TimePageLink> {
{key: 'dataSearch', title: 'event.data'},
{key: 'metadataSearch', title: 'event.metadata'},
{key: 'isError', title: 'event.error'},
{key: 'error', title: 'event.error'}
{key: 'errorStr', title: 'event.error'}
);
break;
}

View File

@ -91,13 +91,13 @@ export interface BaseFilterEventBody {
export interface ErrorFilterEventBody extends BaseFilterEventBody {
method?: string;
error?: string;
errorStr?: string;
}
export interface LcFilterEventEventBody extends BaseFilterEventBody {
event?: string;
status?: string;
error?: string;
errorStr?: string;
}
export interface StatsFilterEventBody extends BaseFilterEventBody {
@ -115,7 +115,7 @@ export interface DebugFilterRuleNodeEventBody extends BaseFilterEventBody {
dataSearch?: string;
metadataSearch?: string;
isError?: boolean;
error?: string;
errorStr?: string;
}
export type FilterEventBody = ErrorFilterEventBody & LcFilterEventEventBody & StatsFilterEventBody & DebugFilterRuleNodeEventBody;