From 1e4a44a265dcb76e495a416860417dcce30cafdb Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Fri, 14 Apr 2023 12:47:52 +0530 Subject: [PATCH] Changed health endpoint response schema --- .../com/uci/dao/service/HealthService.java | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/uci/dao/service/HealthService.java b/src/main/java/com/uci/dao/service/HealthService.java index 6ee69ef..e17a686 100644 --- a/src/main/java/com/uci/dao/service/HealthService.java +++ b/src/main/java/com/uci/dao/service/HealthService.java @@ -4,12 +4,14 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.uci.dao.repository.XMessageRepository; import com.uci.utils.UtilHealthService; import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Status; import org.springframework.stereotype.Service; @Service @@ -17,13 +19,27 @@ public class HealthService extends UtilHealthService { @Autowired private XMessageRepository xMessageRepository; + @Autowired + ObjectMapper mapper; + /** * Returns health node * * @return Mono */ public Mono getCassandraHealthNode() { - return getIsCassandraHealthy().map(healthy -> new ObjectMapper().createObjectNode().put("healthy", healthy)); + return Mono.fromCallable(() -> { + ObjectNode result = mapper.createObjectNode(); + try { + xMessageRepository.existsByUserId("Test"); + result.put("status", Status.UP.getCode()); + return result; + } catch(Exception e) { + result.put("status", Status.DOWN.getCode()); + result.put("message", e.getMessage()); + return result; + } + }); } /** @@ -36,37 +52,18 @@ public Mono getCassandraHealthNode() { public Mono getAllHealthNode() { ObjectNode resultNode = new ObjectMapper().createObjectNode(); Mono externalComponentHealth = super.getAllHealthNode(); - Mono cassandraHealth = getCassandraHealthNode().map(result -> { - ObjectNode objectNode = new ObjectMapper().createObjectNode(); - objectNode.put("name", "cassandra"); - objectNode.set("healthy", result.get("healthy")); - return objectNode; - }); - return Mono.zip(externalComponentHealth, cassandraHealth).map(healths -> { - resultNode.putArray("checks") - .add(healths.getT1().get("checks")) - .add(healths.getT2()); - resultNode.put("healthy", - healths.getT1().get("healthy").booleanValue() && - healths.getT2().get("healthy").booleanValue() + Mono cassandraHealth = getCassandraHealthNode(); + return Mono.zip(externalComponentHealth, cassandraHealth).map(results -> { + ObjectNode detailsNode = mapper.createObjectNode(); + detailsNode.set("cassandra", results.getT2()); + results.getT1().get("details").fields().forEachRemaining(detail -> detailsNode.set(detail.getKey(), detail.getValue())); + resultNode.set("details", detailsNode); + resultNode.put("status", + results.getT1().get("status").textValue().equals(Status.UP.getCode()) && + results.getT2().get("status").textValue().equals(Status.UP.getCode()) + ? Status.UP.getCode() : Status.DOWN.getCode() ); return resultNode; }); } - - /** - * Check if Cassandra connecting or not - * - * @return Mono - */ - private Mono getIsCassandraHealthy() { - return Mono.fromCallable(() -> { - try { - xMessageRepository.existsByUserId("Test"); - return true; - } catch(Exception e) { - return false; - } - }); - } }