removed countEntitiesByTypes and added ability to use Entity count query for sysadmin
This commit is contained in:
parent
76c92c9070
commit
0be3c0b5bb
@ -401,7 +401,6 @@ public class AdminController extends BaseController {
|
||||
@ResponseBody
|
||||
public SystemInfo getSystemInfo() throws ThingsboardException {
|
||||
return systemInfoService.getSystemInfo();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -27,7 +27,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
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.id.TenantId;
|
||||
import org.thingsboard.server.common.data.id.UserId;
|
||||
@ -42,9 +41,6 @@ import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.query.EntityQueryService;
|
||||
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.ENTITY_COUNT_QUERY_DESCRIPTION;
|
||||
import static org.thingsboard.server.controller.ControllerConstants.ENTITY_DATA_QUERY_DESCRIPTION;
|
||||
@ -60,7 +56,7 @@ public class EntityQueryController extends BaseController {
|
||||
private static final int MAX_PAGE_SIZE = 100;
|
||||
|
||||
@ApiOperation(value = "Count Entities by Query", notes = ENTITY_COUNT_QUERY_DESCRIPTION)
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/entitiesQuery/count", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public long countEntitiesByQuery(
|
||||
@ -70,17 +66,6 @@ public class EntityQueryController extends BaseController {
|
||||
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)
|
||||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/entitiesQuery/find", method = RequestMethod.POST)
|
||||
|
||||
@ -29,6 +29,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.common.util.KvUtil;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
import org.thingsboard.server.common.data.id.EntityId;
|
||||
@ -55,7 +56,6 @@ import org.thingsboard.server.dao.attributes.AttributesService;
|
||||
import org.thingsboard.server.dao.entity.EntityService;
|
||||
import org.thingsboard.server.dao.model.ModelConstants;
|
||||
import org.thingsboard.server.dao.timeseries.TimeseriesService;
|
||||
import org.thingsboard.common.util.JacksonUtil;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.executors.DbCallbackExecutorService;
|
||||
import org.thingsboard.server.service.security.AccessValidator;
|
||||
@ -102,11 +102,6 @@ public class DefaultEntityQueryService implements EntityQueryService {
|
||||
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
|
||||
public PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query) {
|
||||
if (query.getKeyFilters() != null) {
|
||||
|
||||
@ -17,7 +17,6 @@ package org.thingsboard.server.service.query;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.page.PageData;
|
||||
import org.thingsboard.server.common.data.query.AlarmData;
|
||||
@ -27,15 +26,10 @@ import org.thingsboard.server.common.data.query.EntityData;
|
||||
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface EntityQueryService {
|
||||
|
||||
long countEntitiesByQuery(SecurityUser securityUser, EntityCountQuery query);
|
||||
|
||||
Map<EntityType, Long> countEntitiesByTypes(SecurityUser securityUser, List<EntityType> entityTypes);
|
||||
|
||||
PageData<EntityData> findEntityDataByQuery(SecurityUser securityUser, EntityDataQuery query);
|
||||
|
||||
PageData<AlarmData> findAlarmDataByQuery(SecurityUser securityUser, AlarmDataQuery query);
|
||||
|
||||
@ -92,7 +92,7 @@ public abstract class BaseEntityQueryControllerTest extends AbstractControllerTe
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountEntitiesByQuery() throws Exception {
|
||||
public void testTenantCountEntitiesByQuery() throws Exception {
|
||||
List<Device> devices = new ArrayList<>();
|
||||
for (int i = 0; i < 97; i++) {
|
||||
Device device = new Device();
|
||||
@ -139,6 +139,56 @@ public abstract class BaseEntityQueryControllerTest extends AbstractControllerTe
|
||||
Assert.assertEquals(97, count2.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSysAdminCountEntitiesByQuery() throws Exception {
|
||||
List<Device> devices = new ArrayList<>();
|
||||
for (int i = 0; i < 97; i++) {
|
||||
Device device = new Device();
|
||||
device.setName("Device" + i);
|
||||
device.setType("default");
|
||||
device.setLabel("testLabel" + (int) (Math.random() * 1000));
|
||||
devices.add(doPost("/api/device", device, Device.class));
|
||||
Thread.sleep(1);
|
||||
}
|
||||
DeviceTypeFilter filter = new DeviceTypeFilter();
|
||||
filter.setDeviceType("default");
|
||||
filter.setDeviceNameFilter("");
|
||||
|
||||
loginSysAdmin();
|
||||
|
||||
EntityCountQuery countQuery = new EntityCountQuery(filter);
|
||||
|
||||
Long count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
||||
Assert.assertEquals(97, count.longValue());
|
||||
|
||||
filter.setDeviceType("unknown");
|
||||
count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
||||
Assert.assertEquals(0, count.longValue());
|
||||
|
||||
filter.setDeviceType("default");
|
||||
filter.setDeviceNameFilter("Device1");
|
||||
|
||||
count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
||||
Assert.assertEquals(11, count.longValue());
|
||||
|
||||
EntityListFilter entityListFilter = new EntityListFilter();
|
||||
entityListFilter.setEntityType(EntityType.DEVICE);
|
||||
entityListFilter.setEntityList(devices.stream().map(Device::getId).map(DeviceId::toString).collect(Collectors.toList()));
|
||||
|
||||
countQuery = new EntityCountQuery(entityListFilter);
|
||||
|
||||
count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
||||
Assert.assertEquals(97, count.longValue());
|
||||
|
||||
EntityTypeFilter filter2 = new EntityTypeFilter();
|
||||
filter2.setEntityType(EntityType.DEVICE);
|
||||
|
||||
EntityCountQuery countQuery2 = new EntityCountQuery(filter2);
|
||||
|
||||
Long count2 = doPostWithResponse("/api/entitiesQuery/count", countQuery2, Long.class);
|
||||
Assert.assertEquals(97, count2.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleFindEntityDataByQuery() throws Exception {
|
||||
List<Device> devices = new ArrayList<>();
|
||||
|
||||
@ -15,18 +15,15 @@
|
||||
*/
|
||||
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.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.EntityId;
|
||||
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.query.EntityCountQuery;
|
||||
import org.thingsboard.server.common.data.query.EntityData;
|
||||
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EntityService {
|
||||
@ -41,7 +38,5 @@ public interface EntityService {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -19,17 +19,16 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
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.HasEmail;
|
||||
import org.thingsboard.server.common.data.HasLabel;
|
||||
import org.thingsboard.server.common.data.HasName;
|
||||
import org.thingsboard.server.common.data.HasTitle;
|
||||
import org.thingsboard.server.common.data.StringUtils;
|
||||
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.EntityId;
|
||||
import org.thingsboard.server.common.data.id.HasId;
|
||||
import org.thingsboard.server.common.data.id.NameLabelAndCustomerDetails;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.query.EntityCountQuery;
|
||||
@ -39,8 +38,6 @@ import org.thingsboard.server.common.data.query.EntityFilterType;
|
||||
import org.thingsboard.server.common.data.query.RelationsQueryFilter;
|
||||
import org.thingsboard.server.dao.exception.IncorrectParameterException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
@ -74,14 +71,6 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
|
||||
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
|
||||
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
|
||||
log.trace("Executing findEntityDataByQuery, tenantId [{}], customerId [{}], query [{}]", tenantId, customerId, query);
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
*/
|
||||
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.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
@ -23,15 +22,10 @@ import org.thingsboard.server.common.data.query.EntityCountQuery;
|
||||
import org.thingsboard.server.common.data.query.EntityData;
|
||||
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface EntityQueryDao {
|
||||
|
||||
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
|
||||
|
||||
PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query);
|
||||
|
||||
Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes);
|
||||
|
||||
}
|
||||
|
||||
@ -53,7 +53,6 @@ import org.thingsboard.server.common.data.query.SingleEntityFilter;
|
||||
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
|
||||
import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -312,12 +311,18 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
|
||||
@Override
|
||||
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) {
|
||||
EntityType entityType = resolveEntityType(query.getEntityFilter());
|
||||
QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType));
|
||||
QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType, TenantId.SYS_TENANT_ID.equals(tenantId)));
|
||||
if (query.getKeyFilters() == null || query.getKeyFilters().isEmpty()) {
|
||||
ctx.append("select count(e.id) from ");
|
||||
ctx.append(addEntityTableQuery(ctx, query.getEntityFilter()));
|
||||
ctx.append(" e where ");
|
||||
ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList()));
|
||||
//
|
||||
// if (TenantId.SYS_TENANT_ID.equals(tenantId)) {
|
||||
// ctx.append(" e");
|
||||
// } else {
|
||||
ctx.append(" e where ");
|
||||
ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList()));
|
||||
// }
|
||||
|
||||
return transactionTemplate.execute(status -> {
|
||||
long startTs = System.currentTimeMillis();
|
||||
try {
|
||||
@ -389,61 +394,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
|
||||
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) {
|
||||
return transactionTemplate.execute(status -> {
|
||||
EntityType entityType = resolveEntityType(query.getEntityFilter());
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
*/
|
||||
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.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
@ -23,9 +22,6 @@ import org.thingsboard.server.common.data.query.EntityCountQuery;
|
||||
import org.thingsboard.server.common.data.query.EntityData;
|
||||
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface EntityQueryRepository {
|
||||
|
||||
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);
|
||||
@ -34,6 +30,4 @@ public interface EntityQueryRepository {
|
||||
|
||||
PageData<EntityData> findEntityDataByQueryInternal(EntityDataQuery query);
|
||||
|
||||
Map<EntityType, Long> countEntitiesByTypes(TenantId tenantId, CustomerId customerId, List<EntityType> entityTypes);
|
||||
|
||||
}
|
||||
|
||||
@ -17,7 +17,6 @@ package org.thingsboard.server.dao.sql.query;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
@ -26,9 +25,6 @@ import org.thingsboard.server.common.data.query.EntityData;
|
||||
import org.thingsboard.server.common.data.query.EntityDataQuery;
|
||||
import org.thingsboard.server.dao.entity.EntityQueryDao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class JpaEntityQueryDao implements EntityQueryDao {
|
||||
|
||||
@ -45,8 +41,4 @@ public class JpaEntityQueryDao implements EntityQueryDao {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1584,13 +1584,6 @@ public class RestClient implements ClientHttpRequestInterceptor, Closeable {
|
||||
return restTemplate.postForObject(baseURL + "/api/entitiesQuery/count", query, Long.class);
|
||||
}
|
||||
|
||||
public Map<Long, EntityType> countEntitiesByTypes(List<EntityType> entityTypes) {
|
||||
return restTemplate.exchange(
|
||||
baseURL + "/api/entitiesTypes/count",
|
||||
HttpMethod.POST, new HttpEntity<>(entityTypes), new ParameterizedTypeReference<Map<Long, EntityType>>() {
|
||||
}).getBody();
|
||||
}
|
||||
|
||||
public PageData<EntityData> findEntityDataByQuery(EntityDataQuery query) {
|
||||
return restTemplate.exchange(
|
||||
baseURL + "/api/entitiesQuery/find",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user