refactor: 优化

This commit is contained in:
zhouhao 2025-10-15 16:56:08 +08:00
parent 89968c8e87
commit fc7d09ce8b
3 changed files with 35 additions and 45 deletions

View File

@ -31,8 +31,9 @@ public class ShortenedThrowableConverter extends ThrowableHandlingConverter {
public static void writeStackTraceElement(StringBuilder builder,
StackTraceElementProxy[] elements) {
int unimportantCount = 0;
int count = 0;
for (StackTraceElementProxy element : elements) {
if (ExceptionUtils.compactEnabled && ExceptionUtils.isUnimportant(element.getStackTraceElement())) {
if (ExceptionUtils.compactEnabled && count++>2 && ExceptionUtils.isUnimportant(element.getStackTraceElement())) {
unimportantCount++;
continue;
}

View File

@ -174,7 +174,7 @@ public class DefaultAlarmHandler implements AlarmHandler {
ReactiveLock lock = ReactiveLockHolder
.getLock("triggerAlarm:" + recordId);
return this
.getRecordCache(createRecordId(relieveInfo))
.getRecordCache(recordId)
.flatMap(cache -> {
if (relieveInfo.getRelieveTime() == null) {
relieveInfo.setRelieveTime(System.currentTimeMillis());

View File

@ -14,22 +14,21 @@
* limitations under the License.
*/
package org.jetlinks.community.rule.engine.measurement;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.jetlinks.community.dashboard.*;
import org.jetlinks.community.dashboard.supports.StaticMeasurement;
import org.jetlinks.community.rule.engine.service.AlarmHistoryService;
import org.jetlinks.community.timeseries.query.Aggregation;
import org.jetlinks.community.timeseries.query.AggregationData;
import org.jetlinks.community.timeseries.query.AggregationQueryParam;
import org.jetlinks.core.metadata.ConfigMetadata;
import org.jetlinks.core.metadata.DataType;
import org.jetlinks.core.metadata.DefaultConfigMetadata;
import org.jetlinks.core.metadata.types.IntType;
import org.jetlinks.core.metadata.types.StringType;
import org.jetlinks.community.dashboard.*;
import org.jetlinks.community.rule.engine.service.AlarmHistoryService;
import org.jetlinks.community.timeseries.query.Aggregation;
import org.jetlinks.community.timeseries.query.AggregationData;
import org.jetlinks.community.timeseries.query.AggregationQueryParam;
import reactor.core.publisher.Flux;
import java.time.LocalDateTime;
@ -53,12 +52,12 @@ public class AlarmRecordRankMeasurement extends StaticMeasurement {
static ConfigMetadata aggConfigMetadata = new DefaultConfigMetadata()
.add("time", "周期", "例如: 1h,10m,30s", StringType.GLOBAL)
.add("agg", "聚合类型", "count,sum,avg,max,min", StringType.GLOBAL)
.add("format", "时间格式", "如: MM-dd:HH", StringType.GLOBAL)
.add("limit", "最大数据量", "", StringType.GLOBAL)
.add("from", "时间从", "", StringType.GLOBAL)
.add("to", "时间至", "", StringType.GLOBAL);
.add("time", "周期", "例如: 1h,10m,30s", StringType.GLOBAL)
.add("agg", "聚合类型", "count,sum,avg,max,min", StringType.GLOBAL)
.add("format", "时间格式", "如: MM-dd:HH", StringType.GLOBAL)
.add("limit", "最大数据量", "", StringType.GLOBAL)
.add("from", "时间从", "", StringType.GLOBAL)
.add("to", "时间至", "", StringType.GLOBAL);
class AggRecordRankDimension implements MeasurementDimension {
@ -88,20 +87,20 @@ public class AlarmRecordRankMeasurement extends StaticMeasurement {
aggregationQueryParam.setTimeProperty("alarmTime");
return aggregationQueryParam
.groupBy(parameter.getString("group", "targetId"))
.count("targetId", "count")
.agg("targetId", Aggregation.TOP)
.agg("targetName", Aggregation.TOP)
.limit(parameter.getInt("limit").orElse(1))
.from(parameter
.getDate("from")
.orElseGet(() -> Date
.from(LocalDateTime
.now()
.plusDays(-1)
.atZone(ZoneId.systemDefault())
.toInstant())))
.to(parameter.getDate("to").orElse(new Date()));
.groupBy(parameter.getString("group", "targetId"))
.count("targetId", "count")
.agg("targetId", Aggregation.TOP)
.agg("targetName", Aggregation.TOP)
.limit(parameter.getInt("limit").orElse(1))
.from(parameter
.getDate("from")
.orElseGet(() -> Date
.from(LocalDateTime
.now()
.plusDays(-1)
.atZone(ZoneId.systemDefault())
.toInstant())))
.to(parameter.getDate("to").orElse(new Date()));
}
@Override
@ -116,29 +115,19 @@ public class AlarmRecordRankMeasurement extends StaticMeasurement {
AggregationQueryParam param = createQueryParam(parameter);
return param
.execute(historyService::aggregation)
.groupBy(a -> a.getString("targetId", null))
.flatMap(fluxGroup -> fluxGroup.reduce(AggregationData::merge))
.sort(comparator)
.map(data -> SimpleMeasurementValue.of(new SimpleResult(data), 0))
.take(param.getLimit());
}
public Flux<SimpleMeasurementValue> getValue(AggregationQueryParam param, Comparator<AggregationData> comparator) {
return param
.execute(historyService::aggregation)
.groupBy(a -> a.getString("targetId", null))
.flatMap(fluxGroup -> fluxGroup.reduce(AggregationData::merge))
.sort(comparator)
.map(data -> SimpleMeasurementValue.of(new SimpleResult(data), 0))
.take(param.getLimit());
.execute(historyService::aggregation)
.groupBy(a -> a.getString("targetId", null),Integer.MAX_VALUE)
.flatMap(fluxGroup -> fluxGroup.reduce(AggregationData::merge))
.sort(comparator)
.map(data -> SimpleMeasurementValue.of(new SimpleResult(data), 0))
.take(param.getLimit());
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
class SimpleResult {
public static class SimpleResult {
private String targetId;