|
| 1 | +package io.quarkus.smallrye.health.runtime.dev.ui; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.concurrent.atomic.AtomicReference; |
| 6 | + |
| 7 | +import jakarta.annotation.PostConstruct; |
| 8 | +import jakarta.inject.Inject; |
| 9 | +import jakarta.json.Json; |
| 10 | +import jakarta.json.JsonObject; |
| 11 | + |
| 12 | +import io.smallrye.health.SmallRyeHealth; |
| 13 | +import io.smallrye.health.SmallRyeHealthReporter; |
| 14 | +import io.smallrye.mutiny.Multi; |
| 15 | +import io.smallrye.mutiny.Uni; |
| 16 | +import io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor; |
| 17 | + |
| 18 | +public class HealthJsonRPCService { |
| 19 | + |
| 20 | + @Inject |
| 21 | + SmallRyeHealthReporter smallRyeHealthReporter; |
| 22 | + |
| 23 | + private final BroadcastProcessor<SmallRyeHealth> healthStream = BroadcastProcessor.create(); |
| 24 | + private final BroadcastProcessor<String> statusStream = BroadcastProcessor.create(); |
| 25 | + private final AtomicReference<String> lastPayload = new AtomicReference<>(""); |
| 26 | + |
| 27 | + @PostConstruct |
| 28 | + void startPolling() { |
| 29 | + Multi.createFrom().ticks().every(Duration.ofSeconds(3)) |
| 30 | + .onItem().transformToUniAndMerge(tick -> smallRyeHealthReporter.getHealthAsync()) |
| 31 | + .subscribe().with(smallRyeHealth -> { |
| 32 | + String jsonStr = smallRyeHealth.getPayload().toString(); |
| 33 | + if (!Objects.equals(lastPayload.getAndSet(jsonStr), jsonStr)) { |
| 34 | + if (smallRyeHealth != null) { |
| 35 | + healthStream.onNext(smallRyeHealth); |
| 36 | + statusStream.onNext(getStatusIcon(smallRyeHealth)); |
| 37 | + } |
| 38 | + } |
| 39 | + }, failure -> { |
| 40 | + JsonObject errorPayload = Json.createObjectBuilder() |
| 41 | + .add("status", "DOWN") |
| 42 | + .add("checks", Json.createArrayBuilder() |
| 43 | + .add(Json.createObjectBuilder() |
| 44 | + .add("name", "Smallrye Health stream") |
| 45 | + .add("status", "DOWN") |
| 46 | + .add("data", Json.createObjectBuilder() |
| 47 | + .add("reason", failure.getMessage())))) |
| 48 | + .build(); |
| 49 | + healthStream.onNext(new SmallRyeHealth(errorPayload)); |
| 50 | + statusStream.onNext(DOWN_ICON); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public Uni<SmallRyeHealth> getHealth() { |
| 55 | + return smallRyeHealthReporter.getHealthAsync(); |
| 56 | + } |
| 57 | + |
| 58 | + public Multi<SmallRyeHealth> streamHealth() { |
| 59 | + return healthStream; |
| 60 | + } |
| 61 | + |
| 62 | + public String getStatus() { |
| 63 | + return getStatusIcon(smallRyeHealthReporter.getHealth()); |
| 64 | + } |
| 65 | + |
| 66 | + public Multi<String> streamStatus() { |
| 67 | + return statusStream; |
| 68 | + } |
| 69 | + |
| 70 | + private String getStatusIcon(SmallRyeHealth smallRyeHealth) { |
| 71 | + if (smallRyeHealth.getPayload() != null && smallRyeHealth.getPayload().containsKey("status")) { |
| 72 | + String status = smallRyeHealth.getPayload().getString("status"); |
| 73 | + if (status.equalsIgnoreCase("UP")) { |
| 74 | + return UP_ICON; |
| 75 | + } |
| 76 | + } |
| 77 | + return DOWN_ICON; |
| 78 | + } |
| 79 | + |
| 80 | + private static final String UP_ICON = "<vaadin-icon style='color:var(--lumo-success-text-color);' icon='font-awesome-solid:thumbs-up'></vaadin-icon>"; |
| 81 | + private static final String DOWN_ICON = "<vaadin-icon style='color:var(--lumo-error-text-color);' icon='font-awesome-solid:thumbs-down'></vaadin-icon>"; |
| 82 | +} |
0 commit comments