You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@linkis.apache.org by GitBox <gi...@apache.org> on 2022/07/06 05:46:18 UTC

[GitHub] [incubator-linkis] duhanmin opened a new pull request, #2415: support Variable Operation

duhanmin opened a new pull request, #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415

   variable | result
   -- | --
   ${yyyy-01-01} | 2021-01-01
   ${yyyy-01-01%-2y} | 2019-01-01
   ${yyyy-MM-01%-2M} | 2021-02-01
   ${yyyy-MM-dd%-2d} | 2021-03-31
   ${yyyy MM ----- HH%-1H} | 2021 04 ----- 14
   ${yyyyMMdd%-1d} | 20210401
   ${yyyyMM01%-1M} | 20210301
   ${HH%-1H} | 14
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] duhanmin closed pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
duhanmin closed pull request #2415: support Variable Operation
URL: https://github.com/apache/incubator-linkis/pull/2415


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] peacewong commented on a diff in pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
peacewong commented on code in PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415#discussion_r914388428


##########
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java:
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.common.utils;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang3.StringUtils;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ *
+ * support variable operation
+ * ${yyyyMMdd%-1d}/${yyyy-MM-01%-2M}
+ * Date: 2021/5/7 11:10
+ */
+public class VariableOperationUtils {
+
+    private static final String DOLLAR = "$";
+    private static final String PLACEHOLDER_SPLIT = "%";
+    private static final String PLACEHOLDER_LEFT = "{";
+    private static final String PLACEHOLDER_RIGHT = "}";
+    private static final String CYCLE_YEAR = "y";
+    private static final String CYCLE_MONTH = "M";
+    private static final String CYCLE_DAY = "d";
+    private static final String CYCLE_HOUR = "H";
+    private static final String CYCLE_MINUTE = "m";
+    private static final String CYCLE_SECOND = "s";
+    private static final String[] CYCLES  = new String[]{CYCLE_YEAR, CYCLE_MONTH, CYCLE_DAY, CYCLE_HOUR, CYCLE_MINUTE, CYCLE_SECOND};
+
+    /**
+     * yyyy-MM-dd HH:mm:ss
+     * @param date
+     * @return
+     */
+    public static ZonedDateTime toZonedDateTime(Date date) {
+        Instant instant = date.toInstant();
+        ZoneId zoneId = ZoneId.systemDefault();
+        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
+        return ZonedDateTime.of(localDateTime,zoneId);
+    }
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str){
+        return replaces(dateTime,str,true);
+    }
+
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @param format
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str,boolean format){
+        try {
+            JsonNode rootNode = JsonUtils.jackson().readTree(str);
+            if (rootNode.isArray() || rootNode.isObject()){
+                replaceJson(dateTime,rootNode);
+                return rootNode.toString();
+            }
+        }catch (Exception e){}
+        return replace(dateTime,str);
+    }
+
+    /**
+     *
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    private static String replace(ZonedDateTime dateTime, String str){
+        StringBuilder buffer = new StringBuilder(str);
+        int startIndex = str.indexOf(PLACEHOLDER_LEFT);
+
+        while (startIndex != -1) {
+            int endIndex = buffer.indexOf(PLACEHOLDER_RIGHT, startIndex);
+            if (endIndex != -1) {
+                String placeHolder = buffer.substring(startIndex, endIndex + 1);
+                String content = placeHolder.replace(PLACEHOLDER_LEFT, "").replace(PLACEHOLDER_RIGHT, "").trim();
+                String[] parts = content.split(PLACEHOLDER_SPLIT);
+                try{
+                    ZonedDateTime ndt = dateTime;
+                    for(int i = 1;i< parts.length;i++){
+                        ndt = changeDateTime(ndt,parts[i]);
+                    }
+
+                    String newContent = ndt.format(DateTimeFormatter.ofPattern(parts[0]));
+                    if (buffer.substring(startIndex -1 ,endIndex + 1).contains(DOLLAR)){
+                        buffer.replace(startIndex - 1, endIndex + 1, newContent);
+                    }else {
+                        buffer.replace(startIndex, endIndex + 1, newContent);
+                    }
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, startIndex + newContent.length());
+                }catch (IllegalArgumentException e1){
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, endIndex);
+
+                } catch (Exception e2){
+                    throw new RuntimeException(e2);

Review Comment:
   Linkis exception should be thrown, you can refer to https://linkis.apache.org/zh-CN/community/development_specification/exception_catch



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] duhanmin commented on pull request #2415: Variable Operation

Posted by GitBox <gi...@apache.org>.
duhanmin commented on PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415#issuecomment-1175074305

   https://github.com/apache/incubator-linkis/issues/2416


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] codecov[bot] commented on pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
codecov[bot] commented on PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415#issuecomment-1176985478

   # [Codecov](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#2415](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (b58ad89) into [dev-1.2.0](https://codecov.io/gh/apache/incubator-linkis/commit/b4627300e2dc80ff3031a5d8140cb34c26941cba?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (b462730) will **increase** coverage by `0.28%`.
   > The diff coverage is `45.38%`.
   
   ```diff
   @@               Coverage Diff               @@
   ##             dev-1.2.0    #2415      +/-   ##
   ===============================================
   + Coverage        17.83%   18.11%   +0.28%     
   - Complexity        1077     1093      +16     
   ===============================================
     Files              595      596       +1     
     Lines            17667    17795     +128     
     Branches          2635     2660      +25     
   ===============================================
   + Hits              3151     3224      +73     
   - Misses           14092    14136      +44     
   - Partials           424      435      +11     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [...he/linkis/common/utils/VariableOperationUtils.java](https://codecov.io/gh/apache/incubator-linkis/pull/2415/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlua2lzLWNvbW1vbnMvbGlua2lzLWNvbW1vbi9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvbGlua2lzL2NvbW1vbi91dGlscy9WYXJpYWJsZU9wZXJhdGlvblV0aWxzLmphdmE=) | `44.62% <44.62%> (ø)` | |
   | [...org/apache/linkis/common/utils/VariableUtils.scala](https://codecov.io/gh/apache/incubator-linkis/pull/2415/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlua2lzLWNvbW1vbnMvbGlua2lzLWNvbW1vbi9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2xpbmtpcy9jb21tb24vdXRpbHMvVmFyaWFibGVVdGlscy5zY2FsYQ==) | `59.77% <50.00%> (-0.35%)` | :arrow_down: |
   | [.../org/apache/linkis/common/conf/Configuration.scala](https://codecov.io/gh/apache/incubator-linkis/pull/2415/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlua2lzLWNvbW1vbnMvbGlua2lzLWNvbW1vbi9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2xpbmtpcy9jb21tb24vY29uZi9Db25maWd1cmF0aW9uLnNjYWxh) | `53.33% <100.00%> (+53.33%)` | :arrow_up: |
   | [...s/scheduler/queue/fifoqueue/FIFOUserConsumer.scala](https://codecov.io/gh/apache/incubator-linkis/pull/2415/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlua2lzLWNvbW1vbnMvbGlua2lzLXNjaGVkdWxlci9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2xpbmtpcy9zY2hlZHVsZXIvcXVldWUvZmlmb3F1ZXVlL0ZJRk9Vc2VyQ29uc3VtZXIuc2NhbGE=) | `35.55% <0.00%> (-2.23%)` | :arrow_down: |
   | [...ala/org/apache/linkis/common/utils/JsonUtils.scala](https://codecov.io/gh/apache/incubator-linkis/pull/2415/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlua2lzLWNvbW1vbnMvbGlua2lzLWNvbW1vbi9zcmMvbWFpbi9zY2FsYS9vcmcvYXBhY2hlL2xpbmtpcy9jb21tb24vdXRpbHMvSnNvblV0aWxzLnNjYWxh) | `100.00% <0.00%> (+100.00%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [b462730...b58ad89](https://codecov.io/gh/apache/incubator-linkis/pull/2415?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] duhanmin closed pull request #2415: Variable Operation

Posted by GitBox <gi...@apache.org>.
duhanmin closed pull request #2415: Variable Operation
URL: https://github.com/apache/incubator-linkis/pull/2415


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] peacewong merged pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
peacewong merged PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] duhanmin commented on a diff in pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
duhanmin commented on code in PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415#discussion_r914435391


##########
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java:
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.common.utils;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang3.StringUtils;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ *
+ * support variable operation
+ * ${yyyyMMdd%-1d}/${yyyy-MM-01%-2M}
+ * Date: 2021/5/7 11:10
+ */
+public class VariableOperationUtils {
+
+    private static final String DOLLAR = "$";
+    private static final String PLACEHOLDER_SPLIT = "%";
+    private static final String PLACEHOLDER_LEFT = "{";
+    private static final String PLACEHOLDER_RIGHT = "}";
+    private static final String CYCLE_YEAR = "y";
+    private static final String CYCLE_MONTH = "M";
+    private static final String CYCLE_DAY = "d";
+    private static final String CYCLE_HOUR = "H";
+    private static final String CYCLE_MINUTE = "m";
+    private static final String CYCLE_SECOND = "s";
+    private static final String[] CYCLES  = new String[]{CYCLE_YEAR, CYCLE_MONTH, CYCLE_DAY, CYCLE_HOUR, CYCLE_MINUTE, CYCLE_SECOND};
+
+    /**
+     * yyyy-MM-dd HH:mm:ss
+     * @param date
+     * @return
+     */
+    public static ZonedDateTime toZonedDateTime(Date date) {
+        Instant instant = date.toInstant();
+        ZoneId zoneId = ZoneId.systemDefault();
+        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
+        return ZonedDateTime.of(localDateTime,zoneId);
+    }
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str){
+        return replaces(dateTime,str,true);
+    }
+
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @param format
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str,boolean format){
+        try {
+            JsonNode rootNode = JsonUtils.jackson().readTree(str);
+            if (rootNode.isArray() || rootNode.isObject()){
+                replaceJson(dateTime,rootNode);
+                return rootNode.toString();
+            }
+        }catch (Exception e){}
+        return replace(dateTime,str);
+    }
+
+    /**
+     *
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    private static String replace(ZonedDateTime dateTime, String str){
+        StringBuilder buffer = new StringBuilder(str);
+        int startIndex = str.indexOf(PLACEHOLDER_LEFT);
+
+        while (startIndex != -1) {
+            int endIndex = buffer.indexOf(PLACEHOLDER_RIGHT, startIndex);
+            if (endIndex != -1) {
+                String placeHolder = buffer.substring(startIndex, endIndex + 1);
+                String content = placeHolder.replace(PLACEHOLDER_LEFT, "").replace(PLACEHOLDER_RIGHT, "").trim();
+                String[] parts = content.split(PLACEHOLDER_SPLIT);
+                try{
+                    ZonedDateTime ndt = dateTime;
+                    for(int i = 1;i< parts.length;i++){
+                        ndt = changeDateTime(ndt,parts[i]);
+                    }
+
+                    String newContent = ndt.format(DateTimeFormatter.ofPattern(parts[0]));
+                    if (buffer.substring(startIndex -1 ,endIndex + 1).contains(DOLLAR)){
+                        buffer.replace(startIndex - 1, endIndex + 1, newContent);
+                    }else {
+                        buffer.replace(startIndex, endIndex + 1, newContent);
+                    }
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, startIndex + newContent.length());
+                }catch (IllegalArgumentException e1){
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, endIndex);
+
+                } catch (Exception e2){
+                    throw new RuntimeException(e2);

Review Comment:
   这里可以不抛异的,我改下



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


[GitHub] [incubator-linkis] duhanmin closed pull request #2415: support Variable Operation

Posted by GitBox <gi...@apache.org>.
duhanmin closed pull request #2415: support Variable Operation
URL: https://github.com/apache/incubator-linkis/pull/2415


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org