@FlowPreview
@ExperimentalTime
@Controller
internal class StatusController @Autowired constructor(private val supervisor: ObserveeSupervisor) {
@GetMapping("/status", produces = [MediaType.TEXT_HTML_VALUE])
suspend fun index(model: Model): String {
val observees = ObserveeInfo.from(supervisor.observeeHealthFlows)
model.addAttribute("observees", observees)
return "status"
}
/**
* Stream of change to health status of any Observee
*/
@GetMapping("/status.stream", produces = [MediaType.TEXT_EVENT_STREAM_VALUE, CustomMediaType.TURBO_STREAM_VALUE])
suspend fun stream(model: Model): String {
val observees = ObserveeInfo.from(supervisor.observeeHealthFlows.asFlow())
model.addAttribute("observees", dataDrivenEach(observees))
return "observee-status.turbo-stream"
}
private fun dataDrivenEach(stream: Flow<Any>) = ReactiveDataDriverContextVariable(stream, 1)
}