|
25 | 25 | import org.jetlinks.community.things.data.AggregationRequest; |
26 | 26 | import org.jetlinks.community.things.data.PropertyAggregation; |
27 | 27 | import org.jetlinks.community.things.data.ThingPropertyDetail; |
28 | | -import org.jetlinks.community.things.data.ThingsDataConstants; |
| 28 | +import org.jetlinks.community.things.data.ThingsDataUtils; |
29 | 29 | import org.jetlinks.community.things.data.operations.DataSettings; |
30 | 30 | import org.jetlinks.community.things.data.operations.MetricBuilder; |
31 | 31 | import org.jetlinks.community.things.data.operations.RowModeQueryOperationsBase; |
32 | 32 | import org.jetlinks.community.timeseries.TimeSeriesData; |
33 | 33 | import org.jetlinks.community.timeseries.query.Aggregation; |
34 | 34 | import org.jetlinks.community.timeseries.query.AggregationData; |
35 | 35 | import org.jetlinks.community.utils.SqlSecurityUtils; |
36 | | -import org.jetlinks.reactor.ql.utils.CastUtils; |
37 | 36 | import reactor.core.publisher.Flux; |
38 | 37 | import reactor.core.publisher.Mono; |
39 | 38 |
|
|
44 | 43 | import java.time.format.DateTimeFormatter; |
45 | 44 | import java.util.*; |
46 | 45 | import java.util.function.Function; |
| 46 | +import java.util.stream.Collectors; |
47 | 47 |
|
48 | 48 | class TDengineRowModeQueryOperations extends RowModeQueryOperationsBase { |
49 | 49 |
|
@@ -138,36 +138,32 @@ protected Flux<AggregationData> doAggregation(String metric, |
138 | 138 | .take(request.getLimit()) |
139 | 139 | ; |
140 | 140 | } |
| 141 | + NavigableMap<Long, Map<String, Object>> prepares = |
| 142 | + ThingsDataUtils.prepareAggregationData(request, properties); |
| 143 | + Map<String, List<PropertyAggregation>> propertyAgg = Arrays |
| 144 | + .stream(properties) |
| 145 | + .collect(Collectors.groupingBy(PropertyAggregation::getProperty)); |
141 | 146 | return helper |
142 | 147 | .query(dataSql) |
143 | | - .map(timeSeriesData -> { |
144 | | - long ts = timeSeriesData.getTimestamp(); |
145 | | - Map<String, Object> newData = timeSeriesData.getData(); |
146 | | - newData.put("time", formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault()))); |
147 | | - newData.put("_time", ts); |
148 | | - return newData; |
149 | | - }) |
150 | | - .groupBy(data -> (String) data.get("time"), Integer.MAX_VALUE) |
151 | | - .flatMap(group -> group |
152 | | - .reduceWith(HashMap::new, (a, b) -> { |
153 | | - a.putAll(b); |
154 | | - return a; |
155 | | - }) |
156 | | - .map(map -> { |
157 | | - Map<String, Object> newResult = new HashMap<>(); |
158 | | - for (PropertyAggregation property : properties) { |
159 | | - String alias = property.getAlias(); |
160 | | - String key = aliases.get(alias); |
161 | | - newResult.put(alias, Optional.ofNullable(map.get(key)).orElse(property.getDefaultValue())); |
| 148 | + .doOnNext(data -> { |
| 149 | + long timestamp = data.getTimestamp(); |
| 150 | + Map<String, Object> prepare = ThingsDataUtils.findAggregationData(timestamp, prepares); |
| 151 | + if (prepare != null) { |
| 152 | + Object propertyValue = data.getData().get("property"); |
| 153 | + List<PropertyAggregation> proAggs = propertyValue == null |
| 154 | + ? null |
| 155 | + : propertyAgg.get(propertyValue.toString()); |
| 156 | + // 每个 partition 行都会计算全部投影,只消费当前 property 对应的聚合列。 |
| 157 | + if (proAggs != null) { |
| 158 | + for (PropertyAggregation proAgg : proAggs) { |
| 159 | + String alias = proAgg.getAlias(); |
| 160 | + prepare.put(alias, data.get(aliases.get(alias)).orElse(proAgg.getDefaultValue())); |
| 161 | + } |
162 | 162 | } |
163 | | - newResult.put("time", group.key()); |
164 | | - newResult.put("_time", map.getOrDefault("_time", new Date())); |
165 | | - return AggregationData.of(newResult); |
166 | | - })) |
167 | | - .sort(Comparator |
168 | | - .<AggregationData, Date>comparing(data -> CastUtils.castDate(data.values().get("_time"))) |
169 | | - .reversed()) |
170 | | - .doOnNext(data -> data.values().remove("_time")) |
| 163 | + } |
| 164 | + }) |
| 165 | + .thenMany(Flux.fromIterable(prepares.descendingMap().values())) |
| 166 | + .map(AggregationData::of) |
171 | 167 | .take(request.getLimit()); |
172 | 168 | } |
173 | 169 |
|
|
0 commit comments