Skip to content

Commit

Permalink
[Refactor] Use static methods instead of constructors for Message.java (
Browse files Browse the repository at this point in the history
#1247)

Co-authored-by: Musk.Chen <Musk.Chen@fanruan.com>
  • Loading branch information
gcdd1993 and Musk.Chen authored Sep 13, 2023
1 parent 70f2f04 commit 45bc5de
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
/**
* Alarm Converge management API
* 告警收敛管理API
* @author tom
*
* @author tom
*/
@Tag(name = "Alert Converge API | 告警收敛管理API")
@RestController
Expand All @@ -51,15 +51,15 @@ public class AlertConvergeController {
public ResponseEntity<Message<Void>> addNewAlertConverge(@Valid @RequestBody AlertConverge alertConverge) {
alertConvergeService.validate(alertConverge, false);
alertConvergeService.addAlertConverge(alertConverge);
return ResponseEntity.ok(new Message<>("Add success"));
return ResponseEntity.ok(Message.success("Add success"));
}

@PutMapping
@Operation(summary = "Modifying an Alarm Converge | 修改告警收敛", description = "Modify an existing alarm Converge | 修改一个已存在告警收敛")
public ResponseEntity<Message<Void>> modifyAlertConverge(@Valid @RequestBody AlertConverge alertConverge) {
alertConvergeService.validate(alertConverge, true);
alertConvergeService.modifyAlertConverge(alertConverge);
return ResponseEntity.ok(new Message<>("Modify success"));
return ResponseEntity.ok(Message.success("Modify success"));
}

@GetMapping(path = "/{id}")
Expand All @@ -68,13 +68,11 @@ public ResponseEntity<Message<Void>> modifyAlertConverge(@Valid @RequestBody Ale
public ResponseEntity<Message<AlertConverge>> getAlertConverge(
@Parameter(description = "Alarm Converge ID | 告警收敛ID", example = "6565463543") @PathVariable("id") long id) {
AlertConverge alertConverge = alertConvergeService.getAlertConverge(id);
Message.MessageBuilder<AlertConverge> messageBuilder = Message.builder();
if (alertConverge == null) {
messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("AlertConverge not exist.");
return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertConverge not exist."));
} else {
messageBuilder.data(alertConverge);
return ResponseEntity.ok(Message.success(alertConverge));
}
return ResponseEntity.ok(messageBuilder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
/**
* Converge the batch API for alarms
* 收敛告警批量API
* @author tom
*
* @author tom
*/
@Tag(name = "Alert Converge Batch API | 告警收敛管理API")
@RestController
Expand All @@ -66,7 +66,7 @@ public ResponseEntity<Message<Page<AlertConverge>>> getAlertConverges(
Specification<AlertConverge> specification = (root, query, criteriaBuilder) -> {
List<Predicate> andList = new ArrayList<>();
if (ids != null && !ids.isEmpty()) {
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
for (long id : ids) {
inPredicate.value(id);
}
Expand All @@ -77,9 +77,8 @@ public ResponseEntity<Message<Page<AlertConverge>>> getAlertConverges(
};
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<AlertConverge> alertConvergePage = alertConvergeService.getAlertConverges(specification,pageRequest);
Message<Page<AlertConverge>> message = new Message<>(alertConvergePage);
return ResponseEntity.ok(message);
Page<AlertConverge> alertConvergePage = alertConvergeService.getAlertConverges(specification, pageRequest);
return ResponseEntity.ok(Message.success(alertConvergePage));
}

@DeleteMapping
Expand All @@ -91,8 +90,7 @@ public ResponseEntity<Message<Void>> deleteAlertDefines(
if (ids != null && !ids.isEmpty()) {
alertConvergeService.deleteAlertConverges(new HashSet<>(ids));
}
Message<Void> message = new Message<>();
return ResponseEntity.ok(message);
return ResponseEntity.ok(Message.success());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
/**
* Alarm definition management API
* 告警定义管理API
* @author tom
*
* @author tom
*/
@Tag(name = "Alert Define API | 告警定义管理API")
@RestController
Expand All @@ -64,7 +64,7 @@ public ResponseEntity<Message<Void>> addNewAlertDefine(@Valid @RequestBody Alert
// 校验请求数据
alertDefineService.validate(alertDefine, false);
alertDefineService.addAlertDefine(alertDefine);
return ResponseEntity.ok(new Message<>("Add success"));
return ResponseEntity.ok(Message.success("Add success"));
}

@PutMapping
Expand All @@ -74,7 +74,7 @@ public ResponseEntity<Message<Void>> modifyAlertDefine(@Valid @RequestBody Alert
// 校验请求数据
alertDefineService.validate(alertDefine, true);
alertDefineService.modifyAlertDefine(alertDefine);
return ResponseEntity.ok(new Message<>("Modify success"));
return ResponseEntity.ok(Message.success("Modify success"));
}

@GetMapping(path = "/{id}")
Expand All @@ -85,13 +85,11 @@ public ResponseEntity<Message<AlertDefine>> getAlertDefine(
// Obtaining Monitoring Information
// 获取监控信息
AlertDefine alertDefine = alertDefineService.getAlertDefine(id);
Message.MessageBuilder<AlertDefine> messageBuilder = Message.builder();
if (alertDefine == null) {
messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("AlertDefine not exist.");
return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertDefine not exist."));
} else {
messageBuilder.data(alertDefine);
return ResponseEntity.ok(Message.success(alertDefine));
}
return ResponseEntity.ok(messageBuilder.build());
}

@DeleteMapping(path = "/{id}")
Expand All @@ -102,7 +100,7 @@ public ResponseEntity<Message<Void>> deleteAlertDefine(
// If the alarm definition does not exist or is deleted successfully, the deletion succeeds
// 删除告警定义不存在或删除成功都返回成功
alertDefineService.deleteAlertDefine(id);
return ResponseEntity.ok(new Message<>("Delete success"));
return ResponseEntity.ok(Message.success("Delete success"));
}

@PostMapping(path = "/{alertDefineId}/monitors")
Expand All @@ -112,7 +110,7 @@ public ResponseEntity<Message<Void>> applyAlertDefineMonitorsBind(
@Parameter(description = "Alarm Definition ID | 告警定义ID", example = "6565463543") @PathVariable("alertDefineId") long alertDefineId,
@RequestBody List<AlertDefineMonitorBind> alertDefineMonitorBinds) {
alertDefineService.applyBindAlertDefineMonitors(alertDefineId, alertDefineMonitorBinds);
return ResponseEntity.ok(new Message<>("Apply success"));
return ResponseEntity.ok(Message.success("Apply success"));
}

@GetMapping(path = "/{alertDefineId}/monitors")
Expand All @@ -122,7 +120,7 @@ public ResponseEntity<Message<List<AlertDefineMonitorBind>>> getAlertDefineMonit
@Parameter(description = "Alarm Definition ID | 告警定义ID", example = "6565463543") @PathVariable("alertDefineId") long alertDefineId) {
List<AlertDefineMonitorBind> defineBinds = alertDefineService.getBindAlertDefineMonitors(alertDefineId);
defineBinds = defineBinds.stream().filter(item -> item.getMonitor() != null).collect(Collectors.toList());
return ResponseEntity.ok(new Message<>(defineBinds));
return ResponseEntity.ok(Message.success(defineBinds));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
/**
* Define the batch API for alarms
* 告警定义批量API
* @author tom
*
* @author tom
*/
@Tag(name = "Alert Define Batch API | 告警定义管理API")
@RestController
Expand All @@ -71,7 +71,7 @@ public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefines(
Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
List<Predicate> andList = new ArrayList<>();
if (ids != null && !ids.isEmpty()) {
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
for (long id : ids) {
inPredicate.value(id);
}
Expand All @@ -87,9 +87,8 @@ public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefines(
// 分页是必须的
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification,pageRequest);
Message<Page<AlertDefine>> message = new Message<>(alertDefinePage);
return ResponseEntity.ok(message);
Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification, pageRequest);
return ResponseEntity.ok(Message.success(alertDefinePage));
}

@DeleteMapping
Expand All @@ -101,8 +100,7 @@ public ResponseEntity<Message<Void>> deleteAlertDefines(
if (ids != null && !ids.isEmpty()) {
alertDefineService.deleteAlertDefines(new HashSet<>(ids));
}
Message<Void> message = new Message<>();
return ResponseEntity.ok(message);
return ResponseEntity.ok(Message.success());
}

@GetMapping("/app")
Expand Down Expand Up @@ -136,9 +134,8 @@ public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefinesByName(
// 分页是必须的
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification,pageRequest);
Message<Page<AlertDefine>> message = new Message<>(alertDefinePage);
return ResponseEntity.ok(message);
Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification, pageRequest);
return ResponseEntity.ok(Message.success(alertDefinePage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
@RestController
@RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE})
public class AlertReportController {

@Autowired
AlertConvertTenCloudServiceImpl alertConvertTenCloudService;

@Autowired
private AlertService alertService;

@PostMapping("/tencloud")
@Operation(summary = "Interface for reporting external alarm information of tencloud | 对外上报告警信息 接口",
description = "对外 新增一个腾讯云告警")
public ResponseEntity<Message<Void>> addNewAlertReportTencent(@Valid @RequestBody String alertReport) {
AlertReport convert = alertConvertTenCloudService.convert(alertReport);
alertService.addNewAlertReport(convert);
return ResponseEntity.ok(new Message<>("Add report success"));
return ResponseEntity.ok(Message.success("Add report success"));
}

@PostMapping
Expand All @@ -47,6 +47,6 @@ public ResponseEntity<Message<Void>> addNewAlertReportTencent(@Valid @RequestBod
public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody AlertReport alertReport) {
// 校验请求数据 TODO
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(new Message<>("Add report success"));
return ResponseEntity.ok(Message.success("Add report success"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
/**
* Alarm Silence management API
* 告警静默管理API
* @author tom
*
* @author tom
*/
@Tag(name = "Alert Silence API | 告警静默管理API")
@RestController
Expand All @@ -52,15 +52,15 @@ public class AlertSilenceController {
public ResponseEntity<Message<Void>> addNewAlertSilence(@Valid @RequestBody AlertSilence alertSilence) {
alertSilenceService.validate(alertSilence, false);
alertSilenceService.addAlertSilence(alertSilence);
return ResponseEntity.ok(new Message<>("Add success"));
return ResponseEntity.ok(Message.success("Add success"));
}

@PutMapping
@Operation(summary = "Modifying an Alarm Silence | 修改告警静默", description = "Modify an existing alarm Silence | 修改一个已存在告警静默")
public ResponseEntity<Message<Void>> modifyAlertSilence(@Valid @RequestBody AlertSilence alertSilence) {
alertSilenceService.validate(alertSilence, true);
alertSilenceService.modifyAlertSilence(alertSilence);
return ResponseEntity.ok(new Message<>("Modify success"));
return ResponseEntity.ok(Message.success("Modify success"));
}

@GetMapping(path = "/{id}")
Expand All @@ -69,13 +69,11 @@ public ResponseEntity<Message<Void>> modifyAlertSilence(@Valid @RequestBody Aler
public ResponseEntity<Message<AlertSilence>> getAlertSilence(
@Parameter(description = "Alarm Silence ID | 告警静默ID", example = "6565463543") @PathVariable("id") long id) {
AlertSilence alertSilence = alertSilenceService.getAlertSilence(id);
Message.MessageBuilder<AlertSilence> messageBuilder = Message.builder();
if (alertSilence == null) {
messageBuilder.code(MONITOR_NOT_EXIST_CODE).msg("AlertSilence not exist.");
return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertSilence not exist."));
} else {
messageBuilder.data(alertSilence);
return ResponseEntity.ok(Message.success(alertSilence));
}
return ResponseEntity.ok(messageBuilder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ResponseEntity<Message<Page<AlertSilence>>> getAlertSilences(
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<AlertSilence> alertSilencePage = alertSilenceService.getAlertSilences(specification,pageRequest);
Message<Page<AlertSilence>> message = new Message<>(alertSilencePage);
Message<Page<AlertSilence>> message = Message.success(alertSilencePage);
return ResponseEntity.ok(message);
}

Expand All @@ -91,7 +91,7 @@ public ResponseEntity<Message<Void>> deleteAlertDefines(
if (ids != null && !ids.isEmpty()) {
alertSilenceService.deleteAlertSilences(new HashSet<>(ids));
}
Message<Void> message = new Message<>();
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ResponseEntity<Message<Page<Alert>>> getAlerts(
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
andList.add(predicate);
}
if (content != null && !"".equals(content)) {
if (content != null && !content.isEmpty()) {
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
andList.add(predicateContent);
}
Expand All @@ -100,7 +100,7 @@ public ResponseEntity<Message<Page<Alert>>> getAlerts(
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<Alert> alertPage = alertService.getAlerts(specification, pageRequest);
Message<Page<Alert>> message = new Message<>(alertPage);
Message<Page<Alert>> message = Message.success(alertPage);
return ResponseEntity.ok(message);
}

Expand All @@ -111,15 +111,15 @@ public ResponseEntity<Message<Void>> deleteAlerts(
if (ids != null && !ids.isEmpty()) {
alertService.deleteAlerts(new HashSet<>(ids));
}
Message<Void> message = new Message<>();
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
}

@DeleteMapping("/clear")
@Operation(summary = "Delete alarms in batches", description = "清空所有告警信息")
public ResponseEntity<Message<Void>> clearAllAlerts() {
alertService.clearAlerts();
Message<Void> message = new Message<>();
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
}

Expand All @@ -131,15 +131,15 @@ public ResponseEntity<Message<Void>> applyAlertDefinesStatus(
if (ids != null && status != null && !ids.isEmpty()) {
alertService.editAlertStatus(status, ids);
}
Message<Void> message = new Message<>();
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
}

@GetMapping(path = "/summary")
@Operation(summary = "Get alarm statistics", description = "获取告警统计信息")
public ResponseEntity<Message<AlertSummary>> getAlertsSummary() {
AlertSummary alertSummary = alertService.getAlertsSummary();
Message<AlertSummary> message = new Message<>(alertSummary);
Message<AlertSummary> message = Message.success(alertSummary);
return ResponseEntity.ok(message);
}

Expand Down
Loading

0 comments on commit 45bc5de

Please sign in to comment.