Skip to content

Commit

Permalink
bugfix monitoring template app name already exists (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Mar 10, 2024
1 parent b8c75b7 commit 682fb64
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,30 @@ public ResponseEntity<Message<Void>> deleteAppDefineYml(
}

@PostMapping(path = "/define/yml")
@Operation(summary = "Save and apply monitoring type define yml", description = "保存并应用监控类型的定义YML")
public ResponseEntity<Message<Void>> applyAppDefineYml(@Valid @RequestBody MonitorDefineDto defineDto) {
@Operation(summary = "Add new monitoring type define yml", description = "新增监控类型的定义YML")
public ResponseEntity<Message<Void>> newAppDefineYml(@Valid @RequestBody MonitorDefineDto defineDto) {
try {
appService.applyMonitorDefineYml(defineDto.getDefine());
appService.applyMonitorDefineYml(defineDto.getDefine(), false);
} catch (Exception e) {
return ResponseEntity.ok(Message.<Void>builder()
.code(CommonConstants.FAIL_CODE)
.msg(e.getMessage()).build());
}
return ResponseEntity.ok(Message.<Void>builder().build());
}

@PutMapping(path = "/define/yml")
@Operation(summary = "Update monitoring type define yml", description = "更新监控类型的定义YML")
public ResponseEntity<Message<Void>> updateAppDefineYml(@Valid @RequestBody MonitorDefineDto defineDto) {
try {
appService.applyMonitorDefineYml(defineDto.getDefine(), true);
} catch (Exception e) {
return ResponseEntity.ok(Message.<Void>builder()
.code(CommonConstants.FAIL_CODE)
.msg(e.getMessage()).build());
}
return ResponseEntity.ok(Message.<Void>builder().build());
}

@GetMapping(path = "/hierarchy")
@Operation(summary = "Query all monitored types-indicator group-indicator level, output in a hierarchical structure", description = "查询所有监控的类型-指标组-指标层级,以层级结构输出")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public interface AppService {

/**
* update and apply app define yml
*
* @param ymlContent yml content
* @param isModify is modified?
*/
void applyMonitorDefineYml(String ymlContent);
void applyMonitorDefineYml(String ymlContent, boolean isModify);

/**
* delete monitor define yml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public String getMonitorDefineFileContent(String app) {
}

@Override
public void applyMonitorDefineYml(String ymlContent) {
public void applyMonitorDefineYml(String ymlContent, boolean isModify) {
Yaml yaml = new Yaml();
Job app;
try {
Expand All @@ -231,7 +231,7 @@ public void applyMonitorDefineYml(String ymlContent) {
throw new IllegalArgumentException("parse yml define error: " + e.getMessage());
}
// app params verify
verifyDefineAppContent(app);
verifyDefineAppContent(app, isModify);
String classpath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("")).getPath();
String defineAppPath = classpath + "define" + File.separator + "app-" + app.getApp() + ".yml";
File defineAppFile = new File(defineAppPath);
Expand All @@ -247,17 +247,21 @@ public void applyMonitorDefineYml(String ymlContent) {
SpringContextHolder.getBean(MonitorService.class).updateAppCollectJob(app);
}

private void verifyDefineAppContent(Job app) {
Assert.notNull(app, "define yml can not null");
Assert.notNull(app.getApp(), "define yml require attributes app");
Assert.notNull(app.getCategory(), "define yml require attributes category");
Assert.notEmpty(app.getName(), "define yml require attributes name");
Assert.notEmpty(app.getParams(), "define yml require attributes params");
private void verifyDefineAppContent(Job app, boolean isModify) {
Assert.notNull(app, "monitoring template can not null");
Assert.notNull(app.getApp(), "monitoring template require attributes app");
Assert.notNull(app.getCategory(), "monitoring template require attributes category");
Assert.notEmpty(app.getName(), "monitoring template require attributes name");
Assert.notEmpty(app.getParams(), "monitoring template require attributes params");
boolean hasParamHost = app.getParams().stream().anyMatch(item -> "host".equals(item.getField()));
Assert.isTrue(hasParamHost, "define yml attributes params must have param host");
Assert.notEmpty(app.getMetrics(), "define yml require attributes metrics");
Assert.isTrue(hasParamHost, "monitoring template attributes params must have param host");
Assert.notEmpty(app.getMetrics(), "monitoring template require attributes metrics");
boolean hasAvailableMetrics = app.getMetrics().stream().anyMatch(item -> item.getPriority() == 0);
Assert.isTrue(hasAvailableMetrics, "define yml metrics list must have one priority 0 metrics");
Assert.isTrue(hasAvailableMetrics, "monitoring template metrics list must have one priority 0 metrics");
if (!isModify) {
Assert.isNull(appDefines.get(app.getApp().toLowerCase()),
"monitoring template name " + app.getApp() + " already exists.");
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/app/routes/setting/define/define.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class DefineComponent implements OnInit {
saveAndApply() {
this.saveLoading = true;
const saveDefine$ = this.appDefineSvc
.saveAppDefineYmlContent(this.code)
.newAppDefineYmlContent(this.code, this.currentApp == null)
.pipe(
finalize(() => {
saveDefine$.unsubscribe();
Expand Down
8 changes: 6 additions & 2 deletions web-app/src/app/service/app-define.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ export class AppDefineService {
return this.http.get<Message<any>>(`/apps/${app}/define/yml`);
}

public saveAppDefineYmlContent(defineContent: string | undefined | null): Observable<Message<any>> {
public newAppDefineYmlContent(defineContent: string | undefined | null, isNew: boolean): Observable<Message<any>> {
if (defineContent === null || defineContent === undefined) {
console.log('defineContent can not null');
}
let body = {
define: defineContent
};
return this.http.post<Message<any>>(`/apps/define/yml`, body);
if (isNew) {
return this.http.post<Message<any>>(`/apps/define/yml`, body);
} else {
return this.http.put<Message<any>>(`/apps/define/yml`, body);
}
}

public getAppHierarchy(lang: string | undefined): Observable<Message<any>> {
Expand Down

0 comments on commit 682fb64

Please sign in to comment.