Browse Source

Email notifications for API state

pull/3760/head
Andrii Shvaika 6 years ago
committed by Andrew Shvayka
parent
commit
031f579bd6
  1. 49
      application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java
  2. 3
      application/src/main/resources/templates/state.disabled.ftl
  3. 2
      application/src/main/resources/templates/state.enabled.ftl
  4. 4
      application/src/main/resources/templates/state.warning.ftl
  5. 2
      common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java

49
application/src/main/java/org/thingsboard/server/service/mail/DefaultMailService.java

@ -20,6 +20,7 @@ import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.NestedRuntimeException;
@ -55,6 +56,8 @@ 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;
@Autowired
private MessageSource messages;
@ -266,8 +269,7 @@ public class DefaultMailService implements MailService {
message = mergeTemplateIntoString("state.enabled.ftl", model);
break;
case WARNING:
model.put("apiLimitValueLabel", toDisabledValueLabel(msg.getKey(), msg.getThreshold()));
model.put("apiValueLabel", toDisabledValueLabel(apiFeature) + " " + toDisabledValueLabel(msg.getKey(), msg.getValue()));
model.put("apiValueLabel", toDisabledValueLabel(apiFeature) + " " + toWarningValueLabel(msg.getKey(), msg.getValue(), msg.getThreshold()));
message = mergeTemplateIntoString("state.warning.ftl", model);
break;
case DISABLED:
@ -285,8 +287,9 @@ public class DefaultMailService implements MailService {
case TRANSPORT:
return "receive";
case JS:
case RE:
return "invoke";
case RE:
return "process";
default:
throw new RuntimeException("Not implemented!");
}
@ -299,8 +302,27 @@ public class DefaultMailService implements MailService {
case TRANSPORT:
return "received";
case JS:
case RE:
return "invoked";
case RE:
return "processed";
default:
throw new RuntimeException("Not implemented!");
}
}
private String toWarningValueLabel(ApiUsageRecordKey key, long value, long threshold) {
String valueInM = getValueAsString(value);
String thresholdInM = getValueAsString(threshold);
switch (key) {
case STORAGE_DP_COUNT:
case TRANSPORT_DP_COUNT:
return valueInM + " out of " + thresholdInM + " allowed data points";
case TRANSPORT_MSG_COUNT:
return valueInM + " out of " + thresholdInM + " allowed messages";
case JS_EXEC_COUNT:
return valueInM + " out of " + thresholdInM + " allowed JavaScript functions";
case RE_EXEC_COUNT:
return valueInM + " out of " + thresholdInM + " allowed Rule Engine messages";
default:
throw new RuntimeException("Not implemented!");
}
@ -310,18 +332,29 @@ public class DefaultMailService implements MailService {
switch (key) {
case STORAGE_DP_COUNT:
case TRANSPORT_DP_COUNT:
return (value / 1000000) + "M data points";
return getValueAsString(value) + " data points";
case TRANSPORT_MSG_COUNT:
return (value / 1000000) + "M messages";
return getValueAsString(value) + " messages";
case JS_EXEC_COUNT:
return (value / 1000000) + "M JavaScript functions";
return "JavaScript functions " + getValueAsString(value) + " times";
case RE_EXEC_COUNT:
return (value / 1000000) + "M Rule Engine nodes";
return getValueAsString(value) + " Rule Engine messages";
default:
throw new RuntimeException("Not implemented!");
}
}
@NotNull
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) throws ThingsboardException {

3
application/src/main/resources/templates/state.disabled.ftl

@ -123,8 +123,7 @@
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<td class="content-block"
style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0px; padding: 0px 0px 20px; height: 40px;"
valign="top">We have disabled the ${apiFeature} for your account because ThingsBoard has already&nbsp; ${apiLimitValueLabel}
.
valign="top">We have disabled the ${apiFeature} for your account because ThingsBoard has already ${apiLimitValueLabel}.
</td>
</tr>
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">

2
application/src/main/resources/templates/state.enabled.ftl

@ -123,7 +123,7 @@
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<td class="content-block"
style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0px; padding: 0px 0px 20px; height: 40px;"
valign="top">We have enabled the ${apiFeature} for your account and ThingsBoard already able to ${apiLabel} messages.
valign="top">We have enabled the ${apiFeature} for your account and ThingsBoard is already able to ${apiLabel} messages.
</td>
</tr>
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">

4
application/src/main/resources/templates/state.warning.ftl

@ -117,14 +117,14 @@
<td class="content-block"
style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #348eda; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0px; padding: 0px 0px 20px; height: 84px;"
valign="top">
<h2>Your ThingsBoard account feature may be disabled</h2>
<h2>Your ThingsBoard account feature may be disabled soon</h2>
</td>
</tr>
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<td class="content-block"
style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0px; padding: 0px 0px 20px; height: 40px;"
valign="top">
Your ${apiFeature} limit (${apiLimitValueLabel}) is almost exhausted. <br>ThingsBoard has already&nbsp;${apiValueLabel}.<br> ${apiFeature} will be disabled for your account when limit will be reached.
ThingsBoard has already&nbsp;${apiValueLabel}.<br> ${apiFeature} will be disabled for your account once the limit will be reached.
</td>
</tr>
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">

2
common/data/src/main/java/org/thingsboard/server/common/data/ApiFeature.java

@ -20,7 +20,7 @@ import lombok.Getter;
public enum ApiFeature {
TRANSPORT("transportApiState", "Device API"),
DB("dbApiState", "Telemetry persistence"),
RE("ruleEngineApiState", "Rule engine execution"),
RE("ruleEngineApiState", "Rule Engine execution"),
JS("jsExecutionApiState", "JavaScript functions execution");
@Getter

Loading…
Cancel
Save