You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampark.apache.org by mo...@apache.org on 2022/11/07 14:50:34 UTC

[incubator-streampark] branch dev updated: [bug] milliseconds to duration bug fixed (#1979)

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

monster 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 ff0488e4b [bug] milliseconds to duration bug fixed (#1979)
ff0488e4b is described below

commit ff0488e4bc8a4f88aeea8925ae60ca644aaf9f58
Author: benjobs <be...@apache.org>
AuthorDate: Mon Nov 7 22:50:27 2022 +0800

    [bug] milliseconds to duration bug fixed (#1979)
---
 .../apache/streampark/common/util/DateUtils.scala  | 33 +++++++++++++---------
 .../console/core/bean/AlertTemplate.java           |  6 ++--
 .../console/core/controller/AlertController.java   |  2 +-
 .../org/apache/commons/mail/SendEmailTest.java     |  5 ++--
 .../core/service/alert/AlertServiceTest.java       |  4 +--
 5 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/streampark-common/src/main/scala/org/apache/streampark/common/util/DateUtils.scala b/streampark-common/src/main/scala/org/apache/streampark/common/util/DateUtils.scala
index 168cd7994..948065023 100644
--- a/streampark-common/src/main/scala/org/apache/streampark/common/util/DateUtils.scala
+++ b/streampark-common/src/main/scala/org/apache/streampark/common/util/DateUtils.scala
@@ -17,7 +17,7 @@
 package org.apache.streampark.common.util
 
 import java.text.{ParseException, SimpleDateFormat}
-import java.time.LocalDateTime
+import java.time.{Duration, LocalDateTime}
 import java.time.format.DateTimeFormatter
 import java.util._
 import java.util.concurrent.TimeUnit
@@ -137,22 +137,27 @@ object DateUtils {
   /**
    * Convert duration in seconds to rich time duration format. e.g. 2 days 3 hours 4 minutes 5 seconds
    *
-   * @param duration in second
+   * @param milliseconds
    * @return
    */
-  def toRichTimeDuration(duration: Long): String = {
-    val days = TimeUnit.SECONDS.toDays(duration)
-    val duration1 = duration - TimeUnit.DAYS.toSeconds(days)
-    val hours = TimeUnit.SECONDS.toHours(duration1)
-    val duration2 = duration1 - TimeUnit.HOURS.toSeconds(hours)
-    val minutes = TimeUnit.SECONDS.toMinutes(duration2)
-    val duration3 = duration2 - TimeUnit.MINUTES.toSeconds(minutes)
-    val seconds = TimeUnit.SECONDS.toSeconds(duration3)
+  def toDuration(milliseconds: Long): String = {
+    val duration = Duration.ofMillis(milliseconds)
+    val days = duration.toDays
+
+    val duration1 = milliseconds - TimeUnit.DAYS.toMillis(days)
+    lazy val hours = TimeUnit.MILLISECONDS.toHours(duration1)
+
+    lazy val duration2 = duration1 - TimeUnit.HOURS.toMillis(hours)
+    lazy val minutes = TimeUnit.MILLISECONDS.toMinutes(duration2)
+
+    lazy val duration3 = duration2 - TimeUnit.MINUTES.toMillis(minutes)
+    lazy val seconds = TimeUnit.MILLISECONDS.toSeconds(duration3)
+
     val builder = new StringBuilder
-    if (days != 0) builder.append(days + " days ")
-    if (days != 0 || hours != 0) builder.append(hours + " hours ")
-    if (days != 0 || hours != 0 || minutes != 0) builder.append(minutes + " minutes ")
-    builder.append(seconds + " seconds")
+    if (days > 0) builder.append(days + " days ")
+    if (hours > 0 || minutes > 0 || seconds > 0) builder.append(hours + " hours ")
+    if (minutes > 0 || seconds > 0) builder.append(minutes + " minutes ")
+    if (seconds > 0) builder.append(seconds + " seconds ")
     builder.toString
   }
 
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/AlertTemplate.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/AlertTemplate.java
index 265e1fa5a..aec0ff81a 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/AlertTemplate.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/AlertTemplate.java
@@ -55,8 +55,6 @@ public class AlertTemplate implements Serializable {
         } else {
             duration = application.getEndTime().getTime() - application.getStartTime().getTime();
         }
-        duration = duration / 1000 / 60;
-
         AlertTemplate template = new AlertTemplate();
         template.setJobName(application.getJobName());
 
@@ -70,7 +68,7 @@ public class AlertTemplate implements Serializable {
 
         template.setStartTime(DateUtils.format(application.getStartTime(), DateUtils.fullFormat(), TimeZone.getDefault()));
         template.setEndTime(DateUtils.format(application.getEndTime() == null ? new Date() : application.getEndTime(), DateUtils.fullFormat(), TimeZone.getDefault()));
-        template.setDuration(DateUtils.toRichTimeDuration(duration));
+        template.setDuration(DateUtils.toDuration(duration));
         boolean needRestart = application.isNeedRestartOnFailed() && application.getRestartCount() > 0;
         template.setRestart(needRestart);
         if (needRestart) {
@@ -92,7 +90,7 @@ public class AlertTemplate implements Serializable {
     public static AlertTemplate of(Application application, CheckPointStatus checkPointStatus) {
         AlertTemplate template = of(application);
         template.setType(2);
-        template.setCpFailureRateInterval(DateUtils.toRichTimeDuration(application.getCpFailureRateInterval()));
+        template.setCpFailureRateInterval(DateUtils.toDuration(application.getCpFailureRateInterval() * 1000 * 60));
         template.setCpMaxFailureInterval(application.getCpMaxFailureInterval());
         template.setTitle(String.format("Notify: %s checkpoint FAILED", application.getJobName()));
         template.setSubject(String.format("StreamPark Alert: %s, checkPoint is Failed", template.getJobName()));
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/AlertController.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/AlertController.java
index 025d924b6..d683ffb4d 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/AlertController.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/AlertController.java
@@ -134,7 +134,7 @@ public class AlertController {
         Date date = new Date();
         alertTemplate.setStartTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
         alertTemplate.setEndTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
-        alertTemplate.setDuration(DateUtils.toRichTimeDuration(0));
+        alertTemplate.setDuration("");
         boolean alert = alertService.alert(AlertConfigWithParams.of(alertConfigService.getById(id)), alertTemplate);
         return RestResponse.success(alert);
     }
diff --git a/streampark-console/streampark-console-service/src/test/java/org/apache/commons/mail/SendEmailTest.java b/streampark-console/streampark-console-service/src/test/java/org/apache/commons/mail/SendEmailTest.java
index 7ce35bdca..580eeab64 100644
--- a/streampark-console/streampark-console-service/src/test/java/org/apache/commons/mail/SendEmailTest.java
+++ b/streampark-console/streampark-console-service/src/test/java/org/apache/commons/mail/SendEmailTest.java
@@ -99,14 +99,13 @@ class SendEmailTest {
         } else {
             duration = application.getEndTime().getTime() - application.getStartTime().getTime();
         }
-        duration = duration / 1000 / 60;
         String format = "%s/proxy/%s/";
         String url = String.format(format, YarnUtils.getRMWebAppURL(), application.getAppId());
 
         AlertTemplate template = new AlertTemplate();
         template.setJobName(application.getJobName());
         template.setStartTime(DateUtils.format(application.getStartTime(), DateUtils.fullFormat(), TimeZone.getDefault()));
-        template.setDuration(DateUtils.toRichTimeDuration(duration));
+        template.setDuration(DateUtils.toDuration(duration));
         template.setLink(url);
         template.setEndTime(
             DateUtils.format(application.getEndTime() == null ? new Date() : application.getEndTime(), DateUtils.fullFormat(),
@@ -114,7 +113,7 @@ class SendEmailTest {
         template.setRestart(application.isNeedRestartOnFailed());
         template.setRestartIndex(application.getRestartCount());
         template.setTotalRestart(application.getRestartSize());
-        template.setCpFailureRateInterval(DateUtils.toRichTimeDuration(application.getCpFailureRateInterval()));
+        template.setCpFailureRateInterval(DateUtils.toDuration(application.getCpFailureRateInterval() * 1000 * 60));
         template.setCpMaxFailureInterval(application.getCpMaxFailureInterval());
 
         return template;
diff --git a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/alert/AlertServiceTest.java b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/alert/AlertServiceTest.java
index 224e1d660..c74f8ffc0 100644
--- a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/alert/AlertServiceTest.java
+++ b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/alert/AlertServiceTest.java
@@ -56,7 +56,7 @@ class AlertServiceTest {
         Date date = new Date();
         alertTemplate.setStartTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
         alertTemplate.setEndTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
-        alertTemplate.setDuration(DateUtils.toRichTimeDuration(0));
+        alertTemplate.setDuration("");
     }
 
     void before2() {
@@ -72,7 +72,7 @@ class AlertServiceTest {
         Date date = new Date();
         alertTemplate.setStartTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
         alertTemplate.setEndTime(DateUtils.format(date, DateUtils.fullFormat(), TimeZone.getDefault()));
-        alertTemplate.setDuration(DateUtils.toRichTimeDuration(0));
+        alertTemplate.setDuration("");
     }
 
     @Test