Commit 682c15cfe1d533b5bb9526a6081063aeefb92896

Authored by Andrii Shvaika
1 parent 374cf2a1

Test Fix

... ... @@ -272,6 +272,8 @@ sql:
272 272 batch_threads: "${SQL_TS_LATEST_BATCH_THREADS:4}"
273 273 # Specify whether to remove null characters from strValue of attributes and timeseries before insert
274 274 remove_null_chars: "${SQL_REMOVE_NULL_CHARS:true}"
  275 + # Specify whether to log database queries and their parameters generated by entity query repository
  276 + log_entity_queries: "${SQL_LOG_ENTITY_QUERIES:false}"
275 277 postgres:
276 278 # Specify partitioning size for timestamp key-value storage. Example: DAYS, MONTHS, YEARS, INDEFINITE.
277 279 ts_key_value_partitioning: "${SQL_POSTGRES_TS_KV_PARTITIONING:MONTHS}"
... ...
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.sql.query;
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.apache.commons.lang3.StringUtils;
20 20 import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.beans.factory.annotation.Value;
21 22 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
22 23 import org.springframework.stereotype.Repository;
23 24 import org.springframework.transaction.TransactionStatus;
... ... @@ -41,6 +42,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
41 42 import org.thingsboard.server.dao.util.SqlDao;
42 43
43 44 import java.util.ArrayList;
  45 +import java.util.Arrays;
44 46 import java.util.Collection;
45 47 import java.util.HashMap;
46 48 import java.util.HashSet;
... ... @@ -114,6 +116,9 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository {
114 116 protected final NamedParameterJdbcTemplate jdbcTemplate;
115 117 private final TransactionTemplate transactionTemplate;
116 118
  119 + @Value("${sql.log_entity_queries:false}")
  120 + private boolean logSqlQueries;
  121 +
117 122 public DefaultAlarmQueryRepository(NamedParameterJdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
118 123 this.jdbcTemplate = jdbcTemplate;
119 124 this.transactionTemplate = transactionTemplate;
... ... @@ -237,6 +242,10 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository {
237 242 dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
238 243 }
239 244 List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx);
  245 + if (logSqlQueries) {
  246 + log.error("QUERY: {}", dataQuery);
  247 + Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
  248 + }
240 249 return AlarmDataAdapter.createAlarmData(pageLink, rows, totalElements, orderedEntityIds);
241 250 });
242 251 }
... ...
... ... @@ -5,7 +5,7 @@
5 5 * you may not use this file except in compliance with the License.
6 6 * You may obtain a copy of the License at
7 7 *
8   - * http://www.apache.org/licenses/LICENSE-2.0
  8 + * http://www.apache.org/licenses/LICENSE-2.0
9 9 *
10 10 * Unless required by applicable law or agreed to in writing, software
11 11 * distributed under the License is distributed on an "AS IS" BASIS,
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.sql.query;
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.apache.commons.lang3.StringUtils;
20 20 import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.beans.factory.annotation.Value;
21 22 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
22 23 import org.springframework.stereotype.Repository;
23 24 import org.springframework.transaction.TransactionStatus;
... ... @@ -223,6 +224,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
223 224 private final NamedParameterJdbcTemplate jdbcTemplate;
224 225 private final TransactionTemplate transactionTemplate;
225 226
  227 + @Value("${sql.log_entity_queries:false}")
  228 + private boolean logSqlQueries;
  229 +
226 230 public DefaultEntityQueryRepository(NamedParameterJdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
227 231 this.jdbcTemplate = jdbcTemplate;
228 232 this.transactionTemplate = transactionTemplate;
... ... @@ -236,8 +240,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
236 240 ctx.append(addEntityTableQuery(ctx, query.getEntityFilter()));
237 241 ctx.append(" e where ");
238 242 ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList()));
239   -// log.error("QUERY: {}", ctx.getQuery());
240   -// Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
  243 + if (logSqlQueries) {
  244 + log.info("QUERY: {}", ctx.getQuery());
  245 + Arrays.asList(ctx.getParameterNames()).forEach(param -> log.info("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
  246 + }
241 247 return transactionTemplate.execute(status -> jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class));
242 248 }
243 249
... ... @@ -331,8 +337,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
331 337 if (pageLink.getPageSize() > 0) {
332 338 dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
333 339 }
334   -// log.error("QUERY: {}", dataQuery);
335   -// Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
  340 + if (logSqlQueries) {
  341 + log.error("QUERY: {}", dataQuery);
  342 + Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
  343 + }
336 344 List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx);
337 345 return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements);
338 346 });
... ...
... ... @@ -245,13 +245,13 @@ public class EntityKeyMapping {
245 245 filterQuery = " AND (" + filterQuery + ")";
246 246 }
247 247 if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) {
248   - String join = hasFilter() ? "left join" : "left outer join";
  248 + String join = hasFilter() ? "inner join" : "left join";
249 249 return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s",
250 250 join, alias, alias, alias, alias, filterQuery);
251 251 } else {
252 252 String query;
253 253 if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) {
254   - String join = hasFilter() ? "left join" : "left outer join";
  254 + String join = hasFilter() ? "inner join" : "left join";
255 255 query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ",
256 256 join, alias, alias, alias, entityTypeStr, alias, alias);
257 257 String scope;
... ...
... ... @@ -46,4 +46,6 @@ queue.rule-engine.queues[0].poll-interval=25
46 46 queue.rule-engine.queues[0].partitions=3
47 47 queue.rule-engine.queues[0].pack-processing-timeout=3000
48 48 queue.rule-engine.queues[0].processing-strategy.type=SKIP_ALL_FAILURES
49   -queue.rule-engine.queues[0].submit-strategy.type=BURST
\ No newline at end of file
  49 +queue.rule-engine.queues[0].submit-strategy.type=BURST
  50 +
  51 +sql.log_entity_queries=true
\ No newline at end of file
... ...