// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/commands/UpdateApplicationInstanceHealth.java
// public final class UpdateApplicationInstanceHealth {
// 
// 	private final String id;
// 	private final Status healthStatus;
// 
// 	public UpdateApplicationInstanceHealth(String id, Status healthStatus) {
// 		Assert.notNull(id, "id must not be null!");
// 		Assert.notNull(healthStatus, "healthStatus must not be null!");
// 
// 		this.id = id;
// 		this.healthStatus = healthStatus;
// 	}
// 
// 	public String getId() {
// 		return this.id;
// 	}
// 
// 	public Status getHealthStatus() {
// 		return this.healthStatus;
// 	}
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ActuatorEndpointsUpdated.java
// public class ActuatorEndpointsUpdated extends ApplicationEvent {
// 
// 	public ActuatorEndpointsUpdated(ApplicationInstance instance) {
// 		super(instance);
// 	}
// 
// 	public ApplicationInstance getApplicationInstance() {
// 		return (ApplicationInstance) this.source;
// 	}
// 
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ApplicationInstanceHealthDataRetrievalFailed.java
// public class ApplicationInstanceHealthDataRetrievalFailed extends ApplicationEvent {
// 
// 	public ApplicationInstanceHealthDataRetrievalFailed(ApplicationInstance instance) {
// 		super(instance);
// 	}
// 
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ApplicationInstanceHealthDataRetrieved.java
// public class ApplicationInstanceHealthDataRetrieved extends ApplicationEvent {
// 
// 	private Health health;
// 
// 	public ApplicationInstanceHealthDataRetrieved(ApplicationInstance instance, Health health) {
// 		super(instance);
// 		this.health = health;
// 	}
// 
// 	public ApplicationInstance getApplicationInstance() {
// 		return (ApplicationInstance) this.source;
// 	}
// 
// 	public Health getHealth() {
// 		return this.health;
// 	}
// 
// }


