|
|
@ -17,15 +17,27 @@ package org.thingsboard.server.service.query; |
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.AlarmData; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.AlarmDataQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityData; |
|
|
import org.thingsboard.server.common.data.query.EntityData; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.EntityDataPageLink; |
|
|
import org.thingsboard.server.common.data.query.EntityDataQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityDataQuery; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.EntityDataSortOrder; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.EntityKey; |
|
|
|
|
|
import org.thingsboard.server.common.data.query.EntityKeyType; |
|
|
|
|
|
import org.thingsboard.server.dao.alarm.AlarmService; |
|
|
import org.thingsboard.server.dao.entity.EntityService; |
|
|
import org.thingsboard.server.dao.entity.EntityService; |
|
|
|
|
|
import org.thingsboard.server.dao.model.ModelConstants; |
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
import org.thingsboard.server.service.security.model.SecurityUser; |
|
|
import org.thingsboard.server.service.security.model.SecurityUser; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@TbCoreComponent |
|
|
@TbCoreComponent |
|
|
@ -34,6 +46,12 @@ public class DefaultEntityQueryService implements EntityQueryService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private EntityService entityService; |
|
|
private EntityService entityService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private AlarmService alarmService; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${server.ws.max_entities_per_alarm_subscription:1000}") |
|
|
|
|
|
private int maxEntitiesPerAlarmSubscription; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query) { |
|
|
public long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query) { |
|
|
return entityService.countEntitiesByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query); |
|
|
return entityService.countEntitiesByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query); |
|
|
@ -43,4 +61,43 @@ public class DefaultEntityQueryService implements EntityQueryService { |
|
|
public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) { |
|
|
public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) { |
|
|
return entityService.findEntityDataByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query); |
|
|
return entityService.findEntityDataByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query) { |
|
|
|
|
|
EntityDataQuery entityDataQuery = this.buildEntityDataQuery(query); |
|
|
|
|
|
PageData<EntityData> entities = entityService.findEntityDataByQuery(securityUser.getTenantId(), |
|
|
|
|
|
securityUser.getCustomerId(), entityDataQuery); |
|
|
|
|
|
if (entities.getTotalElements() > 0) { |
|
|
|
|
|
LinkedHashMap<EntityId, EntityData> entitiesMap = new LinkedHashMap<>(); |
|
|
|
|
|
for (EntityData entityData : entities.getData()) { |
|
|
|
|
|
entitiesMap.put(entityData.getEntityId(), entityData); |
|
|
|
|
|
} |
|
|
|
|
|
PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(securityUser.getTenantId(), |
|
|
|
|
|
securityUser.getCustomerId(), query, entitiesMap.keySet()); |
|
|
|
|
|
for (AlarmData alarmData : alarms.getData()) { |
|
|
|
|
|
EntityId entityId = alarmData.getEntityId(); |
|
|
|
|
|
if (entityId != null) { |
|
|
|
|
|
EntityData entityData = entitiesMap.get(entityId); |
|
|
|
|
|
if (entityData != null) { |
|
|
|
|
|
alarmData.getLatest().putAll(entityData.getLatest()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return alarms; |
|
|
|
|
|
} else { |
|
|
|
|
|
return new PageData<>(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private EntityDataQuery buildEntityDataQuery(AlarmDataQuery query) { |
|
|
|
|
|
EntityDataSortOrder sortOrder = query.getPageLink().getSortOrder(); |
|
|
|
|
|
EntityDataSortOrder entitiesSortOrder; |
|
|
|
|
|
if (sortOrder == null || sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) { |
|
|
|
|
|
entitiesSortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, ModelConstants.CREATED_TIME_PROPERTY)); |
|
|
|
|
|
} else { |
|
|
|
|
|
entitiesSortOrder = sortOrder; |
|
|
|
|
|
} |
|
|
|
|
|
EntityDataPageLink edpl = new EntityDataPageLink(maxEntitiesPerAlarmSubscription, 0, null, entitiesSortOrder); |
|
|
|
|
|
return new EntityDataQuery(query.getEntityFilter(), edpl, query.getEntityFields(), query.getLatestValues(), query.getKeyFilters()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|