Skip to content

Commit

Permalink
Merge pull request #11 from chinmoy12c/enhancement/change_health_schema
Browse files Browse the repository at this point in the history
Changed health endpoint response schema
  • Loading branch information
pankajjangid05 authored Apr 27, 2023
2 parents 474183b + 1e4a44a commit 5a153da
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions src/main/java/com/uci/dao/service/HealthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@

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
public class HealthService extends UtilHealthService {
@Autowired
private XMessageRepository xMessageRepository;

@Autowired
ObjectMapper mapper;

/**
* Returns health node
*
* @return Mono<JsonNode>
*/
public Mono<JsonNode> 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;
}
});
}

/**
Expand All @@ -36,37 +52,18 @@ public Mono<JsonNode> getCassandraHealthNode() {
public Mono<JsonNode> getAllHealthNode() {
ObjectNode resultNode = new ObjectMapper().createObjectNode();
Mono<JsonNode> externalComponentHealth = super.getAllHealthNode();
Mono<JsonNode> 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<JsonNode> 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<Boolean>
*/
private Mono<Boolean> getIsCassandraHealthy() {
return Mono.fromCallable(() -> {
try {
xMessageRepository.existsByUserId("Test");
return true;
} catch(Exception e) {
return false;
}
});
}
}

0 comments on commit 5a153da

Please sign in to comment.