diff --git a/application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java b/application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java index f845634aa0..1c96468d72 100644 --- a/application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java +++ b/application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java @@ -24,13 +24,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; -import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.rule.engine.api.MailService; import org.thingsboard.server.cluster.TbClusterService; import org.thingsboard.server.common.data.ApiFeature; import org.thingsboard.server.common.data.ApiUsageRecordKey; +import org.thingsboard.server.common.data.ApiUsageRecordState; import org.thingsboard.server.common.data.ApiUsageState; -import org.thingsboard.server.common.data.ApiUsageStateMailMessage; import org.thingsboard.server.common.data.ApiUsageStateValue; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.StringUtils; @@ -54,6 +53,7 @@ import org.thingsboard.server.common.msg.queue.TbCallback; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; import org.thingsboard.server.common.msg.tools.SchedulerUtils; import org.thingsboard.server.dao.notification.NotificationRuleProcessingService; +import org.thingsboard.server.dao.notification.trigger.ApiUsageLimitTrigger; import org.thingsboard.server.dao.tenant.TbTenantProfileCache; import org.thingsboard.server.dao.tenant.TenantService; import org.thingsboard.server.dao.timeseries.TimeseriesService; @@ -63,6 +63,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto; import org.thingsboard.server.queue.common.TbProtoQueueMsg; import org.thingsboard.server.queue.discovery.PartitionService; import org.thingsboard.server.service.executors.DbCallbackExecutorService; +import org.thingsboard.server.service.mail.MailExecutorService; import org.thingsboard.server.service.partition.AbstractPartitionBasedService; import org.thingsboard.server.service.telemetry.InternalTelemetryService; @@ -79,8 +80,6 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -110,6 +109,7 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService private final MailService mailService; private final NotificationRuleProcessingService notificationRuleProcessingService; private final DbCallbackExecutorService dbExecutor; + private final MailExecutorService mailExecutor; @Lazy @Autowired @@ -130,8 +130,6 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService private final Lock updateLock = new ReentrantLock(); - private final ExecutorService mailExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("api-usage-svc-mail")); - @PostConstruct public void init() { super.init(); @@ -340,32 +338,35 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService tsWsService.saveAndNotifyInternal(state.getTenantId(), state.getApiUsageState().getId(), stateTelemetry, VOID_CALLBACK); if (state.getEntityType() == EntityType.TENANT && !state.getEntityId().equals(TenantId.SYS_TENANT_ID)) { - String email = tenantService.findTenantById(state.getTenantId()).getEmail(); - if (StringUtils.isNotEmpty(email)) { - result.forEach((apiFeature, stateValue) -> { + result.forEach((apiFeature, stateValue) -> { + ApiUsageRecordState recordState = createApiUsageRecordState((TenantApiUsageState) state, apiFeature, stateValue); + notificationRuleProcessingService.process(ApiUsageLimitTrigger.builder() + .tenantId(state.getTenantId()) + .state(recordState) + .status(stateValue) + .build()); + if (StringUtils.isNotEmpty(email)) { mailExecutor.submit(() -> { try { - mailService.sendApiFeatureStateEmail(apiFeature, stateValue, email, createStateMailMessage((TenantApiUsageState) state, apiFeature, stateValue)); + mailService.sendApiFeatureStateEmail(apiFeature, stateValue, email, recordState); } catch (ThingsboardException e) { log.warn("[{}] Can't send update of the API state to tenant with provided email [{}]", state.getTenantId(), email, e); } }); - }); - } else { - log.warn("[{}] Can't send update of the API state to tenant with empty email!", state.getTenantId()); - } + } + }); } } - private ApiUsageStateMailMessage createStateMailMessage(TenantApiUsageState state, ApiFeature apiFeature, ApiUsageStateValue stateValue) { + private ApiUsageRecordState createApiUsageRecordState(TenantApiUsageState state, ApiFeature apiFeature, ApiUsageStateValue stateValue) { StateChecker checker = getStateChecker(stateValue); for (ApiUsageRecordKey apiUsageRecordKey : ApiUsageRecordKey.getKeys(apiFeature)) { long threshold = state.getProfileThreshold(apiUsageRecordKey); long warnThreshold = state.getProfileWarnThreshold(apiUsageRecordKey); long value = state.get(apiUsageRecordKey); if (checker.check(threshold, warnThreshold, value)) { - return new ApiUsageStateMailMessage(apiUsageRecordKey, threshold, value); + return new ApiUsageRecordState(apiFeature, apiUsageRecordKey, threshold, value); } } return null; @@ -377,7 +378,7 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService } else if (ApiUsageStateValue.WARNING.equals(stateValue)) { return (t, wt, v) -> v < t && v >= wt; } else { - return (t, wt, v) -> v >= t; + return (t, wt, v) -> t > 0 && v >= t; } } @@ -529,8 +530,5 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService @PreDestroy private void destroy() { super.stop(); - if (mailExecutor != null) { - mailExecutor.shutdownNow(); - } } } diff --git a/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java b/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java index a9e77901d5..55eae83af4 100644 --- a/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java +++ b/application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java @@ -35,7 +35,7 @@ import org.thingsboard.rule.engine.api.TbEmail; import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.ApiFeature; import org.thingsboard.server.common.data.ApiUsageRecordKey; -import org.thingsboard.server.common.data.ApiUsageStateMailMessage; +import org.thingsboard.server.common.data.ApiUsageRecordState; import org.thingsboard.server.common.data.ApiUsageStateValue; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; @@ -64,8 +64,6 @@ public class DefaultMailService implements MailService { public static final String MAIL_PROP = "mail."; public static final String TARGET_EMAIL = "targetEmail"; public static final String UTF_8 = "UTF-8"; - public static final int _10K = 10000; - public static final int _1M = 1000000; private final MessageSource messages; private final Configuration freemarkerConfig; @@ -335,7 +333,7 @@ public class DefaultMailService implements MailService { } @Override - public void sendApiFeatureStateEmail(ApiFeature apiFeature, ApiUsageStateValue stateValue, String email, ApiUsageStateMailMessage msg) throws ThingsboardException { + public void sendApiFeatureStateEmail(ApiFeature apiFeature, ApiUsageStateValue stateValue, String email, ApiUsageRecordState recordState) throws ThingsboardException { String subject = messages.getMessage("api.usage.state", null, Locale.US); Map model = new HashMap<>(); @@ -350,11 +348,11 @@ public class DefaultMailService implements MailService { message = mergeTemplateIntoString("state.enabled.ftl", model); break; case WARNING: - model.put("apiValueLabel", toDisabledValueLabel(apiFeature) + " " + toWarningValueLabel(msg.getKey(), msg.getValue(), msg.getThreshold())); + model.put("apiValueLabel", toDisabledValueLabel(apiFeature) + " " + toWarningValueLabel(recordState)); message = mergeTemplateIntoString("state.warning.ftl", model); break; case DISABLED: - model.put("apiLimitValueLabel", toDisabledValueLabel(apiFeature) + " " + toDisabledValueLabel(msg.getKey(), msg.getThreshold())); + model.put("apiLimitValueLabel", toDisabledValueLabel(apiFeature) + " " + toDisabledValueLabel(recordState)); message = mergeTemplateIntoString("state.disabled.ftl", model); break; } @@ -406,10 +404,10 @@ public class DefaultMailService implements MailService { } } - private String toWarningValueLabel(ApiUsageRecordKey key, long value, long threshold) { - String valueInM = getValueAsString(value); - String thresholdInM = getValueAsString(threshold); - switch (key) { + private String toWarningValueLabel(ApiUsageRecordState recordState) { + String valueInM = recordState.getValueAsString(); + String thresholdInM = recordState.getThresholdAsString(); + switch (recordState.getKey()) { case STORAGE_DP_COUNT: case TRANSPORT_DP_COUNT: return valueInM + " out of " + thresholdInM + " allowed data points"; @@ -428,36 +426,26 @@ public class DefaultMailService implements MailService { } } - private String toDisabledValueLabel(ApiUsageRecordKey key, long value) { - switch (key) { + private String toDisabledValueLabel(ApiUsageRecordState recordState) { + switch (recordState.getKey()) { case STORAGE_DP_COUNT: case TRANSPORT_DP_COUNT: - return getValueAsString(value) + " data points"; + return recordState.getValueAsString() + " data points"; case TRANSPORT_MSG_COUNT: - return getValueAsString(value) + " messages"; + return recordState.getValueAsString() + " messages"; case JS_EXEC_COUNT: - return "JavaScript functions " + getValueAsString(value) + " times"; + return "JavaScript functions " + recordState.getValueAsString() + " times"; case RE_EXEC_COUNT: - return getValueAsString(value) + " Rule Engine messages"; + return recordState.getValueAsString() + " Rule Engine messages"; case EMAIL_EXEC_COUNT: - return getValueAsString(value) + " Email messages"; + return recordState.getValueAsString() + " Email messages"; case SMS_EXEC_COUNT: - return getValueAsString(value) + " SMS messages"; + return recordState.getValueAsString() + " SMS messages"; default: throw new RuntimeException("Not implemented!"); } } - private String getValueAsString(long value) { - if (value > _1M && value % _1M < _10K) { - return value / _1M + "M"; - } else if (value > _10K) { - return String.format("%.2fM", ((double) value) / 1000000); - } else { - return value + ""; - } - } - private void sendMail(JavaMailSenderImpl mailSender, String mailFrom, String email, String subject, String message, long timeout) throws ThingsboardException { try { diff --git a/application/src/main/java/org/thingsboard/server/service/notification/rule/DefaultNotificationRuleProcessingService.java b/application/src/main/java/org/thingsboard/server/service/notification/rule/DefaultNotificationRuleProcessingService.java index 58c93bd189..69b94a4ceb 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/rule/DefaultNotificationRuleProcessingService.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/rule/DefaultNotificationRuleProcessingService.java @@ -156,7 +156,7 @@ public class DefaultNotificationRuleProcessingService implements NotificationRul log.debug("Submitting notification request for rule '{}' with delay of {} sec to targets {}", rule.getName(), delayInSec, targets); notificationCenter.processNotificationRequest(rule.getTenantId(), notificationRequest); } catch (Exception e) { - log.error("Failed to process notification request for rule {}", rule.getId(), e); + log.error("Failed to process notification request for tenant {} for rule {}", rule.getTenantId(), rule.getId(), e); } }); } diff --git a/application/src/main/java/org/thingsboard/server/service/notification/rule/trigger/ApiUsageLimitTriggerProcessor.java b/application/src/main/java/org/thingsboard/server/service/notification/rule/trigger/ApiUsageLimitTriggerProcessor.java new file mode 100644 index 0000000000..b9c1d7b2d0 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/notification/rule/trigger/ApiUsageLimitTriggerProcessor.java @@ -0,0 +1,59 @@ +/** + * 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.service.notification.rule.trigger; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.notification.info.ApiUsageLimitNotificationInfo; +import org.thingsboard.server.common.data.notification.info.RuleOriginatedNotificationInfo; +import org.thingsboard.server.common.data.notification.rule.trigger.ApiUsageLimitNotificationRuleTriggerConfig; +import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; +import org.thingsboard.server.dao.notification.trigger.ApiUsageLimitTrigger; +import org.thingsboard.server.dao.tenant.TenantService; + +import static org.apache.commons.collections.CollectionUtils.isEmpty; + +@Service +@RequiredArgsConstructor +public class ApiUsageLimitTriggerProcessor implements NotificationRuleTriggerProcessor { + + private final TenantService tenantService; + + @Override + public boolean matchesFilter(ApiUsageLimitTrigger trigger, ApiUsageLimitNotificationRuleTriggerConfig triggerConfig) { + return (isEmpty(triggerConfig.getApiFeatures()) || triggerConfig.getApiFeatures().contains(trigger.getState().getApiFeature())) && + (isEmpty(triggerConfig.getNotifyOn()) || triggerConfig.getNotifyOn().contains(trigger.getStatus())); + } + + @Override + public RuleOriginatedNotificationInfo constructNotificationInfo(ApiUsageLimitTrigger trigger) { + return ApiUsageLimitNotificationInfo.builder() + .feature(trigger.getState().getApiFeature()) + .recordKey(trigger.getState().getKey()) + .status(trigger.getStatus()) + .limit(trigger.getState().getThresholdAsString()) + .currentValue(trigger.getState().getValueAsString()) + .tenantId(trigger.getTenantId()) + .tenantName(tenantService.findTenantById(trigger.getTenantId()).getName()) + .build(); + } + + @Override + public NotificationRuleTriggerType getTriggerType() { + return NotificationRuleTriggerType.API_USAGE_LIMIT; + } + +} diff --git a/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java b/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java index f748dea86e..3e12cb6d05 100644 --- a/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java +++ b/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java @@ -132,7 +132,7 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { notificationRule.setTriggerType(NotificationRuleTriggerType.ENTITY_ACTION); EntityActionNotificationRuleTriggerConfig triggerConfig = new EntityActionNotificationRuleTriggerConfig(); - triggerConfig.setEntityType(EntityType.DEVICE); + triggerConfig.setEntityTypes(Set.of(EntityType.DEVICE)); triggerConfig.setCreated(true); triggerConfig.setUpdated(true); triggerConfig.setDeleted(true); diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/ApiUsageLimitTrigger.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/ApiUsageLimitTrigger.java new file mode 100644 index 0000000000..8a62b1c8d0 --- /dev/null +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/ApiUsageLimitTrigger.java @@ -0,0 +1,49 @@ +/** + * 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.notification.trigger; + +import lombok.Builder; +import lombok.Data; +import org.thingsboard.server.common.data.ApiUsageRecordState; +import org.thingsboard.server.common.data.ApiUsageStateValue; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; + +@Data +@Builder +public class ApiUsageLimitTrigger implements NotificationRuleTrigger { + + private final TenantId tenantId; + private final ApiUsageRecordState state; + private final ApiUsageStateValue status; + + @Override + public NotificationRuleTriggerType getType() { + return NotificationRuleTriggerType.API_USAGE_LIMIT; + } + + @Override + public TenantId getTenantId() { + return tenantId; + } + + @Override + public EntityId getOriginatorEntityId() { + return tenantId; + } + +} diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/EntitiesLimitTrigger.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/EntitiesLimitTrigger.java index 4d0f09bc94..a93a64776d 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/EntitiesLimitTrigger.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/trigger/EntitiesLimitTrigger.java @@ -38,7 +38,7 @@ public class EntitiesLimitTrigger implements NotificationRuleTrigger { @Override public EntityId getOriginatorEntityId() { - return TenantId.SYS_TENANT_ID; + return tenantId; } } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java b/common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java index 6f723b0d7b..31d8b7aeea 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java @@ -24,7 +24,7 @@ public enum ApiFeature { JS("jsExecutionApiState", "JavaScript functions execution"), EMAIL("emailApiState", "Email messages"), SMS("smsApiState", "SMS messages"), - ALARM("alarmApiState", "Created alarms"); + ALARM("alarmApiState", "Alarms"); @Getter private final String apiStateKey; diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordKey.java b/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordKey.java index 7b28bf63cd..dcad5c908a 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordKey.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordKey.java @@ -19,14 +19,14 @@ import lombok.Getter; public enum ApiUsageRecordKey { - TRANSPORT_MSG_COUNT(ApiFeature.TRANSPORT, "transportMsgCount", "transportMsgLimit"), - TRANSPORT_DP_COUNT(ApiFeature.TRANSPORT, "transportDataPointsCount", "transportDataPointsLimit"), - STORAGE_DP_COUNT(ApiFeature.DB, "storageDataPointsCount", "storageDataPointsLimit"), - RE_EXEC_COUNT(ApiFeature.RE, "ruleEngineExecutionCount", "ruleEngineExecutionLimit"), - JS_EXEC_COUNT(ApiFeature.JS, "jsExecutionCount", "jsExecutionLimit"), - EMAIL_EXEC_COUNT(ApiFeature.EMAIL, "emailCount", "emailLimit"), - SMS_EXEC_COUNT(ApiFeature.SMS, "smsCount", "smsLimit"), - CREATED_ALARMS_COUNT(ApiFeature.ALARM, "createdAlarmsCount", "createdAlarmsLimit"); + TRANSPORT_MSG_COUNT(ApiFeature.TRANSPORT, "transportMsgCount", "transportMsgLimit", "message"), + TRANSPORT_DP_COUNT(ApiFeature.TRANSPORT, "transportDataPointsCount", "transportDataPointsLimit", "data point"), + STORAGE_DP_COUNT(ApiFeature.DB, "storageDataPointsCount", "storageDataPointsLimit", "data point"), + RE_EXEC_COUNT(ApiFeature.RE, "ruleEngineExecutionCount", "ruleEngineExecutionLimit", "Rule Engine execution"), + JS_EXEC_COUNT(ApiFeature.JS, "jsExecutionCount", "jsExecutionLimit", "JavaScript execution"), + EMAIL_EXEC_COUNT(ApiFeature.EMAIL, "emailCount", "emailLimit", "email message"), + SMS_EXEC_COUNT(ApiFeature.SMS, "smsCount", "smsLimit", "SMS message"), + CREATED_ALARMS_COUNT(ApiFeature.ALARM, "createdAlarmsCount", "createdAlarmsLimit", "alarm"); private static final ApiUsageRecordKey[] JS_RECORD_KEYS = {JS_EXEC_COUNT}; private static final ApiUsageRecordKey[] RE_RECORD_KEYS = {RE_EXEC_COUNT}; @@ -42,11 +42,14 @@ public enum ApiUsageRecordKey { private final String apiCountKey; @Getter private final String apiLimitKey; + @Getter + private final String unitLabel; - ApiUsageRecordKey(ApiFeature apiFeature, String apiCountKey, String apiLimitKey) { + ApiUsageRecordKey(ApiFeature apiFeature, String apiCountKey, String apiLimitKey, String unitLabel) { this.apiFeature = apiFeature; this.apiCountKey = apiCountKey; this.apiLimitKey = apiLimitKey; + this.unitLabel = unitLabel; } public static ApiUsageRecordKey[] getKeys(ApiFeature feature) { diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageStateMailMessage.java b/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordState.java similarity index 57% rename from common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageStateMailMessage.java rename to common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordState.java index 0166aea723..eddcb34c66 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageStateMailMessage.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/ApiUsageRecordState.java @@ -18,8 +18,29 @@ package org.thingsboard.server.common.data; import lombok.Data; @Data -public class ApiUsageStateMailMessage { +public class ApiUsageRecordState { + + private final ApiFeature apiFeature; private final ApiUsageRecordKey key; private final long threshold; private final long value; + + public String getValueAsString() { + return valueAsString(value); + } + + public String getThresholdAsString() { + return valueAsString(threshold); + } + + private String valueAsString(long value) { + if (value > 1_000_000 && value % 1_000_000 < 10_000) { + return value / 1_000_000 + "M"; + } else if (value > 10_000) { + return String.format("%.2fM", ((double) value) / 1_000_000); + } else { + return value + ""; + } + } + } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationType.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationType.java index f0f0f6e7ea..15246fd72f 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationType.java @@ -25,6 +25,7 @@ public enum NotificationType { RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT, ALARM_ASSIGNMENT, NEW_PLATFORM_VERSION, - ENTITIES_LIMIT + ENTITIES_LIMIT, + API_USAGE_LIMIT } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/info/ApiUsageLimitNotificationInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/info/ApiUsageLimitNotificationInfo.java new file mode 100644 index 0000000000..5d631c21c0 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/info/ApiUsageLimitNotificationInfo.java @@ -0,0 +1,63 @@ +/** + * 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.notification.info; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.thingsboard.server.common.data.ApiFeature; +import org.thingsboard.server.common.data.ApiUsageRecordKey; +import org.thingsboard.server.common.data.ApiUsageStateValue; +import org.thingsboard.server.common.data.id.TenantId; + +import java.util.Map; + +import static org.thingsboard.server.common.data.util.CollectionsUtil.mapOf; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class ApiUsageLimitNotificationInfo implements RuleOriginatedNotificationInfo { + + private ApiFeature feature; + private ApiUsageRecordKey recordKey; + private ApiUsageStateValue status; + private String limit; + private String currentValue; + private TenantId tenantId; + private String tenantName; + + @Override + public Map getTemplateData() { + return mapOf( + "feature", feature.getLabel(), + "unitLabel", recordKey.getUnitLabel(), + "status", status.name().toLowerCase(), + "limit", limit, + "currentValue", currentValue, + "tenantId", tenantId.toString(), + "tenantName", tenantName + ); + } + + @Override + public TenantId getAffectedTenantId() { + return tenantId; + } + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/ApiUsageLimitNotificationRuleTriggerConfig.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/ApiUsageLimitNotificationRuleTriggerConfig.java new file mode 100644 index 0000000000..ca2fa01cae --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/ApiUsageLimitNotificationRuleTriggerConfig.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.common.data.notification.rule.trigger; + +import lombok.Data; +import org.thingsboard.server.common.data.ApiFeature; +import org.thingsboard.server.common.data.ApiUsageStateValue; + +import java.util.Set; + +@Data +public class ApiUsageLimitNotificationRuleTriggerConfig implements NotificationRuleTriggerConfig { + + private Set apiFeatures; + private Set notifyOn; + + @Override + public NotificationRuleTriggerType getTriggerType() { + return NotificationRuleTriggerType.API_USAGE_LIMIT; + } + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerConfig.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerConfig.java index 9b3316cbaf..3406a802c7 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerConfig.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerConfig.java @@ -32,7 +32,8 @@ import java.io.Serializable; @Type(value = RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig.class, name = "RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT"), @Type(value = AlarmAssignmentNotificationRuleTriggerConfig.class, name = "ALARM_ASSIGNMENT"), @Type(value = NewPlatformVersionNotificationRuleTriggerConfig.class, name = "NEW_PLATFORM_VERSION"), - @Type(value = EntitiesLimitNotificationRuleTriggerConfig.class, name = "ENTITIES_LIMIT") + @Type(value = EntitiesLimitNotificationRuleTriggerConfig.class, name = "ENTITIES_LIMIT"), + @Type(value = ApiUsageLimitNotificationRuleTriggerConfig.class, name = "API_USAGE_LIMIT"), }) public interface NotificationRuleTriggerConfig extends Serializable { diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerType.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerType.java index f094179ec0..0489ac344b 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/trigger/NotificationRuleTriggerType.java @@ -27,7 +27,8 @@ public enum NotificationRuleTriggerType { RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT, ALARM_ASSIGNMENT, NEW_PLATFORM_VERSION(false), - ENTITIES_LIMIT(false); + ENTITIES_LIMIT(false), + API_USAGE_LIMIT(false); private final boolean tenantLevel; diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java index 3bcc07af43..7476855384 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationSettingsService.java @@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.AdminSettings; +import org.thingsboard.server.common.data.ApiUsageStateValue; import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.alarm.AlarmSearchStatus; @@ -40,6 +41,7 @@ import org.thingsboard.server.common.data.notification.rule.trigger.AlarmAssignm import org.thingsboard.server.common.data.notification.rule.trigger.AlarmCommentNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.AlarmNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.AlarmNotificationRuleTriggerConfig.AlarmAction; +import org.thingsboard.server.common.data.notification.rule.trigger.ApiUsageLimitNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.DeviceActivityNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.DeviceActivityNotificationRuleTriggerConfig.DeviceEvent; import org.thingsboard.server.common.data.notification.rule.trigger.EntitiesLimitNotificationRuleTriggerConfig; @@ -132,7 +134,16 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS entitiesLimitRuleTriggerConfig.setEntityTypes(null); entitiesLimitRuleTriggerConfig.setThreshold(0.8f); createRule(tenantId, "Entities count limit", entitiesLimitNotificationTemplate.getId(), entitiesLimitRuleTriggerConfig, - List.of(affectedTenantAdmins.getId(), sysAdmins.getId()), "Send notification to tenant admins when count of entities of some type reached 80% threshold of the limit"); + List.of(affectedTenantAdmins.getId(), sysAdmins.getId()), "Send notification to tenant admins and system admins when count of entities of some type reached 80% threshold of the limit"); + + NotificationTemplate apiUsageLimitNotificationTemplate = createTemplate(tenantId, "API usage limit notification", NotificationType.API_USAGE_LIMIT, + "${feature} feature - ${status:upperCase}", + "Tenant '${tenantName}': usage - ${currentValue} out of ${limit} ${unitLabel}s"); + ApiUsageLimitNotificationRuleTriggerConfig apiUsageLimitRuleTriggerConfig = new ApiUsageLimitNotificationRuleTriggerConfig(); + apiUsageLimitRuleTriggerConfig.setApiFeatures(null); + apiUsageLimitRuleTriggerConfig.setNotifyOn(Set.of(ApiUsageStateValue.WARNING, ApiUsageStateValue.DISABLED)); + createRule(tenantId, "API usage limit", apiUsageLimitNotificationTemplate.getId(), apiUsageLimitRuleTriggerConfig, + List.of(affectedTenantAdmins.getId(), sysAdmins.getId()), "Send notification to tenant admins and system admins when API feature usage state changed"); return; } diff --git a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/MailService.java b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/MailService.java index 0102d9c645..7666157400 100644 --- a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/MailService.java +++ b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/MailService.java @@ -18,7 +18,7 @@ package org.thingsboard.rule.engine.api; import com.fasterxml.jackson.databind.JsonNode; import org.springframework.mail.javamail.JavaMailSender; import org.thingsboard.server.common.data.ApiFeature; -import org.thingsboard.server.common.data.ApiUsageStateMailMessage; +import org.thingsboard.server.common.data.ApiUsageRecordState; import org.thingsboard.server.common.data.ApiUsageStateValue; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.CustomerId; @@ -50,7 +50,7 @@ public interface MailService { void send(TenantId tenantId, CustomerId customerId, TbEmail tbEmail, JavaMailSender javaMailSender, long timeout) throws ThingsboardException; - void sendApiFeatureStateEmail(ApiFeature apiFeature, ApiUsageStateValue stateValue, String email, ApiUsageStateMailMessage msg) throws ThingsboardException; + void sendApiFeatureStateEmail(ApiFeature apiFeature, ApiUsageStateValue stateValue, String email, ApiUsageRecordState recordState) throws ThingsboardException; void testConnection(TenantId tenantId) throws Exception; diff --git a/ui-ngx/src/assets/help/en_US/notification/api_usage_limit.md b/ui-ngx/src/assets/help/en_US/notification/api_usage_limit.md new file mode 100644 index 0000000000..29bd757067 --- /dev/null +++ b/ui-ngx/src/assets/help/en_US/notification/api_usage_limit.md @@ -0,0 +1,48 @@ +#### API usage limit notification templatization + +
+
+ +Notification subject and message fields support templatization. The list of available templatization parameters depends on the template type. +See the available types and parameters below: + +Available template parameters: + + * *recipientEmail* - email of the recipient; + * *recipientFirstName* - first name of the recipient; + * *recipientLastName* - last name of the recipient; + * *feature* - API feature for which the limit is applied; one of: 'Device API', 'Telemetry persistence', 'Rule Engine execution', 'JavaScript functions execution', 'Email messages', 'SMS messages', 'Alarms'; + * *status* - one of: 'enabled', 'warning', 'disabled'; + * *unitLabel* - name of the limited unit; one of: 'message', 'data point', 'Rule Engine execution', 'JavaScript execution', 'email message', 'SMS message', 'alarm'; + * *limit* - the limit on used feature units; + * *currentValue* - current number of used units; + * *tenantId* - id of the tenant; + * *tenantName* - name of the tenant; + +Parameter names must be wrapped using `${...}`. For example: `${recipientFirstName}`. +You may also modify the value of the parameter with one of the suffixes: + + * `upperCase`, for example - `${recipientFirstName:upperCase}` + * `lowerCase`, for example - `${recipientFirstName:lowerCase}` + * `capitalize`, for example - `${recipientFirstName:capitalize}` + +
+ +##### Examples + +Let's assume tenant's devices pushed 8K messages with the max allowed number of 10K and warn threshold in tenant profile set to 0.8 (80%). The following template: + +```text +${feature} feature - ${status:upperCase} (usage: ${currentValue} out of ${limit} ${unitLabel}s) +{:copy-code} +``` + +will be transformed to: + +```text +Device API feature - WARNING (usage: 8000 out of 10000 messages) +{:copy-code} +``` + +
+