feat:ararm信息查询
This commit is contained in:
parent
a9682bf4dc
commit
406fefc5c7
|
@ -11,12 +11,22 @@
|
|||
|
||||
<artifactId>jeecg-module-abnormal-alarm</artifactId>
|
||||
|
||||
<properties>
|
||||
<hutool.version>5.8.19</hutool.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>-->
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -4,6 +4,7 @@ import org.jeecg.common.api.QueryRequest;
|
|||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.entity.AlarmLog;
|
||||
import org.jeecg.modules.service.IAlarmLogService;
|
||||
import org.jeecg.modules.vo.AlarmVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -14,9 +15,22 @@ public class AlarmLogController {
|
|||
@Autowired
|
||||
private IAlarmLogService alarmLogService;
|
||||
|
||||
@GetMapping("viewAll")
|
||||
public Result viewAll(@RequestBody AlarmVo alarmVo){ return alarmLogService.viewAll(alarmVo); }
|
||||
|
||||
@GetMapping("findPage")
|
||||
public Result findPage(QueryRequest queryRequest, AlarmLog alarmLog){
|
||||
return alarmLogService.findPage(queryRequest, alarmLog);
|
||||
public Result findPage(@RequestBody AlarmVo alarmVo){
|
||||
return alarmLogService.findPage(alarmVo);
|
||||
}
|
||||
|
||||
@GetMapping("typeAlarms")
|
||||
public Result typeAlarms(@RequestBody AlarmVo alarmVo){
|
||||
return alarmLogService.typeAlarms(alarmVo);
|
||||
}
|
||||
|
||||
@GetMapping("ruleTop")
|
||||
public Result ruleTop5(@RequestBody AlarmVo alarmVo){
|
||||
return alarmLogService.ruleTop5(alarmVo);
|
||||
}
|
||||
|
||||
@GetMapping("findInfo")
|
||||
|
@ -38,7 +52,4 @@ public class AlarmLogController {
|
|||
public Result deleteById(String id){
|
||||
return alarmLogService.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package org.jeecg.modules.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TypeDto {
|
||||
// 1.资源类型 Server|Database|Email
|
||||
// 2.规则名
|
||||
private String name;
|
||||
// 1.报警量
|
||||
// 2.规则使用量
|
||||
private Integer value;
|
||||
}
|
|
@ -19,4 +19,5 @@ public class AlarmHistory {
|
|||
|
||||
private String operator;
|
||||
|
||||
private String sourceType;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,24 @@
|
|||
package org.jeecg.modules.mapper;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.jeecg.modules.dto.TypeDto;
|
||||
import org.jeecg.modules.entity.AlarmHistory;
|
||||
import org.jeecg.modules.entity.AlarmLog;
|
||||
import org.jeecg.modules.vo.AlarmVo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface AlarmLogMapper extends BaseMapper<AlarmLog> {
|
||||
|
||||
List<Date> oneDay(Map<String,Object> params);
|
||||
|
||||
List<Date> oneMoreDay(Map<String,Object> params);
|
||||
List<AlarmHistory> findPage(Map<String,Object> params);
|
||||
|
||||
List<TypeDto> typeAlarms(Map<String,Object> params);
|
||||
|
||||
List<TypeDto> ruleTop5(Map<String,Object> params);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
<?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">
|
||||
<mapper namespace="org.jeecg.modules.mapper.AlarmLogMapper">
|
||||
<select id="findPage" parameterType="Map" resultType="org.jeecg.modules.entity.AlarmHistory">
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
(SELECT
|
||||
l.alarm_start_date,
|
||||
l.alarm_info,
|
||||
r.OPERATOR,
|
||||
r.source_type,
|
||||
CASE
|
||||
r.source_type
|
||||
WHEN 'Server' THEN
|
||||
( SELECT NAME FROM sys_server comm WHERE ID = r.source_id )
|
||||
WHEN 'Database' THEN
|
||||
( SELECT NAME FROM sys_database comm WHERE ID = r.source_id )
|
||||
WHEN 'Email' THEN
|
||||
( SELECT NAME FROM sys_email WHERE ID = r.source_id )
|
||||
END AS NAME
|
||||
FROM
|
||||
alarm_log l
|
||||
INNER JOIN alarm_rule r ON l.rule_id = r.ID
|
||||
) AS res
|
||||
<where>
|
||||
<if test="type != null and type != ''">
|
||||
source_type = #{type}
|
||||
</if>
|
||||
<if test="name != null and name != ''">
|
||||
AND NAME LIKE concat ( '%', #{name} , '%' )
|
||||
</if>
|
||||
<if test="startDate != null and startDate != ''">
|
||||
AND alarm_start_date >= #{startDate}
|
||||
</if>
|
||||
<if test="endDate != null and endDate != ''">
|
||||
AND alarm_start_date <= #{endDate}
|
||||
</if>
|
||||
</where>
|
||||
LIMIT #{pageStart}, #{pageSize}
|
||||
</select>
|
||||
<select id="typeAlarms" parameterType="Map" resultType="org.jeecg.modules.dto.TypeDto">
|
||||
SELECT
|
||||
r.source_type AS name,
|
||||
COUNT (r.source_type) AS value
|
||||
FROM
|
||||
alarm_log l
|
||||
INNER JOIN alarm_rule r ON l.rule_id = r.ID
|
||||
<where>
|
||||
<if test="type != null and type != ''">
|
||||
r.source_type = #{type}
|
||||
</if>
|
||||
<if test="startDate != null and startDate != ''">
|
||||
AND l.alarm_start_date >= #{startDate}
|
||||
</if>
|
||||
<if test="endDate != null and endDate != ''">
|
||||
AND l.alarm_start_date <= #{endDate}
|
||||
</if>
|
||||
</where>
|
||||
GROUP BY r.source_type
|
||||
</select>
|
||||
<select id="ruleTop5" parameterType="Map" resultType="org.jeecg.modules.dto.TypeDto">
|
||||
SELECT
|
||||
r.name,
|
||||
l.ruleCount AS value
|
||||
FROM (
|
||||
SELECT
|
||||
rule_id,
|
||||
COUNT (rule_id) AS ruleCount
|
||||
FROM
|
||||
alarm_log
|
||||
<where>
|
||||
<if test="startDate != null and startDate != ''">
|
||||
alarm_start_date >= #{startDate}
|
||||
</if>
|
||||
<if test="endDate != null and endDate != ''">
|
||||
AND alarm_start_date <= #{endDate}
|
||||
</if>
|
||||
</where>
|
||||
GROUP BY rule_id
|
||||
LIMIT 5
|
||||
) AS l
|
||||
INNER JOIN alarm_rule r ON l.rule_id = r.id
|
||||
<where>
|
||||
<if test="type != null and type != ''">
|
||||
r.source_type = #{type}
|
||||
</if>
|
||||
</where>
|
||||
ORDER BY l.ruleCount DESC
|
||||
</select>
|
||||
<select id="oneDay" parameterType="Map" resultType="Date">
|
||||
SELECT
|
||||
l.alarm_start_date
|
||||
FROM
|
||||
alarm_log l
|
||||
INNER JOIN alarm_rule r ON l.rule_id = r.ID
|
||||
<where>
|
||||
<if test="type != null and type != ''">
|
||||
r.source_type = #{type}
|
||||
</if>
|
||||
<if test="startDate != null and startDate != ''">
|
||||
AND l.alarm_start_date = #{startDate}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<select id="oneMoreDay" parameterType="Map" resultType="Date">
|
||||
SELECT
|
||||
l.alarm_start_date
|
||||
FROM
|
||||
alarm_log l
|
||||
INNER JOIN alarm_rule r ON l.rule_id = r.ID
|
||||
<where>
|
||||
<if test="type != null and type != ''">
|
||||
r.source_type = #{type}
|
||||
</if>
|
||||
<if test="startDate != null and startDate != ''">
|
||||
AND l.alarm_start_date >= #{startDate}
|
||||
</if>
|
||||
<if test="endDate != null and endDate != ''">
|
||||
AND l.alarm_start_date <= #{endDate}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
|
@ -4,10 +4,17 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
import org.jeecg.common.api.QueryRequest;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.entity.AlarmLog;
|
||||
import org.jeecg.modules.vo.AlarmVo;
|
||||
|
||||
public interface IAlarmLogService extends IService<AlarmLog> {
|
||||
|
||||
Result findPage(QueryRequest queryRequest, AlarmLog alarmLog);
|
||||
Result viewAll(AlarmVo alarmVo);
|
||||
|
||||
Result findPage(AlarmVo alarmVo);
|
||||
|
||||
Result typeAlarms(AlarmVo alarmVo);
|
||||
|
||||
Result ruleTop5(AlarmVo alarmVo);
|
||||
|
||||
Result findInfo(String id);
|
||||
|
||||
|
|
|
@ -1,32 +1,183 @@
|
|||
package org.jeecg.modules.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.jeecg.common.api.QueryRequest;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.dto.TypeDto;
|
||||
import org.jeecg.modules.entity.AlarmHistory;
|
||||
import org.jeecg.modules.entity.AlarmLog;
|
||||
import org.jeecg.modules.mapper.AlarmLogMapper;
|
||||
import org.jeecg.modules.service.IAlarmLogService;
|
||||
import org.jeecg.modules.vo.AlarmVo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service("alarmLogService")
|
||||
public class AlarmLogServiceImpl extends ServiceImpl<AlarmLogMapper, AlarmLog> implements IAlarmLogService {
|
||||
|
||||
/**
|
||||
* 总体统计柱状图
|
||||
* @param alarmVo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result findPage(QueryRequest queryRequest, AlarmLog alarmLog) {
|
||||
Result result = new Result();
|
||||
Page<AlarmLog> page = new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize());
|
||||
LambdaQueryWrapper<AlarmLog> queryWrapper = new LambdaQueryWrapper<>();
|
||||
Page<AlarmLog> alarmLogPage = this.baseMapper.selectPage(page, queryWrapper);
|
||||
result.setSuccess(true);
|
||||
result.setResult(alarmLogPage);
|
||||
return result;
|
||||
public Result viewAll(AlarmVo alarmVo) {
|
||||
String startDate = alarmVo.getStartDate();
|
||||
String endDate = alarmVo.getEndDate();
|
||||
Map<String, Object> params = BeanUtil.beanToMap(alarmVo);
|
||||
List<Date> allDate;
|
||||
/* 选择日期为同一天 按照小时划分 0-23小时 */
|
||||
if (startDate.equals(endDate)){
|
||||
allDate = baseMapper.oneDay(params);
|
||||
Map<Integer, List<Date>> groupTime = allDate.stream()
|
||||
.sorted(Comparator.comparing(Date::getHours))
|
||||
.collect(Collectors.groupingBy(Date::getHours));
|
||||
Map<String,Integer> statistic = new HashMap<>();
|
||||
for (int i = 0; i < 24; i++) {
|
||||
if(groupTime.containsKey(i)){
|
||||
Integer count = groupTime.get(i).size();
|
||||
statistic.put(timeStr(i),count);
|
||||
}else {
|
||||
statistic.put(timeStr(i),0);
|
||||
}
|
||||
}
|
||||
// 返回x轴和y轴数据
|
||||
Set<String> xData = statistic.keySet();
|
||||
Collection<Integer> YData = statistic.values();
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("xData",xData);
|
||||
result.put("yData",YData);
|
||||
return Result.OK(result);
|
||||
}
|
||||
/* 选择日期不是同一天 按照天划分(可以跨月选择,但不包括跨年) */
|
||||
else {
|
||||
allDate = baseMapper.oneMoreDay(params);
|
||||
|
||||
Map<String,Integer> statistic = new HashMap<>();
|
||||
// 通过mounth分组
|
||||
Map<Integer, List<Date>> groupMounth = allDate.stream()
|
||||
.sorted(Comparator.comparing(Date::getMonth))
|
||||
.collect(Collectors.groupingBy(Date::getMonth));
|
||||
// 通过day分组
|
||||
for (Map.Entry<Integer, List<Date>> entry : groupMounth.entrySet()) {
|
||||
Integer mounth = entry.getKey() + 1;// 当前月份
|
||||
List<Date> allDay = entry.getValue(); // 当前月份有报警的day
|
||||
Map<Integer, List<Date>> groupDay = allDay.stream()
|
||||
.sorted(Comparator.comparing(Date::getDate))
|
||||
.collect(Collectors.groupingBy(Date::getDate));
|
||||
for (Map.Entry<Integer, List<Date>> entryDay : groupDay.entrySet()) {
|
||||
Integer day = entryDay.getKey(); // 当前是几号
|
||||
Integer count = entryDay.getValue().size();
|
||||
statistic.put(dateStr(mounth,day),count);
|
||||
}
|
||||
}
|
||||
// 列举startDate和endDate之间所有日期(已考虑闰年情况)
|
||||
LocalDate start = LocalDate.parse(startDate);
|
||||
LocalDate end = LocalDate.parse(endDate);
|
||||
List<LocalDate> dateList = new ArrayList<>();
|
||||
LocalDate current = start;
|
||||
while (!current.isAfter(end)) {
|
||||
dateList.add(current);
|
||||
current = current.plusDays(1);
|
||||
}
|
||||
List<String> allDateStr = dateList.stream()
|
||||
.map(item -> {
|
||||
// 2023-06-15 格式截取为 06-15
|
||||
String dateStr = item
|
||||
.format(DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
.substring(5);
|
||||
return dateStr;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
// 将startDate和endDate中没有预警信息的day设置为0
|
||||
for (String dateStr : allDateStr) {
|
||||
if (!allDateStr.contains(dateStr)){
|
||||
statistic.put(dateStr,0);
|
||||
}
|
||||
}
|
||||
// 返回x轴和y轴数据
|
||||
Set<String> xData = statistic.keySet();
|
||||
Collection<Integer> YData = statistic.values();
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("xData",xData);
|
||||
result.put("yData",YData);
|
||||
return Result.OK(result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 报警信息分页列表
|
||||
* @param alarmVo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result findPage(AlarmVo alarmVo) {
|
||||
Integer pageNo = alarmVo.getPageNo();
|
||||
Integer pageSize = alarmVo.getPageSize();
|
||||
Page<AlarmHistory> page = new Page<>(pageNo,pageSize);
|
||||
Integer pageStart = (pageNo - 1) * pageSize;
|
||||
alarmVo.setPageStart(pageStart);
|
||||
Map<String, Object> params = BeanUtil.beanToMap(alarmVo);
|
||||
List<AlarmHistory> alarmHistories = baseMapper.findPage(params);
|
||||
// 当前页数据
|
||||
page.setRecords(alarmHistories);
|
||||
// 数据总条数
|
||||
page.setTotal(this.count());
|
||||
return Result.OK(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* ALARM ANALYSIS -> Monitor Type Alarms
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result typeAlarms(AlarmVo alarmVo) {
|
||||
/* 饼图数据 */
|
||||
// 警报类型-警报数
|
||||
Map<String, Object> params = BeanUtil.beanToMap(alarmVo);
|
||||
List<TypeDto> typeAlarms = baseMapper.typeAlarms(params);
|
||||
// 警报总数
|
||||
Integer total = typeAlarms.stream().mapToInt(TypeDto::getValue).sum();
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("pieData",typeAlarms);
|
||||
result.put("pieTotal",total);
|
||||
return Result.OK(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* ALARM ANALYSIS -> Alarms Rule Top5
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result ruleTop5(AlarmVo alarmVo) {
|
||||
/* 柱状图数据 */
|
||||
Map<String, Object> params = BeanUtil.beanToMap(alarmVo);
|
||||
List<TypeDto> ruleTop5 = baseMapper.ruleTop5(params);
|
||||
// x轴数据
|
||||
List<String> xData = ruleTop5.stream()
|
||||
.map(TypeDto::getName)
|
||||
.collect(Collectors.toList());
|
||||
// y轴数据
|
||||
List<Integer> yData = ruleTop5.stream()
|
||||
.map(TypeDto::getValue)
|
||||
.collect(Collectors.toList());
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("xData",xData);
|
||||
result.put("yData",yData);
|
||||
return Result.OK(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -88,4 +239,36 @@ public class AlarmLogServiceImpl extends ServiceImpl<AlarmLogMapper, AlarmLog> i
|
|||
return result;
|
||||
}
|
||||
|
||||
private String timeStr(Integer time){
|
||||
if (time < 10){
|
||||
return "0" + time + ":00";
|
||||
}
|
||||
return time + ":00";
|
||||
}
|
||||
|
||||
private String dateStr(Integer mounth,Integer day){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (mounth < 10){
|
||||
sb.append("0").append(mounth).append("-");
|
||||
}else {
|
||||
sb.append(mounth).append("-");
|
||||
}
|
||||
if (day < 10){
|
||||
sb.append("0").append(day);
|
||||
}else {
|
||||
sb.append(day);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
List<Date> allDate = new ArrayList<>();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date date1 = sdf.parse("2013-04-29");
|
||||
Date date2 = sdf.parse("2013-04-29");
|
||||
Date date3 = sdf.parse("2013-05-01");
|
||||
Date date4 = sdf.parse("2013-05-02");
|
||||
allDate.addAll(Arrays.asList(date1,date2,date3,date4));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package org.jeecg.modules.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jeecg.common.api.QueryRequest;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class AlarmVo extends QueryRequest implements Serializable {
|
||||
private String name;
|
||||
private String type;
|
||||
private String startDate;
|
||||
private String endDate;
|
||||
private Integer pageStart;
|
||||
}
|
Loading…
Reference in New Issue
Block a user