diff --git a/application/src/main/java/org/thingsboard/server/controller/UsageInfoController.java b/application/src/main/java/org/thingsboard/server/controller/UsageInfoController.java new file mode 100644 index 0000000000..a7d4250b65 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/controller/UsageInfoController.java @@ -0,0 +1,45 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.thingsboard.server.common.data.UsageInfo; +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.dao.usage.UsageInfoService; +import org.thingsboard.server.queue.util.TbCoreComponent; + +@RestController +@TbCoreComponent +@RequestMapping("/api") +@Slf4j +public class UsageInfoController extends BaseController { + + @Autowired + private UsageInfoService usageInfoService; + + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/usage", method = RequestMethod.GET) + @ResponseBody + public UsageInfo getTenantUsageInfo() throws ThingsboardException { + return checkNotNull(usageInfoService.getUsageInfo(getCurrentUser().getTenantId())); + } +} diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index e040e0bc5e..f745e03ffc 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -477,7 +477,9 @@ cache: dashboardTitles: timeToLiveInMinutes: "${CACHE_SPECS_DASHBOARD_TITLES_TTL:1440}" maxSize: "${CACHE_SPECS_DASHBOARD_TITLES_MAX_SIZE:100000}" - + entityCount: + timeToLiveInMinutes: "${CACHE_SPECS_ENTITY_COUNT_TTL:1440}" + maxSize: "${CACHE_SPECS_ENTITY_COUNT_MAX_SIZE:100000}" #Disable this because it is not required. spring.data.redis.repositories.enabled: false diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseHomePageApiTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseHomePageApiTest.java index b4742bdd77..94ffad30fe 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseHomePageApiTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseHomePageApiTest.java @@ -26,11 +26,13 @@ import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.ApiUsageState; import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.FeaturesInfo; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantProfile; +import org.thingsboard.server.common.data.UsageInfo; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.id.TenantId; @@ -49,7 +51,9 @@ import org.thingsboard.server.common.data.query.EntityData; import org.thingsboard.server.common.data.query.EntityTypeFilter; import org.thingsboard.server.common.data.query.TsValue; import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; import org.thingsboard.server.common.stats.TbApiUsageStateClient; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityCountCmd; import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityCountUpdate; import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityDataUpdate; @@ -69,6 +73,9 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest { @Autowired private TbApiUsageStateClient apiUsageStateClient; + @Autowired + private TbTenantProfileCache tenantProfileCache; + //For system administrator @Test public void testTenantsCountWsCmd() throws Exception { @@ -330,6 +337,100 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest { Assert.assertTrue(featuresInfo.isOauthEnabled()); } + @Test + public void testUsageInfo() throws Exception { + loginTenantAdmin(); + + TenantProfile tenantProfile = tenantProfileCache.get(tenantId); + + Assert.assertNotNull(tenantProfile); + + DefaultTenantProfileConfiguration configuration = (DefaultTenantProfileConfiguration) tenantProfile.getProfileData().getConfiguration(); + + UsageInfo usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertNotNull(usageInfo); + Assert.assertEquals(0, usageInfo.getDevices()); + Assert.assertEquals(configuration.getMaxDevices(), usageInfo.getMaxDevices()); + + Assert.assertEquals(0, usageInfo.getAssets()); + Assert.assertEquals(configuration.getMaxAssets(), usageInfo.getMaxAssets()); + + Assert.assertEquals(1, usageInfo.getCustomers()); + Assert.assertEquals(configuration.getMaxCustomers(), usageInfo.getMaxCustomers()); + + Assert.assertEquals(2, usageInfo.getUsers()); + Assert.assertEquals(configuration.getMaxUsers(), usageInfo.getMaxUsers()); + + Assert.assertEquals(0, usageInfo.getDashboards()); + Assert.assertEquals(configuration.getMaxDashboards(), usageInfo.getMaxDashboards()); + + Assert.assertEquals(0, usageInfo.getTransportMessages()); + Assert.assertEquals(configuration.getMaxTransportMessages(), usageInfo.getMaxTransportMessages()); + + Assert.assertEquals(0, usageInfo.getJsExecutions()); + Assert.assertEquals(configuration.getMaxJSExecutions(), usageInfo.getMaxJsExecutions()); + + Assert.assertEquals(0, usageInfo.getEmails()); + Assert.assertEquals(configuration.getMaxEmails(), usageInfo.getMaxEmails()); + + Assert.assertEquals(0, usageInfo.getSms()); + Assert.assertEquals(configuration.getMaxSms(), usageInfo.getMaxSms()); + + Assert.assertEquals(0, usageInfo.getAlarms()); + Assert.assertEquals(configuration.getMaxCreatedAlarms(), usageInfo.getMaxAlarms()); + + List devices = new ArrayList<>(); + for (int i = 0; i < 97; i++) { + Device device = new Device(); + device.setName("device" + i); + devices.add(doPost("/api/device", device, Device.class)); + } + + usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertEquals(devices.size(), usageInfo.getDevices()); + + List assets = new ArrayList<>(); + for (int i = 0; i < 97; i++) { + Asset asset = new Asset(); + asset.setName("asset" + i); + assets.add(doPost("/api/asset", asset, Asset.class)); + } + + usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertEquals(assets.size(), usageInfo.getAssets()); + + List customers = new ArrayList<>(); + for (int i = 0; i < 97; i++) { + Customer customer = new Customer(); + customer.setTitle("customer" + i); + customers.add(doPost("/api/customer", customer, Customer.class)); + } + + usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertEquals(customers.size() + 1, usageInfo.getCustomers()); + + List users = new ArrayList<>(); + for (int i = 0; i < 97; i++) { + User user = new User(); + user.setAuthority(Authority.TENANT_ADMIN); + user.setEmail(i + "user@thingsboard.org"); + users.add(doPost("/api/user", user, User.class)); + } + + usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertEquals(users.size() + 2, usageInfo.getUsers()); + + List dashboards = new ArrayList<>(); + for (int i = 0; i < 97; i++) { + Dashboard dashboard = new Dashboard(); + dashboard.setTitle("dashboard" + i); + dashboards.add(doPost("/api/dashboard", dashboard, Dashboard.class)); + } + + usageInfo = doGet("/api/usage", UsageInfo.class); + Assert.assertEquals(dashboards.size(), usageInfo.getDashboards()); + } + private OAuth2Info createDefaultOAuth2Info() { return new OAuth2Info(true, Lists.newArrayList( OAuth2ParamsInfo.builder() diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java index 451efafe84..4383ccc0a7 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceService.java @@ -17,11 +17,11 @@ package org.thingsboard.server.dao.device; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.data.DeviceIdInfo; import org.thingsboard.server.common.data.DeviceInfo; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.EntitySubtype; -import org.thingsboard.server.common.data.DeviceIdInfo; import org.thingsboard.server.common.data.device.DeviceSearchQuery; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; @@ -118,5 +118,4 @@ public interface DeviceService extends EntityDaoService { PageData findDevicesByTenantIdAndEdgeIdAndType(TenantId tenantId, EdgeId edgeId, String type, PageLink pageLink); - long countByTenantId(TenantId tenantId); } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityCountService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityCountService.java new file mode 100644 index 0000000000..955f658a8d --- /dev/null +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityCountService.java @@ -0,0 +1,26 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entity; + +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.TenantId; + +public interface EntityCountService { + + long countByTenantIdAndEntityType(TenantId tenantId, EntityType entityType); + + void publishCountEntityEvictEvent(TenantId tenantId, EntityType entityType); +} diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityDaoService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityDaoService.java index 23b7e8b91c..e1b8bec633 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityDaoService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityDaoService.java @@ -26,6 +26,10 @@ public interface EntityDaoService { Optional> findEntity(TenantId tenantId, EntityId entityId); + default long countByTenantId(TenantId tenantId) { + throw new IllegalArgumentException("Not implemented for " + getEntityType()); + } + EntityType getEntityType(); } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/usage/UsageInfoService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/usage/UsageInfoService.java new file mode 100644 index 0000000000..01e90b86e5 --- /dev/null +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/usage/UsageInfoService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.usage; + +import org.thingsboard.server.common.data.UsageInfo; +import org.thingsboard.server.common.data.id.TenantId; + +public interface UsageInfoService { + + UsageInfo getUsageInfo(TenantId tenantId); + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java index b09d92bd19..df2375fd03 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java @@ -43,4 +43,5 @@ public class CacheConstants { public static final String VERSION_CONTROL_TASK_CACHE = "versionControlTask"; public static final String USER_SETTINGS_CACHE = "userSettings"; public static final String DASHBOARD_TITLES_CACHE = "dashboardTitles"; + public static final String ENTITY_COUNT_CACHE = "entityCount"; } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/UsageInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/UsageInfo.java new file mode 100644 index 0000000000..297e050011 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/UsageInfo.java @@ -0,0 +1,43 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data; + +import lombok.Data; + +@Data +public class UsageInfo { + private long devices; + private long maxDevices; + private long assets; + private long maxAssets; + private long customers; + private long maxCustomers; + private long users; + private long maxUsers; + private long dashboards; + private long maxDashboards; + + private long transportMessages; + private long maxTransportMessages; + private long jsExecutions; + private long maxJsExecutions; + private long emails; + private long maxEmails; + private long sms; + private long maxSms; + private long alarms; + private long maxAlarms; +} \ No newline at end of file diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java index 97dedb717b..38f030e9ec 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java @@ -46,6 +46,7 @@ import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.RelationTypeGroup; import org.thingsboard.server.dao.entity.AbstractCachedEntityService; +import org.thingsboard.server.dao.entity.EntityCountService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.PaginatedRemover; @@ -83,6 +84,9 @@ public class BaseAssetService extends AbstractCachedEntityService assetValidator; + @Autowired + private EntityCountService countService; + @TransactionalEventListener(classes = AssetCacheEvictEvent.class) @Override public void handleEvictEvent(AssetCacheEvictEvent event) { @@ -151,6 +155,9 @@ public class BaseAssetService extends AbstractCachedEntityService customerValidator; + @Autowired + private EntityCountService countService; + @Override public Customer findCustomerById(TenantId tenantId, CustomerId customerId) { log.trace("Executing findCustomerById [{}]", customerId); @@ -105,6 +109,9 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom try { Customer savedCustomer = customerDao.save(customer.getTenantId(), customer); dashboardService.updateCustomerDashboards(savedCustomer.getTenantId(), savedCustomer.getId()); + if (customer.getId() == null) { + countService.publishCountEntityEvictEvent(savedCustomer.getTenantId(), EntityType.CUSTOMER); + } return savedCustomer; } catch (Exception e) { checkConstraintViolation(e, "customer_external_id_unq_key", "Customer with such external id already exists!"); @@ -131,6 +138,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom deleteEntityRelations(tenantId, customerId); apiUsageStateService.deleteApiUsageStateByEntityId(customerId); customerDao.removeById(tenantId, customerId.getId()); + countService.publishCountEntityEvictEvent(tenantId, EntityType.CUSTOMER); } @Override @@ -149,7 +157,9 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom } catch (IOException e) { throw new IncorrectParameterException("Unable to create public customer.", e); } - return customerDao.save(tenantId, publicCustomer); + Customer savedCustomer = customerDao.save(tenantId, publicCustomer); + countService.publishCountEntityEvictEvent(tenantId, EntityType.CUSTOMER); + return savedCustomer; } } @@ -187,6 +197,11 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom return Optional.ofNullable(findCustomerById(tenantId, new CustomerId(entityId.getId()))); } + @Override + public long countByTenantId(TenantId tenantId) { + return customerDao.countByTenantId(tenantId); + } + @Override public EntityType getEntityType() { return EntityType.CUSTOMER; diff --git a/dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java index d1198d4d7a..4e9b1700b7 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java @@ -16,6 +16,7 @@ package org.thingsboard.server.dao.dashboard; import com.google.common.util.concurrent.ListenableFuture; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; @@ -44,6 +45,7 @@ import org.thingsboard.server.common.data.settings.UserSettingsCompositeKey; import org.thingsboard.server.dao.customer.CustomerDao; import org.thingsboard.server.dao.edge.EdgeDao; import org.thingsboard.server.dao.entity.AbstractEntityService; +import org.thingsboard.server.dao.entity.EntityCountService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.PaginatedRemover; @@ -57,6 +59,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId; @Service("DashboardDaoService") @Slf4j +@RequiredArgsConstructor public class DashboardServiceImpl extends AbstractEntityService implements DashboardService { public static final String INCORRECT_DASHBOARD_ID = "Incorrect dashboardId "; @@ -79,6 +82,9 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb @Autowired protected TbTransactionalCache cache; + @Autowired + private EntityCountService countService; + @Autowired private ApplicationEventPublisher eventPublisher; @@ -136,6 +142,9 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb try { var saved = dashboardDao.save(dashboard.getTenantId(), dashboard); publishEvictEvent(new DashboardTitleEvictEvent(saved.getId())); + if (dashboard.getId() == null) { + countService.publishCountEntityEvictEvent(saved.getTenantId(), EntityType.DASHBOARD); + } return saved; } catch (Exception e) { if (dashboard.getId() != null) { @@ -207,6 +216,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb try { dashboardDao.removeById(tenantId, dashboardId.getId()); publishEvictEvent(new DashboardTitleEvictEvent(dashboardId)); + countService.publishCountEntityEvictEvent(tenantId, EntityType.DASHBOARD); } catch (Exception t) { ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_default_dashboard_device_profile")) { @@ -353,6 +363,11 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb return Optional.ofNullable(findDashboardById(tenantId, new DashboardId(entityId.getId()))); } + @Override + public long countByTenantId(TenantId tenantId) { + return dashboardDao.countByTenantId(tenantId); + } + @Override public EntityType getEntityType() { return EntityType.DASHBOARD; diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index e5dcc810a6..a32f93d801 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -67,6 +67,7 @@ import org.thingsboard.server.dao.device.provision.ProvisionFailedException; import org.thingsboard.server.dao.device.provision.ProvisionRequest; import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus; import org.thingsboard.server.dao.entity.AbstractCachedEntityService; +import org.thingsboard.server.dao.entity.EntityCountService; import org.thingsboard.server.dao.event.EventService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.service.DataValidator; @@ -113,6 +114,9 @@ public class DeviceServiceImpl extends AbstractCachedEntityService deviceValidator; + @Autowired + private EntityCountService countService; + @Override public DeviceInfo findDeviceInfoById(TenantId tenantId, DeviceId deviceId) { log.trace("Executing findDeviceInfoById [{}]", deviceId); @@ -235,6 +239,9 @@ public class DeviceServiceImpl extends AbstractCachedEntityService findDevicesByTenantId(TenantId tenantId, PageLink pageLink) { log.trace("Executing findDevicesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); @@ -611,6 +618,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService implements EntityCountService { + + @Lazy + @Autowired + private EntityServiceRegistry entityServiceRegistry; + + @Override + public long countByTenantIdAndEntityType(TenantId tenantId, EntityType entityType) { + return cache.getAndPutInTransaction(new EntityCountCacheKey(tenantId, entityType), + () -> entityServiceRegistry.getServiceByEntityType(entityType).countByTenantId(tenantId), false); + } + + @Override + public void publishCountEntityEvictEvent(TenantId tenantId, EntityType entityType) { + publishEvictEvent(new EntityCountCacheEvictEvent(tenantId, entityType)); + } + + @TransactionalEventListener(classes = EntityCountCacheEvictEvent.class) + @Override + public void handleEvictEvent(EntityCountCacheEvictEvent event) { + cache.evict(new EntityCountCacheKey(event.getTenantId(), event.getEntityType())); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheEvictEvent.java b/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheEvictEvent.java new file mode 100644 index 0000000000..4d743500e2 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheEvictEvent.java @@ -0,0 +1,28 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entity; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.TenantId; + +@Data +@RequiredArgsConstructor +class EntityCountCacheEvictEvent { + private final TenantId tenantId; + private final EntityType entityType; +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheKey.java b/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheKey.java new file mode 100644 index 0000000000..aac00b3cb8 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/EntityCountCacheKey.java @@ -0,0 +1,41 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entity; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.TenantId; + +import java.io.Serializable; + +@Getter +@EqualsAndHashCode +@RequiredArgsConstructor +public class EntityCountCacheKey implements Serializable { + + private static final long serialVersionUID = -1992105662738434178L; + + private final TenantId tenantId; + private final EntityType entityType; + + @Override + public String toString() { + return tenantId + "_" + entityType; + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountCaffeineCache.java b/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountCaffeineCache.java new file mode 100644 index 0000000000..1d91af4e98 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountCaffeineCache.java @@ -0,0 +1,33 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entity.count; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; +import org.thingsboard.server.cache.CaffeineTbTransactionalCache; +import org.thingsboard.server.common.data.CacheConstants; +import org.thingsboard.server.dao.entity.EntityCountCacheKey; + +@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) +@Service("EntityCountCache") +public class EntityCountCaffeineCache extends CaffeineTbTransactionalCache { + + public EntityCountCaffeineCache(CacheManager cacheManager) { + super(cacheManager, CacheConstants.ENTITY_COUNT_CACHE); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountRedisCache.java new file mode 100644 index 0000000000..af431f3085 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/count/EntityCountRedisCache.java @@ -0,0 +1,35 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.entity.count; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.stereotype.Service; +import org.thingsboard.server.cache.CacheSpecsMap; +import org.thingsboard.server.cache.RedisTbTransactionalCache; +import org.thingsboard.server.cache.TBRedisCacheConfiguration; +import org.thingsboard.server.cache.TbFSTRedisSerializer; +import org.thingsboard.server.common.data.CacheConstants; +import org.thingsboard.server.dao.entity.EntityCountCacheKey; + +@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") +@Service("EntityCountCache") +public class EntityCountRedisCache extends RedisTbTransactionalCache { + + public EntityCountRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { + super(CacheConstants.ENTITY_COUNT_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbFSTRedisSerializer<>()); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/usage/BasicUsageInfoService.java b/dao/src/main/java/org/thingsboard/server/dao/usage/BasicUsageInfoService.java new file mode 100644 index 0000000000..cf540a205d --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/usage/BasicUsageInfoService.java @@ -0,0 +1,98 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.usage; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.ApiUsageRecordKey; +import org.thingsboard.server.common.data.ApiUsageState; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.UsageInfo; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.kv.TsKvEntry; +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; +import org.thingsboard.server.dao.entity.EntityCountService; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; +import org.thingsboard.server.dao.timeseries.TimeseriesService; +import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class BasicUsageInfoService implements UsageInfoService { + + private final EntityCountService countService; + private final ApiUsageStateService apiUsageStateService; + private final TimeseriesService tsService; + @Lazy + private final TbTenantProfileCache tenantProfileCache; + + @Override + public UsageInfo getUsageInfo(TenantId tenantId) { + DefaultTenantProfileConfiguration profileConfiguration = + (DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration(); + UsageInfo usageInfo = new UsageInfo(); + usageInfo.setDevices(countService.countByTenantIdAndEntityType(tenantId, EntityType.DEVICE)); + usageInfo.setMaxDevices(profileConfiguration.getMaxDevices()); + usageInfo.setAssets(countService.countByTenantIdAndEntityType(tenantId, EntityType.ASSET)); + usageInfo.setMaxAssets(profileConfiguration.getMaxAssets()); + usageInfo.setCustomers(countService.countByTenantIdAndEntityType(tenantId, EntityType.CUSTOMER)); + usageInfo.setMaxCustomers(profileConfiguration.getMaxCustomers()); + usageInfo.setUsers(countService.countByTenantIdAndEntityType(tenantId, EntityType.USER)); + usageInfo.setMaxUsers(profileConfiguration.getMaxUsers()); + usageInfo.setDashboards(countService.countByTenantIdAndEntityType(tenantId, EntityType.DASHBOARD)); + usageInfo.setMaxDashboards(profileConfiguration.getMaxDashboards()); + + usageInfo.setMaxAlarms(profileConfiguration.getMaxCreatedAlarms()); + usageInfo.setMaxTransportMessages(profileConfiguration.getMaxTransportMessages()); + usageInfo.setMaxJsExecutions(profileConfiguration.getMaxJSExecutions()); + usageInfo.setMaxEmails(profileConfiguration.getMaxEmails()); + usageInfo.setMaxSms(profileConfiguration.getMaxSms()); + ApiUsageState apiUsageState = apiUsageStateService.findTenantApiUsageState(tenantId); + if (apiUsageState != null) { + Collection keys = Arrays.asList( + ApiUsageRecordKey.TRANSPORT_MSG_COUNT.getApiCountKey(), + ApiUsageRecordKey.JS_EXEC_COUNT.getApiCountKey(), + ApiUsageRecordKey.EMAIL_EXEC_COUNT.getApiCountKey(), + ApiUsageRecordKey.SMS_EXEC_COUNT.getApiCountKey(), + ApiUsageRecordKey.CREATED_ALARMS_COUNT.getApiCountKey()); + try { + List entries = tsService.findLatest(tenantId, apiUsageState.getId(), keys).get(); + usageInfo.setTransportMessages(getLongValueFromTsEntries(entries, ApiUsageRecordKey.TRANSPORT_MSG_COUNT.getApiCountKey())); + usageInfo.setJsExecutions(getLongValueFromTsEntries(entries, ApiUsageRecordKey.JS_EXEC_COUNT.getApiCountKey())); + usageInfo.setEmails(getLongValueFromTsEntries(entries, ApiUsageRecordKey.EMAIL_EXEC_COUNT.getApiCountKey())); + usageInfo.setSms(getLongValueFromTsEntries(entries, ApiUsageRecordKey.SMS_EXEC_COUNT.getApiCountKey())); + usageInfo.setAlarms(getLongValueFromTsEntries(entries, ApiUsageRecordKey.CREATED_ALARMS_COUNT.getApiCountKey())); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException("Failed to fetch api usage values from timeseries!"); + } + } + return usageInfo; + } + + private long getLongValueFromTsEntries(List entries, String key) { + Optional entryOpt = entries.stream().filter(e -> e.getKey().equals(key)).findFirst(); + return entryOpt.map(entry -> entry.getLongValue().orElse(0L)).orElse(0L); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java index b9d7422ef0..e3d30c8b01 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java @@ -43,6 +43,7 @@ import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.UserCredentials; import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent; import org.thingsboard.server.dao.entity.AbstractEntityService; +import org.thingsboard.server.dao.entity.EntityCountService; import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.PaginatedRemover; @@ -79,10 +80,10 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic private final UserDao userDao; private final UserCredentialsDao userCredentialsDao; private final UserAuthSettingsDao userAuthSettingsDao; - private final UserSettingsDao userSettingsDao; private final DataValidator userValidator; private final DataValidator userCredentialsValidator; private final ApplicationEventPublisher eventPublisher; + private final EntityCountService countService; @Override public User findUserByEmail(TenantId tenantId, String email) { @@ -126,6 +127,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic } User savedUser = userDao.save(user.getTenantId(), user); if (user.getId() == null) { + countService.publishCountEntityEvictEvent(savedUser.getTenantId(), EntityType.USER); UserCredentials userCredentials = new UserCredentials(); userCredentials.setEnabled(false); userCredentials.setActivateToken(generateSafeToken(DEFAULT_TOKEN_LENGTH)); @@ -234,6 +236,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic deleteEntityRelations(tenantId, userId); userDao.removeById(tenantId, userId.getId()); eventPublisher.publishEvent(new UserCredentialsInvalidationEvent(userId)); + countService.publishCountEntityEvictEvent(tenantId, EntityType.USER); } @Override @@ -431,6 +434,11 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic return Optional.ofNullable(findUserById(tenantId, new UserId(entityId.getId()))); } + @Override + public long countByTenantId(TenantId tenantId) { + return userDao.countByTenantId(tenantId); + } + @Override public EntityType getEntityType() { return EntityType.USER; diff --git a/dao/src/test/resources/application-test.properties b/dao/src/test/resources/application-test.properties index 2d3b5197ae..b55fba1f3b 100644 --- a/dao/src/test/resources/application-test.properties +++ b/dao/src/test/resources/application-test.properties @@ -71,6 +71,9 @@ cache.specs.notificationRules.maxSize=10000 cache.specs.dashboardTitles.timeToLiveInMinutes=1440 cache.specs.dashboardTitles.maxSize=10000 +cache.specs.entityCount.timeToLiveInMinutes=1440 +cache.specs.entityCount.maxSize=10000 + redis.connection.host=localhost redis.connection.port=6379 redis.connection.db=0 diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index f0a002714a..87dd868138 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -63,6 +63,7 @@ import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantInfo; import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.UpdateMessage; +import org.thingsboard.server.common.data.UsageInfo; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmComment; @@ -2472,6 +2473,14 @@ public class RestClient implements Closeable { }, params).getBody(); } + public UsageInfo getUsageInfo() { + return restTemplate.exchange( + baseURL + "/api/usage", + HttpMethod.GET, + HttpEntity.EMPTY, + UsageInfo.class).getBody(); + } + public Optional getTenantProfileById(TenantProfileId tenantProfileId) { try { ResponseEntity tenantProfile = restTemplate.getForEntity(baseURL + "/api/tenantProfile/{tenantProfileId}", TenantProfile.class, tenantProfileId);