Skip to content

Commit

Permalink
[manager] refactor monitor service (#851)
Browse files Browse the repository at this point in the history
  [style] Improve MonitorServiceImpl.java related logic

  [style] change String Param verification in MonitorsController.java

---------

Co-authored-by: Carpe-Wang <wangcarpe@126.com>
  • Loading branch information
Carpe-Wang and Carpe-Wang authored Apr 6, 2023
1 parent f5217bd commit b89e7d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ResponseEntity<Message<Page<Monitor>>> getMonitors(
}
andList.add(inPredicate);
}
if (app != null && !"".equals(app)) {
if (app != null && !app.chars().allMatch(Character::isSpaceChar)) {
Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app);
andList.add(predicateApp);
}
Expand All @@ -92,11 +92,11 @@ public ResponseEntity<Message<Page<Monitor>>> getMonitors(
Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));

List<Predicate> orList = new ArrayList<>();
if (host != null && !"".equals(host)) {
if (host != null && !host.chars().allMatch(Character::isSpaceChar)) {
Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
orList.add(predicateHost);
}
if (name != null && !"".equals(name)) {
if (name != null && !name.chars().allMatch(Character::isSpaceChar)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
orList.add(predicateName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
}
}
}
// todo 校验标签
if (monitor.getTags() != null) {
monitor.setTags(monitor.getTags().stream().distinct().collect(Collectors.toList()));
}
Expand Down Expand Up @@ -274,10 +273,15 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
param.setType(CommonConstants.PARAM_TYPE_NUMBER);
break;
case "textarea":
if (param.getValue().chars().allMatch(Character::isSpaceChar)){
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + param.getValue());
}
break;
case "text":
Short limit = paramDefine.getLimit();
if (limit != null) {
if (param.getValue() != null && param.getValue().length() > limit) {
if (param.getValue().length() > limit) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + limit);
}
Expand Down Expand Up @@ -378,6 +382,7 @@ public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeExc
throw new IllegalArgumentException("Can not modify monitor's app type");
}
// Auto Update Default Tags: monitorName
// 自动更新默认的Tag: 监控名字
List<Tag> tags = monitor.getTags();
if (tags == null) {
tags = new LinkedList<>();
Expand All @@ -396,9 +401,11 @@ public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeExc
appDefine.setInterval(monitor.getIntervals());
appDefine.setCyclic(true);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param ->
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
if (params != null) {
List<Configmap> configmaps = params.stream().map(param ->
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
}
long newJobId = collectJobService.updateAsyncCollectJob(appDefine);
monitor.setJobId(newJobId);
}
Expand All @@ -409,7 +416,9 @@ public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeExc
// force update gmtUpdate time, due the case: monitor not change, param change. we also think monitor change
monitor.setGmtUpdate(LocalDateTime.now());
monitorDao.save(monitor);
paramDao.saveAll(params);
if (params != null) {
paramDao.saveAll(params);
}
calculateAlarm.triggeredAlertMap.remove(String.valueOf(monitorId));
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down

0 comments on commit b89e7d9

Please sign in to comment.