implemented countEntitiesByTypes for SysAdmin

This commit is contained in:
YevhenBondarenko 2023-03-13 10:57:04 +01:00
parent b13c9d7368
commit b936d48419
9 changed files with 120 additions and 1 deletions

View File

@ -27,6 +27,7 @@ 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.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
@ -41,6 +42,9 @@ import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.query.EntityQueryService; import org.thingsboard.server.service.query.EntityQueryService;
import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Operation;
import java.util.List;
import java.util.Map;
import static org.thingsboard.server.controller.ControllerConstants.ALARM_DATA_QUERY_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.ALARM_DATA_QUERY_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.ENTITY_COUNT_QUERY_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.ENTITY_COUNT_QUERY_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.ENTITY_DATA_QUERY_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.ENTITY_DATA_QUERY_DESCRIPTION;
@ -66,6 +70,17 @@ public class EntityQueryController extends BaseController {
return this.entityQueryService.countEntitiesByQuery(getCurrentUser(), query); return this.entityQueryService.countEntitiesByQuery(getCurrentUser(), query);
} }
@ApiOperation(value = "Count Entities by Entity Types")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
@RequestMapping(value = "/entitiesTypes/count", method = RequestMethod.POST)
@ResponseBody
public Map<EntityType, Long> countEntitiesByQuery(
@ApiParam(value = "A JSON value representing the entity types array.")
@RequestBody List<EntityType> entityTypes) throws ThingsboardException {
checkNotNull(entityTypes);
return this.entityQueryService.countEntitiesByTypes(getCurrentUser(), entityTypes);
}
@ApiOperation(value = "Find Entity Data by Query", notes = ENTITY_DATA_QUERY_DESCRIPTION) @ApiOperation(value = "Find Entity Data by Query", notes = ENTITY_DATA_QUERY_DESCRIPTION)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/entitiesQuery/find", method = RequestMethod.POST) @RequestMapping(value = "/entitiesQuery/find", method = RequestMethod.POST)

View File

@ -102,6 +102,11 @@ public class DefaultEntityQueryService implements EntityQueryService {
return entityService.countEntitiesByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query); return entityService.countEntitiesByQuery(securityUser.getTenantId(), securityUser.getCustomerId(), query);
} }
@Override
public Map<EntityType, Long> countEntitiesByTypes(SecurityUser securityUser, List<EntityType> entityTypes) {
return entityService.countEntitiesByTypes(securityUser.getTenantId(), securityUser.getCustomerId(), entityTypes);
}
@Override @Override
public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) { public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) {
if (query.getKeyFilters() != null) { if (query.getKeyFilters() != null) {

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.service.query;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import org.thingsboard.server.common.data.EntityType;
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.query.AlarmData; import org.thingsboard.server.common.data.query.AlarmData;
@ -26,10 +27,15 @@ import org.thingsboard.server.common.data.query.EntityData;
import org.thingsboard.server.common.data.query.EntityDataQuery; import org.thingsboard.server.common.data.query.EntityDataQuery;
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
import java.util.List;
import java.util.Map;
public interface EntityQueryService { public interface EntityQueryService {
long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query); long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query);
Map<EntityType, Long> countEntitiesByTypes(SecurityUser securityUser, List<EntityType> entityTypes);
PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query); PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query);
PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query); PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query);

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.dao.entity; package org.thingsboard.server.dao.entity;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails; import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
@ -24,6 +25,8 @@ 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.EntityDataQuery; import org.thingsboard.server.common.data.query.EntityDataQuery;
import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
public interface EntityService { public interface EntityService {
@ -38,5 +41,7 @@ public interface EntityService {
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query); long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes);
PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query); PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query);
} }

View File

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.HasCustomerId; import org.thingsboard.server.common.data.HasCustomerId;
import org.thingsboard.server.common.data.HasEmail; import org.thingsboard.server.common.data.HasEmail;
import org.thingsboard.server.common.data.HasLabel; import org.thingsboard.server.common.data.HasLabel;
@ -38,6 +39,8 @@ import org.thingsboard.server.common.data.query.EntityFilterType;
import org.thingsboard.server.common.data.query.RelationsQueryFilter; import org.thingsboard.server.common.data.query.RelationsQueryFilter;
import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.exception.IncorrectParameterException;
import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
@ -71,6 +74,14 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
return this.entityQueryDao.countEntitiesByQuery(tenantId, customerId, query); return this.entityQueryDao.countEntitiesByQuery(tenantId, customerId, query);
} }
@Override
public Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes) {
log.trace("Executing countEntitiesByQuery, tenantId [{}], customerId [{}], entityTypes [{}]", tenantId, customerId, entityTypes);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
validateId(customerId, INCORRECT_CUSTOMER_ID + customerId);
return this.entityQueryDao.countEntitiesByTypes(tenantId, customerId, entityTypes);
}
@Override @Override
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) { public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
log.trace("Executing findEntityDataByQuery, tenantId [{}], customerId [{}], query [{}]", tenantId, customerId, query); log.trace("Executing findEntityDataByQuery, tenantId [{}], customerId [{}], query [{}]", tenantId, customerId, query);

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.dao.entity; package org.thingsboard.server.dao.entity;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
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;
@ -22,10 +23,15 @@ 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.EntityDataQuery; import org.thingsboard.server.common.data.query.EntityDataQuery;
import java.util.List;
import java.util.Map;
public interface EntityQueryDao { public interface EntityQueryDao {
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query); long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query); PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query);
Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes);
} }