import org.springframework.boot.actuate.health.Status;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import be.ordina.msdashboard.applicationinstance.commands.UpdateApplicationInstanceHealth;
import be.ordina.msdashboard.applicationinstance.events.ActuatorEndpointsUpdated;
import be.ordina.msdashboard.applicationinstance.events.ApplicationInstanceHealthDataRetrievalFailed;
import be.ordina.msdashboard.applicationinstance.events.ApplicationInstanceHealthDataRetrieved;
import org.springframework.boot.actuate.health.Health;

	@Scheduled(fixedRateString = "${applicationinstance.healthwatcher.rate:10000}")
	public void retrieveHealthDataForAllApplicationInstances() {
		logger.debug("Retrieving [HEALTH] data for all application instances");
		this.applicationInstanceService.getApplicationInstances()
				.parallelStream()
				.filter(instance -> instance.getState() != ApplicationInstance.State.DELETED)
				.filter(instance -> instance.hasActuatorEndpointFor("health"))
				.forEach(this::retrieveHealthData);
	}

	private void retrieveHealthData(ApplicationInstance instance) {
		instance.getActuatorEndpoint("health").ifPresent(link -> {
			logger.debug("Retrieving [HEALTH] data for {}", instance.getId());
			this.webClient.get().uri(link.getHref()).retrieve().bodyToMono(HealthWrapper.class)
					.defaultIfEmpty(new HealthWrapper(Status.UNKNOWN, new HashMap<>()))
					.map(HealthWrapper::getHealth)
					.doOnError(exception -> {
						logger.debug("Could not retrieve health information for [{}]", link.getHref(), exception);
						this.publisher.publishEvent(new ApplicationInstanceHealthDataRetrievalFailed(instance));
					})
					.subscribe(healthInfo -> {
						logger.debug("Retrieved health information for application instance [{}]", instance.getId());
						this.publisher.publishEvent(new ApplicationInstanceHealthDataRetrieved(instance, healthInfo));
					});
		});
	}

	@EventListener({ ApplicationInstanceHealthDataRetrieved.class })
	public void updateHealthForApplicationInstance(ApplicationInstanceHealthDataRetrieved event) {
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/commands/UpdateApplicationInstanceHealth.java
// public final class UpdateApplicationInstanceHealth {
// 
// 	private final String id;
// 	private final Status healthStatus;
// 
// 	public UpdateApplicationInstanceHealth(String id, Status healthStatus) {
// 		Assert.notNull(id, "id must not be null!");
// 		Assert.notNull(healthStatus, "healthStatus must not be null!");
// 
// 		this.id = id;
// 		this.healthStatus = healthStatus;
// 	}
// 
// 	public String getId() {
// 		return this.id;
// 	}
// 
// 	public Status getHealthStatus() {
// 		return this.healthStatus;
// 	}
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ActuatorEndpointsUpdated.java
// public class ActuatorEndpointsUpdated extends ApplicationEvent {
// 
// 	public ActuatorEndpointsUpdated(ApplicationInstance instance) {
// 		super(instance);
// 	}
// 
// 	public ApplicationInstance getApplicationInstance() {
// 		return (ApplicationInstance) this.source;
// 	}
// 
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ApplicationInstanceHealthDataRetrievalFailed.java
// public class ApplicationInstanceHealthDataRetrievalFailed extends ApplicationEvent {
// 
// 	public ApplicationInstanceHealthDataRetrievalFailed(ApplicationInstance instance) {
// 		super(instance);
// 	}
// 
// }
// 
// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/events/ApplicationInstanceHealthDataRetrieved.java
// public class ApplicationInstanceHealthDataRetrieved extends ApplicationEvent {
// 
// 	private Health health;
// 
// 	public ApplicationInstanceHealthDataRetrieved(ApplicationInstance instance, Health health) {
// 		super(instance);
// 		this.health = health;
// 	}
// 
// 	public ApplicationInstance getApplicationInstance() {
// 		return (ApplicationInstance) this.source;
// 	}
// 
// 	public Health getHealth() {
// 		return this.health;
// 	}
// 
// }


// Path: microservices-dashboard-server-core/src/main/java/be/ordina/msdashboard/applicationinstance/ApplicationInstanceHealthWatcher.java
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import be.ordina.msdashboard.applicationinstance.commands.UpdateApplicationInstanceHealth;
import be.ordina.msdashboard.applicationinstance.events.ActuatorEndpointsUpdated;
import be.ordina.msdashboard.applicationinstance.events.ApplicationInstanceHealthDataRetrievalFailed;
import be.ordina.msdashboard.applicationinstance.events.ApplicationInstanceHealthDataRetrieved;
import org.springframework.boot.actuate.health.Health;


	@Scheduled(fixedRateString = "${applicationinstance.healthwatcher.rate:10000}")
	public void retrieveHealthDataForAllApplicationInstances() {
		logger.debug("Retrieving [HEALTH] data for all application instances");
		this.applicationInstanceService.getApplicationInstances()
				.parallelStream()
				.filter(instance -> instance.getState() != ApplicationInstance.State.DELETED)
				.filter(instance -> instance.hasActuatorEndpointFor("health"))
				.forEach(this::retrieveHealthData);
	}

	private void retrieveHealthData(ApplicationInstance instance) {
		instance.getActuatorEndpoint("health").ifPresent(link -> {
			logger.debug("Retrieving [HEALTH] data for {}", instance.getId());
			this.webClient.get().uri(link.getHref()).retrieve().bodyToMono(HealthWrapper.class)
					.defaultIfEmpty(new HealthWrapper(Status.UNKNOWN, new HashMap<>()))
					.map(HealthWrapper::getHealth)
					.doOnError(exception -> {
						logger.debug("Could not retrieve health information for [{}]", link.getHref(), exception);
						this.publisher.publishEvent(new ApplicationInstanceHealthDataRetrievalFailed(instance));
					})
					.subscribe(healthInfo -> {
						logger.debug("Retrieved health information for application instance [{}]", instance.getId());
						this.publisher.publishEvent(new ApplicationInstanceHealthDataRetrieved(instance, healthInfo));
					});
		});
	}

	@EventListener({ ApplicationInstanceHealthDataRetrieved.class })
	public void updateHealthForApplicationInstance(ApplicationInstanceHealthDataRetrieved event) {
		UpdateApplicationInstanceHealth command =