Browse Source

Merge branch 'master' of github.com:thingsboard/thingsboard

pull/5552/head
Igor Kulikov 5 years ago
parent
commit
2426ef52eb
  1. 2
      application/src/main/resources/thingsboard.yml
  2. 9
      dao/src/main/java/org/thingsboard/server/dao/sql/query/DefaultEntityQueryRepository.java
  3. 42
      dao/src/test/java/org/thingsboard/server/dao/sql/query/DefaultEntityQueryRepositoryTest.java
  4. 14
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbAbstractAlarmNodeConfiguration.java
  5. 14
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeConfiguration.java
  6. 6
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeConfiguration.java

2
application/src/main/resources/thingsboard.yml

@ -279,6 +279,8 @@ sql:
rpc: rpc:
enabled: "${SQL_TTL_RPC_ENABLED:true}" enabled: "${SQL_TTL_RPC_ENABLED:true}"
checking_interval: "${SQL_RPC_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours checking_interval: "${SQL_RPC_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours
relations:
max_level: "${SQL_RELATIONS_MAX_LEVEL:50}" # //This value has to be reasonable small to prevent infinite recursion as early as possible
# Actor system parameters # Actor system parameters
actors: actors:

9
dao/src/main/java/org/thingsboard/server/dao/sql/query/DefaultEntityQueryRepository.java

@ -15,8 +15,10 @@
*/ */
package org.thingsboard.server.dao.sql.query; package org.thingsboard.server.dao.sql.query;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
@ -223,7 +225,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
private static final String SELECT_API_USAGE_STATE = "(select aus.id, aus.created_time, aus.tenant_id, aus.entity_id, " + private static final String SELECT_API_USAGE_STATE = "(select aus.id, aus.created_time, aus.tenant_id, aus.entity_id, " +
"coalesce((select title from tenant where id = aus.entity_id), (select title from customer where id = aus.entity_id)) as name " + "coalesce((select title from tenant where id = aus.entity_id), (select title from customer where id = aus.entity_id)) as name " +
"from api_usage_state as aus)"; "from api_usage_state as aus)";
static final int MAX_LEVEL_DEFAULT = 50; //This value has to be reasonable small to prevent infinite recursion as early as possible
static { static {
entityTableMap.put(EntityType.ASSET, "asset"); entityTableMap.put(EntityType.ASSET, "asset");
@ -268,6 +269,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from"); private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from");
private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to"); private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to");
@Getter
@Value("${sql.relations.max_level:50}")
int maxLevelAllowed; //This value has to be reasonable small to prevent infinite recursion as early as possible
private final NamedParameterJdbcTemplate jdbcTemplate; private final NamedParameterJdbcTemplate jdbcTemplate;
private final TransactionTemplate transactionTemplate; private final TransactionTemplate transactionTemplate;
private final DefaultQueryLogComponent queryLog; private final DefaultQueryLogComponent queryLog;
@ -711,7 +716,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
} }
int getMaxLevel(int maxLevel) { int getMaxLevel(int maxLevel) {
return maxLevel > 0 ? maxLevel : MAX_LEVEL_DEFAULT; return (maxLevel <= 0 || maxLevel > this.maxLevelAllowed) ? this.maxLevelAllowed : maxLevel;
} }
private String getQueryTemplate(EntitySearchDirection direction) { private String getQueryTemplate(EntitySearchDirection direction) {

42
dao/src/test/java/org/thingsboard/server/dao/sql/query/DefaultEntityQueryRepositoryTest.java

@ -16,42 +16,54 @@
package org.thingsboard.server.dao.sql.query; package org.thingsboard.server.dao.sql.query;
import org.junit.Test; import org.junit.Test;
import org.thingsboard.server.common.data.id.CustomerId; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.support.TransactionTemplate;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.willCallRealMethod;
import static org.mockito.Mockito.mock;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DefaultEntityQueryRepository.class)
public class DefaultEntityQueryRepositoryTest { public class DefaultEntityQueryRepositoryTest {
@MockBean
NamedParameterJdbcTemplate jdbcTemplate;
@MockBean
TransactionTemplate transactionTemplate;
@MockBean
DefaultQueryLogComponent queryLog;
@Autowired
DefaultEntityQueryRepository repo;
/* /*
* This value has to be reasonable small to prevent infinite recursion as early as possible * This value has to be reasonable small to prevent infinite recursion as early as possible
* */ * */
@Test @Test
public void givenDefaultMaxLevel_whenStaticConstant_thenEqualsTo() { public void givenDefaultMaxLevel_whenStaticConstant_thenEqualsTo() {
assertThat(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT, equalTo(10)); assertThat(repo.getMaxLevelAllowed(), equalTo(50));
} }
@Test @Test
public void givenMaxLevelZeroOrNegative_whenGetMaxLevel_thenReturnDefaultMaxLevel() { public void givenMaxLevelZeroOrNegative_whenGetMaxLevel_thenReturnDefaultMaxLevel() {
DefaultEntityQueryRepository repo = mock(DefaultEntityQueryRepository.class); assertThat(repo.getMaxLevel(0), equalTo(repo.getMaxLevelAllowed()));
willCallRealMethod().given(repo).getMaxLevel(anyInt()); assertThat(repo.getMaxLevel(-1), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(0), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT)); assertThat(repo.getMaxLevel(-2), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(-1), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT)); assertThat(repo.getMaxLevel(Integer.MIN_VALUE), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(-2), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
assertThat(repo.getMaxLevel(Integer.MIN_VALUE), equalTo(DefaultEntityQueryRepository.MAX_LEVEL_DEFAULT));
} }
@Test @Test
public void givenMaxLevelPositive_whenGetMaxLevel_thenValueTheSame() { public void givenMaxLevelPositive_whenGetMaxLevel_thenValueTheSame() {
DefaultEntityQueryRepository repo = mock(DefaultEntityQueryRepository.class);
willCallRealMethod().given(repo).getMaxLevel(anyInt());
assertThat(repo.getMaxLevel(1), equalTo(1)); assertThat(repo.getMaxLevel(1), equalTo(1));
assertThat(repo.getMaxLevel(2), equalTo(2)); assertThat(repo.getMaxLevel(2), equalTo(2));
assertThat(repo.getMaxLevel(Integer.MAX_VALUE), equalTo(Integer.MAX_VALUE)); assertThat(repo.getMaxLevel(repo.getMaxLevelAllowed()), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(repo.getMaxLevelAllowed() + 1), equalTo(repo.getMaxLevelAllowed()));
assertThat(repo.getMaxLevel(Integer.MAX_VALUE), equalTo(repo.getMaxLevelAllowed()));
} }
} }

