定时任务提醒
continuous-integration/drone/push Build is passing Details

于相涌/robot_optimize
YXY 1 year ago
parent 59431a713e
commit 0b4b665d1b

@ -0,0 +1,25 @@
package com.ruoyi.flyingbook.domain;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* event_log
*
* @author ruoyi
* @date 2023-03-15
*/
@Data
public class LogCount implements Serializable {
private static final long serialVersionUID = 1L;
private Date startTime;
private Date endTime;
private String name;
private Integer count;
}

@ -2,6 +2,7 @@ package com.ruoyi.flyingbook.mapper;
import com.ruoyi.flyingbook.domain.EventLog;
import com.ruoyi.flyingbook.domain.LogCount;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -62,4 +63,6 @@ public interface EventLogMapper
* @return
*/
public int deleteEventLogByIds(Long[] ids);
public List<LogCount> countByType(LogCount eventLog);
}

@ -123,4 +123,19 @@
#{id}
</foreach>
</delete>
<select id="countByType" parameterType="com.ruoyi.flyingbook.domain.LogCount" resultType="com.ruoyi.flyingbook.domain.LogCount">
select operate_type as name,count(id) as `count`
from event_log
<where>
<if test="startTime != null">
<![CDATA[ and create_time >= #{startTime} ]]>
</if>
<if test="endTime != null">
<![CDATA[ and create_time <= #{endTime} ]]>
</if>
</where>
group by operate_type
</select>
</mapper>

@ -2,6 +2,7 @@ package com.ruoyi.quartz.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.quartz.task.MailSyncTask;
import com.ruoyi.quartz.task.MonitorJobTask;
import com.ruoyi.quartz.task.ScheduledRemindersTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,6 +18,8 @@ public class MailInfoController extends BaseController {
private MailSyncTask mailSyncTask;
@Autowired
private ScheduledRemindersTask scheduledRemindersTask;
@Autowired
private MonitorJobTask monitorJobTask;
@PostMapping("/syncEmail")
public void syncEmail(Boolean readFlag) {
@ -25,7 +28,8 @@ public class MailInfoController extends BaseController {
@PostMapping("/scheduledReminders")
public void scheduledReminders() {
scheduledRemindersTask.scheduledReminders();
monitorJobTask.monitor();
// scheduledRemindersTask.scheduledReminders();
}
}

@ -0,0 +1,21 @@
package com.ruoyi.quartz.domain;
import lombok.Data;
import java.io.Serializable;
/**
* sys_job_log
*
* @author ruoyi
*/
@Data
public class JobLogCount implements Serializable
{
private static final long serialVersionUID = 1L;
private String jobName;
private Integer count;
}

@ -1,6 +1,8 @@
package com.ruoyi.quartz.mapper;
import java.util.List;
import com.ruoyi.quartz.domain.JobLogCount;
import com.ruoyi.quartz.domain.SysJobLog;
/**
@ -61,4 +63,8 @@ public interface SysJobLogMapper
*
*/
public void cleanJobLog();
/**
*
*/
public List<JobLogCount> countJobLogByName(SysJobLog jobLog);
}

@ -0,0 +1,100 @@
package com.ruoyi.quartz.task;
import com.ruoyi.common.enums.EventOperateType;
import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper;
import com.ruoyi.flyingbook.domain.LogCount;
import com.ruoyi.flyingbook.mapper.EventLogMapper;
import com.ruoyi.flyingbook.mapper.EventMapper;
import com.ruoyi.flyingbook.service.IEventLogService;
import com.ruoyi.flyingbook.service.IEventService;
import com.ruoyi.quartz.domain.JobLogCount;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.mapper.SysJobLogMapper;
import com.ruoyi.quartz.service.ISysJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
/**
*
*
* @author ruoyi
*/
@Slf4j
@Component("monitorJobTask")
public class MonitorJobTask {
@Autowired
private SysJobLogMapper jobLogMapper;
@Autowired
private EventLogMapper eventLogMapper;
@Autowired
private LarkRobotHelper larkRobotHelper;
@Value("${lark.robot.group}")
private String robotGroup;
public void monitor() {
log.info("scheduledRemindersTask start");
Date todayStartTime = this.getTodayStartTime();
Date endTime = new Date();
Map<String, Integer> jobLogMap = getJobLogCountMap(todayStartTime,endTime);
Map<String, Integer> eventLogCountMap = getEventLogCountMap(todayStartTime,endTime);
Map<String, String> map = this.getMap();
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet()) {
Integer jobCount = jobLogMap.get(entry.getKey());
sb.append(entry.getKey()).append("\r\n");
if (jobCount != null && jobCount > 0){
Integer eventLogCount = eventLogCountMap.getOrDefault(entry.getValue(),0);
sb.append(String.format("执行成功%s条",eventLogCount));
}else {
sb.append("执行失败");
}
sb.append("\r\n");
}
larkRobotHelper.sendMessageByBot(robotGroup,sb.toString());
log.info("scheduledRemindersTask end");
}
private Date getTodayStartTime(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
private Map<String, Integer> getJobLogCountMap(Date startDate,Date endDate){
SysJobLog sysJobLog = new SysJobLog();
sysJobLog.setStartTime(startDate);
sysJobLog.setStopTime(endDate);
List<JobLogCount> jobLogCounts = jobLogMapper.countJobLogByName(sysJobLog);
return jobLogCounts.stream()
.collect(Collectors.toMap(JobLogCount::getJobName, JobLogCount::getCount, (k1, k2) -> k1));
}
private Map<String, Integer> getEventLogCountMap(Date startDate,Date endDate){
LogCount logCount = new LogCount();
logCount.setStartTime(startDate);
logCount.setEndTime(endDate);
List<LogCount> logCountList = eventLogMapper.countByType(logCount);
return logCountList.stream()
.collect(Collectors.toMap(LogCount::getName, LogCount::getCount, (k1, k2) -> k1));
}
private Map<String,String> getMap(){
Map<String,String> map = new HashMap<>();
map.put("邮箱信息同步到飞书-新",EventOperateType.SYNC_MAIL.getCode());
map.put("飞书定时提醒-新", EventOperateType.SCHEDULE_REMINDER.getCode());
return map;
}
}

@ -1,93 +1,120 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.quartz.mapper.SysJobLogMapper">
<resultMap type="SysJobLog" id="SysJobLogResult">
<id property="jobLogId" column="job_log_id" />
<result property="jobName" column="job_name" />
<result property="jobGroup" column="job_group" />
<result property="invokeTarget" column="invoke_target" />
<result property="jobMessage" column="job_message" />
<result property="status" column="status" />
<result property="exceptionInfo" column="exception_info" />
<result property="createTime" column="create_time" />
</resultMap>
<sql id="selectJobLogVo">
select job_log_id, job_name, job_group, invoke_target, job_message, status, exception_info, create_time
from sys_job_log
<resultMap type="SysJobLog" id="SysJobLogResult">
<id property="jobLogId" column="job_log_id"/>
<result property="jobName" column="job_name"/>
<result property="jobGroup" column="job_group"/>
<result property="invokeTarget" column="invoke_target"/>
<result property="jobMessage" column="job_message"/>
<result property="status" column="status"/>
<result property="exceptionInfo" column="exception_info"/>
<result property="createTime" column="create_time"/>
</resultMap>
<sql id="selectJobLogVo">
select job_log_id,
job_name,
job_group,
invoke_target,
job_message,
status,
exception_info,
create_time
from sys_job_log
</sql>
<select id="selectJobLogList" parameterType="SysJobLog" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
<where>
<if test="jobName != null and jobName != ''">
AND job_name like concat('%', #{jobName}, '%')
</if>
<if test="jobGroup != null and jobGroup != ''">
AND job_group = #{jobGroup}
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="invokeTarget != null and invokeTarget != ''">
AND invoke_target like concat('%', #{invokeTarget}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
</if>
</where>
</select>
<select id="selectJobLogAll" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
</select>
<select id="selectJobLogById" parameterType="Long" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
where job_log_id = #{jobLogId}
</select>
<delete id="deleteJobLogById" parameterType="Long">
delete from sys_job_log where job_log_id = #{jobLogId}
</delete>
<delete id="deleteJobLogByIds" parameterType="Long">
delete from sys_job_log where job_log_id in
<foreach collection="array" item="jobLogId" open="(" separator="," close=")">
#{jobLogId}
</foreach>
</delete>
<update id="cleanJobLog">
<select id="selectJobLogList" parameterType="SysJobLog" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
<where>
<if test="jobName != null and jobName != ''">
AND job_name like concat('%', #{jobName}, '%')
</if>
<if test="jobGroup != null and jobGroup != ''">
AND job_group = #{jobGroup}
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="invokeTarget != null and invokeTarget != ''">
AND invoke_target like concat('%', #{invokeTarget}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
</if>
</where>
</select>
<select id="countJobLogByName" parameterType="SysJobLog" resultType="com.ruoyi.quartz.domain.JobLogCount">
select job_name as jobName,count(job_log_id) as `count`
from sys_job_log
<where>
<if test="jobName != null and jobName != ''">
AND job_name like concat('%', #{jobName}, '%')
</if>
<if test="startTime != null">
<![CDATA[ and create_time >= #{startTime} ]]>
</if>
<if test="stopTime != null">
<![CDATA[ and create_time <= #{stopTime} ]]>
</if>
</where>
group by job_name
</select>
<select id="selectJobLogAll" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
</select>
<select id="selectJobLogById" parameterType="Long" resultMap="SysJobLogResult">
<include refid="selectJobLogVo"/>
where job_log_id = #{jobLogId}
</select>
<delete id="deleteJobLogById" parameterType="Long">
delete
from sys_job_log
where job_log_id = #{jobLogId}
</delete>
<delete id="deleteJobLogByIds" parameterType="Long">
delete from sys_job_log where job_log_id in
<foreach collection="array" item="jobLogId" open="(" separator="," close=")">
#{jobLogId}
</foreach>
</delete>
<update id="cleanJobLog">
truncate table sys_job_log
</update>
<insert id="insertJobLog" parameterType="SysJobLog">
insert into sys_job_log(
<if test="jobLogId != null and jobLogId != 0">job_log_id,</if>
<if test="jobName != null and jobName != ''">job_name,</if>
<if test="jobGroup != null and jobGroup != ''">job_group,</if>
<if test="invokeTarget != null and invokeTarget != ''">invoke_target,</if>
<if test="jobMessage != null and jobMessage != ''">job_message,</if>
<if test="status != null and status != ''">status,</if>
<if test="exceptionInfo != null and exceptionInfo != ''">exception_info,</if>
create_time
)values(
<if test="jobLogId != null and jobLogId != 0">#{jobLogId},</if>
<if test="jobName != null and jobName != ''">#{jobName},</if>
<if test="jobGroup != null and jobGroup != ''">#{jobGroup},</if>
<if test="invokeTarget != null and invokeTarget != ''">#{invokeTarget},</if>
<if test="jobMessage != null and jobMessage != ''">#{jobMessage},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="exceptionInfo != null and exceptionInfo != ''">#{exceptionInfo},</if>
sysdate()
)
</insert>
<insert id="insertJobLog" parameterType="SysJobLog">
insert into sys_job_log(
<if test="jobLogId != null and jobLogId != 0">job_log_id,</if>
<if test="jobName != null and jobName != ''">job_name,</if>
<if test="jobGroup != null and jobGroup != ''">job_group,</if>
<if test="invokeTarget != null and invokeTarget != ''">invoke_target,</if>
<if test="jobMessage != null and jobMessage != ''">job_message,</if>
<if test="status != null and status != ''">status,</if>
<if test="exceptionInfo != null and exceptionInfo != ''">exception_info,</if>
create_time
)values(
<if test="jobLogId != null and jobLogId != 0">#{jobLogId},</if>
<if test="jobName != null and jobName != ''">#{jobName},</if>
<if test="jobGroup != null and jobGroup != ''">#{jobGroup},</if>
<if test="invokeTarget != null and invokeTarget != ''">#{invokeTarget},</if>
<if test="jobMessage != null and jobMessage != ''">#{jobMessage},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="exceptionInfo != null and exceptionInfo != ''">#{exceptionInfo},</if>
sysdate()
)
</insert>
</mapper>
Loading…
Cancel
Save