AlertDefineController.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.usthe.alert.controller;
  2. import com.usthe.alert.pojo.entity.AlertDefine;
  3. import com.usthe.alert.service.AlertDefineService;
  4. import com.usthe.common.entity.dto.Message;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import io.swagger.annotations.ApiParam;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.DeleteMapping;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.PathVariable;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.PutMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.validation.Valid;
  19. import java.util.Map;
  20. import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST;
  21. import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
  22. /**
  23. * 告警定义管理API
  24. * @author tom
  25. * @date 2021/12/9 10:32
  26. */
  27. @Api(tags = "告警定义管理API")
  28. @RestController
  29. @RequestMapping(path = "/alert/define", produces = {APPLICATION_JSON_VALUE})
  30. public class AlertDefineController {
  31. @Autowired
  32. private AlertDefineService alertDefineService;
  33. @PostMapping
  34. @ApiOperation(value = "新增告警定义", notes = "新增一个告警定义")
  35. public ResponseEntity<Message<Void>> addNewAlertDefine(@Valid @RequestBody AlertDefine alertDefine) {
  36. // 校验请求数据
  37. alertDefineService.validate(alertDefine, false);
  38. alertDefineService.addAlertDefine(alertDefine);
  39. return ResponseEntity.ok(new Message<>("Add success"));
  40. }
  41. @PutMapping
  42. @ApiOperation(value = "修改告警定义", notes = "修改一个已存在告警定义")
  43. public ResponseEntity<Message<Void>> modifyAlertDefine(@Valid @RequestBody AlertDefine alertDefine) {
  44. // 校验请求数据
  45. alertDefineService.validate(alertDefine, true);
  46. alertDefineService.modifyAlertDefine(alertDefine);
  47. return ResponseEntity.ok(new Message<>("Modify success"));
  48. }
  49. @GetMapping(path = "/{id}")
  50. @ApiOperation(value = "查询告警定义", notes = "根据告警定义ID获取告警定义信息")
  51. public ResponseEntity<Message<AlertDefine>> getAlertDefine(
  52. @ApiParam(value = "告警定义ID", example = "6565463543") @PathVariable("id") long id) {
  53. // 获取监控信息
  54. AlertDefine alertDefine = alertDefineService.getAlertDefine(id);
  55. Message.MessageBuilder<AlertDefine> messageBuilder = Message.builder();
  56. if (alertDefine == null) {
  57. messageBuilder.code(MONITOR_NOT_EXIST).msg("AlertDefine not exist.");
  58. } else {
  59. messageBuilder.data(alertDefine);
  60. }
  61. return ResponseEntity.ok(messageBuilder.build());
  62. }
  63. @DeleteMapping(path = "/{id}")
  64. @ApiOperation(value = "删除告警定义", notes = "根据告警定义ID删除告警定义,告警定义不存在也是删除成功")
  65. public ResponseEntity<Message<Void>> deleteAlertDefine(
  66. @ApiParam(value = "告警定义ID", example = "6565463543") @PathVariable("id") long id) {
  67. // 删除告警定义不存在或删除成功都返回成功
  68. alertDefineService.deleteAlertDefine(id);
  69. return ResponseEntity.ok(new Message<>("Delete success"));
  70. }
  71. @PostMapping(path = "/{alertId}/monitors")
  72. @ApiOperation(value = "应用告警定义与监控关联", notes = "应用指定告警定义与监控关联关系")
  73. public ResponseEntity<Message<Void>> applyAlertDefineMonitorsBind(
  74. @ApiParam(value = "告警定义ID", example = "6565463543") @PathVariable("alertId") long alertId,
  75. @RequestBody Map<Long, String> monitorMap) {
  76. alertDefineService.applyBindAlertDefineMonitors(alertId, monitorMap);
  77. return ResponseEntity.ok(new Message<>("Apply success"));
  78. }
  79. }