...
|
...
|
@@ -17,15 +17,27 @@ package org.thingsboard.server.service.query; |
17
|
17
|
|
18
|
18
|
import lombok.extern.slf4j.Slf4j;
|
19
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
20
|
+import org.springframework.beans.factory.annotation.Value;
|
20
|
21
|
import org.springframework.stereotype.Service;
|
|
22
|
+import org.thingsboard.server.common.data.id.EntityId;
|
21
|
23
|
import org.thingsboard.server.common.data.page.PageData;
|
|
24
|
+import org.thingsboard.server.common.data.query.AlarmData;
|
|
25
|
+import org.thingsboard.server.common.data.query.AlarmDataQuery;
|
22
|
26
|
import org.thingsboard.server.common.data.query.EntityCountQuery;
|
23
|
27
|
import org.thingsboard.server.common.data.query.EntityData;
|
|
28
|
+import org.thingsboard.server.common.data.query.EntityDataPageLink;
|
24
|
29
|
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
|
30
|
+import org.thingsboard.server.common.data.query.EntityDataSortOrder;
|
|
31
|
+import org.thingsboard.server.common.data.query.EntityKey;
|
|
32
|
+import org.thingsboard.server.common.data.query.EntityKeyType;
|
|
33
|
+import org.thingsboard.server.dao.alarm.AlarmService;
|
25
|
34
|
import org.thingsboard.server.dao.entity.EntityService;
|
|
35
|
+import org.thingsboard.server.dao.model.ModelConstants;
|
26
|
36
|
import org.thingsboard.server.queue.util.TbCoreComponent;
|
27
|
37
|
import org.thingsboard.server.service.security.model.SecurityUser;
|
28
|
38
|
|
|
39
|
+import java.util.LinkedHashMap;
|
|
40
|
+
|
29
|
41
|
@Service
|
30
|
42
|
@Slf4j
|
31
|
43
|
@TbCoreComponent
|
...
|
...
|
@@ -34,6 +46,12 @@ public class DefaultEntityQueryService implements EntityQueryService { |
34
|
46
|
@Autowired
|
35
|
47
|
private EntityService entityService;
|
36
|
48
|
|
|
49
|
+ @Autowired
|
|
50
|
+ private AlarmService alarmService;
|
|
51
|
+
|
|
52
|
+ @Value("${server.ws.max_entities_per_alarm_subscription:1000}")
|
|
53
|
+ private int maxEntitiesPerAlarmSubscription;
|
|
54
|
+
|
37
|
55
|
@Override
|
38
|
56
|
public long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query) {
|
39
|
57
|
return entityService.countEntitiesByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query);
|
...
|
...
|
@@ -43,4 +61,43 @@ public class DefaultEntityQueryService implements EntityQueryService { |
43
|
61
|
public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) {
|
44
|
62
|
return entityService.findEntityDataByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query);
|
45
|
63
|
}
|
|
64
|
+
|
|
65
|
+ @Override
|
|
66
|
+ public PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query) {
|
|
67
|
+ EntityDataQuery entityDataQuery = this.buildEntityDataQuery(query);
|
|
68
|
+ PageData<EntityData> entities = entityService.findEntityDataByQuery(securityUser.getTenantId(),
|
|
69
|
+ securityUser.getCustomerId(), entityDataQuery);
|
|
70
|
+ if (entities.getTotalElements() > 0) {
|
|
71
|
+ LinkedHashMap<EntityId, EntityData> entitiesMap = new LinkedHashMap<>();
|
|
72
|
+ for (EntityData entityData : entities.getData()) {
|
|
73
|
+ entitiesMap.put(entityData.getEntityId(), entityData);
|
|
74
|
+ }
|
|
75
|
+ PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(securityUser.getTenantId(),
|
|
76
|
+ securityUser.getCustomerId(), query, entitiesMap.keySet());
|
|
77
|
+ for (AlarmData alarmData : alarms.getData()) {
|
|
78
|
+ EntityId entityId = alarmData.getEntityId();
|
|
79
|
+ if (entityId != null) {
|
|
80
|
+ EntityData entityData = entitiesMap.get(entityId);
|
|
81
|
+ if (entityData != null) {
|
|
82
|
+ alarmData.getLatest().putAll(entityData.getLatest());
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ return alarms;
|
|
87
|
+ } else {
|
|
88
|
+ return new PageData<>();
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ private EntityDataQuery buildEntityDataQuery(AlarmDataQuery query) {
|
|
93
|
+ EntityDataSortOrder sortOrder = query.getPageLink().getSortOrder();
|
|
94
|
+ EntityDataSortOrder entitiesSortOrder;
|
|
95
|
+ if (sortOrder == null || sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) {
|
|
96
|
+ entitiesSortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, ModelConstants.CREATED_TIME_PROPERTY));
|
|
97
|
+ } else {
|
|
98
|
+ entitiesSortOrder = sortOrder;
|
|
99
|
+ }
|
|
100
|
+ EntityDataPageLink edpl = new EntityDataPageLink(maxEntitiesPerAlarmSubscription, 0, null, entitiesSortOrder);
|
|
101
|
+ return new EntityDataQuery(query.getEntityFilter(), edpl, query.getEntityFields(), query.getLatestValues(), query.getKeyFilters());
|
|
102
|
+ }
|
46
|
103
|
} |
...
|
...
|
|