From 15a5e34bd0e0fbc2ebe8e98fb00271fd8fa27f79 Mon Sep 17 00:00:00 2001 From: casionone Date: Sun, 8 Oct 2023 18:28:44 +0800 Subject: [PATCH] code optimize and add some unit test --- .../execute/ComputationExecutor.scala | 2 +- .../interceptor/impl/TemplateConfUtils.scala | 21 +-- .../impl/TemplateConfUtilsTest.java | 120 ++++++++++++++++++ 3 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 linkis-computation-governance/linkis-entrance/src/test/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtilsTest.java diff --git a/linkis-computation-governance/linkis-engineconn/linkis-computation-engineconn/src/main/scala/org/apache/linkis/engineconn/computation/executor/execute/ComputationExecutor.scala b/linkis-computation-governance/linkis-engineconn/linkis-computation-engineconn/src/main/scala/org/apache/linkis/engineconn/computation/executor/execute/ComputationExecutor.scala index e25fa1c6cb..c4f9711507 100644 --- a/linkis-computation-governance/linkis-engineconn/linkis-computation-engineconn/src/main/scala/org/apache/linkis/engineconn/computation/executor/execute/ComputationExecutor.scala +++ b/linkis-computation-governance/linkis-engineconn/linkis-computation-engineconn/src/main/scala/org/apache/linkis/engineconn/computation/executor/execute/ComputationExecutor.scala @@ -373,7 +373,7 @@ abstract class ComputationExecutor(val outputPrintLimit: Int = 1000) sb.append("\n") engineExecutorContext.appendStdout( - LogUtils.generateInfo(s" Your job exec with configs:\n${sb.toString()}\n") + LogUtils.generateInfo(s"Your job exec with configs:\n${sb.toString()}\n") ) } diff --git a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtils.scala b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtils.scala index 8f030f42fe..5cfdf3037d 100644 --- a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtils.scala +++ b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtils.scala @@ -114,17 +114,20 @@ object TemplateConfUtils extends Logging { * * @param code * :code - * @param languageType - * :SQL,PYTHON + * @param codeType + * :sql,hql,scala * @return + * String the last one of template conf name */ - def getCustomTemplateConfName(code: String, languageType: String): String = { + def getCustomTemplateConfName(code: String, codeType: String): String = { var templateConfName = ""; var varString: String = null var errString: String = null var rightVarString: String = null + val languageType = CodeAndRunTypeUtils.getLanguageTypeByCodeType(codeType) + languageType match { case CodeAndRunTypeUtils.LANGUAGE_TYPE_SQL => varString = s"""\\s*---@set ${confTemplateNameKey}=\\s*.+\\s*""" @@ -211,13 +214,13 @@ object TemplateConfUtils extends Logging { logger.info("try to get template conf list with template uid:{} ", templateUuid) logAppender.append( LogUtils - .generateInfo(s"try to get template conf data with template uid:$templateUuid\nn") + .generateInfo(s"Try to get template conf data with template uid:$templateUuid\nn") ) templateConflist = templateCache.get(templateUuid) if (templateConflist == null || templateConflist.size() == 0) { logAppender.append( LogUtils.generateWarn( - s"can not get any template conf data with template uid:$templateUuid\n" + s"Can not get any template conf data with template uid:$templateUuid\n" ) ) } else { @@ -225,22 +228,22 @@ object TemplateConfUtils extends Logging { LabelBuilderFactoryContext.getLabelBuilderFactory.createLabel( classOf[ExecuteOnceLabel] ) - logger.info("add once label for task id:{}", requestPersistTask.getId.toString) + logger.info("Add once label for task id:{}", requestPersistTask.getId.toString) requestPersistTask.getLabels.add(onceLabel) } } } else { - logger.info("try to get template conf list with template name:{} ", templateName) + logger.info("Try to get template conf list with template name:{} ", templateName) logAppender.append( LogUtils - .generateInfo(s"try to get template conf data with template name:$templateName\n") + .generateInfo(s"Try to get template conf data with template name:$templateName\n") ) templateConflist = templateCacheName.get(templateName) if (templateConflist == null || templateConflist.size() == 0) { logAppender.append( LogUtils.generateWarn( - s"can not get any template conf data with template name:$templateName\n" + s"Can not get any template conf data with template name:$templateName\n" ) ) } diff --git a/linkis-computation-governance/linkis-entrance/src/test/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtilsTest.java b/linkis-computation-governance/linkis-entrance/src/test/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtilsTest.java new file mode 100644 index 0000000000..a7dce6f3d6 --- /dev/null +++ b/linkis-computation-governance/linkis-entrance/src/test/scala/org/apache/linkis/entrance/interceptor/impl/TemplateConfUtilsTest.java @@ -0,0 +1,120 @@ +/* + * 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.linkis.entrance.interceptor.impl; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class TemplateConfUtilsTest { + + @Test + void getCustomTemplateConfName() { + String sqlCode="" + + "--注解\n" + + "select * from table;\n" + + " --注解 \n" + + "--注解\n" + + " select \"--注解\" as test\n" + + " --@set yy=123\n" + + " --注解"; + + + String res=TemplateConfUtils.getCustomTemplateConfName(sqlCode,"sql"); + assertEquals(res,""); + + String sqlCode2="" + + "---@set 123=注解\n" + + "select * from table;\n" + + " --注解 \n" + + "--注解\n" + + " select \"--注解\" as test\n" + + " --@set yy=123\n" + + " --注解"; + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode2,"sql"); + assertEquals(res,""); + + String sqlCode3="" + + "---@set ec.resource.name=345\n" + + "select * from table;\n" + + " --注解 \n" + + "--注解\n" + + "---@set ec.resource.name=456\n" + + " select \"--注解\" as test\n" + + " --@set yy=123\n" + + " --注解"; + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode3,"sql"); + assertEquals(res,"456"); + + String sqlCode4="" + + "---@set ec.resource.name=345\n" + + "select * from table;\n" + + " --注解 \n" + + "--注解\n" + + "---@set ec.resource.name= name1 \n" + + " select \"--注解\" as test\n" + + " --@set yy=123\n" + + " --注解"; + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode4,"sql"); + assertEquals(res,"name1"); + + + String sqlCode5="" + + "##@set ec.resource.name=345\n" + + "select * from table;\n" + + " --注解 \n" + + "#注解\n" + + "##@set ec.resource.name= pyname1 \n" + + " select \"--注解\" as test\n" + + "#@set yy=123\n" + + " #注解"; + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode5,"python"); + assertEquals(res,"pyname1"); + + String sqlCode6="" + + "///@set ec.resource.name=345\n" + + "select * from table;\n" + + " //注解 \n" + + "//注解\n" + + "///@set ec.resource.name= scalaname1 \n" + + " select \"//注解\" as test\n" + + "//@set yy=123\n" + + " #注解"; + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode6,"scala"); + assertEquals(res,"scalaname1"); + + + String sqlCode7="" + + "---@set ec.resource.name=345\n" + + "select * from table;\n" + + " --注解 \n" + + "--注解\n" + + "---@set ec.resource.name= hqlname1 \n" + + " select \"--注解\" as test\n" + + " --@set yy=123\n" + + " --注解"; + + + res=TemplateConfUtils.getCustomTemplateConfName(sqlCode7,"hql"); + assertEquals(res,"hqlname1"); + } +} \ No newline at end of file