14
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbAbstractAlarmNodeConfiguration.java

@ -20,6 +20,20 @@ import lombok.Data;
@Data @Data
public abstract class TbAbstractAlarmNodeConfiguration { public abstract class TbAbstractAlarmNodeConfiguration {
static final String ALARM_DETAILS_BUILD_JS_TEMPLATE = "" +
"//***DO NOT CHANGE THIS LINES***\n" +
"var details = {};\n" +
"if (metadata.prevAlarmDetails) {\n" +
" details = JSON.parse(metadata.prevAlarmDetails);\n" +
" //remove prevAlarmDetails from metadata\n" +
" delete metadata.prevAlarmDetails;\n" +
" //now metadata is the same as it comes IN this rule node" +
"}\n" +
"//***PLACE YOUR CODE BELOW***\n" +
"\n" +
"\n" +
"return details;";
private String alarmType; private String alarmType;
private String alarmDetailsBuildJs; private String alarmDetailsBuildJs;

14
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeConfiguration.java

@ -25,19 +25,7 @@ public class TbClearAlarmNodeConfiguration extends TbAbstractAlarmNodeConfigurat
@Override @Override
public TbClearAlarmNodeConfiguration defaultConfiguration() { public TbClearAlarmNodeConfiguration defaultConfiguration() {
TbClearAlarmNodeConfiguration configuration = new TbClearAlarmNodeConfiguration(); TbClearAlarmNodeConfiguration configuration = new TbClearAlarmNodeConfiguration();
configuration.setAlarmDetailsBuildJs("" + configuration.setAlarmDetailsBuildJs(ALARM_DETAILS_BUILD_JS_TEMPLATE);
"//***DO NOT CHANGE THIS LINES***\n" +
"var details = {};\n" +
"if (metadata.prevAlarmDetails) {\n" +
" details = JSON.parse(metadata.prevAlarmDetails);\n" +
" //remove prevAlarmDetails from metadata\n" +
" delete metadata.prevAlarmDetails;\n" +
" //now metadata is the same as it comes IN this rule node" +
"}\n" +
"//***PLACE YOUR CODE BELOW***\n" +
"\n" +
"\n" +
"return details;");
configuration.setAlarmType("General Alarm"); configuration.setAlarmType("General Alarm");
return configuration; return configuration;
} }

6
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeConfiguration.java

@ -35,11 +35,7 @@ public class TbCreateAlarmNodeConfiguration extends TbAbstractAlarmNodeConfigura
@Override @Override
public TbCreateAlarmNodeConfiguration defaultConfiguration() { public TbCreateAlarmNodeConfiguration defaultConfiguration() {
TbCreateAlarmNodeConfiguration configuration = new TbCreateAlarmNodeConfiguration(); TbCreateAlarmNodeConfiguration configuration = new TbCreateAlarmNodeConfiguration();
configuration.setAlarmDetailsBuildJs("var details = {};\n" + configuration.setAlarmDetailsBuildJs(ALARM_DETAILS_BUILD_JS_TEMPLATE);
"if (metadata.prevAlarmDetails) {\n" +
" details = JSON.parse(metadata.prevAlarmDetails);\n" +
"}\n" +
"return details;");
configuration.setAlarmType("General Alarm"); configuration.setAlarmType("General Alarm");
configuration.setSeverity(AlarmSeverity.CRITICAL.name()); configuration.setSeverity(AlarmSeverity.CRITICAL.name());
configuration.setPropagate(false); configuration.setPropagate(false);

Loading…
Cancel
Save