Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix tdengine query interval history metrics data with instance error #1348

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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