Commit c26d7d1662608affbc8507afac36f64b7c9ac65e

Authored by Andrii Shvaika
2 parents 11447ac4 3b5f11df

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

…/thingsboard into feature/swagger
... ... @@ -208,6 +208,14 @@ public abstract class BaseController {
208 208
209 209 protected final String EVENT_START_TIME_DESCRIPTION = "Timestamp. Events with creation time before it won't be queried.";
210 210 protected final String EVENT_END_TIME_DESCRIPTION = "Timestamp. Events with creation time after it won't be queried.";
  211 +
  212 + protected final String EVENT_ERROR_FILTER_OBJ = "{ \"eventType\": \"ERROR\", \"server\": \"ip-172-31-24-152\", \"method\": \"onClusterEventMsg\", \"error\": \"Error Message\" }";
  213 + protected final String EVENT_LC_EVENT_FILTER_OBJ = "{ \"eventType\": \"LC_EVENT\", \"server\": \"ip-172-31-24-152\", \"event\": \"STARTED\", \"status\": \"Success\", \"error\": \"Error Message\" }";
  214 + protected final String EVENT_STATS_FILTER_OBJ = "{ \"eventType\": \"STATS\", \"server\": \"ip-172-31-24-152\", \"messagesProcessed\": 10, \"errorsOccurred\": 5 }";
  215 + 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\" }";
  216 + protected final String EVENT_DEBUG_RULE_NODE_FILTER_OBJ = "{ \"eventType\": \"DEBUG_RULE_NODE\"," + DEBUG_FILTER_OBJ;
  217 + protected final String EVENT_DEBUG_RULE_CHAIN_FILTER_OBJ = "{ \"eventType\": \"DEBUG_RULE_CHAIN\"," + DEBUG_FILTER_OBJ;
  218 +
211 219 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.";
212 220 protected static final String RELATION_TYPE_GROUP_PARAM_DESCRIPTION = "A string value representing relation type group. For example, 'COMMON'";
213 221
... ...
... ... @@ -59,17 +59,14 @@ public class EntityRelationController extends BaseController {
59 59 "If the user has the authority of 'Tenant Administrator', the server checks that 'from' and 'to' entities are owned by the same tenant. " +
60 60 "If the user has the authority of 'Customer User', the server checks that the 'from' and 'to' entities are assigned to the same customer.";
61 61
62   - 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. " +
  62 + 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. " +
63 63 "If the user has the authority of 'Tenant Administrator', the server checks that the entity is owned by the same tenant. " +
64 64 "If the user has the authority of 'Customer User', the server checks that the entity is assigned to the same customer.";
65 65
66   -
67 66 @ApiOperation(value = "Create Relation (saveRelation)",
68 67 notes = "Creates or updates a relation between two entities in the platform. " +
69 68 "Relations unique key is a combination of from/to entity id and relation type group and relation type. " +
70   - "\n\nIf the user has the authority of 'System Administrator', the server checks that 'from' and 'to' entities are owned by the sysadmin. " +
71   - "If the user has the authority of 'Tenant Administrator', the server checks that 'from' and 'to' entities are owned by the same tenant. " +
72   - "If the user has the authority of 'Customer User', the server checks that the 'from' and 'to' entities are assigned to the same customer.")
  69 + SECURITY_CHECKS_ENTITIES_DESCRIPTION)
73 70 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
74 71 @RequestMapping(value = "/relation", method = RequestMethod.POST)
75 72 @ResponseStatus(value = HttpStatus.OK)
... ... @@ -163,7 +160,7 @@ public class EntityRelationController extends BaseController {
163 160 }
164 161
165 162 @ApiOperation(value = "Get Relation (getRelation)",
166   - notes = "Returns relation object between two specified entities if present. Otherwise throws exception." + SECURITY_CHECKS_ENTITIES_DESCRIPTION,
  163 + notes = "Returns relation object between two specified entities if present. Otherwise throws exception. " + SECURITY_CHECKS_ENTITIES_DESCRIPTION,
167 164 produces = MediaType.APPLICATION_JSON_VALUE)
168 165 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
169 166 @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 {
216 213
217 214 @ApiOperation(value = "Get List of Relation Infos (findInfoByFrom)",
218 215 notes = "Returns list of relation info objects for the specified entity by the 'from' direction. " +
219   - SECURITY_CHECKS_ENTITY_DESCRIPTION +" " + RELATION_INFO_DESCRIPTION,
  216 + SECURITY_CHECKS_ENTITY_DESCRIPTION + " " + RELATION_INFO_DESCRIPTION,
220 217 produces = MediaType.APPLICATION_JSON_VALUE)
221 218 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
222 219 @RequestMapping(value = "/relations/info", method = RequestMethod.GET, params = {FROM_ID, FROM_TYPE})
... ...
... ... @@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestParam;
28 28 import org.springframework.web.bind.annotation.ResponseBody;
29 29 import org.springframework.web.bind.annotation.RestController;
30 30 import org.thingsboard.server.common.data.Event;
31   -import org.thingsboard.server.common.data.event.EventFilter;
  31 +import org.thingsboard.server.common.data.event.BaseEventFilter;
32 32 import org.thingsboard.server.common.data.exception.ThingsboardException;
33 33 import org.thingsboard.server.common.data.id.EntityId;
34 34 import org.thingsboard.server.common.data.id.EntityIdFactory;
... ... @@ -45,11 +45,13 @@ import org.thingsboard.server.service.security.permission.Operation;
45 45 @RequestMapping("/api")
46 46 public class EventController extends BaseController {
47 47
  48 + private static final String NEW_LINE = "\n\n";
  49 +
48 50 @Autowired
49 51 private EventService eventService;
50 52
51   - @ApiOperation(value = "Get Events (getEvents)",
52   - notes = "Returns a page of events for specified entity by specifying event type." +
  53 + @ApiOperation(value = "Get Events by type (getEvents)",
  54 + notes = "Returns a page of events for specified entity by specifying event type. " +
53 55 PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
54 56 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
55 57 @RequestMapping(value = "/events/{entityType}/{entityId}/{eventType}", method = RequestMethod.GET)
... ... @@ -92,7 +94,7 @@ public class EventController extends BaseController {
92 94 }
93 95
94 96 @ApiOperation(value = "Get Events (getEvents)",
95   - notes = "Returns a page of events for specified entity." +
  97 + notes = "Returns a page of events for specified entity. " +
96 98 PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
97 99 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
98 100 @RequestMapping(value = "/events/{entityType}/{entityId}", method = RequestMethod.GET)
... ... @@ -134,9 +136,17 @@ public class EventController extends BaseController {
134 136 }
135 137 }
136 138
137   - @ApiOperation(value = "Get Events (getEvents)",
138   - notes = "Returns a page of events for specified entity by specifying event filter." +
139   - PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
  139 + @ApiOperation(value = "Get Events by event filter (getEvents)",
  140 + notes = "Returns a page of events for the chosen entity by specifying the event filter. " +
  141 + PAGE_DATA_PARAMETERS + NEW_LINE + "5 different eventFilter objects could be set for different event types. " +
  142 + "The eventType field is required. Others are optional. If some of them are set, the filtering will be applied according to them. " +
  143 + "See the examples below for all the fields used for each event type filtering. " + NEW_LINE +
  144 + EVENT_ERROR_FILTER_OBJ + NEW_LINE +
  145 + EVENT_LC_EVENT_FILTER_OBJ + NEW_LINE +
  146 + EVENT_STATS_FILTER_OBJ + NEW_LINE +
  147 + EVENT_DEBUG_RULE_NODE_FILTER_OBJ + NEW_LINE +
  148 + EVENT_DEBUG_RULE_CHAIN_FILTER_OBJ + NEW_LINE,
  149 + produces = MediaType.APPLICATION_JSON_VALUE)
140 150 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
141 151 @RequestMapping(value = "/events/{entityType}/{entityId}", method = RequestMethod.POST)
142 152 @ResponseBody
... ... @@ -152,7 +162,7 @@ public class EventController extends BaseController {
152 162 @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true)
153 163 @RequestParam int page,
154 164 @ApiParam(value = "A JSON value representing the event filter.", required = true)
155   - @RequestBody EventFilter eventFilter,
  165 + @RequestBody BaseEventFilter eventFilter,
156 166 @ApiParam(value = EVENT_TEXT_SEARCH_DESCRIPTION)
157 167 @RequestParam(required = false) String textSearch,
158 168 @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = EVENT_SORT_PROPERTY_ALLOWABLE_VALUES)
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.common.data.event;
  17 +
  18 +import io.swagger.annotations.ApiModel;
  19 +import io.swagger.annotations.ApiModelProperty;
  20 +import lombok.Data;
  21 +
  22 +@ApiModel
  23 +@Data
  24 +public abstract class BaseEventFilter implements EventFilter {
  25 +
  26 + @ApiModelProperty(position = 1, value = "String value representing msg direction type (incoming to entity or outcoming from entity)", allowableValues = "IN, OUT")
  27 + protected String msgDirectionType;
  28 + @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")
  29 + protected String server;
  30 + @ApiModelProperty(position = 3, value = "The case insensitive 'contains' filter based on data (key and value) for the message.", example = "humidity")
  31 + protected String dataSearch;
  32 + @ApiModelProperty(position = 4, value = "The case insensitive 'contains' filter based on metadata (key and value) for the message.", example = "deviceName")
  33 + protected String metadataSearch;
  34 + @ApiModelProperty(position = 5, value = "String value representing the entity type", allowableValues = "DEVICE")
  35 + protected String entityName;
  36 + @ApiModelProperty(position = 6, value = "String value representing the type of message routing", example = "Success")
  37 + protected String relationType;
  38 + @ApiModelProperty(position = 7, value = "String value representing the entity id in the event body (originator of the message)", example = "de9d54a0-2b7a-11ec-a3cc-23386423d98f")
  39 + protected String entityId;
  40 + @ApiModelProperty(position = 8, value = "String value representing the message type", example = "POST_TELEMETRY_REQUEST")
  41 + protected String msgType;
  42 + @ApiModelProperty(position = 9, value = "Boolean value to filter the errors", allowableValues = "false, true")
  43 + protected boolean isError;
  44 + @ApiModelProperty(position = 10, value = "The case insensitive 'contains' filter based on error message", example = "not present in the DB")
  45 + protected String errorStr;
  46 + @ApiModelProperty(position = 11, value = "String value representing the method name when the error happened", example = "onClusterEventMsg")
  47 + protected String method;
  48 + @ApiModelProperty(position = 12, value = "The minimum number of successfully processed messages", example = "25")
  49 + protected Integer messagesProcessed;
  50 + @ApiModelProperty(position = 13, value = "The minimum number of errors occurred during messages processing", example = "30")
  51 + protected Integer errorsOccurred;
  52 + @ApiModelProperty(position = 14, value = "String value representing the lifecycle event type", example = "STARTED")
  53 + protected String event;
  54 + @ApiModelProperty(position = 15, value = "String value representing status of the lifecycle event", allowableValues = "Success, Failure")
  55 + protected String status;
  56 +
  57 +}
... ...
... ... @@ -16,23 +16,10 @@
16 16 package org.thingsboard.server.common.data.event;
17 17
18 18 import io.swagger.annotations.ApiModel;
19   -import lombok.Data;
20 19 import org.thingsboard.server.common.data.StringUtils;
21 20
22   -@Data
23 21 @ApiModel
24   -public abstract class DebugEvent implements EventFilter {
25   -
26   - private String msgDirectionType;
27   - private String server;
28   - private String dataSearch;
29   - private String metadataSearch;
30   - private String entityName;
31   - private String relationType;
32   - private String entityId;
33   - private String msgType;
34   - private boolean isError;
35   - private String error;
  22 +public abstract class DebugEvent extends BaseEventFilter implements EventFilter {
36 23
37 24 public void setIsError(boolean isError) {
38 25 this.isError = isError;
... ... @@ -41,7 +28,7 @@ public abstract class DebugEvent implements EventFilter {
41 28 @Override
42 29 public boolean hasFilterForJsonBody() {
43 30 return !StringUtils.isEmpty(msgDirectionType) || !StringUtils.isEmpty(server) || !StringUtils.isEmpty(dataSearch) || !StringUtils.isEmpty(metadataSearch)
44   - || !StringUtils.isEmpty(entityName) || !StringUtils.isEmpty(relationType) || !StringUtils.isEmpty(entityId) || !StringUtils.isEmpty(msgType) || !StringUtils.isEmpty(error) || isError;
  31 + || !StringUtils.isEmpty(entityName) || !StringUtils.isEmpty(relationType) || !StringUtils.isEmpty(entityId) || !StringUtils.isEmpty(msgType) || !StringUtils.isEmpty(errorStr) || isError;
45 32 }
46 33
47 34 }
... ...
... ... @@ -16,15 +16,10 @@
16 16 package org.thingsboard.server.common.data.event;
17 17
18 18 import io.swagger.annotations.ApiModel;
19   -import lombok.Data;
20 19 import org.thingsboard.server.common.data.StringUtils;
21 20
22   -@Data
23 21 @ApiModel
24   -public class ErrorEventFilter implements EventFilter {
25   - private String server;
26   - private String method;
27   - private String error;
  22 +public class ErrorEventFilter extends BaseEventFilter implements EventFilter {
28 23
29 24 @Override
30 25 public EventType getEventType() {
... ... @@ -33,6 +28,6 @@ public class ErrorEventFilter implements EventFilter {
33 28
34 29 @Override
35 30 public boolean hasFilterForJsonBody() {
36   - return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(method) || !StringUtils.isEmpty(error);
  31 + return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(method) || !StringUtils.isEmpty(errorStr);
37 32 }
38 33 }
... ...
... ... @@ -15,10 +15,10 @@
15 15 */
16 16 package org.thingsboard.server.common.data.event;
17 17
18   -import com.fasterxml.jackson.annotation.JsonIgnore;
19 18 import com.fasterxml.jackson.annotation.JsonSubTypes;
20 19 import com.fasterxml.jackson.annotation.JsonTypeInfo;
21 20 import io.swagger.annotations.ApiModel;
  21 +import io.swagger.annotations.ApiModelProperty;
22 22
23 23 @ApiModel
24 24 @JsonTypeInfo(
... ... @@ -33,7 +33,8 @@ import io.swagger.annotations.ApiModel;
33 33 @JsonSubTypes.Type(value = StatisticsEventFilter.class, name = "STATS")
34 34 })
35 35 public interface EventFilter {
36   - @JsonIgnore
  36 +
  37 + @ApiModelProperty(position = 1, required = true, value = "String value representing the event type", example = "STATS")
37 38 EventType getEventType();
38 39
39 40 boolean hasFilterForJsonBody();
... ...
... ... @@ -16,16 +16,10 @@
16 16 package org.thingsboard.server.common.data.event;
17 17
18 18 import io.swagger.annotations.ApiModel;
19   -import lombok.Data;
20 19 import org.thingsboard.server.common.data.StringUtils;
21 20
22   -@Data
23 21 @ApiModel
24   -public class LifeCycleEventFilter implements EventFilter {
25   - private String server;
26   - private String event;
27   - private String status;
28   - private String error;
  22 +public class LifeCycleEventFilter extends BaseEventFilter implements EventFilter {
29 23
30 24 @Override
31 25 public EventType getEventType() {
... ... @@ -34,6 +28,6 @@ public class LifeCycleEventFilter implements EventFilter {
34 28
35 29 @Override
36 30 public boolean hasFilterForJsonBody() {
37   - return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(event) || !StringUtils.isEmpty(status) || !StringUtils.isEmpty(error);
  31 + return !StringUtils.isEmpty(server) || !StringUtils.isEmpty(event) || !StringUtils.isEmpty(status) || !StringUtils.isEmpty(errorStr);
38 32 }
39 33 }
... ...
... ... @@ -16,15 +16,10 @@
16 16 package org.thingsboard.server.common.data.event;
17 17
18 18 import io.swagger.annotations.ApiModel;
19   -import lombok.Data;
20 19 import org.thingsboard.server.common.data.StringUtils;
21 20
22   -@Data
23 21 @ApiModel
24   -public class StatisticsEventFilter implements EventFilter {
25   - private String server;
26   - private Integer messagesProcessed;
27   - private Integer errorsOccurred;
  22 +public class StatisticsEventFilter extends BaseEventFilter implements EventFilter {
28 23
29 24 @Override
30 25 public EventType getEventType() {
... ...
... ... @@ -39,10 +39,6 @@ import org.thingsboard.server.dao.event.EventDao;
39 39 import org.thingsboard.server.dao.model.sql.EventEntity;
40 40 import org.thingsboard.server.dao.sql.JpaAbstractDao;
41 41
42   -import java.sql.Connection;
43   -import java.sql.PreparedStatement;
44   -import java.sql.ResultSet;
45   -import java.sql.SQLException;
46 42 import java.util.List;
47 43 import java.util.Objects;
48 44 import java.util.Optional;
... ... @@ -196,7 +192,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
196 192 eventFilter.getEntityId(),
197 193 eventFilter.getMsgType(),
198 194 eventFilter.isError(),
199   - eventFilter.getError(),
  195 + eventFilter.getErrorStr(),
200 196 eventFilter.getDataSearch(),
201 197 eventFilter.getMetadataSearch(),
202 198 DaoUtil.toPageable(pageLink)));
... ... @@ -212,7 +208,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
212 208 notNull(pageLink.getEndTime()),
213 209 eventFilter.getServer(),
214 210 eventFilter.getMethod(),
215   - eventFilter.getError(),
  211 + eventFilter.getErrorStr(),
216 212 DaoUtil.toPageable(pageLink))
217 213 );
218 214 }
... ... @@ -231,7 +227,7 @@ public class JpaBaseEventDao extends JpaAbstractDao<EventEntity, Event> implemen
231 227 eventFilter.getEvent(),
232 228 statusFilterEnabled,
233 229 statusFilter,
234   - eventFilter.getError(),
  230 + eventFilter.getErrorStr(),
235 231 DaoUtil.toPageable(pageLink))
236 232 );
237 233 }
... ...
... ... @@ -44,10 +44,10 @@
44 44 {{ 'event.has-error' | translate }}
45 45 </tb-checkbox>
46 46 </ng-template>
47   - <ng-template [ngSwitchCase]="'error'">
  47 + <ng-template [ngSwitchCase]="'errorStr'">
48 48 <mat-form-field fxHide [fxShow]="showErrorMsgFields()">
49 49 <mat-label>{{ column.title | translate}}</mat-label>
50   - <input matInput type="text" name="errorSearchText" formControlName="error">
  50 + <input matInput type="text" name="errorSearchText" formControlName="errorStr">
51 51 </mat-form-field>
52 52 </ng-template>
53 53 <ng-container *ngSwitchDefault>
... ...
... ... @@ -292,14 +292,14 @@ export class EventTableConfig extends EntityTableConfig<Event, TimePageLink> {
292 292 case EventType.ERROR:
293 293 this.filterColumns.push(
294 294 {key: 'method', title: 'event.method'},
295   - {key: 'error', title: 'event.error'}
  295 + {key: 'errorStr', title: 'event.error'}
296 296 );
297 297 break;
298 298 case EventType.LC_EVENT:
299 299 this.filterColumns.push(
300 300 {key: 'event', title: 'event.event'},
301 301 {key: 'status', title: 'event.status'},
302   - {key: 'error', title: 'event.error'}
  302 + {key: 'errorStr', title: 'event.error'}
303 303 );
304 304 break;
305 305 case EventType.STATS:
... ... @@ -319,7 +319,7 @@ export class EventTableConfig extends EntityTableConfig<Event, TimePageLink> {
319 319 {key: 'dataSearch', title: 'event.data'},
320 320 {key: 'metadataSearch', title: 'event.metadata'},
321 321 {key: 'isError', title: 'event.error'},
322   - {key: 'error', title: 'event.error'}
  322 + {key: 'errorStr', title: 'event.error'}
323 323 );
324 324 break;
325 325 }
... ...
... ... @@ -91,13 +91,13 @@ export interface BaseFilterEventBody {
91 91
92 92 export interface ErrorFilterEventBody extends BaseFilterEventBody {
93 93 method?: string;
94   - error?: string;
  94 + errorStr?: string;
95 95 }
96 96
97 97 export interface LcFilterEventEventBody extends BaseFilterEventBody {
98 98 event?: string;
99 99 status?: string;
100   - error?: string;
  100 + errorStr?: string;
101 101 }
102 102
103 103 export interface StatsFilterEventBody extends BaseFilterEventBody {
... ... @@ -115,7 +115,7 @@ export interface DebugFilterRuleNodeEventBody extends BaseFilterEventBody {
115 115 dataSearch?: string;
116 116 metadataSearch?: string;
117 117 isError?: boolean;
118   - error?: string;
  118 + errorStr?: string;
119 119 }
120 120
121 121 export type FilterEventBody = ErrorFilterEventBody & LcFilterEventEventBody & StatsFilterEventBody & DebugFilterRuleNodeEventBody;
... ...