AlertDefineController.java 4.6 KB

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