diff --git a/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewThresholdManage.java b/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewThresholdManage.java index 94a9821e85..d4a2f4d262 100644 --- a/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewThresholdManage.java +++ b/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewThresholdManage.java @@ -2,10 +2,10 @@ package io.dataease.chart.manage; import io.dataease.api.chart.request.ThresholdCheckRequest; import io.dataease.api.chart.vo.ThresholdCheckVO; -import io.dataease.dataset.manage.DatasetTableFieldManage; import io.dataease.engine.constant.DeTypeConstants; import io.dataease.extensions.datasource.dto.DatasetTableFieldDTO; import io.dataease.extensions.view.dto.ChartViewDTO; +import io.dataease.extensions.view.dto.ChartViewFieldDTO; import io.dataease.extensions.view.filter.FilterTreeItem; import io.dataease.extensions.view.filter.FilterTreeObj; import io.dataease.utils.DateUtils; @@ -25,19 +25,51 @@ import java.util.stream.Collectors; @Component("chartViewThresholdManage") public class ChartViewThresholdManage { - @Resource - private DatasetTableFieldManage datasetTableFieldManage; @Resource private ChartViewManege chartViewManege; - public String convertThresholdRules(Long tableId, String thresholdRules) { - List fieldList = datasetTableFieldManage.selectByDatasetGroupId(tableId); + public String convertThresholdRules(Long chartId, String thresholdRules) { + ChartViewDTO details = chartViewManege.getDetails(chartId); + return convertThresholdRules(details, thresholdRules); + } + + private String convertThresholdRules(ChartViewDTO chart, String thresholdRules) { + List fieldList = chartFields(chart); FilterTreeObj filterTreeObj = JsonUtil.parseObject(thresholdRules, FilterTreeObj.class); Map fieldMap = fieldList.stream().collect(Collectors.toMap(item -> item.getId().toString(), item -> item)); return convertTree(filterTreeObj, fieldMap); } + private List chartFields(ChartViewDTO details) { + List result = new ArrayList<>(); + List xAxis = details.getXAxis(); + if (CollectionUtils.isNotEmpty(xAxis)) { + result.addAll(xAxis); + } + List xAxisExt = details.getXAxisExt(); + if (CollectionUtils.isNotEmpty(xAxisExt)) { + result.addAll(xAxisExt); + } + List yAxis = details.getYAxis(); + if (CollectionUtils.isNotEmpty(yAxis)) { + result.addAll(yAxis); + } + List yAxisExt = details.getYAxisExt(); + if (CollectionUtils.isNotEmpty(yAxisExt)) { + result.addAll(yAxisExt); + } + List extStack = details.getExtStack(); + if (CollectionUtils.isNotEmpty(extStack)) { + result.addAll(extStack); + } + List extBubble = details.getExtBubble(); + if (CollectionUtils.isNotEmpty(extBubble)) { + result.addAll(extBubble); + } + return result; + } + private String convertTree(FilterTreeObj filterTreeObj, Map fieldMap) { String logic = filterTreeObj.getLogic(); String logicText = translateLogic(logic); @@ -73,7 +105,26 @@ public class ChartViewThresholdManage { String enumValueText = String.join(",", enumValue); return fieldName + " 属于 " + "( " + enumValueText + " )"; } else { - return fieldName + " " + translateTerm(item.getTerm()) + " " + item.getValue(); + String valueType = item.getValueType(); + return fieldName + " " + translateTerm(item.getTerm()) + " " + formatFieldValue(item.getValue(), valueType); + } + } + + private String formatFieldValue(String value, String valueType) { + if (StringUtils.isBlank(valueType)) { + valueType = "fixed"; + } + if (StringUtils.equals("fixed", valueType)) { + return value; + } + if (StringUtils.equals("max", value)) { + return "最大值"; + } else if (StringUtils.equals("min", value)) { + return "最小值"; + } else if (StringUtils.equals("average", value)) { + return "平均值"; + } else { + return value; } } @@ -88,11 +139,11 @@ public class ChartViewThresholdManage { case "in" -> "属于"; case "not in" -> "不属于"; case "like" -> "包含"; - case "not like" -> "不包含"; + case "not_like" -> "不包含"; case "null" -> "为空"; - case "not null" -> "不为空"; + case "not_null" -> "不为空"; case "empty" -> "空字符串"; - case "not empty" -> "非字符串"; + case "not_empty" -> "非字符串"; case "between" -> "范围是"; default -> " 等于 "; }; @@ -123,6 +174,9 @@ public class ChartViewThresholdManage { try { ChartViewDTO chart = chartViewManege.getChart(chartId); Map data = chart.getData(); + thresholdTemplate = thresholdTemplate.replace("[检测时间]", DateUtils.time2String(System.currentTimeMillis())); + String s = convertThresholdRules(chart, thresholdRules); + thresholdTemplate = convertStyle(thresholdTemplate.replace("[触发告警]", s)); List> tableRow = (List>) data.get("tableRow"); List fields = (List) data.get("fields"); Map fieldMap = fields.stream().collect(Collectors.toMap(DatasetTableFieldDTO::getId, item -> item)); @@ -131,7 +185,7 @@ public class ChartViewThresholdManage { if (CollectionUtils.isEmpty(rows)) { return new ThresholdCheckVO(false, null, null, null); } - String regex = "]*id=\"changeText-(\\d+)(?!0$)(?!1$)\"[^>]*>.*?"; + String regex = "]*id=\"changeText-(-?\\d+)(?!0$)(?!1$)\"[^>]*>.*?"; Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); Matcher matcher = pattern.matcher(thresholdTemplate); StringBuilder sb = new StringBuilder(); @@ -150,11 +204,7 @@ public class ChartViewThresholdManage { matcher.appendTail(sb); // 输出替换后的HTML内容 - String msgContent = sb.toString(); - msgContent = msgContent.replace("[检测时间]", DateUtils.time2String(System.currentTimeMillis())); - Long tableId = chart.getTableId(); - String s = convertThresholdRules(tableId, thresholdRules); - String result = convertStyle(msgContent.replace("[触发告警]", s)); + String result = sb.toString(); return new ThresholdCheckVO(true, result, null, null); } catch (Exception e) { LogUtil.error(e.getMessage(), new Throwable(e)); @@ -162,7 +212,50 @@ public class ChartViewThresholdManage { } } + private void chartDynamicMap(List> rows, FilterTreeObj conditionTree, Map fieldMap) { + List items = conditionTree.getItems(); + items.forEach(item -> { + if (!StringUtils.equals("item", item.getType())) { + chartDynamicMap(rows, item.getSubTree(), fieldMap); + } else { + Long fieldId = item.getFieldId(); + DatasetTableFieldDTO fieldDTO = fieldMap.get(fieldId); + if ((Objects.equals(fieldDTO.getDeType(), DeTypeConstants.DE_INT) || Objects.equals(fieldDTO.getDeType(), DeTypeConstants.DE_FLOAT)) && StringUtils.equals("dynamic", item.getValueType())) { + item.setField(fieldDTO); + item.setValue(formatValue(rows, item)); + } + } + }); + } + + private String formatValue(List> rows, FilterTreeItem item) { + DatasetTableFieldDTO field = item.getField(); + String dataeaseName = field.getDataeaseName(); + String value = item.getValue(); + float tempFVal = 0f; + int validLen = 0; + + for (Map row : rows) { + Object o = row.get(dataeaseName); + if (ObjectUtils.isEmpty(o)) continue; + float fvalue = Float.parseFloat(o.toString()); + if (StringUtils.equals("min", value)) { + tempFVal = Math.min(tempFVal, fvalue); + } else if (StringUtils.equals("max", value)) { + tempFVal = Math.max(tempFVal, fvalue); + } else if (StringUtils.equals("average", value)) { + tempFVal += fvalue; + validLen++; + } + } + if (StringUtils.equals("average", value)) { + return validLen == 0 ? "0f" : String.valueOf((tempFVal / validLen)); + } + return String.valueOf(tempFVal); + } + public List> filterRows(List> rows, FilterTreeObj conditionTree, Map fieldMap) { + chartDynamicMap(rows, conditionTree, fieldMap); List> filteredRows = new ArrayList<>(); for (Map row : rows) { if (matchesConditionTree(row, conditionTree, fieldMap)) { diff --git a/de-xpack b/de-xpack index b0891ed098..290b7dd191 160000 --- a/de-xpack +++ b/de-xpack @@ -1 +1 @@ -Subproject commit b0891ed09873a48be31e2d2558aaf3751130a88d +Subproject commit 290b7dd191fd0c79c5efde3a4673cc6167e69947 diff --git a/sdk/api/api-base/src/main/java/io/dataease/api/threshold/dto/ThresholdPreviewRequest.java b/sdk/api/api-base/src/main/java/io/dataease/api/threshold/dto/ThresholdPreviewRequest.java index 9b516d801f..19a0c8d314 100644 --- a/sdk/api/api-base/src/main/java/io/dataease/api/threshold/dto/ThresholdPreviewRequest.java +++ b/sdk/api/api-base/src/main/java/io/dataease/api/threshold/dto/ThresholdPreviewRequest.java @@ -10,7 +10,7 @@ public class ThresholdPreviewRequest implements Serializable { @Serial private static final long serialVersionUID = 3609209252630995739L; - private Long tableId; + private Long chartId; private String thresholdRules;