| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.lims.pojo.RealtimeMonitor; |
| | | import com.ruoyi.lims.service.LimsAlertService; |
| | | import com.ruoyi.lims.service.RealtimeMonitorService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @RestController |
| | | @AllArgsConstructor |
| | |
| | | public class RealtimeMonitorController { |
| | | |
| | | private RealtimeMonitorService realtimeMonitorservice; |
| | | private LimsAlertService alertService; |
| | | private com.ruoyi.lims.service.AlertRecordService alertRecordService; |
| | | |
| | | @GetMapping("/listPage") |
| | | @ApiOperation("分页查询所有实时监控与预警表") |
| | |
| | | if (CollectionUtils.isEmpty(ids)) return AjaxResult.error("请传入要删除的ID"); |
| | | return AjaxResult.success(realtimeMonitorservice.removeBatchByIds(ids)); |
| | | } |
| | | |
| | | @GetMapping("/{id}") |
| | | @ApiOperation("根据ID查询监控项") |
| | | public AjaxResult getById(@PathVariable Long id) { |
| | | return AjaxResult.success(realtimeMonitorservice.getById(id)); |
| | | } |
| | | |
| | | /* ==================== ⑥ 预警判定与告警闭环 ==================== */ |
| | | |
| | | /** |
| | | * 手动检查单个监控项是否越限 |
| | | */ |
| | | @PostMapping("/check/{id}") |
| | | @ApiOperation("检查单个监控项是否越限") |
| | | public AjaxResult check(@PathVariable Long id) { |
| | | Map<String, Object> r = alertService.checkOne(id); |
| | | return AjaxResult.success("检查完成", r); |
| | | } |
| | | |
| | | /** |
| | | * 扫描所有监控项做一次判定 |
| | | */ |
| | | @PostMapping("/checkAll") |
| | | @ApiOperation("扫描所有监控项判定越限") |
| | | public AjaxResult checkAll(@RequestParam(defaultValue = "500") int limit) { |
| | | return AjaxResult.success("检查完成", alertService.checkAll(limit)); |
| | | } |
| | | |
| | | /** |
| | | * 把采集记录同步到监控项并立即判定 |
| | | */ |
| | | @PostMapping("/sync/{collectionId}") |
| | | @ApiOperation("采集数据同步到监控项并判定") |
| | | public AjaxResult sync(@PathVariable Long collectionId) { |
| | | Map<String, Object> r = alertService.syncFromCollection(collectionId); |
| | | return r.containsKey("message") && r.get("monitorId") == null |
| | | ? AjaxResult.error(String.valueOf(r.get("message"))) |
| | | : AjaxResult.success("同步完成", r); |
| | | } |
| | | |
| | | /** |
| | | * 告警记录列表 |
| | | */ |
| | | @GetMapping("/alerts") |
| | | @ApiOperation("告警记录列表") |
| | | public AjaxResult alerts(com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.ruoyi.lims.pojo.AlertRecord> page, |
| | | com.ruoyi.lims.pojo.AlertRecord query) { |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.ruoyi.lims.pojo.AlertRecord> w = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | if (query != null) { |
| | | if (query.getMonitorId() != null) w.eq("monitor_id", query.getMonitorId()); |
| | | if (query.getStatus() != null && !query.getStatus().trim().isEmpty()) w.eq("status", query.getStatus()); |
| | | if (query.getAlertLevel() != null && !query.getAlertLevel().trim().isEmpty()) w.eq("alert_level", query.getAlertLevel()); |
| | | if (query.getMonitorCode() != null && !query.getMonitorCode().trim().isEmpty()) w.like("monitor_code", query.getMonitorCode()); |
| | | } |
| | | w.orderByDesc("alert_time").orderByDesc("id"); |
| | | return AjaxResult.success(alertRecordService.page(page, w)); |
| | | } |
| | | |
| | | /** |
| | | * 处理告警(闭环) |
| | | */ |
| | | @PostMapping("/alert/handle") |
| | | @ApiOperation("处理告警") |
| | | public AjaxResult handleAlert(@RequestBody Map<String, Object> body) { |
| | | Long alertId = null; |
| | | try { alertId = Long.valueOf(String.valueOf(body.get("alertId"))); } catch (Exception ignore) {} |
| | | if (alertId == null) return AjaxResult.error("alertId 不能为空"); |
| | | String status = body.get("status") == null ? "resolved" : String.valueOf(body.get("status")); |
| | | String measure = body.get("measure") == null ? null : String.valueOf(body.get("measure")); |
| | | String handler; |
| | | try { handler = SecurityUtils.getUsername(); } catch (Exception e) { handler = "system"; } |
| | | Map<String, Object> r = alertService.handleAlert(alertId, status, measure, handler); |
| | | return AjaxResult.success(String.valueOf(r.get("message")), r); |
| | | } |
| | | |
| | | /** |
| | | * 告警统计 |
| | | */ |
| | | @GetMapping("/alertStatistics") |
| | | @ApiOperation("告警统计") |
| | | public AjaxResult alertStatistics() { |
| | | return AjaxResult.success(alertService.statistics()); |
| | | } |
| | | |
| | | /** |
| | | * 监控看板:监控项状态汇总 |
| | | */ |
| | | @GetMapping("/dashboard") |
| | | @ApiOperation("实时监控看板") |
| | | public AjaxResult dashboard() { |
| | | Map<String, Object> ret = new LinkedHashMap<>(); |
| | | long total = realtimeMonitorservice.count(); |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.ruoyi.lims.pojo.RealtimeMonitor> w1 = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | w1.eq("alert_status", "alert"); |
| | | long alerting = realtimeMonitorservice.count(w1); |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.ruoyi.lims.pojo.RealtimeMonitor> w2 = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | w2.eq("alert_status", "warning"); |
| | | long warning = realtimeMonitorservice.count(w2); |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.ruoyi.lims.pojo.RealtimeMonitor> w3 = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | w3.and(x -> x.eq("alert_status", "normal").or().isNull("alert_status")); |
| | | long normal = realtimeMonitorservice.count(w3); |
| | | |
| | | ret.put("total", total); |
| | | ret.put("alerting", alerting); |
| | | ret.put("warning", warning); |
| | | ret.put("normal", normal); |
| | | ret.put("alerts", alertService.statistics()); |
| | | return AjaxResult.success(ret); |
| | | }} |