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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-fence.git


The following commit(s) were added to refs/heads/master by this push:
     new 4ac968f  Fix time stamp handle (#73)
4ac968f is described below

commit 4ac968ff5c65c79d17d93ba66679e00dd9d9c3ad
Author: liubao68 <[email protected]>
AuthorDate: Fri Jun 7 15:33:14 2024 +0800

    Fix time stamp handle (#73)
---
 admin-website/src/main/web/src/utils/time.ts       | 12 +++----
 .../cloud/problems/components/call-chain-list.vue  | 12 +++++--
 .../src/views/cloud/problems/components/main.vue   |  3 +-
 .../fence/observability/ObservabilityEndpoint.java | 42 ++++++++++------------
 4 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/admin-website/src/main/web/src/utils/time.ts 
b/admin-website/src/main/web/src/utils/time.ts
index 7ba711a..bb645d1 100644
--- a/admin-website/src/main/web/src/utils/time.ts
+++ b/admin-website/src/main/web/src/utils/time.ts
@@ -23,11 +23,11 @@ export function timesHandle(times: any, isCovert:boolean) {
   const date = new Date(times)
 
   const year = date.getFullYear();
-  const month = String(date.getMonth() + 1)
-  const day = String(date.getDate())
-  const hours = String(date.getHours())
-  const minutes = String(date.getMinutes())
-  const seconds = String(date.getSeconds())
+  const month = date.getMonth() + 1 < 10 ? `0${String(date.getMonth() + 1)}` : 
`${String(date.getMonth() + 1)}`;
+  const day = date.getDate() < 10 ? `0${String(date.getDate())}` : 
`${String(date.getDate())}`;
+  const hours = date.getHours() < 10 ? `0${String(date.getHours())}` : 
`${String(date.getHours())}`;
+  const minutes = date.getMinutes() < 10 ? `0${String(date.getMinutes())}` : 
`${String(date.getMinutes())}`;
+  const seconds = date.getSeconds() < 10 ? `0${String(date.getSeconds())}` : 
`${String(date.getSeconds())}`;
 
-  return 
`${year}-${month}-${day}${isCovert?'T':''}${Number(hours)<10?`0${hours}`:hours}:${Number(minutes)<10?`0${minutes}`:minutes}:${Number(seconds)<10?`0${seconds}`:seconds}`
+  return `${year}-${month}-${day}${isCovert?'T':' 
'}${hours}:${minutes}:${seconds}`;
 }
diff --git 
a/admin-website/src/main/web/src/views/cloud/problems/components/call-chain-list.vue
 
b/admin-website/src/main/web/src/views/cloud/problems/components/call-chain-list.vue
index c1f44dc..cefabd6 100644
--- 
a/admin-website/src/main/web/src/views/cloud/problems/components/call-chain-list.vue
+++ 
b/admin-website/src/main/web/src/views/cloud/problems/components/call-chain-list.vue
@@ -42,9 +42,17 @@
     <tiny-grid-column field="localhost" title="localhost"></tiny-grid-column>
     <tiny-grid-column field="serviceName" 
title="serviceName"></tiny-grid-column>
     <tiny-grid-column title="操作" width="150">
-      <template #default>
+      <template #default="data">
         <div>
-          <span class="list-operation">查看日志</span>&nbsp;|&nbsp;<span 
class="list-operation">查看Metrics</span>
+          <span class="list-operation">
+          <tiny-link type="primary"
+              
href="/v1/scb/observability/downloadLog?timestamp={{state.filterOptions.timestamp}}&service-name={{serviceName}}&instanceId={{instanceId}}">
+              下载日志</tiny-link></span>
+          &nbsp;|&nbsp;
+          <span class="list-operation">
+          <tiny-link type="primary"
+              
href="/v1/scb/observability/downloadMetrics?timestamp={{state.filterOptions.timestamp}}&service-name={{serviceName}}&instanceId={{instanceId}}">
+              下载Metrics</tiny-link></span>
         </div>
       </template>
     </tiny-grid-column>
diff --git 
a/admin-website/src/main/web/src/views/cloud/problems/components/main.vue 
b/admin-website/src/main/web/src/views/cloud/problems/components/main.vue
index ddfa082..d1aab46 100644
--- a/admin-website/src/main/web/src/views/cloud/problems/components/main.vue
+++ b/admin-website/src/main/web/src/views/cloud/problems/components/main.vue
@@ -99,9 +99,10 @@ const searchCallChain = () => {
 
     if (valid) {
       const date = new Date(state.filterOptions.startTime)
+      state.filterOptions.timestamp = timesHandle(date,true);
       // 获取调用链列表
       searchTrace({
-        timestamp: timesHandle(date,true),
+        timestamp: state.filterOptions.timestamp,
         traceId: state.filterOptions.traceId,
       }).then(response => {
         state.traceData = response as any
diff --git 
a/apis/observability-api-impl/src/main/java/org/apache/servicecomb/fence/observability/ObservabilityEndpoint.java
 
b/apis/observability-api-impl/src/main/java/org/apache/servicecomb/fence/observability/ObservabilityEndpoint.java
index e0dfec9..56d04a4 100644
--- 
a/apis/observability-api-impl/src/main/java/org/apache/servicecomb/fence/observability/ObservabilityEndpoint.java
+++ 
b/apis/observability-api-impl/src/main/java/org/apache/servicecomb/fence/observability/ObservabilityEndpoint.java
@@ -21,9 +21,10 @@ import java.io.FileNotFoundException;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
-import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Scanner;
 
@@ -36,7 +37,6 @@ import org.apache.servicecomb.foundation.common.net.NetUtils;
 import org.apache.servicecomb.foundation.common.part.FilePart;
 import org.apache.servicecomb.provider.rest.common.RestSchema;
 import org.apache.servicecomb.registry.RegistrationId;
-import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -61,8 +61,6 @@ public class ObservabilityEndpoint implements 
ObservabilityService {
 
   private static final String METRICS_FILE_PREFIX = "metrics";
 
-  private static final int ROLLING_HOUR = 3;
-
   public static final ObjectMapper OBJ_MAPPER = new ObjectMapper();
 
   private final File logPath;
@@ -169,27 +167,25 @@ public class ObservabilityEndpoint implements 
ObservabilityService {
     if (targetFiles == null) {
       return null;
     }
-    try {
-      LocalDateTime time = LocalDateTime.parse(timestamp, 
DateTimeFormatter.ISO_DATE_TIME);
+    Arrays.sort(targetFiles, Comparator.comparing(File::getName));
 
-      File result = null;
-      for (File target : targetFiles) {
-        if (prefix.length() + 1 >= target.getName().length() - 
FILE_SUFFIX.length()) {
-          result = target;
-          continue;
-        }
-        String fileTime = target.getName().substring(prefix.length() + 1,
-            target.getName().length() - FILE_SUFFIX.length());
-        LocalDateTime temp = LocalDateTime.parse(fileTime, 
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
-        if (temp.plus(1, ChronoUnit.HOURS).isAfter(time)
-            && temp.minus(ROLLING_HOUR - 1, ChronoUnit.HOURS).isBefore(time)) {
-          result = target;
-          break;
-        }
-      }
-      return result;
+    LocalDateTime time = LocalDateTime.now();
+    try {
+      time = LocalDateTime.parse(timestamp, DateTimeFormatter.ISO_DATE_TIME);
     } catch (DateTimeParseException e) {
-      throw new InvocationException(Status.BAD_REQUEST, new 
CommonExceptionData(e.getMessage()));
+      LOGGER.error("time format error, using current time. {}. ", 
e.getMessage());
+    }
+    for (File target : targetFiles) {
+      if (prefix.length() + FILE_SUFFIX.length() + 1 >= 
target.getName().length()) {
+        return target;
+      }
+      String fileTime = target.getName().substring(prefix.length() + 1,
+          target.getName().length() - FILE_SUFFIX.length());
+      LocalDateTime temp = LocalDateTime.parse(fileTime, 
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
+      if (temp.isAfter(time)) {
+        return target;
+      }
     }
+    return null;
   }
 }

Reply via email to