|
1 | 1 | package pl.jwizard.jwa.rest.route.status
|
2 | 2 |
|
3 | 3 | import io.javalin.http.Context
|
| 4 | +import io.javalin.http.pathParamAsClass |
| 5 | +import io.javalin.http.queryParamAsClass |
4 | 6 | import org.springframework.stereotype.Component
|
| 7 | +import pl.jwizard.jwa.core.server.ValidatorChainFacade |
5 | 8 | import pl.jwizard.jwl.server.route.RestControllerBase
|
6 | 9 | import pl.jwizard.jwl.server.route.RouteDefinitionBuilder
|
7 | 10 |
|
8 | 11 | @Component
|
9 | 12 | class StatusController(private val statusService: StatusService) : RestControllerBase {
|
10 | 13 | override val basePath = "/v1/status"
|
11 | 14 |
|
12 |
| - private fun getGlobalStatus(ctx: Context, language: String?) { |
13 |
| - val resDto = statusService.getGlobalStatus(language) |
| 15 | + private fun getGlobalStatus(ctx: Context) { |
| 16 | + val resDto = statusService.getGlobalStatus() |
| 17 | + ctx.json(resDto) |
| 18 | + } |
| 19 | + |
| 20 | + // all instances combined status from all running shards |
| 21 | + private fun getInstancesStatus(ctx: Context) { |
| 22 | + val avatarSize = ctx.queryParam("avatarSize") |
| 23 | + val resDto = statusService.getInstancesStatus(avatarSize?.toIntOrNull()) |
| 24 | + ctx.json(resDto) |
| 25 | + } |
| 26 | + |
| 27 | + // single instance shards status |
| 28 | + private fun getInstanceShardsStatus(ctx: Context) { |
| 29 | + val instanceId = ctx.pathParamAsClass<Int>("instanceId") |
| 30 | + val parsedInstanceId = ValidatorChainFacade(instanceId).get() |
| 31 | + val resDto = statusService.getInstanceShardsStatus(parsedInstanceId) |
| 32 | + ctx.json(resDto) |
| 33 | + } |
| 34 | + |
| 35 | + // all audio nodes status (also inactive) |
| 36 | + private fun getAudioNodesStatus(ctx: Context) { |
| 37 | + val resDto = statusService.getAudioNodesStatus() |
| 38 | + ctx.json(resDto) |
| 39 | + } |
| 40 | + |
| 41 | + // check, if shard for selected guild and (optional instance) is up and running |
| 42 | + // if shard is down or if bot is not on selected guild, return false |
| 43 | + // instanceId=null -> search in all bot instances |
| 44 | + private fun checkIfShardForGuildNameOrIdIsUp(ctx: Context) { |
| 45 | + val guildNameOrId = ctx.queryParamAsClass<String>("guild") |
| 46 | + val instanceId = ctx.queryParam("instanceId") |
| 47 | + val parsedGuildNameOrId = ValidatorChainFacade(guildNameOrId).disallowBlanks().get() |
| 48 | + val resDto = statusService.checkIfShardForGuildNameOrIdIsUp( |
| 49 | + parsedGuildNameOrId, |
| 50 | + instanceId?.toIntOrNull(), |
| 51 | + ) |
14 | 52 | ctx.json(resDto)
|
15 | 53 | }
|
16 | 54 |
|
17 | 55 | override val routes = RouteDefinitionBuilder()
|
18 |
| - .getWithI18n("/global", ::getGlobalStatus) |
| 56 | + .get("/global", ::getGlobalStatus) |
| 57 | + .get("/instance/all", ::getInstancesStatus) |
| 58 | + .get("/instance/<instanceId>/shard/all", ::getInstanceShardsStatus) |
| 59 | + .get("/audio/all", ::getAudioNodesStatus) |
| 60 | + .get("/shard/up", ::checkIfShardForGuildNameOrIdIsUp) |
19 | 61 | .compositeRoutes()
|
20 | 62 | }
|
0 commit comments