You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampark.apache.org by be...@apache.org on 2023/08/23 13:07:59 UTC

[incubator-streampark] branch dev updated: [Refactor] Refactor multiple enum classes and update related methods to be more readable (#2965)

This is an automated email from the ASF dual-hosted git repository.

benjobs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git


The following commit(s) were added to refs/heads/dev by this push:
     new 6c0468ac6 [Refactor] Refactor multiple enum classes and update related methods to be more readable (#2965)
6c0468ac6 is described below

commit 6c0468ac6fb61b10c564be62caae486f73f179b4
Author: gongzhongqiang <76...@qq.com>
AuthorDate: Wed Aug 23 21:07:53 2023 +0800

    [Refactor] Refactor multiple enum classes and update related methods to be more readable (#2965)
    
    * [Refactor] Refactor multiple enum classes and update related methods to be more readable
---
 .../streampark/console/core/entity/FlinkSql.java   | 14 ++--
 .../console/core/enums/ChangeTypeEnum.java         | 98 ++++++++++++++++++++++
 .../streampark/console/core/enums/ChangedType.java | 80 ------------------
 .../console/core/enums/ConfigFileType.java         | 11 +--
 .../console/core/enums/EffectiveType.java          |  7 +-
 .../streampark/console/core/enums/EngineType.java  |  6 +-
 .../streampark/console/core/enums/LoginType.java   |  6 +-
 .../console/core/enums/ResourceFrom.java           |  7 +-
 .../console/core/enums/ResourceType.java           |  6 +-
 .../streampark/console/core/enums/UserType.java    |  6 +-
 .../service/impl/ApplicationConfigServiceImpl.java | 14 ++--
 .../core/service/impl/ApplicationServiceImpl.java  | 10 +--
 .../core/service/impl/ProjectServiceImpl.java      | 27 +++---
 13 files changed, 144 insertions(+), 148 deletions(-)

diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/entity/FlinkSql.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/entity/FlinkSql.java
index 60fc07dbd..8dc427834 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/entity/FlinkSql.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/entity/FlinkSql.java
@@ -19,7 +19,7 @@ package org.apache.streampark.console.core.entity;
 
 import org.apache.streampark.common.util.DeflaterUtils;
 import org.apache.streampark.console.core.bean.Dependency;
-import org.apache.streampark.console.core.enums.ChangedType;
+import org.apache.streampark.console.core.enums.ChangeTypeEnum;
 
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
@@ -83,7 +83,7 @@ public class FlinkSql {
     application.setSqlId(this.id);
   }
 
-  public ChangedType checkChange(FlinkSql target) {
+  public ChangeTypeEnum checkChange(FlinkSql target) {
     // 1) determine if sql statement has changed
     boolean sqlDifference = !this.getSql().trim().equals(target.getSql().trim());
     // 2) determine if dependency has changed
@@ -94,18 +94,18 @@ public class FlinkSql {
     boolean teamResDifference = !Objects.equals(this.teamResource, target.getTeamResource());
 
     if (sqlDifference && depDifference && teamResDifference) {
-      return ChangedType.ALL;
+      return ChangeTypeEnum.ALL;
     }
     if (sqlDifference) {
-      return ChangedType.SQL;
+      return ChangeTypeEnum.SQL;
     }
     if (depDifference) {
-      return ChangedType.DEPENDENCY;
+      return ChangeTypeEnum.DEPENDENCY;
     }
     if (teamResDifference) {
-      return ChangedType.TEAM_RESOURCE;
+      return ChangeTypeEnum.TEAM_RESOURCE;
     }
-    return ChangedType.NONE;
+    return ChangeTypeEnum.NONE;
   }
 
   public void base64Encode() {
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangeTypeEnum.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangeTypeEnum.java
new file mode 100644
index 000000000..d6a74cab9
--- /dev/null
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangeTypeEnum.java
@@ -0,0 +1,98 @@
+/*
+ * 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.streampark.console.core.enums;
+
+import lombok.Getter;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+/** This is an enumeration representing the types of changes that can occur. */
+@Getter
+public enum ChangeTypeEnum implements Serializable {
+
+  /** Represents no change. */
+  NONE(0, "[NONE], nothing to changed"),
+
+  /** Represents a change in dependencies. */
+  DEPENDENCY(1, "[DEPENDENCY], Dependency has changed"),
+
+  /** Represents a change in SQL. */
+  SQL(2, "[SQL], Flink Sql is changed"),
+
+  /** Represents a change in both dependencies and SQL. */
+  ALL(3, "[ALL], Dependency and Flink Sql all changed"),
+
+  /** Represents a change in team resources. */
+  TEAM_RESOURCE(4, "[TEAM_RESOURCE], Team resource has changed");
+
+  private final int value;
+  private final String description;
+
+  /**
+   * Constructor for the enum.
+   *
+   * @param value The integer value of the enum item.
+   * @param description A description of the change type.
+   */
+  ChangeTypeEnum(int value, String description) {
+    this.value = value;
+    this.description = description;
+  }
+
+  /**
+   * Returns the enum item that matches the given integer value.
+   *
+   * @param value The integer value.
+   * @return The matching enum item, or null if no match is found.
+   */
+  public static ChangeTypeEnum of(Integer value) {
+    return Arrays.stream(values())
+        .filter(changeTypeEnum -> changeTypeEnum.value == value)
+        .findFirst()
+        .orElse(null);
+  }
+
+  /**
+   * Checks if there are any changes.
+   *
+   * @return True if there are any changes, false otherwise.
+   */
+  public boolean hasChanged() {
+    return !(this == NONE);
+  }
+
+  /**
+   * Checks if there are any dependency changes.
+   *
+   * @return True if there are dependency changes, false otherwise.
+   */
+  public boolean isDependencyChanged() {
+    return this == ALL || this == DEPENDENCY || this == TEAM_RESOURCE;
+  }
+
+  /**
+   * Returns the description of the change type.
+   *
+   * @return The description of the change type.
+   */
+  @Override
+  public String toString() {
+    return description;
+  }
+}
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangedType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangedType.java
deleted file mode 100644
index 1fcdcf2cd..000000000
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ChangedType.java
+++ /dev/null
@@ -1,80 +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.streampark.console.core.enums;
-
-import java.io.Serializable;
-import java.util.Arrays;
-
-public enum ChangedType implements Serializable {
-  /** none changed */
-  NONE(0),
-
-  /** dependency has changed */
-  DEPENDENCY(1),
-
-  /** sql has changed */
-  SQL(2),
-
-  /** both */
-  ALL(3),
-
-  /** team dependency has changed */
-  TEAM_RESOURCE(4);
-
-  private final int value;
-
-  ChangedType(int value) {
-    this.value = value;
-  }
-
-  public int get() {
-    return this.value;
-  }
-
-  public static ChangedType of(Integer value) {
-    return Arrays.stream(values()).filter((x) -> x.value == value).findFirst().orElse(null);
-  }
-
-  public boolean noChanged() {
-    return this.equals(NONE);
-  }
-
-  public boolean hasChanged() {
-    return !noChanged();
-  }
-
-  public boolean isDependencyChanged() {
-    return this.equals(ALL) || this.equals(DEPENDENCY) || this.equals(TEAM_RESOURCE);
-  }
-
-  @Override
-  public String toString() {
-    switch (this) {
-      case NONE:
-        return "[NONE], nothing to changed";
-      case DEPENDENCY:
-        return "[DEPENDENCY], Dependency is changed";
-      case SQL:
-        return "[SQL], Flink Sql is changed";
-      case ALL:
-        return "[ALL], Dependency and Flink Sql all changed";
-      default:
-        return null;
-    }
-  }
-}
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ConfigFileType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ConfigFileType.java
index 5142333fe..a71c6d393 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ConfigFileType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ConfigFileType.java
@@ -17,10 +17,13 @@
 
 package org.apache.streampark.console.core.enums;
 
+import lombok.Getter;
+
 import java.io.Serializable;
 import java.util.Arrays;
 
 /** configFile Type enum */
+@Getter
 public enum ConfigFileType implements Serializable {
   YAML(1, "yaml"),
 
@@ -38,14 +41,6 @@ public enum ConfigFileType implements Serializable {
     this.typeName = name;
   }
 
-  public int getValue() {
-    return value;
-  }
-
-  public String getTypeName() {
-    return typeName;
-  }
-
   public static ConfigFileType of(Integer value) {
     return Arrays.stream(values()).filter((x) -> x.value == value).findFirst().orElse(null);
   }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EffectiveType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EffectiveType.java
index bb71fa74e..6817358ae 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EffectiveType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EffectiveType.java
@@ -17,8 +17,11 @@
 
 package org.apache.streampark.console.core.enums;
 
+import lombok.Getter;
+
 import java.io.Serializable;
 
+@Getter
 public enum EffectiveType implements Serializable {
   /** config */
   CONFIG(1),
@@ -30,8 +33,4 @@ public enum EffectiveType implements Serializable {
   EffectiveType(int value) {
     this.type = value;
   }
-
-  public int getType() {
-    return type;
-  }
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EngineType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EngineType.java
index b47b7da3e..d0310e37b 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EngineType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/EngineType.java
@@ -18,10 +18,12 @@
 package org.apache.streampark.console.core.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
+import lombok.Getter;
 
 import java.util.Arrays;
 
 /** Compute engine type. */
+@Getter
 public enum EngineType {
 
   /** Apache Flink: activated by default */
@@ -36,10 +38,6 @@ public enum EngineType {
     this.code = code;
   }
 
-  public int getCode() {
-    return code;
-  }
-
   public static EngineType of(Integer code) {
     return Arrays.stream(values()).filter((x) -> x.code == code).findFirst().orElse(null);
   }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/LoginType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/LoginType.java
index 7467d6531..3b4f7c796 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/LoginType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/LoginType.java
@@ -18,11 +18,13 @@
 package org.apache.streampark.console.core.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
+import lombok.Getter;
 
 import java.util.Arrays;
 import java.util.Objects;
 
 /** The user login type. */
+@Getter
 public enum LoginType {
 
   /** sign in with password */
@@ -40,10 +42,6 @@ public enum LoginType {
     this.code = code;
   }
 
-  public int getCode() {
-    return code;
-  }
-
   public static LoginType of(Integer code) {
     return Arrays.stream(values()).filter((x) -> x.code == code).findFirst().orElse(null);
   }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
index 32d4fba40..38d7888fb 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
@@ -17,9 +17,12 @@
 
 package org.apache.streampark.console.core.enums;
 
+import lombok.Getter;
+
 import java.io.Serializable;
 import java.util.Arrays;
 
+@Getter
 public enum ResourceFrom implements Serializable {
 
   /** cicd(build from cvs) */
@@ -37,8 +40,4 @@ public enum ResourceFrom implements Serializable {
   public static ResourceFrom of(Integer value) {
     return Arrays.stream(values()).filter((x) -> x.value.equals(value)).findFirst().orElse(null);
   }
-
-  public Integer getValue() {
-    return value;
-  }
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceType.java
index bf61517e5..2a000a448 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceType.java
@@ -18,10 +18,12 @@
 package org.apache.streampark.console.core.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
+import lombok.Getter;
 
 import java.util.Arrays;
 
 /** The resource type. */
+@Getter
 public enum ResourceType {
 
   /** Flink application */
@@ -45,10 +47,6 @@ public enum ResourceType {
     this.code = code;
   }
 
-  public int getCode() {
-    return code;
-  }
-
   public static ResourceType of(Integer code) {
     return Arrays.stream(values()).filter((x) -> x.code == code).findFirst().orElse(null);
   }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/UserType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/UserType.java
index f90dc031c..73e8a07b1 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/UserType.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/UserType.java
@@ -18,10 +18,12 @@
 package org.apache.streampark.console.core.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
+import lombok.Getter;
 
 import java.util.Arrays;
 
 /** The user type. */
+@Getter
 public enum UserType {
 
   /** The admin of StreamPark. */
@@ -36,10 +38,6 @@ public enum UserType {
     this.code = code;
   }
 
-  public int getCode() {
-    return code;
-  }
-
   public static UserType of(Integer code) {
     return Arrays.stream(values()).filter((x) -> x.code == code).findFirst().orElse(null);
   }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationConfigServiceImpl.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationConfigServiceImpl.java
index bb020c11c..5b560fc29 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationConfigServiceImpl.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationConfigServiceImpl.java
@@ -256,13 +256,13 @@ public class ApplicationConfigServiceImpl
   private void fillEffectiveField(Long id, List<ApplicationConfig> configList) {
     ApplicationConfig effective = getEffective(id);
 
-    if (effective != null) {
-      for (ApplicationConfig config : configList) {
-        if (config.getId().equals(effective.getId())) {
-          config.setEffective(true);
-          break;
-        }
-      }
+    if (effective == null) {
+      return;
     }
+
+    configList.stream()
+        .filter(config -> config.getId().equals(effective.getId()))
+        .findFirst()
+        .ifPresent(config -> config.setEffective(true));
   }
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationServiceImpl.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationServiceImpl.java
index 76c5089b7..ded9b40a8 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationServiceImpl.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ApplicationServiceImpl.java
@@ -54,7 +54,7 @@ import org.apache.streampark.console.core.entity.Resource;
 import org.apache.streampark.console.core.entity.SavePoint;
 import org.apache.streampark.console.core.enums.AppExistsState;
 import org.apache.streampark.console.core.enums.CandidateType;
-import org.apache.streampark.console.core.enums.ChangedType;
+import org.apache.streampark.console.core.enums.ChangeTypeEnum;
 import org.apache.streampark.console.core.enums.CheckPointType;
 import org.apache.streampark.console.core.enums.ConfigFileType;
 import org.apache.streampark.console.core.enums.FlinkAppState;
@@ -1000,12 +1000,12 @@ public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Appli
       FlinkSql targetFlinkSql = new FlinkSql(appParam);
 
       // judge sql and dependency has changed
-      ChangedType changedType = copySourceFlinkSql.checkChange(targetFlinkSql);
+      ChangeTypeEnum changeTypeEnum = copySourceFlinkSql.checkChange(targetFlinkSql);
 
-      log.info("updateFlinkSqlJob changedType: {}", changedType);
+      log.info("updateFlinkSqlJob changeTypeEnum: {}", changeTypeEnum);
 
       // if has been changed
-      if (changedType.hasChanged()) {
+      if (changeTypeEnum.hasChanged()) {
         // check if there is a candidate version for the newly added record
         FlinkSql newFlinkSql = flinkSqlService.getCandidate(application.getId(), CandidateType.NEW);
         // If the candidate version of the new record exists, it will be deleted directly,
@@ -1025,7 +1025,7 @@ public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Appli
         }
         FlinkSql sql = new FlinkSql(appParam);
         flinkSqlService.create(sql);
-        if (changedType.isDependencyChanged()) {
+        if (changeTypeEnum.isDependencyChanged()) {
           application.setBuild(true);
         }
       } else {
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java
index e190b6e6a..e568d976b 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java
@@ -28,7 +28,6 @@ import org.apache.streampark.console.base.domain.RestRequest;
 import org.apache.streampark.console.base.domain.RestResponse;
 import org.apache.streampark.console.base.exception.ApiAlertException;
 import org.apache.streampark.console.base.mybatis.pager.MybatisPager;
-import org.apache.streampark.console.base.util.CommonUtils;
 import org.apache.streampark.console.base.util.FileUtils;
 import org.apache.streampark.console.base.util.GZipUtils;
 import org.apache.streampark.console.core.entity.Application;
@@ -72,6 +71,8 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @Slf4j
 @Service
@@ -233,24 +234,16 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
   public List<String> modules(Long id) {
     Project project = getById(id);
     Utils.notNull(project);
-    BuildState buildState = BuildState.of(project.getBuildState());
-    if (BuildState.SUCCESSFUL.equals(buildState)) {
-      File appHome = project.getDistHome();
-      if (appHome.exists()) {
-        List<String> list = new ArrayList<>();
-        File[] files = appHome.listFiles();
-        if (CommonUtils.notEmpty(files)) {
-          for (File file : files) {
-            list.add(file.getName());
-          }
-        }
-        return list;
-      } else {
-        return Collections.emptyList();
-      }
-    } else {
+
+    if (!BuildState.SUCCESSFUL.equals(BuildState.of(project.getBuildState()))
+        || !project.getDistHome().exists()) {
       return Collections.emptyList();
     }
+
+    File[] files = project.getDistHome().listFiles();
+    return files == null
+        ? Collections.emptyList()
+        : Stream.of(files).map(File::getName).collect(Collectors.toList());
   }
 
   @Override