From dcd61bd7af1c76fddc594fbb7a5b0cffe700c91d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Thu, 23 Nov 2023 10:16:52 +0800 Subject: [PATCH] bugfix tdengine query interval history metrics data with instance error (#1348) --- .../warehouse/store/HistoryTdEngineDataStorage.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/warehouse/src/main/java/org/dromara/hertzbeat/warehouse/store/HistoryTdEngineDataStorage.java b/warehouse/src/main/java/org/dromara/hertzbeat/warehouse/store/HistoryTdEngineDataStorage.java index 05952fe529a..ffbd701b4a7 100644 --- a/warehouse/src/main/java/org/dromara/hertzbeat/warehouse/store/HistoryTdEngineDataStorage.java +++ b/warehouse/src/main/java/org/dromara/hertzbeat/warehouse/store/HistoryTdEngineDataStorage.java @@ -51,6 +51,7 @@ public class HistoryTdEngineDataStorage extends AbstractHistoryDataStorage { private static final Pattern SQL_SPECIAL_STRING_PATTERN = Pattern.compile("(\\\\)|(')"); + private static final String INSTANCE_NULL = "''"; private static final String INSERT_TABLE_DATA_SQL = "INSERT INTO `%s` USING `%s` TAGS (%s) VALUES %s"; private static final String CREATE_SUPER_TABLE_SQL = "CREATE STABLE IF NOT EXISTS `%s` %s TAGS (monitor BIGINT)"; private static final String NO_SUPER_TABLE_ERROR = "Table does not exist"; @@ -59,7 +60,7 @@ public class HistoryTdEngineDataStorage extends AbstractHistoryDataStorage { private static final String QUERY_HISTORY_SQL = "SELECT ts, instance, `%s` FROM `%s` WHERE ts >= now - %s order by ts desc"; private static final String QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL - = "SELECT first(`ts`), first(`%s`), avg(`%s`), min(`%s`), max(`%s`) FROM `%s` WHERE instance = '%s' AND ts >= now - %s interval(4h)"; + = "SELECT first(ts), first(`%s`), avg(`%s`), min(`%s`), max(`%s`) FROM `%s` WHERE instance = '%s' AND ts >= now - %s interval(4h)"; private static final String QUERY_INSTANCE_SQL = "SELECT DISTINCT instance FROM `%s` WHERE ts >= now - 1w"; @@ -241,6 +242,9 @@ public void destroy() { @Override public Map> getHistoryMetricData(Long monitorId, String app, String metrics, String metric, String instance, String history) { String table = app + "_" + metrics + "_" + monitorId; + if (INSTANCE_NULL.equals(instance)) { + instance = ""; + } String selectSql = instance == null ? String.format(QUERY_HISTORY_SQL, metric, table, history) : String.format(QUERY_HISTORY_WITH_INSTANCE_SQL, metric, table, instance, history); log.debug(selectSql); @@ -334,12 +338,12 @@ public Map> getHistoryIntervalMetricData(Long monitorId, Str } Map> instanceValuesMap = new HashMap<>(instances.size()); for (String instanceValue : instances) { + if (INSTANCE_NULL.equals(instanceValue)) { + instanceValue = ""; + } String selectSql = String.format(QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL, metric, metric, metric, metric, table, instanceValue, history); log.debug(selectSql); - if ("''".equals(instanceValue)) { - instanceValue = ""; - } List values = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>()); Connection connection = null; try {