feat:自动处理邮箱邮件监控报警逻辑

This commit is contained in:
nieziyan 2024-05-06 18:11:29 +08:00
parent ebf8958f60
commit 035e842240
6 changed files with 26 additions and 38 deletions

View File

@ -9,7 +9,7 @@ import lombok.Getter;
public enum Item {
EMAIL_CONN("1", "Email Connection Status"),
TABLESPACE_USAGE("2", "TableSpace Usage"),
EMAIL_PROCESS("3", "Email Process");
EMAIL_UNPROCESSED("3", "Email Unprocessed");
private final String value;

View File

@ -69,9 +69,8 @@ public class SysEmailLogController {
return sysEmailLogService.analysis(emailId, startDate, endDate);
}
@GetMapping("hoursAgo")
public Boolean hoursAgo(@RequestParam("emailId") String emailId,
@RequestParam("hoursAgo") String hoursAgo){
return sysEmailLogService.hoursAgo(emailId, hoursAgo);
@GetMapping("getMinus")
public Integer getMinus(@RequestParam("emailId") String emailId){
return sysEmailLogService.getMinus(emailId);
}
}

View File

@ -18,5 +18,5 @@ public interface ISysEmailLogService extends IService<SysEmailLog> {
Result<?> analysis(String emailId, String startDate, String endDate);
Boolean hoursAgo(String emailId, String hoursAgo);
Integer getMinus(String emailId);
}

View File

@ -1,6 +1,9 @@
package org.jeecg.modules.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
@ -229,10 +232,18 @@ public class SysEmailLogServiceImpl extends ServiceImpl<SysEmailLogMapper, SysEm
}
@Override
public Boolean hoursAgo(String emailId, String hoursAgo) {
public Integer getMinus(String emailId) {
LambdaQueryWrapper<SysEmailLog> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysEmailLog::getEmailId, emailId);
wrapper.gt(SysEmailLog::getCreateTime, hoursAgo);
return CollUtil.isEmpty(this.list(wrapper));
wrapper.orderByDesc(SysEmailLog::getCreateTime);
wrapper.last("limit 1");
SysEmailLog emailLog = getOne(wrapper, false);
if (ObjectUtil.isNull(emailLog))
return -1;
Date createTime = emailLog.getCreateTime();
if (ObjectUtil.isNull(createTime))
return -1;
// 获取最新一条数据和当前时间所间隔的分钟数
return Math.toIntExact(DateUtil.between(createTime, new Date(), DateUnit.MINUTE));
}
}

View File

@ -45,7 +45,6 @@ public interface AbnormalAlarmClient {
String getServerName(@RequestParam String id);
/* SysEmailLogController下相关接口 */
@GetMapping("/sysEmailLog/hoursAgo")
Boolean hoursAgo(@RequestParam String emailId,
@RequestParam String hoursAgo);
@GetMapping("/sysEmailLog/getMinus")
Integer getMinus(@RequestParam String emailId);
}

View File

@ -1,16 +1,12 @@
package org.jeecg.modules.quartz.jobs;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.DataTool;
import org.jeecg.common.util.NumUtil;
@ -21,16 +17,11 @@ import org.jeecg.modules.base.entity.postgre.AlarmLog;
import org.jeecg.modules.base.entity.postgre.AlarmRule;
import org.jeecg.modules.base.enums.Item;
import org.jeecg.modules.quartz.entity.Monitor;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.jeecg.modules.base.enums.SourceType.EMAIL;
import static org.jeecg.modules.base.enums.Template.MONITOR_EMAIL;
@ -39,9 +30,6 @@ import static org.jeecg.modules.base.enums.Template.MONITOR_EMAIL;
@Component
public class EmailJob extends Monitor{
@Value("${task.undealHours:2}")
private Integer undealHours; // 邮箱邮件未处理时长 单位: h 默认: 2h
/**
* 解析Email预警规则
**/
@ -82,8 +70,8 @@ public class EmailJob extends Monitor{
case EMAIL_CONN: // 监控项id 1: 测试邮箱服务是否可以连接成功
current = connectionStatus(sourceId);
break;
case EMAIL_PROCESS: // 监控项id 3: 邮箱邮件是否被自动处理程序处理
current = process(sourceId);
case EMAIL_UNPROCESSED: // 监控项id 3: 邮箱邮件是否被自动处理程序处理
current = unProcess(sourceId);
break;
// 追加的监控项...
default:
@ -141,18 +129,9 @@ public class EmailJob extends Monitor{
}
/*
* 监控项id: 3 邮箱邮件在一定时间内是否被处理 (预警值: 0 非0值则不需要报警)
* 监控项id: 3 邮箱邮件在一定时间内是否被自动处理程序处理
* */
private Integer process(String emailId){
int res = 1;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateConstant.DATE_TIME);
LocalDateTime minusHours = LocalDateTime.now().minusHours(undealHours);
LocalDateTime minusMinutes = LocalDateTime.now().minusMinutes(undealHours);
String hoursAgo = minusHours.format(formatter);
String minutesAgo = minusMinutes.format(formatter);
Boolean result = getAlarmClient().hoursAgo(emailId, minutesAgo);
if (ObjectUtil.isNotNull(result) && result)
res = 0;
return res;
private Integer unProcess(String emailId){
return getAlarmClient().getMinus(emailId);
}
}