Skip to content

Commit

Permalink
chore: style optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambert-Rao committed Feb 20, 2024
1 parent d504b6d commit b4215a7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, HashMap<Long, CheckResult>> cacheMap = new HashMap<>();

public void update(String type, Long typeId, HealthCheckStatus status, String resultDesc, Long latency) {
Expand All @@ -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);
}
Expand All @@ -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<CheckResult> getAll() {
List<CheckResult> results = new ArrayList<>();
for (HashMap<Long, CheckResult> subMap : cacheMap.values()) {
results.addAll(subMap.values());
}
return results;
public Map<String, HashMap<Long, CheckResult>> getCacheMap() {
return Collections.unmodifiableMap(cacheMap);
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

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;
import org.apache.eventmesh.dashboard.console.service.health.HealthDataService;

import java.util.ArrayList;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -39,6 +40,7 @@ public class HealthExecutor {
/**
* memory cache is used to store real-time health check result.
*/
@Getter
@Setter
private CheckResultCache memoryCache;

Expand Down Expand Up @@ -127,7 +129,7 @@ public void endExecute() {
private void addToResultList(CheckResult result, ArrayList<HealthCheckResultEntity> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -71,6 +72,10 @@ private static void setClassCache(Class<?> clazz) {

private ScheduledThreadPoolExecutor scheduledExecutor;

public Map<String, HashMap<Long, CheckResult>> getCheckResultCacheMap() {
return healthExecutor.getMemoryCache().getCacheMap();
}

public void insertCheckService(List<HealthCheckObjectConfig> configList) {
configList.forEach(this::insertCheckService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -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());
}
Expand Down

0 comments on commit b4215a7

Please sign in to comment.