From b4215a7699799b877e9b790401fdfc3d15db9e13 Mon Sep 17 00:00:00 2001 From: "lambert@arch" Date: Tue, 20 Feb 2024 11:41:26 +0800 Subject: [PATCH] chore: style optimize --- .../console/health/CheckResultCache.java | 26 +++++----------- .../console/health/HealthExecutor.java | 6 ++-- .../console/health/HealthService.java | 9 ++++-- .../health/annotation/HealthCheckType.java | 2 +- .../RealTimeHealthCheckResultService.java | 31 ------------------- .../spring/support/FunctionManager.java | 2 +- .../spring/support/FunctionManagerBean.java | 4 ++- .../console/health/HealthExecutorTest.java | 10 +++--- 8 files changed, 28 insertions(+), 62 deletions(-) delete mode 100644 eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/service/RealTimeHealthCheckResultService.java diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/CheckResultCache.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/CheckResultCache.java index 9e694c00..540e3832 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/CheckResultCache.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/CheckResultCache.java @@ -19,21 +19,19 @@ import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckStatus; import org.apache.eventmesh.dashboard.console.health.check.config.HealthCheckObjectConfig; -import org.apache.eventmesh.dashboard.console.health.service.RealTimeHealthCheckResultService; import java.time.LocalDateTime; -import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; -import java.util.List; +import java.util.Map; import java.util.Objects; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; -public class CheckResultCache implements RealTimeHealthCheckResultService { +public class CheckResultCache { - @Getter private final HashMap> cacheMap = new HashMap<>(); public void update(String type, Long typeId, HealthCheckStatus status, String resultDesc, Long latency) { @@ -43,8 +41,8 @@ public void update(String type, Long typeId, HealthCheckStatus status, String re cacheMap.put(type, subMap); } CheckResult oldResult = subMap.get(typeId); - String oldDesc = Objects.isNull(oldResult.getResultDesc()) ? "" : oldResult.getResultDesc(); - CheckResult result = new CheckResult(status, oldDesc + "\n" + resultDesc, LocalDateTime.now(), + String oldDesc = Objects.isNull(oldResult.getResultDesc()) ? "" : oldResult.getResultDesc() + "\n"; + CheckResult result = new CheckResult(status, oldDesc + resultDesc, LocalDateTime.now(), latency, oldResult.getConfig()); subMap.put(typeId, result); } @@ -59,18 +57,8 @@ public void update(String type, Long typeId, HealthCheckStatus status, String re subMap.put(typeId, new CheckResult(status, resultDesc, LocalDateTime.now(), latency, config)); } - @Override - public CheckResult get(String type, Long typeId) { - return cacheMap.get(type).get(typeId); - } - - @Override - public List getAll() { - List results = new ArrayList<>(); - for (HashMap subMap : cacheMap.values()) { - results.addAll(subMap.values()); - } - return results; + public Map> getCacheMap() { + return Collections.unmodifiableMap(cacheMap); } @Getter diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthExecutor.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthExecutor.java index c43bc984..765bdab9 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthExecutor.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthExecutor.java @@ -19,7 +19,7 @@ import org.apache.eventmesh.dashboard.console.entity.health.HealthCheckResultEntity; import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckStatus; -import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckTypeEnum; +import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckType; import org.apache.eventmesh.dashboard.console.health.CheckResultCache.CheckResult; import org.apache.eventmesh.dashboard.console.health.callback.HealthCheckCallback; import org.apache.eventmesh.dashboard.console.health.check.AbstractHealthCheckService; @@ -27,6 +27,7 @@ import java.util.ArrayList; +import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -39,6 +40,7 @@ public class HealthExecutor { /** * memory cache is used to store real-time health check result. */ + @Getter @Setter private CheckResultCache memoryCache; @@ -127,7 +129,7 @@ public void endExecute() { private void addToResultList(CheckResult result, ArrayList resultList) { HealthCheckResultEntity newEntity = new HealthCheckResultEntity(); newEntity.setClusterId(result.getConfig().getClusterId()); - newEntity.setType(HealthCheckTypeEnum.toNumber(result.getConfig().getHealthCheckResourceType())); + newEntity.setType(HealthCheckType.toNumber(result.getConfig().getHealthCheckResourceType())); newEntity.setTypeId(result.getConfig().getInstanceId()); newEntity.setResultDesc(result.getResultDesc()); newEntity.setStatus(result.getStatus().getNumber()); diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthService.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthService.java index d8b395d5..39485e19 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthService.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/HealthService.java @@ -17,8 +17,7 @@ package org.apache.eventmesh.dashboard.console.health; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import org.apache.eventmesh.dashboard.console.health.CheckResultCache.CheckResult; import org.apache.eventmesh.dashboard.console.health.annotation.HealthCheckType; import org.apache.eventmesh.dashboard.console.health.check.AbstractHealthCheckService; import org.apache.eventmesh.dashboard.console.health.check.config.HealthCheckObjectConfig; @@ -33,6 +32,8 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; @@ -71,6 +72,10 @@ private static void setClassCache(Class clazz) { private ScheduledThreadPoolExecutor scheduledExecutor; + public Map> getCheckResultCacheMap() { + return healthExecutor.getMemoryCache().getCacheMap(); + } + public void insertCheckService(List configList) { configList.forEach(this::insertCheckService); } diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/annotation/HealthCheckType.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/annotation/HealthCheckType.java index e6c65fcb..6f396581 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/annotation/HealthCheckType.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/annotation/HealthCheckType.java @@ -24,7 +24,7 @@ /** * This annotation is used to mark the type of health check service implement. - * @see org.apache.eventmesh.dashboard.console.enums.health.HealthCheckTypeEnum + * @see org.apache.eventmesh.dashboard.console.enums.health.HealthCheckType */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/service/RealTimeHealthCheckResultService.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/service/RealTimeHealthCheckResultService.java deleted file mode 100644 index f54349a3..00000000 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/health/service/RealTimeHealthCheckResultService.java +++ /dev/null @@ -1,31 +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.eventmesh.dashboard.console.health.service; - -import org.apache.eventmesh.dashboard.console.health.CheckResultCache.CheckResult; - -import java.util.List; - -/** - * This interface is used get cached health check result from health check module. - */ -public interface RealTimeHealthCheckResultService { - public CheckResult get(String type, Long typeId); - - public List getAll(); -} diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManager.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManager.java index 7535c95f..50f5efb0 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManager.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManager.java @@ -17,11 +17,11 @@ package org.apache.eventmesh.dashboard.console.spring.support; -import lombok.Getter; import org.apache.eventmesh.dashboard.console.health.CheckResultCache; import org.apache.eventmesh.dashboard.console.health.HealthService; import org.apache.eventmesh.dashboard.console.service.health.HealthDataService; +import lombok.Getter; import lombok.Setter; /** diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManagerBean.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManagerBean.java index a149479c..716ff669 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManagerBean.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/spring/support/FunctionManagerBean.java @@ -17,9 +17,11 @@ package org.apache.eventmesh.dashboard.console.spring.support; -import javax.annotation.PostConstruct; import org.apache.eventmesh.dashboard.console.health.HealthService; import org.apache.eventmesh.dashboard.console.service.health.HealthDataService; + +import javax.annotation.PostConstruct; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; diff --git a/eventmesh-dashboard-console/src/test/java/org/apache/eventmesh/dashboard/console/health/HealthExecutorTest.java b/eventmesh-dashboard-console/src/test/java/org/apache/eventmesh/dashboard/console/health/HealthExecutorTest.java index 290a1fd8..c2bc1780 100644 --- a/eventmesh-dashboard-console/src/test/java/org/apache/eventmesh/dashboard/console/health/HealthExecutorTest.java +++ b/eventmesh-dashboard-console/src/test/java/org/apache/eventmesh/dashboard/console/health/HealthExecutorTest.java @@ -26,7 +26,7 @@ import org.apache.eventmesh.dashboard.console.EventMeshDashboardApplication; import org.apache.eventmesh.dashboard.console.entity.health.HealthCheckResultEntity; import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckStatus; -import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckTypeEnum; +import org.apache.eventmesh.dashboard.console.enums.health.HealthCheckType; import org.apache.eventmesh.dashboard.console.health.callback.HealthCheckCallback; import org.apache.eventmesh.dashboard.console.health.check.AbstractHealthCheckService; import org.apache.eventmesh.dashboard.console.health.check.config.HealthCheckObjectConfig; @@ -128,7 +128,7 @@ public void testEndExecute() { healthExecutor.endExecute(); HealthCheckResultEntity query = new HealthCheckResultEntity(); query.setClusterId(1L); - query.setType(HealthCheckTypeEnum.STORAGE.getNumber()); + query.setType(HealthCheckType.STORAGE.getNumber()); query.setTypeId(2L); assertNotNull(healthDataService.queryHealthCheckResultByClusterIdAndTypeAndTypeId(query).get(0).getStatus()); } @@ -142,7 +142,7 @@ public void testStartExecute() { healthExecutor.startExecute(); HealthCheckResultEntity query = new HealthCheckResultEntity(); query.setClusterId(1L); - query.setType(HealthCheckTypeEnum.STORAGE.getNumber()); + query.setType(HealthCheckType.STORAGE.getNumber()); query.setTypeId(1L); assertEquals(1, healthDataService.queryHealthCheckResultByClusterIdAndTypeAndTypeId(query).get(0).getStatus()); } @@ -155,7 +155,7 @@ public void testTimeout() { HealthCheckResultEntity query = new HealthCheckResultEntity(); query.setClusterId(1L); - query.setType(HealthCheckTypeEnum.STORAGE.getNumber()); + query.setType(HealthCheckType.STORAGE.getNumber()); query.setTypeId(1L); assertEquals(HealthCheckStatus.TIMEOUT.getNumber(), healthDataService.queryHealthCheckResultByClusterIdAndTypeAndTypeId(query).get(0).getStatus()); @@ -174,7 +174,7 @@ public void testFull() throws InterruptedException { healthExecutor.endExecute(); HealthCheckResultEntity query = new HealthCheckResultEntity(); query.setClusterId(1L); - query.setType(HealthCheckTypeEnum.STORAGE.getNumber()); + query.setType(HealthCheckType.STORAGE.getNumber()); query.setTypeId(1L); assertEquals(2, healthDataService.queryHealthCheckResultByClusterIdAndTypeAndTypeId(query).size()); }