This is an automated email from the ASF dual-hosted git repository. zhaoqingran pushed a commit to branch script in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
commit 81c879dc9ec7a1a0830986f9a44ccff45e4b317b Author: Logic <[email protected]> AuthorDate: Thu Sep 5 00:43:29 2024 +0800 refactor(script): standardize script type naming and remove unused imports - Change the script type naming convention to lowercase across all related components. - Remove unused ScriptTypeEnum import statements following the naming changes. - Update the mapping method in ScriptExecutorFactory to use the new naming convention. - Ensure consistency in script type validation using the updated constants. --- .../collector/script/GraalJavaScriptExecutor.java | 6 +- .../collector/script/PluginScriptTest.java | 6 +- .../ScriptTypeConstants.java} | 37 ++++++---- .../hertzbeat/common/constants/ScriptTypeEnum.java | 79 ---------------------- .../hertzbeat/common/script/ScriptExecutor.java | 4 +- .../common/script/ScriptExecutorFactory.java | 2 +- .../common/script/executor/JavaScriptExecutor.java | 6 +- .../common/support/valid/ScriptValidator.java | 11 ++- .../hertzbeat/manager/pojo/dto/PluginParam.java | 1 - .../manager/service/impl/PluginServiceImpl.java | 1 - .../org.apache.hertzbeat.plugin.PostAlertPlugin | 2 +- plugin/src/main/resources/define/define.yml | 42 ++++++------ .../components/form-field/form-field.component.ts | 1 - 13 files changed, 60 insertions(+), 138 deletions(-) diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java b/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java index c5cb35a8b..38eb48334 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java @@ -18,7 +18,7 @@ package org.apache.hertzbeat.collector.script; import org.apache.hertzbeat.common.cache.CacheFactory; -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; +import org.apache.hertzbeat.common.constants.ScriptTypeConstants; import org.apache.hertzbeat.common.script.ScriptExecutor; import org.graalvm.polyglot.Context; import org.graalvm.polyglot.Engine; @@ -68,8 +68,8 @@ public class GraalJavaScriptExecutor extends ScriptExecutor { } @Override - public ScriptTypeEnum scriptType() { - return ScriptTypeEnum.JS; + public String scriptType() { + return ScriptTypeConstants.JAVASCRIPT; } @Override diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java index 196c4d671..1ab87e581 100644 --- a/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java +++ b/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java @@ -19,7 +19,7 @@ package org.apache.hertzbeat.collector.script; -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; +import org.apache.hertzbeat.common.constants.ScriptTypeConstants; import org.apache.hertzbeat.common.script.ScriptExecutor; import org.apache.hertzbeat.common.support.valid.ScriptValidator; import org.junit.jupiter.api.Assertions; @@ -76,8 +76,8 @@ class PluginScriptTest { Assertions.assertTrue(scriptValidator.validate(correctScript)); Assertions.assertFalse(scriptValidator.validate(wrongScript)); - Assertions.assertTrue(scriptValidator.validate(correctScript, ScriptTypeEnum.JS)); - Assertions.assertFalse(scriptValidator.validate(correctScript, ScriptTypeEnum.JAVA)); + Assertions.assertTrue(scriptValidator.validate(correctScript, ScriptTypeConstants.JAVASCRIPT)); + Assertions.assertFalse(scriptValidator.validate(correctScript, ScriptTypeConstants.JAVA)); } @Test diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java b/common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeConstants.java similarity index 59% copy from common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java copy to common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeConstants.java index 7a129e1b5..7a34d1dd8 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeConstants.java @@ -15,22 +15,29 @@ * limitations under the License. */ -package org.apache.hertzbeat.common.script.executor; - -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; +package org.apache.hertzbeat.common.constants; /** - * This class is used to execute JavaScript code in the plugin. - * This code references liteflow, thanks to the author Bryan. + * ScriptTypeConstants */ -public class JavaScriptExecutor extends Jsr223ScriptExecutor { - @Override - protected String convertScript(String script) { - return String.format("function process(){%s} process();", script); - } - - @Override - public ScriptTypeEnum scriptType() { - return ScriptTypeEnum.JS; - } +public interface ScriptTypeConstants { + + String GROOVY = "groovy"; + + String SHELL = "shell"; + + String JAVASCRIPT = "javascript"; + + String JAVA = "java"; + + String QLEXPRESS = "qlexpress"; + + String PYTHON = "python"; + + String LUA = "lua"; + + String AVIATOR = "aviator"; + + String KOTLIN = "kotlin"; + } diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeEnum.java b/common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeEnum.java deleted file mode 100644 index 0834452e7..000000000 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/ScriptTypeEnum.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.common.constants; - -import lombok.Getter; - -/** - * This code references dromara/liteflow, thanks to the author Bryan. - */ -@Getter -public enum ScriptTypeEnum { - - CUSTOM("custom", "custom"), - GROOVY("groovy", "groovy"), - QLEXPRESS("qlexpress", "qlexpress"), - JS("javascript", "js"), - PYTHON("python", "python"), - LUA("luaj", "lua"), - AVIATOR("AviatorScript", "aviator"), - JAVA("java", "java"), - KOTLIN("kotlin", "kotlin"); - - private String engineName; - - private String displayName; - - ScriptTypeEnum(String engineName, String displayName) { - this.engineName = engineName; - this.displayName = displayName; - } - - public void setEngineName(String engineName) { - this.engineName = engineName; - } - - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - public static ScriptTypeEnum getEnumByDisplayName(String displayName) { - for (ScriptTypeEnum e : ScriptTypeEnum.values()) { - if (e.getDisplayName().equals(displayName)) { - return e; - } - } - return null; - } - - /** - * 校验脚本类型是否合法 - * - * @param scriptType 脚本类型 - * @return true:合法,false:不合法 - */ - public static boolean checkScriptType(String scriptType) { - for (ScriptTypeEnum e : ScriptTypeEnum.values()) { - if (e.getDisplayName().equals(scriptType)) { - return true; - } - } - return false; - } - -} diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java index 79b250b88..9c3f0844b 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java +++ b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java @@ -17,8 +17,6 @@ package org.apache.hertzbeat.common.script; -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; - /** * script executor interface * This code references dromara/liteflow, thanks to the author Bryan. @@ -33,7 +31,7 @@ public abstract class ScriptExecutor { public abstract void cleanCache(); - public abstract ScriptTypeEnum scriptType(); + public abstract String scriptType(); /** diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutorFactory.java b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutorFactory.java index 0c81b22ee..b8c780a64 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutorFactory.java +++ b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutorFactory.java @@ -44,7 +44,7 @@ public class ScriptExecutorFactory { @PostConstruct private void init() { for (ScriptExecutor executor : scriptExecutors) { - scriptExecutorMap.put(executor.scriptType().getDisplayName(), executor); + scriptExecutorMap.put(executor.scriptType(), executor); } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java b/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java index 7a129e1b5..78f997b1b 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java +++ b/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java @@ -17,7 +17,7 @@ package org.apache.hertzbeat.common.script.executor; -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; +import org.apache.hertzbeat.common.constants.ScriptTypeConstants; /** * This class is used to execute JavaScript code in the plugin. @@ -30,7 +30,7 @@ public class JavaScriptExecutor extends Jsr223ScriptExecutor { } @Override - public ScriptTypeEnum scriptType() { - return ScriptTypeEnum.JS; + public String scriptType() { + return ScriptTypeConstants.JAVASCRIPT; } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/support/valid/ScriptValidator.java b/common/src/main/java/org/apache/hertzbeat/common/support/valid/ScriptValidator.java index d3df0d8e2..2bcccc44f 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/support/valid/ScriptValidator.java +++ b/common/src/main/java/org/apache/hertzbeat/common/support/valid/ScriptValidator.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import lombok.extern.slf4j.Slf4j; -import org.apache.hertzbeat.common.constants.ScriptTypeEnum; import org.apache.hertzbeat.common.script.ScriptExecutor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -34,7 +33,7 @@ import org.springframework.stereotype.Component; @Component public class ScriptValidator { - private final Map<ScriptTypeEnum, ScriptExecutor> scriptExecutors = new ConcurrentHashMap<>(); + private final Map<String, ScriptExecutor> scriptExecutors = new ConcurrentHashMap<>(); @Autowired public ScriptValidator(List<ScriptExecutor> scriptExecutorList) { @@ -50,7 +49,7 @@ public class ScriptValidator { * @param scriptType scriptType * @return boolean */ - private boolean validateScript(String script, ScriptTypeEnum scriptType) { + private boolean validateScript(String script, String scriptType) { if (scriptExecutors.isEmpty()) { log.error("No script modules loaded."); @@ -92,7 +91,7 @@ public class ScriptValidator { * @param scriptType scriptType * @return boolean */ - public boolean validate(String script, ScriptTypeEnum scriptType) { + public boolean validate(String script, String scriptType) { return validateScript(script, scriptType); } @@ -100,8 +99,8 @@ public class ScriptValidator { * @param scripts scripts * @return boolean */ - public boolean validate(Map<ScriptTypeEnum, String> scripts) { - for (Map.Entry<ScriptTypeEnum, String> script : scripts.entrySet()) { + public boolean validate(Map<String, String> scripts) { + for (Map.Entry<String, String> script : scripts.entrySet()) { if (!validate(script.getValue(), script.getKey())) { return false; } 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 index 2c64f98dd..bfb15d2c9 100644 --- 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 @@ -20,7 +20,6 @@ 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; 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 0e2bba563..423041080 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 @@ -37,7 +37,6 @@ import java.util.Map; import java.util.Optional; import java.util.ServiceLoader; import java.util.Set; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; diff --git a/plugin/src/main/resources/META-INF/services/org.apache.hertzbeat.plugin.PostAlertPlugin b/plugin/src/main/resources/META-INF/services/org.apache.hertzbeat.plugin.PostAlertPlugin index 73c9c47e5..4352e4186 100644 --- a/plugin/src/main/resources/META-INF/services/org.apache.hertzbeat.plugin.PostAlertPlugin +++ b/plugin/src/main/resources/META-INF/services/org.apache.hertzbeat.plugin.PostAlertPlugin @@ -15,4 +15,4 @@ # limitations under the License. # -org.apache.hertzbeat.plugin.impl.ScriptPluginImpl \ No newline at end of file +org.apache.hertzbeat.plugin.impl.ScriptExecutorPluginImpl \ No newline at end of file diff --git a/plugin/src/main/resources/define/define.yml b/plugin/src/main/resources/define/define.yml index 7456a9cc4..e015f011d 100644 --- a/plugin/src/main/resources/define/define.yml +++ b/plugin/src/main/resources/define/define.yml @@ -22,38 +22,38 @@ params: required: true options: - label: Groovy - value: Groovy + value: groovy - label: Shell - value: Shell + value: shell - label: JavaScript - value: JavaScript + value: javascript - label: Java - value: Java + value: java - label: QLExpress - value: QLExpress + value: qlexpress - label: Python - value: Python + value: python - label: Lua - value: Lua + value: lua - label: Aviator - value: Aviator + value: aviator - label: Kotlin - value: Kotlin + value: kotlin - field: script name: zh-CN: 脚本内容 en-US: Script Content type: script required: true - - field: collector-config - name: - zh-CN: 采集器 - en-US: Collector - type: collectors-selection - required: true - - field: alert-rule - name: - zh-CN: 阈值规则ID - en-US: Alert Rule ID - type: text - required: true \ No newline at end of file +# - field: collector-config +# name: +# zh-CN: 采集器 +# en-US: Collector +# type: collectors-selection +# required: true +# - field: alert-rule +# name: +# zh-CN: 阈值规则ID +# en-US: Alert Rule ID +# type: text +# required: true \ No newline at end of file diff --git a/web-app/src/app/shared/components/form-field/form-field.component.ts b/web-app/src/app/shared/components/form-field/form-field.component.ts index 35da45aac..3a4d46d1a 100644 --- a/web-app/src/app/shared/components/form-field/form-field.component.ts +++ b/web-app/src/app/shared/components/form-field/form-field.component.ts @@ -23,7 +23,6 @@ import { AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR @Component({ selector: 'app-form-field', templateUrl: './form-field.component.html', - styleUrls: ['./form-field.component.less'], providers: [ { provide: NG_VALUE_ACCESSOR, --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
