Compare commits

...

2 Commits

  1. 1
      ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/equManagement/ScheduleRulesController.java
  2. 7
      ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/exceptionController/AlertMessageController.java
  3. 1
      ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/AlertRule.java
  4. 1
      ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/vo/AlertRuleVo.java
  5. 4
      ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/DictAlarmParamMapper.java
  6. 38
      ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertLaunchServiceImpl.java
  7. 124
      ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertRuleServiceImpl.java
  8. 8
      ALOps_sys_backend/alops-system/src/main/resources/mapper/system/DictAlarmParamMapper.xml

@ -106,6 +106,7 @@ public class ScheduleRulesController extends BaseController
// util.exportExcel(response, list, "定时采集规则数据");
// }
//
/**
* 获取定时采集规则详细信息
*/

@ -1,6 +1,7 @@
package com.alops.web.controller.exceptionController;
import java.util.List;
import java.util.Map;
import com.alops.common.annotation.Log;
import com.alops.common.core.controller.BaseController;
@ -49,12 +50,12 @@ public class AlertMessageController extends BaseController
// @PostMapping("/executeImmediate")
// public AjaxResult executeImmediate(@RequestBody Map<String, List<Long>> paramMap) {
// try {
// List<Long> ruleIds = paramMap.get("ruleIds");
// List<Long> ruleIds = paramMap.get("ruleIds");
// if (ruleIds == null || ruleIds.isEmpty()) {
// return AjaxResult.error("请选择至少一条规则");
// return AjaxResult.error("请选择至少一条规则");
// }
//
// iAlertLaunchService.executeImmediateAlerts(); // 调用 Service 执行
// iAlertLaunchService.executeImmediateAlerts(); // 调用 Service 执行
// return AjaxResult.success("立即预警任务已触发,存入成功,请查看");
//
// } catch (Exception e) {

@ -54,6 +54,7 @@ public class AlertRule extends BaseEntity
/** 预警模版(参数用数据库命名法) */
@Excel(name = "预警模版", readConverterExp = "参=数用数据库命名法")
@JsonIgnore
private String alertTemplate;
/** 预警类型 */

