Skip to content

Commit

Permalink
bugfix tdengine query interval history metrics data with instance err…
Browse files Browse the repository at this point in the history
…or (#1348)
  • Loading branch information
tomsun28 authored Nov 23, 2023
1 parent 108ea42 commit b339dbb
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -241,6 +242,9 @@ public void destroy() {
@Override
public Map<String, List<Value>> 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);
Expand Down Expand Up @@ -334,12 +338,12 @@ public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, Str
}
Map<String, List<Value>> 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<Value> values = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
Connection connection = null;
try {
Expand Down

0 comments on commit b339dbb

Please sign in to comment.