Skip to content

Commit

Permalink
fix(通用模块): 修复TDengine排序逻辑 (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
bestfeng1020 authored Dec 31, 2024
1 parent 6e40f6f commit bc0c01d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected Flux<AggregationData> doAggregation(String metric,
joiner.add(" ")
.add(TDengineThingDataHelper.getGroupByTime(request.getInterval()));
}
joiner.add(TDengineThingDataHelper.buildOrderBy(request.getFilter()));
joiner.add(helper.buildOrderBy(metric, request.getFilter()));

String format = request.getFormat();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hswebframework.web.exception.BusinessException;
import org.jetlinks.community.tdengine.TDEngineUtils;
import org.jetlinks.community.tdengine.term.TDengineQueryConditionBuilder;
import org.jetlinks.community.things.data.ThingsDataConstants;
import org.jetlinks.core.metadata.Converter;
import org.jetlinks.core.metadata.DataType;
import org.jetlinks.community.Interval;
Expand Down Expand Up @@ -121,14 +122,24 @@ public static Object prepareTimestampValue(Object value, String type) {
});
}

public static String buildOrderBy(QueryParamEntity param) {
public String buildOrderBy(String metric, QueryParamEntity param) {
List<Sort> sorts = param.getSorts();
if (CollectionUtils.isEmpty(sorts)) {
return " order by `_ts` desc";
}
StringJoiner joiner = new StringJoiner(", ", " order by ", "");
for (Sort sort : sorts) {

for (Sort sort : param.getSorts()) {
if (sort.getName().equalsIgnoreCase("timestamp")) {
return " order by `_ts` " + sort.getOrder();
if ((ThingsDataConstants.COLUMN_TIMESTAMP.equals(sort.getName()) || "_ts".equals(sort.getName()))) {
joiner.add("`_ts` " + sort.getOrder());
}else {
metadataManager
.getColumn(metric, sort.getName())
.orElseThrow(() -> new IllegalArgumentException("Unexpected TD engine order column: " + sort.getName()));
joiner.add("`"+ sort.getName() +"` " + sort.getOrder());
}
}
return " order by `_ts` desc";
return joiner.toString();
}

public String buildWhere(String metric, QueryParamEntity param, String... and) {
Expand Down Expand Up @@ -168,7 +179,7 @@ protected Flux<TimeSeriesData> doQuery(String metric, Query<?, QueryParamEntity>
.add(metric)
.add("` ")
.add(buildWhere(metric, param))
.add(buildOrderBy(param));
.add(buildOrderBy(metric, param));

if (param.isPaging()) {
joiner.add(" limit ").add(String.valueOf(param.getPageSize()))
Expand All @@ -192,7 +203,7 @@ protected <T> Mono<PagerResult<T>> doQueryPage(String metric,
QueryParamEntity param = query.getParam();
String sql = "`" + metric + "` " + buildWhere(metric, param);
String countSql = "select count(1) total from " + sql;
String dataSql = "select * from " + sql + buildOrderBy(param) + " limit " + param.getPageSize() + " offset " + param
String dataSql = "select * from " + sql + buildOrderBy(metric, param) + " limit " + param.getPageSize() + " offset " + param
.getPageIndex() * param.getPageSize();

return Mono.zip(
Expand Down

0 comments on commit bc0c01d

Please sign in to comment.