@ -49,6 +49,7 @@ public class AlertRuleVo extends BaseEntity {
@JsonIgnore
private String alertSql;
@JsonIgnore
/** 预警模版(参数用数据库命名法) */
@Excel(name = "预警模版", readConverterExp = "参=数用数据库命名法")
private String alertTemplate;

@ -2,6 +2,7 @@ package com.alops.system.mapper;
import com.alops.system.domain.DictAlarmParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;
import java.util.List;
@ -16,6 +17,9 @@ import java.util.List;
public interface DictAlarmParamMapper
{
String selectCnNameByParamName(@Param("paramName") String paramName);
List<DictAlarmParam> selectByTableName();
/**
* 查询请填写功能名称

@ -208,32 +208,32 @@ public class AlertLaunchServiceImpl implements IAlertLaunchService {
return new ArrayList<>();
}
// 2. 需要比较的字段,如 health、temperature 等
// 2. 需要比较的字段,如 health、temperature 等
String col = conditions.get(0).getParamName();
// 3. SQL:取每台设备最新记录和次新记录,比较该字段是否变化,并关联设备类型
// 3. SQL:取每台设备最新记录和次新记录,比较该字段是否变化,并关联设备类型
String sql =
"SELECT g1.*, t.name AS equ_type,b.name AS equ_name,b.equ_number" +
"SELECT g1.*, t.name AS equ_type, b.name AS equ_name, b.equ_number " +
"FROM equ_gather g1 " +
"JOIN equ_gather g2 ON g1.equ_id = g2.equ_id " +
"JOIN equ_base b ON g1.equ_id = b.id " +
"JOIN equ_type t ON b.equ_type = t.id " +
"JOIN equ_base b ON g1.equ_id = b.id " +
"JOIN equ_type t ON b.equ_type = t.id " +
"WHERE g1.gather_time = ( " +
" SELECT MAX(tg.gather_time) " +
" FROM equ_gather tg " +
" WHERE tg.equ_id = g1.equ_id " +
" SELECT MAX(tg.gather_time) " +
" FROM equ_gather tg " +
" WHERE tg.equ_id = g1.equ_id " +
") " +
"AND g2.gather_time = ( " +
" SELECT MAX(t2.gather_time) " +
" FROM equ_gather t2 " +
" WHERE t2.equ_id = g1.equ_id " +
" AND t2.gather_time < ( " +
" SELECT MAX(t3.gather_time) " +
" FROM equ_gather t3 " +
" WHERE t3.equ_id = g1.equ_id " +
" ) " +
" AND g2.gather_time = ( " +
" SELECT MAX(t2.gather_time) " +
" FROM equ_gather t2 " +
" WHERE t2.equ_id = g1.equ_id " +
" AND t2.gather_time < ( " +
" SELECT MAX(t3.gather_time) " +
" FROM equ_gather t3 " +
" WHERE t3.equ_id = g1.equ_id " +
" ) " +
" ) " +
" AND g1.`" + col + "` <> g2.`" + col + "`";
") " +
"AND g1.`" + col + "` <> g2.`" + col + "`";
// 4. 返回查询结果
return jdbcTemplate.queryForList(sql);

@ -11,6 +11,7 @@ import com.alops.system.domain.dto.AlertRuleAddDto;
import com.alops.system.domain.vo.AlertRuleVo;
import com.alops.system.mapper.AlertRuleConditionMapper;
import com.alops.system.mapper.AlertRuleMapper;
import com.alops.system.mapper.DictAlarmParamMapper;
import com.alops.system.service.IAlertRuleService;
import org.apache.commons.lang3.StringEscapeUtils;
@ -32,6 +33,9 @@ public class AlertRuleServiceImpl implements IAlertRuleService
@Autowired
private AlertRuleConditionMapper alertRuleConditionMapper;
@Autowired
private DictAlarmParamMapper dictAlarmParamMapper;
//生成预警规则
/**
* 保存预警规则及条件同时生成 SQL
@ -58,6 +62,9 @@ public class AlertRuleServiceImpl implements IAlertRuleService
}
rule.setAlertSql(sql);
rule.setEnable(1);
//生成模版
String alertTemplate=generateAlertTemplate(dto);
rule.setAlertTemplate(alertTemplate);
// 插入规则
alertRuleMapper.insertAlertRule(rule);
@ -91,9 +98,11 @@ public class AlertRuleServiceImpl implements IAlertRuleService
sql = buildSql(rule, conditions);
}
rule.setAlertSql(sql);
//生成模版
String alertTemplate=generateAlertTemplate(dto);
rule.setAlertTemplate(alertTemplate);
// 3. 更新规则表
alertRuleMapper.updateAlertRule(rule);
// 4. 先删旧条件,再插入新条件
@ -306,6 +315,119 @@ public class AlertRuleServiceImpl implements IAlertRuleService
}
/**
* 生成基础告警模板
*/
// public String generateAlertTemplate(AlertRuleAddDto dto) {
//
// AlertRuleCondition condition = dto.getAlertRuleConditionList().get(0);
// String paramName = condition.getParamName();
//
// // 查询字典表获取参数中文名
// String paramCnName = dictAlarmParamMapper.selectCnNameByParamName(paramName);
//
// if (paramCnName == null || paramCnName.isEmpty()) {
// paramCnName = paramName; // 查不到就用原名
// }
//
// String alertTemplate;
// if ("veneer_status".equalsIgnoreCase(paramName)) {
// // 单板运行状态模板
// alertTemplate = "设备名称{equ_name},设备号{equ_number},单板号是{veneer_id},单板运行状态是{veneer_status} 超出正常范围,请尽快检查,";
// } else {
// // 其他参数模板
// alertTemplate = String.format("设备名称{equ_name},设备号{equ_number},%s是{%s},超出正常范围,请尽快检查,", paramCnName, paramName);
// }
//
// return alertTemplate;
// }
public String generateAlertTemplate(AlertRuleAddDto dto) {
AlertRuleCondition condition = dto.getAlertRuleConditionList().get(0);
String paramName = condition.getParamName();
String operator = condition.getOperator();
String compareValue = condition.getCompareValue();
// 查询字典表获取参数中文名
String paramCnName = dictAlarmParamMapper.selectCnNameByParamName(paramName);
if (paramCnName == null || paramCnName.isEmpty()) {
paramCnName = paramName; // 查不到就用原名
}
String alertTemplate;
if ("veneer_status".equalsIgnoreCase(paramName)) {
// 单板运行状态模板
alertTemplate = "设备名称{equ_name},设备号{equ_number},单板号是{veneer_id},单板运行状态是{veneer_status} 超出正常范围,请尽快检查,";
} else {
// 判断是否有比较符号和比较值(即为新逻辑)
if (operator != null && !operator.isEmpty()) {
// 处理比较符号 -> 中文描述
String operatorDesc;
switch (operator) {
case "=":
operatorDesc = "等于";
break;
case "!=":
operatorDesc = "不等于";
break;
case "<":
operatorDesc = "小于";
break;
case "<=":
operatorDesc = "小于等于";
break;
case ">":
operatorDesc = "大于";
break;
case ">=":
operatorDesc = "大于等于";
break;
case "between":
// between 用逗号分隔两个值
String[] range = compareValue != null ? compareValue.split(",") : new String[0];
if (range.length == 2) {
operatorDesc = String.format("不在%s和%s之间", range[0], range[1]);
} else {
operatorDesc = "不在设定范围内";
}
break;
case "CHANGED":
operatorDesc = "发生了改变";
break;
default:
operatorDesc = operator;
}
// 根据不同符号生成模板
if ("CHANGED".equalsIgnoreCase(operator)) {
alertTemplate = String.format(
"设备名称{equ_name},设备号{equ_number},%s是{%s}, %s,请尽快检查,",
paramCnName, paramName, operatorDesc
);
} else if ("between".equalsIgnoreCase(operator)) {
alertTemplate = String.format(
"设备名称{equ_name},设备号{equ_number},%s{%s} %s,请尽快检查,",
paramCnName, paramName, operatorDesc
);
} else {
alertTemplate = String.format(
"设备名称{equ_name},设备号{equ_number},%s是{%s} %s %s,超出正常范围,请尽快检查,",
paramCnName, paramName, operatorDesc, compareValue
);
}
} else {
// 没有比较符号时,保持原逻辑
alertTemplate = String.format(
"设备名称{equ_name},设备号{equ_number},%s是{%s},超出正常范围,请尽快检查,",
paramCnName, paramName
);
}
}
return alertTemplate;
}
/**

@ -21,6 +21,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
select id, param_name, table_name, column_name, param_type from dict_alarm_param
</sql>
<!-- 根据参数英文名查询中文显示名 -->
<select id="selectCnNameByParamName" resultType="java.lang.String" parameterType="java.lang.String">
SELECT param_name
FROM dict_alarm_param
WHERE column_name = #{paramName}
LIMIT 1
</select>
<select id="selectDictAlarmParamList" parameterType="DictAlarmParam" resultMap="DictAlarmParamResult">
<include refid="selectDictAlarmParamVo"/>
<where>

Loading…
Cancel
Save