From 050beb56037e3010662b999afb14ab033166022b Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Mon, 26 Aug 2024 18:50:17 +0800 Subject: [PATCH 01/11] [feature] supports plug-in custom parameters (#2605) Co-authored-by: shown Co-authored-by: Kerwin Bryant --- .../component/alerter/DispatcherAlarm.java | 2 +- .../manager/controller/PluginController.java | 17 +++ .../hertzbeat/manager/dao/PluginParamDao.java | 48 ++++++++ .../manager/pojo/dto/PluginParam.java | 110 ++++++++++++++++++ .../manager/service/PluginService.java | 19 ++- .../service/impl/PluginServiceImpl.java | 109 +++++++++++++++-- .../manager/service/PluginServiceTest.java | 7 +- .../org/apache/hertzbeat/plugin/Plugin.java | 8 ++ 8 files changed, 310 insertions(+), 10 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParam.java diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java index 0e688e1c7ce..c543c08a1ca 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarm.java @@ -131,7 +131,7 @@ public void run() { // Notice distribution sendNotify(alert); // Execute the plugin if enable - pluginService.pluginExecute(Plugin.class, plugin -> plugin.alert(alert)); + pluginService.pluginExecute(Plugin.class, plugin -> plugin.alert(alert), (plugin, configMapList) -> plugin.alert(alert, configMapList)); } } catch (IgnoreException ignored) { } catch (InterruptedException e) { diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java index 1fb73ada760..0bd5d83c6fd 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java @@ -26,7 +26,9 @@ import lombok.RequiredArgsConstructor; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.dto.PluginUpload; +import org.apache.hertzbeat.common.entity.manager.ParamDefine; import org.apache.hertzbeat.common.entity.manager.PluginMetadata; +import org.apache.hertzbeat.manager.pojo.dto.PluginParam; import org.apache.hertzbeat.manager.service.PluginService; import org.springframework.data.domain.Page; import org.springframework.http.ResponseEntity; @@ -85,4 +87,19 @@ public ResponseEntity> updatePluginStatus(@RequestBody PluginMetad pluginService.updateStatus(plugin); return ResponseEntity.ok(Message.success("Update success")); } + + @GetMapping("/getParamDefine") + @Operation(summary = "get param define", description = "get param define by jar path") + public ResponseEntity>> getParamDefine(@RequestParam Long pluginMetadataId) { + List plugins = pluginService.getParamDefine(pluginMetadataId); + return ResponseEntity.ok(Message.success(plugins)); + } + + @PostMapping("/saveParams") + @Operation(summary = "get param define", description = "get param define by jar path") + public ResponseEntity> saveParams(@RequestBody List pluginParams) { + pluginService.savePluginParam(pluginParams); + return ResponseEntity.ok(Message.success(true)); + } + } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java b/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java new file mode 100644 index 00000000000..d7fbeaa96af --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.manager.dao; + +import java.util.List; +import java.util.Set; +import org.apache.hertzbeat.manager.pojo.dto.PluginParam; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * PluginParamDao database operations + */ +public interface PluginParamDao extends JpaRepository { + + /** + * Query the list of parameters associated with the monitoring ID' + * @param pluginMetadataId Monitor ID + * @return list of parameter values + */ + List findParamsByPluginMetadataId(Long pluginMetadataId); + + /** + * Remove the parameter list associated with the pluginMetadata ID based on it + * @param pluginMetadataId Monitor Id + */ + void deleteParamsByPluginMetadataId(long pluginMetadataId); + + /** + * Remove the parameter list associated with the pluginMetadata ID list based on it + * @param pluginMetadataIds Monitoring ID List + */ + void deleteParamsByPluginMetadataIdIn(Set pluginMetadataIds); +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParam.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParam.java new file mode 100644 index 00000000000..b92948f2b10 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParam.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.manager.pojo.dto; + +import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY; +import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Index; +import jakarta.persistence.Table; +import jakarta.persistence.UniqueConstraint; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import java.time.LocalDateTime; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +/** + * PluginParam + */ +@Entity +@Table(name = "hzb_plugin_param", indexes = { @Index(columnList = "pluginMetadataId") }, + uniqueConstraints = @UniqueConstraint(columnNames = {"pluginMetadataId", "field"})) +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Schema(description = "Parameter Entity") +@EntityListeners(AuditingEntityListener.class) +public class PluginParam { + + /** + * Parameter primary key index ID + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Schema(title = "Parameter primary key index ID", example = "87584674384", accessMode = READ_ONLY) + private Long id; + /** + * Monitor ID + */ + @Schema(title = "Plugin task ID", example = "875846754543", accessMode = READ_WRITE) + @NotNull + private Long pluginMetadataId; + + /** + * Parameter Field Identifier + */ + @Schema(title = "Parameter identifier field", example = "port", accessMode = READ_WRITE) + @Size(max = 100) + @NotNull + private String field; + + /** + * Param Value + */ + @Schema(title = "parameter values", example = "8080", accessMode = READ_WRITE) + @Size(max = 8126) + @Column(length = 8126) + private String paramValue; + + /** + * Parameter type 0: number 1: string 2: encrypted string 3: json string mapped by map + */ + @Schema(title = "Parameter types 0: number 1: string 2: encrypted string 3:map mapped json string 4:arrays string", + accessMode = READ_WRITE) + @Min(0) + private byte type; + + /** + * Record create time + */ + @Schema(title = "Record create time", example = "1612198922000", accessMode = READ_ONLY) + @CreatedDate + private LocalDateTime gmtCreate; + + /** + * Record the latest modification time + */ + @Schema(title = "Record modify time", example = "1612198444000", accessMode = READ_ONLY) + @LastModifiedDate + private LocalDateTime gmtUpdate; + +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java index 23d01a30be2..42254d7b532 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java @@ -17,10 +17,15 @@ package org.apache.hertzbeat.manager.service; +import java.util.List; import java.util.Set; +import java.util.function.BiConsumer; import java.util.function.Consumer; import org.apache.hertzbeat.common.entity.dto.PluginUpload; +import org.apache.hertzbeat.common.entity.job.Configmap; +import org.apache.hertzbeat.common.entity.manager.ParamDefine; import org.apache.hertzbeat.common.entity.manager.PluginMetadata; +import org.apache.hertzbeat.manager.pojo.dto.PluginParam; import org.springframework.data.domain.Page; /** @@ -58,7 +63,7 @@ public interface PluginService { * @param execute run plugin logic * @param plugin type */ - void pluginExecute(Class clazz, Consumer execute); + void pluginExecute(Class clazz, Consumer execute, BiConsumer> biConsumer); /** * delete plugin @@ -69,4 +74,16 @@ public interface PluginService { void updateStatus(PluginMetadata plugin); + /** + * get param define + * @param pluginMetadataId plugin id + */ + List getParamDefine(Long pluginMetadataId); + + /** + * save plugin param + * @param params params + */ + void savePluginParam(List params); + } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java index 162d4bc394a..3ebd6d8a69b 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java @@ -39,9 +39,11 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.stream.Collectors; import javax.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -49,17 +51,23 @@ import org.apache.commons.io.FileUtils; import org.apache.hertzbeat.common.constants.PluginType; import org.apache.hertzbeat.common.entity.dto.PluginUpload; +import org.apache.hertzbeat.common.entity.job.Configmap; +import org.apache.hertzbeat.common.entity.manager.ParamDefine; import org.apache.hertzbeat.common.entity.manager.PluginItem; import org.apache.hertzbeat.common.entity.manager.PluginMetadata; import org.apache.hertzbeat.common.support.exception.CommonException; import org.apache.hertzbeat.manager.dao.PluginItemDao; import org.apache.hertzbeat.manager.dao.PluginMetadataDao; +import org.apache.hertzbeat.manager.dao.PluginParamDao; +import org.apache.hertzbeat.manager.pojo.dto.PluginParam; import org.apache.hertzbeat.manager.service.PluginService; import org.apache.hertzbeat.plugin.Plugin; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import org.yaml.snakeyaml.Yaml; /** * plugin service @@ -73,6 +81,8 @@ public class PluginServiceImpl implements PluginService { private final PluginItemDao itemDao; + private final PluginParamDao pluginParamDao; + public static Map, PluginType> PLUGIN_TYPE_MAPPING = new HashMap<>(); /** @@ -80,6 +90,20 @@ public class PluginServiceImpl implements PluginService { */ private static final Map PLUGIN_ENABLE_STATUS = new ConcurrentHashMap<>(); + /** + * plugin param define + */ + private static final Map> PARAMS_Define_MAP = new ConcurrentHashMap<>(); + + /** + * plugin params + */ + private static final Map> PARAMS_MAP = new ConcurrentHashMap<>(); + + /** + * pluginItem Mapping pluginId + */ + private static final Map ITEM_TO_PLUGINMETADATAID_MAP = new ConcurrentHashMap<>(); private final List pluginClassLoaders = new ArrayList<>(); @@ -106,6 +130,8 @@ public void deletePlugins(Set ids) { } // delete metadata metadataDao.deleteById(plugin.getId()); + syncPluginParamMap(plugin.getId(), null, true); + pluginParamDao.deleteParamsByPluginMetadataId(plugin.getId()); } catch (IOException e) { throw new RuntimeException(e); } @@ -139,6 +165,33 @@ public void updateStatus(PluginMetadata plugin) { } } + @Override + public List getParamDefine(Long pluginMetadataId) { + if (PARAMS_Define_MAP.containsKey(pluginMetadataId)) { + return PARAMS_Define_MAP.get(pluginMetadataId); + } + return List.of(); + } + + @Override + public void savePluginParam(List params) { + if (CollectionUtils.isEmpty(params)) { + return; + } + pluginParamDao.deleteParamsByPluginMetadataId(params.get(0).getPluginMetadataId()); + pluginParamDao.saveAll(params); + syncPluginParamMap(params.get(0).getPluginMetadataId(), params, false); + } + + private void syncPluginParamMap(Long pluginMetadataId, List params, boolean isDelete) { + if (isDelete) { + PARAMS_MAP.remove(pluginMetadataId); + return; + } + List configmapList = params.stream().map(item -> new Configmap(item.getField(), item.getParamValue(), item.getType())).toList(); + PARAMS_MAP.put(pluginMetadataId, configmapList); + } + static { PLUGIN_TYPE_MAPPING.put(Plugin.class, PluginType.POST_ALERT); } @@ -268,13 +321,33 @@ public Page getPlugins(String search, int pageIndex, int pageSiz private void syncPluginStatus() { List plugins = metadataDao.findAll(); Map statusMap = new HashMap<>(); + Map itemToPluginMetadataIdMap = new HashMap<>(); for (PluginMetadata plugin : plugins) { for (PluginItem item : plugin.getItems()) { statusMap.put(item.getClassIdentifier(), plugin.getEnableStatus()); + itemToPluginMetadataIdMap.put(item.getClassIdentifier(), plugin.getId()); } } PLUGIN_ENABLE_STATUS.clear(); PLUGIN_ENABLE_STATUS.putAll(statusMap); + ITEM_TO_PLUGINMETADATAID_MAP.clear(); + ITEM_TO_PLUGINMETADATAID_MAP.putAll(itemToPluginMetadataIdMap); + } + + @PostConstruct + private void initParams(){ + try { + List params = pluginParamDao.findAll(); + Map> content = params.stream() + .collect(Collectors.groupingBy(PluginParam::getPluginMetadataId)); + + for (Map.Entry> entry : content.entrySet()) { + syncPluginParamMap(entry.getKey(), entry.getValue(), false); + } + } catch (Exception e) { + log.error("Failed to init params:{}", e.getMessage()); + throw new CommonException("Failed to init params:" + e.getMessage()); + } } /** @@ -288,14 +361,19 @@ private void loadJarToClassLoader() { pluginClassLoader.close(); } } - pluginClassLoaders.clear(); - System.gc(); + + if (!pluginClassLoaders.isEmpty()) { + pluginClassLoaders.clear(); + System.gc(); + } + PARAMS_Define_MAP.clear(); List plugins = metadataDao.findPluginMetadataByEnableStatusTrue(); for (PluginMetadata metadata : plugins) { - List urls = loadLibInPlugin(metadata.getJarFilePath()); + List urls = loadLibInPlugin(metadata.getJarFilePath(), metadata.getId()); urls.add(new File(metadata.getJarFilePath()).toURI().toURL()); pluginClassLoaders.add(new URLClassLoader(urls.toArray(new URL[0]), Plugin.class.getClassLoader())); } + } catch (MalformedURLException e) { log.error("Failed to load plugin:{}", e.getMessage()); throw new CommonException("Failed to load plugin:" + e.getMessage()); @@ -308,19 +386,24 @@ private void loadJarToClassLoader() { * loading other JAR files that are dependencies for the plugin * * @param pluginJarPath jar file path + * @param pluginMetadataId plugin id * @return urls */ @SneakyThrows - private List loadLibInPlugin(String pluginJarPath) { + private List loadLibInPlugin(String pluginJarPath, Long pluginMetadataId) { File libDir = new File(getOtherLibDir(pluginJarPath)); FileUtils.forceMkdir(libDir); List libUrls = new ArrayList<>(); try (JarFile jarFile = new JarFile(pluginJarPath)) { Enumeration entries = jarFile.entries(); + Yaml yaml = new Yaml(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); File file = new File(libDir, entry.getName()); - if (!entry.isDirectory() && entry.getName().endsWith(".jar")) { + if (entry.isDirectory()) { + continue; + } + if (entry.getName().endsWith(".jar")) { if (!file.getParentFile().exists()) { FileUtils.createParentDirectories(file); } @@ -335,18 +418,30 @@ private List loadLibInPlugin(String pluginJarPath) { out.flush(); } } + if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) { + try (InputStream ymlInputStream = jarFile.getInputStream(entry)) { + List params = yaml.loadAs(ymlInputStream, List.class); + PARAMS_Define_MAP.putIfAbsent(pluginMetadataId, params); + } + } } } return libUrls; } @Override - public void pluginExecute(Class clazz, Consumer execute) { + public void pluginExecute(Class clazz, Consumer execute, BiConsumer> biConsumer) { for (URLClassLoader pluginClassLoader : pluginClassLoaders) { ServiceLoader load = ServiceLoader.load(clazz, pluginClassLoader); for (T t : load) { if (pluginIsEnable(t.getClass())) { - execute.accept(t); + Long pluginId = ITEM_TO_PLUGINMETADATAID_MAP.get(t.getClass().getName()); + List configmapList = PARAMS_MAP.get(pluginId); + if (CollectionUtils.isEmpty(configmapList)) { + execute.accept(t); + } else { + biConsumer.accept(t, configmapList); + } } } } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java index ed94f69c6c6..39cf4365a93 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java @@ -39,6 +39,7 @@ import org.apache.hertzbeat.common.entity.manager.PluginMetadata; import org.apache.hertzbeat.manager.dao.PluginItemDao; import org.apache.hertzbeat.manager.dao.PluginMetadataDao; +import org.apache.hertzbeat.manager.dao.PluginParamDao; import org.apache.hertzbeat.manager.service.impl.PluginServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -60,16 +61,20 @@ class PluginServiceTest { @InjectMocks private PluginServiceImpl pluginService; + @Mock private PluginMetadataDao metadataDao; + @Mock + private PluginParamDao pluginParamDao; + @Mock private PluginItemDao itemDao; @BeforeEach void setUp() { - pluginService = new PluginServiceImpl(metadataDao, itemDao); + pluginService = new PluginServiceImpl(metadataDao, itemDao,pluginParamDao); } @Test diff --git a/plugin/src/main/java/org/apache/hertzbeat/plugin/Plugin.java b/plugin/src/main/java/org/apache/hertzbeat/plugin/Plugin.java index d72632925b5..efeed504603 100644 --- a/plugin/src/main/java/org/apache/hertzbeat/plugin/Plugin.java +++ b/plugin/src/main/java/org/apache/hertzbeat/plugin/Plugin.java @@ -17,7 +17,9 @@ package org.apache.hertzbeat.plugin; +import java.util.List; import org.apache.hertzbeat.common.entity.alerter.Alert; +import org.apache.hertzbeat.common.entity.job.Configmap; /** * Plugin @@ -28,4 +30,10 @@ public interface Plugin { * execute when alert */ void alert(Alert alert); + + /** + * Supports user-defined parameters + */ + void alert(Alert alert, List params); + } From c4b3ae70e86c6e9a3de7be84b21ea5f95f57077e Mon Sep 17 00:00:00 2001 From: Kerwin Bryant Date: Mon, 26 Aug 2024 19:40:33 +0800 Subject: [PATCH 02/11] Complete task #2600 (#2611) Co-authored-by: shown --- .../setting/plugins/plugin.component.html | 24 ++++++++ .../setting/plugins/plugin.component.ts | 57 +++++++++++++++++++ web-app/src/app/service/plugin.service.ts | 27 +++++---- web-app/src/assets/i18n/en-US.json | 1 + web-app/src/assets/i18n/zh-CN.json | 1 + web-app/src/assets/i18n/zh-TW.json | 1 + 6 files changed, 97 insertions(+), 14 deletions(-) diff --git a/web-app/src/app/routes/setting/plugins/plugin.component.html b/web-app/src/app/routes/setting/plugins/plugin.component.html index fd749ed975a..4bf3bffd295 100644 --- a/web-app/src/app/routes/setting/plugins/plugin.component.html +++ b/web-app/src/app/routes/setting/plugins/plugin.component.html @@ -92,6 +92,9 @@
+
+ + +
+
+ + + {{ paramDefine.name }} + + + + + +
+
+
diff --git a/web-app/src/app/routes/setting/plugins/plugin.component.ts b/web-app/src/app/routes/setting/plugins/plugin.component.ts index 0ac42a2ed80..b1f9fef5718 100644 --- a/web-app/src/app/routes/setting/plugins/plugin.component.ts +++ b/web-app/src/app/routes/setting/plugins/plugin.component.ts @@ -27,6 +27,7 @@ import { NzTableQueryParams } from 'ng-zorro-antd/table'; import { NzUploadFile } from 'ng-zorro-antd/upload'; import { finalize } from 'rxjs/operators'; +import { ParamDefine } from '../../../pojo/ParamDefine'; import { Plugin } from '../../../pojo/Plugin'; import { PluginService } from '../../../service/plugin.service'; @@ -47,8 +48,10 @@ export class SettingPluginsComponent implements OnInit { jarFile: [null, [Validators.required]], enableStatus: [true, [Validators.required]] }); + this.lang = this.i18nSvc.defaultLang; } + lang: string; pageIndex: number = 1; pageSize: number = 8; total: number = 0; @@ -275,4 +278,58 @@ export class SettingPluginsComponent implements OnInit { }); this.fileList = []; } + + params: any = {}; + paramDefines!: ParamDefine[]; + isEditPluginParamDefineModalVisible = false; + + onEditPluginParamDefine(pluginId: number) { + const getPluginParamDefine$ = this.pluginService + .getPluginParamDefine(pluginId) + .pipe( + finalize(() => { + getPluginParamDefine$.unsubscribe(); + }) + ) + .subscribe((message: any) => { + if (message.code === 0) { + this.paramDefines = message.data.map((i: any) => { + this.params[i.field] = { + pluginMetadataId: pluginId, + // Parameter type 0: number 1: string 2: encrypted string 3: json string mapped by map + type: i.type === 'number' ? 0 : i.type === 'text' ? 1 : i.type === 'json' ? 3 : 2, + field: i.field, + paramValue: null + }; + i.name = i.name[this.lang]; + return i; + }); + this.isEditPluginParamDefineModalVisible = true; + } else { + this.notifySvc.error(this.i18nSvc.fanyi('common.notify.edit-fail'), message.msg); + } + }); + } + + onEditPluginParamDefineModalCancel() { + this.isEditPluginParamDefineModalVisible = false; + } + + onEditPluginParamDefineModalOk() { + const savePluginParamDefine$ = this.pluginService + .savePluginParamDefine(Object.values(this.params)) + .pipe( + finalize(() => { + savePluginParamDefine$.unsubscribe(); + }) + ) + .subscribe((message: any) => { + if (message.code === 0) { + this.isEditPluginParamDefineModalVisible = false; + this.notifySvc.success(this.i18nSvc.fanyi('common.notify.edit-success'), ''); + } else { + this.notifySvc.error(this.i18nSvc.fanyi('common.notify.edit-fail'), message.msg); + } + }); + } } diff --git a/web-app/src/app/service/plugin.service.ts b/web-app/src/app/service/plugin.service.ts index 90278805dbc..1a2bdae5114 100644 --- a/web-app/src/app/service/plugin.service.ts +++ b/web-app/src/app/service/plugin.service.ts @@ -64,20 +64,6 @@ export class PluginService { return this.http.put>(plugin_uri, body); } - public newTags(body: Tag[]): Observable> { - return this.http.post>(plugin_uri, body); - } - - public newTag(body: Tag): Observable> { - const tags = []; - tags.push(body); - return this.http.post>(plugin_uri, tags); - } - - public editTag(body: Tag): Observable> { - return this.http.put>(plugin_uri, body); - } - public deletePlugins(pluginIds: Set): Observable> { let httpParams = new HttpParams(); pluginIds.forEach(pluginId => { @@ -86,4 +72,17 @@ export class PluginService { const options = { params: httpParams }; return this.http.delete>(plugin_uri, options); } + + public getPluginParamDefine(pluginId: number): Observable> { + let httpParams = new HttpParams(); + httpParams = httpParams.appendAll({ + pluginMetadataId: pluginId + }); + const options = { params: httpParams }; + return this.http.get>(`${plugin_uri}/getParamDefine`, options); + } + + public savePluginParamDefine(body: any): Observable> { + return this.http.post>(`${plugin_uri}/saveParams`, body); + } } diff --git a/web-app/src/assets/i18n/en-US.json b/web-app/src/assets/i18n/en-US.json index 4a9cf506e35..c6edf7509a8 100644 --- a/web-app/src/assets/i18n/en-US.json +++ b/web-app/src/assets/i18n/en-US.json @@ -551,6 +551,7 @@ "plugin.delete": "Delete Plugin", "plugin.type.POST_ALERT": "POST ALERT", "plugin.search": "Search plugins", + "plugin.edit": "Edit plugin", "define.help": "The monitor templates define each monitoring type, parameter variable, metrics info, collection protocol, etc. You can select an existing monitoring template from the drop-down menu then make modifications according to your own needs. The bottom-left area is the compare area and the bottom-right area is the editing place.
You can also click \"New Monitor Type\" to custom define an new type. Currently supported protocols include HTTP, JDBC, SSH, JMX, SNMP. Monitor Templates.", "define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/", "define.save-apply": "Save And Apply", diff --git a/web-app/src/assets/i18n/zh-CN.json b/web-app/src/assets/i18n/zh-CN.json index 8218fe13370..1cc90231571 100644 --- a/web-app/src/assets/i18n/zh-CN.json +++ b/web-app/src/assets/i18n/zh-CN.json @@ -549,6 +549,7 @@ "plugin.type": "插件类型", "plugin.type.POST_ALERT": "告警后", "plugin.search": "搜索插件", + "plugin.edit": "编辑插件", "define.help": "监控模版定义每一个监控类型,类型的参数变量,指标信息,采集协议等。您可根据需求在下拉菜单中选择已有监控模板修改。左下区域为对照区,右下区域为编辑区。
您也可以点击“新增监控类型”来自定义新的的监控类型,目前支持 HTTP 协议JDBC协议SSH协议 JMX 协议 SNMP 协议点击查看监控模板。\n", "define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/", "define.save-apply": "保存并应用", diff --git a/web-app/src/assets/i18n/zh-TW.json b/web-app/src/assets/i18n/zh-TW.json index 671de56b5fb..d3c694b2f39 100644 --- a/web-app/src/assets/i18n/zh-TW.json +++ b/web-app/src/assets/i18n/zh-TW.json @@ -548,6 +548,7 @@ "plugin.delete": "刪除插件", "plugin.type.POST_ALERT": "告警後", "plugin.search": "搜尋插件", + "plugin.edit": "編輯插件", "define.help": "監控模版定義每一個監控類型,類型的參數變量,指標信息,採集協議等。您可根據需求在下拉功能表中選擇已有監控模版進行修改。右下區域為編輯區,左下區域為對照區。
您也可以點擊“新增監控類型”來自定義新的的監控類型,現支持 HTTP協議JDBC協定SSH協定 JMX協定 SNMP協定點擊查看監控範本。", "define.help.link": "https://hertzbeat.apache.org/zh-cn/docs/advanced/extend-point/", "define.save-apply": "保存並應用", From f9b5221a4654a3d58462765fbe77a58fac210393 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 27 Aug 2024 08:59:57 +0800 Subject: [PATCH 03/11] [improve]:fix bug (#2615) --- .../manager/controller/PluginController.java | 6 +-- .../hertzbeat/manager/dao/PluginParamDao.java | 4 +- .../manager/pojo/dto/PluginParametersVO.java | 43 +++++++++++++++++++ .../manager/service/PluginService.java | 4 +- .../service/impl/PluginServiceImpl.java | 19 +++++--- 5 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParametersVO.java diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java index 0bd5d83c6fd..496ebfb5bad 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java @@ -26,9 +26,9 @@ import lombok.RequiredArgsConstructor; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.dto.PluginUpload; -import org.apache.hertzbeat.common.entity.manager.ParamDefine; import org.apache.hertzbeat.common.entity.manager.PluginMetadata; import org.apache.hertzbeat.manager.pojo.dto.PluginParam; +import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO; import org.apache.hertzbeat.manager.service.PluginService; import org.springframework.data.domain.Page; import org.springframework.http.ResponseEntity; @@ -90,8 +90,8 @@ public ResponseEntity> updatePluginStatus(@RequestBody PluginMetad @GetMapping("/getParamDefine") @Operation(summary = "get param define", description = "get param define by jar path") - public ResponseEntity>> getParamDefine(@RequestParam Long pluginMetadataId) { - List plugins = pluginService.getParamDefine(pluginMetadataId); + public ResponseEntity> getParamDefine(@RequestParam Long pluginMetadataId) { + PluginParametersVO plugins = pluginService.getParamDefine(pluginMetadataId); return ResponseEntity.ok(Message.success(plugins)); } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java b/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java index d7fbeaa96af..4eeec26b192 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/dao/PluginParamDao.java @@ -38,11 +38,11 @@ public interface PluginParamDao extends JpaRepository { * Remove the parameter list associated with the pluginMetadata ID based on it * @param pluginMetadataId Monitor Id */ - void deleteParamsByPluginMetadataId(long pluginMetadataId); + void deletePluginParamsByPluginMetadataId(long pluginMetadataId); /** * Remove the parameter list associated with the pluginMetadata ID list based on it * @param pluginMetadataIds Monitoring ID List */ - void deleteParamsByPluginMetadataIdIn(Set pluginMetadataIds); + void deletePluginParamsByPluginMetadataIdIn(Set pluginMetadataIds); } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParametersVO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParametersVO.java new file mode 100644 index 00000000000..ba3f2f9ffa1 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/PluginParametersVO.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.manager.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.apache.hertzbeat.common.entity.manager.ParamDefine; +import java.util.List; + +/** + * Popup rendering and parameter values + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class PluginParametersVO { + + /** + * Stencil rendering + */ + private List paramDefines; + + /** + * specific parameter + */ + private List pluginParams; +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java index 42254d7b532..a7f0c706e4f 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/PluginService.java @@ -23,9 +23,9 @@ import java.util.function.Consumer; import org.apache.hertzbeat.common.entity.dto.PluginUpload; import org.apache.hertzbeat.common.entity.job.Configmap; -import org.apache.hertzbeat.common.entity.manager.ParamDefine; import org.apache.hertzbeat.common.entity.manager.PluginMetadata; import org.apache.hertzbeat.manager.pojo.dto.PluginParam; +import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO; import org.springframework.data.domain.Page; /** @@ -78,7 +78,7 @@ public interface PluginService { * get param define * @param pluginMetadataId plugin id */ - List getParamDefine(Long pluginMetadataId); + PluginParametersVO getParamDefine(Long pluginMetadataId); /** * save plugin param diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java index 3ebd6d8a69b..5838bb260d9 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java @@ -60,12 +60,14 @@ import org.apache.hertzbeat.manager.dao.PluginMetadataDao; import org.apache.hertzbeat.manager.dao.PluginParamDao; import org.apache.hertzbeat.manager.pojo.dto.PluginParam; +import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO; import org.apache.hertzbeat.manager.service.PluginService; import org.apache.hertzbeat.plugin.Plugin; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.yaml.snakeyaml.Yaml; @@ -131,12 +133,12 @@ public void deletePlugins(Set ids) { // delete metadata metadataDao.deleteById(plugin.getId()); syncPluginParamMap(plugin.getId(), null, true); - pluginParamDao.deleteParamsByPluginMetadataId(plugin.getId()); } catch (IOException e) { throw new RuntimeException(e); } } + pluginParamDao.deletePluginParamsByPluginMetadataIdIn(ids); syncPluginStatus(); // reload classloader loadJarToClassLoader(); @@ -166,19 +168,26 @@ public void updateStatus(PluginMetadata plugin) { } @Override - public List getParamDefine(Long pluginMetadataId) { + public PluginParametersVO getParamDefine(Long pluginMetadataId) { + + PluginParametersVO pluginParametersVO = new PluginParametersVO(); if (PARAMS_Define_MAP.containsKey(pluginMetadataId)) { - return PARAMS_Define_MAP.get(pluginMetadataId); + List paramDefines = PARAMS_Define_MAP.get(pluginMetadataId); + List paramsByPluginMetadataId = pluginParamDao.findParamsByPluginMetadataId(pluginMetadataId); + pluginParametersVO.setParamDefines(paramDefines); + pluginParametersVO.setPluginParams(paramsByPluginMetadataId); + return pluginParametersVO; } - return List.of(); + return pluginParametersVO; } @Override + @Transactional public void savePluginParam(List params) { if (CollectionUtils.isEmpty(params)) { return; } - pluginParamDao.deleteParamsByPluginMetadataId(params.get(0).getPluginMetadataId()); + pluginParamDao.deletePluginParamsByPluginMetadataId(params.get(0).getPluginMetadataId()); pluginParamDao.saveAll(params); syncPluginParamMap(params.get(0).getPluginMetadataId(), params, false); } From 5ac28c2558e791ae7506991a9e9b2a0ac63bd858 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Tue, 27 Aug 2024 09:25:06 +0800 Subject: [PATCH 04/11] Improve frontend --- .../src/app/routes/setting/plugins/plugin.component.ts | 10 +++++++--- .../components/form-field/form-field.component.html | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/web-app/src/app/routes/setting/plugins/plugin.component.ts b/web-app/src/app/routes/setting/plugins/plugin.component.ts index b1f9fef5718..fc90543c4ea 100644 --- a/web-app/src/app/routes/setting/plugins/plugin.component.ts +++ b/web-app/src/app/routes/setting/plugins/plugin.component.ts @@ -293,13 +293,13 @@ export class SettingPluginsComponent implements OnInit { ) .subscribe((message: any) => { if (message.code === 0) { - this.paramDefines = message.data.map((i: any) => { + this.paramDefines = message.data.paramDefines.map((i: any) => { this.params[i.field] = { pluginMetadataId: pluginId, // Parameter type 0: number 1: string 2: encrypted string 3: json string mapped by map - type: i.type === 'number' ? 0 : i.type === 'text' ? 1 : i.type === 'json' ? 3 : 2, + type: i.type === 'number' ? 0 : (i.type === 'text' || i.type === 'string') ? 1 : i.type === 'json' ? 3 : 2, field: i.field, - paramValue: null + paramValue: this.getParamValue(message.data.pluginParams, i.field) }; i.name = i.name[this.lang]; return i; @@ -332,4 +332,8 @@ export class SettingPluginsComponent implements OnInit { } }); } + + getParamValue(pluginParams: any[], field: string) { + return pluginParams.filter((i: any) => i.field === field)[0].paramValue; + } } diff --git a/web-app/src/app/shared/components/form-field/form-field.component.html b/web-app/src/app/shared/components/form-field/form-field.component.html index 0362b7973e3..8b61e8141d4 100644 --- a/web-app/src/app/shared/components/form-field/form-field.component.html +++ b/web-app/src/app/shared/components/form-field/form-field.component.html @@ -18,7 +18,7 @@ --> Date: Tue, 27 Aug 2024 20:12:43 +0800 Subject: [PATCH 05/11] [task]:fix bug (#2617) --- .../hertzbeat/manager/service/impl/PluginServiceImpl.java | 1 + .../org/apache/hertzbeat/manager/service/PluginServiceTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java index 5838bb260d9..97b9ccd12ae 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java @@ -110,6 +110,7 @@ public class PluginServiceImpl implements PluginService { private final List pluginClassLoaders = new ArrayList<>(); @Override + @Transactional public void deletePlugins(Set ids) { List plugins = metadataDao.findAllById(ids); // disable the plugins that need to be removed diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java index 172719805cd..59dc49b2014 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/PluginServiceTest.java @@ -74,7 +74,7 @@ class PluginServiceTest { @BeforeEach void setUp() { - pluginService = new PluginServiceImpl(metadataDao, itemDao,pluginParamDao); + pluginService = new PluginServiceImpl(metadataDao, itemDao, pluginParamDao); } @Test From c0e77058cc4ba59af7f8ba95663b8f8ef6181364 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Tue, 27 Aug 2024 20:43:20 +0800 Subject: [PATCH 06/11] Fixed lint error --- web-app/src/app/routes/setting/plugins/plugin.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web-app/src/app/routes/setting/plugins/plugin.component.ts b/web-app/src/app/routes/setting/plugins/plugin.component.ts index fc90543c4ea..a9a543a802a 100644 --- a/web-app/src/app/routes/setting/plugins/plugin.component.ts +++ b/web-app/src/app/routes/setting/plugins/plugin.component.ts @@ -297,7 +297,7 @@ export class SettingPluginsComponent implements OnInit { this.params[i.field] = { pluginMetadataId: pluginId, // Parameter type 0: number 1: string 2: encrypted string 3: json string mapped by map - type: i.type === 'number' ? 0 : (i.type === 'text' || i.type === 'string') ? 1 : i.type === 'json' ? 3 : 2, + type: i.type === 'number' ? 0 : i.type === 'text' || i.type === 'string' ? 1 : i.type === 'json' ? 3 : 2, field: i.field, paramValue: this.getParamValue(message.data.pluginParams, i.field) }; From d49a2c6b9bec212d076dbdbd27489b43c1f39643 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Wed, 28 Aug 2024 08:00:31 +0800 Subject: [PATCH 07/11] Fixed the frontend issues mentioned in (https://github.com/apache/hertzbeat/pull/2616#issuecomment-2313019895) --- web-app/src/app/routes/setting/plugins/plugin.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web-app/src/app/routes/setting/plugins/plugin.component.ts b/web-app/src/app/routes/setting/plugins/plugin.component.ts index a9a543a802a..142a7533f49 100644 --- a/web-app/src/app/routes/setting/plugins/plugin.component.ts +++ b/web-app/src/app/routes/setting/plugins/plugin.component.ts @@ -334,6 +334,7 @@ export class SettingPluginsComponent implements OnInit { } getParamValue(pluginParams: any[], field: string) { - return pluginParams.filter((i: any) => i.field === field)[0].paramValue; + const pluginParam = (pluginParams || []).filter((i: any) => i.field === field); + return pluginParam.length > 0 ? pluginParam[0].paramValue : null; } } From 6c5368f2b1692927305c4901efdeeb595132041d Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Wed, 28 Aug 2024 23:47:09 +0800 Subject: [PATCH 08/11] fix bug (#2628) --- .../manager/controller/PluginController.java | 4 ++-- .../service/impl/PluginServiceImpl.java | 23 ++++++++++++++----- plugin/src/main/resources/define/define.yml | 0 web-app/src/app/service/plugin.service.ts | 4 ++-- 4 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 plugin/src/main/resources/define/define.yml diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java index 496ebfb5bad..18c24544fac 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/PluginController.java @@ -88,14 +88,14 @@ public ResponseEntity> updatePluginStatus(@RequestBody PluginMetad return ResponseEntity.ok(Message.success("Update success")); } - @GetMapping("/getParamDefine") + @GetMapping("/params/define") @Operation(summary = "get param define", description = "get param define by jar path") public ResponseEntity> getParamDefine(@RequestParam Long pluginMetadataId) { PluginParametersVO plugins = pluginService.getParamDefine(pluginMetadataId); return ResponseEntity.ok(Message.success(plugins)); } - @PostMapping("/saveParams") + @PostMapping("/params") @Operation(summary = "get param define", description = "get param define by jar path") public ResponseEntity> saveParams(@RequestBody List pluginParams) { pluginService.savePluginParam(pluginParams); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java index 97b9ccd12ae..f1dd9f4bf4d 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java @@ -17,6 +17,7 @@ package org.apache.hertzbeat.manager.service.impl; +import jakarta.persistence.TableGenerator; import jakarta.persistence.criteria.Predicate; import java.io.File; import java.io.FileOutputStream; @@ -70,6 +71,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; /** * plugin service @@ -95,7 +97,7 @@ public class PluginServiceImpl implements PluginService { /** * plugin param define */ - private static final Map> PARAMS_Define_MAP = new ConcurrentHashMap<>(); + private static final Map> PARAMS_DEFINE_MAP = new ConcurrentHashMap<>(); /** * plugin params @@ -172,8 +174,8 @@ public void updateStatus(PluginMetadata plugin) { public PluginParametersVO getParamDefine(Long pluginMetadataId) { PluginParametersVO pluginParametersVO = new PluginParametersVO(); - if (PARAMS_Define_MAP.containsKey(pluginMetadataId)) { - List paramDefines = PARAMS_Define_MAP.get(pluginMetadataId); + if (PARAMS_DEFINE_MAP.containsKey(pluginMetadataId)) { + List paramDefines = PARAMS_DEFINE_MAP.get(pluginMetadataId); List paramsByPluginMetadataId = pluginParamDao.findParamsByPluginMetadataId(pluginMetadataId); pluginParametersVO.setParamDefines(paramDefines); pluginParametersVO.setPluginParams(paramsByPluginMetadataId); @@ -217,8 +219,9 @@ public List validateJarFile(File jarFile) { try { URL jarUrl = new URL("file:" + jarFile.getAbsolutePath()); try (URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader()); - JarFile jar = new JarFile(jarFile)) { + JarFile jar = new JarFile(jarFile)) { Enumeration entries = jar.entries(); + Yaml yaml = new Yaml(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.getName().endsWith(".class")) { @@ -236,6 +239,11 @@ public List validateJarFile(File jarFile) { System.err.println("Failed to load class: " + className); } } + if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) { + try (InputStream ymlInputStream = jar.getInputStream(entry)) { + yaml.loadAs(ymlInputStream, List.class); + } + } } if (pluginItems.isEmpty()) { throw new CommonException("Illegal plug-ins, please refer to https://hertzbeat.apache.org/docs/help/plugin/"); @@ -247,6 +255,8 @@ public List validateJarFile(File jarFile) { } catch (MalformedURLException e) { log.error("Invalid JAR file URL: {}", jarFile.getAbsoluteFile(), e); throw new CommonException("Invalid JAR file URL: " + jarFile.getAbsolutePath()); + } catch (YAMLException e) { + throw new CommonException("YAML the file format is incorrect"); } return pluginItems; } @@ -259,6 +269,7 @@ private void validateMetadata(PluginMetadata metadata) { @Override @SneakyThrows + @Transactional public void savePlugin(PluginUpload pluginUpload) { String jarPath = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath(); Path extLibPath = Paths.get(new File(jarPath).getParent(), "plugin-lib"); @@ -376,7 +387,7 @@ private void loadJarToClassLoader() { pluginClassLoaders.clear(); System.gc(); } - PARAMS_Define_MAP.clear(); + PARAMS_DEFINE_MAP.clear(); List plugins = metadataDao.findPluginMetadataByEnableStatusTrue(); for (PluginMetadata metadata : plugins) { List urls = loadLibInPlugin(metadata.getJarFilePath(), metadata.getId()); @@ -431,7 +442,7 @@ private List loadLibInPlugin(String pluginJarPath, Long pluginMetadataId) { if ((entry.getName().contains("define")) && (entry.getName().endsWith(".yml") || entry.getName().endsWith(".yaml"))) { try (InputStream ymlInputStream = jarFile.getInputStream(entry)) { List params = yaml.loadAs(ymlInputStream, List.class); - PARAMS_Define_MAP.putIfAbsent(pluginMetadataId, params); + PARAMS_DEFINE_MAP.put(pluginMetadataId, params); } } } diff --git a/plugin/src/main/resources/define/define.yml b/plugin/src/main/resources/define/define.yml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/web-app/src/app/service/plugin.service.ts b/web-app/src/app/service/plugin.service.ts index 1a2bdae5114..1b34ed5cb50 100644 --- a/web-app/src/app/service/plugin.service.ts +++ b/web-app/src/app/service/plugin.service.ts @@ -79,10 +79,10 @@ export class PluginService { pluginMetadataId: pluginId }); const options = { params: httpParams }; - return this.http.get>(`${plugin_uri}/getParamDefine`, options); + return this.http.get>(`${plugin_uri}/params/define`, options); } public savePluginParamDefine(body: any): Observable> { - return this.http.post>(`${plugin_uri}/saveParams`, body); + return this.http.post>(`${plugin_uri}/params`, body); } } From 5abfb527f817fa5bf8cc287035a7a1c79c7beb1e Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 29 Aug 2024 09:13:16 +0800 Subject: [PATCH 09/11] fix ci (#2630) From b06050879247ee567392e72d22253fb10d1044ba Mon Sep 17 00:00:00 2001 From: liutianyou Date: Thu, 29 Aug 2024 20:24:54 +0800 Subject: [PATCH 10/11] fix ci :add license header Signed-off-by: liutianyou --- plugin/src/main/resources/define/define.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugin/src/main/resources/define/define.yml b/plugin/src/main/resources/define/define.yml index e69de29bb2d..25b47f6ad57 100644 --- a/plugin/src/main/resources/define/define.yml +++ b/plugin/src/main/resources/define/define.yml @@ -0,0 +1,14 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. From bf5404aa19ca46d2f4172af9a10f5c3225f4770f Mon Sep 17 00:00:00 2001 From: liutianyou Date: Thu, 29 Aug 2024 20:27:03 +0800 Subject: [PATCH 11/11] fix ci: remove unused import Signed-off-by: liutianyou --- .../apache/hertzbeat/manager/service/impl/PluginServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java index f1dd9f4bf4d..4ccae0fd0ed 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/PluginServiceImpl.java @@ -17,7 +17,6 @@ package org.apache.hertzbeat.manager.service.impl; -import jakarta.persistence.TableGenerator; import jakarta.persistence.criteria.Predicate; import java.io.File; import java.io.FileOutputStream;