未验证 提交 c5e89bb0 编写于 作者: sinat_25235033's avatar sinat_25235033 提交者: GitHub

bugfix monitoring template app name already exists (#1152)

上级 9d18b304
......@@ -89,10 +89,10 @@ public class AppController {
}
@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)
......@@ -100,6 +100,19 @@ public class AppController {
}
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 = "查询所有监控的类型-指标组-指标层级,以层级结构输出")
......
......@@ -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
......
......@@ -221,7 +221,7 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
}
@Override
public void applyMonitorDefineYml(String ymlContent) {
public void applyMonitorDefineYml(String ymlContent, boolean isModify) {
Yaml yaml = new Yaml();
Job app;
try {
......@@ -231,7 +231,7 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
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);
......@@ -247,17 +247,21 @@ public class AppServiceImpl implements AppService, CommandLineRunner {
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
......
......@@ -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();
......
......@@ -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>> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册