View File

@ -53,6 +53,7 @@ import org.thingsboard.server.common.data.query.SingleEntityFilter;
import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter; import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -388,6 +389,61 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
return findEntityDataByQuery(tenantId, customerId, query, false); return findEntityDataByQuery(tenantId, customerId, query, false);
} }
@Override
public Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes) {
int size = entityTypes.size();
QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, null, true));
ctx.append("select ");
for (int i = 0; i < size; i++) {
ctx.append("(select count(*) from ");
ctx.append(getTableName(entityTypes.get(i)));
ctx.append(")");
if (i < size - 1) {
ctx.append(", ");
}
}
return transactionTemplate.execute(status -> {
long startTs = System.currentTimeMillis();
try {
List<Long> counts = jdbcTemplate.query(ctx.getQuery(), rs -> {
List<Long> result = new ArrayList<>();
if (rs.next()) {
for (int i = 1; i <= size; i++) {
result.add(rs.getLong(i));
}
}
return result;
});
Map<EntityType, Long> result = new HashMap<>(size);
for (int i = 0; i < size; i++) {
result.put(entityTypes.get(i), counts.get(i));
}
return result;
} finally {
queryLog.logQuery(ctx, ctx.getQuery(), System.currentTimeMillis() - startTs);
}
});
}
private String getTableName(EntityType entityType) {
switch (entityType) {
case TENANT:
case TENANT_PROFILE:
case CUSTOMER:
case DEVICE:
case ASSET:
return entityType.name().toLowerCase();
case USER:
return "tb_user";
default:
throw new IllegalArgumentException("Not supported entity type: " + entityType + "!");
}
}
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query, boolean ignorePermissionCheck) { public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query, boolean ignorePermissionCheck) {
return transactionTemplate.execute(status -> { return transactionTemplate.execute(status -> {
EntityType entityType = resolveEntityType(query.getEntityFilter()); EntityType entityType = resolveEntityType(query.getEntityFilter());
@ -514,7 +570,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
} }
private String buildPermissionQuery(QueryContext ctx, EntityFilter entityFilter) { private String buildPermissionQuery(QueryContext ctx, EntityFilter entityFilter) {
if(ctx.isIgnorePermissionCheck()){ if (ctx.isIgnorePermissionCheck()) {
return "1=1"; return "1=1";
} }
switch (entityFilter.getType()) { switch (entityFilter.getType()) {

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.dao.sql.query; package org.thingsboard.server.dao.sql.query;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
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;
@ -22,6 +23,9 @@ 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.EntityDataQuery; import org.thingsboard.server.common.data.query.EntityDataQuery;
import java.util.List;
import java.util.Map;
public interface EntityQueryRepository { public interface EntityQueryRepository {
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query); long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
@ -30,4 +34,6 @@ public interface EntityQueryRepository {
PageData<EntityData> findEntityDataByQueryInternal(EntityDataQuery query); PageData<EntityData> findEntityDataByQueryInternal(EntityDataQuery query);
Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes);
} }

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.dao.sql.query;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
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;
@ -25,6 +26,9 @@ import org.thingsboard.server.common.data.query.EntityData;
import org.thingsboard.server.common.data.query.EntityDataQuery; import org.thingsboard.server.common.data.query.EntityDataQuery;
import org.thingsboard.server.dao.entity.EntityQueryDao; import org.thingsboard.server.dao.entity.EntityQueryDao;
import java.util.List;
import java.util.Map;
@Component @Component
public class JpaEntityQueryDao implements EntityQueryDao { public class JpaEntityQueryDao implements EntityQueryDao {
@ -40,4 +44,9 @@ public class JpaEntityQueryDao implements EntityQueryDao {
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) { public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
return entityQueryRepository.findEntityDataByQuery(tenantId, customerId, query); return entityQueryRepository.findEntityDataByQuery(tenantId, customerId, query);
} }
@Override
public Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes) {
return entityQueryRepository.countEntitiesByTypes(tenantId, customerId, entityTypes);
}
} }