Skip to content

Commit 8f7c303

Browse files
committed
(feat) add instance and shard status endpoints (with service methods) into status controller
1 parent 47c7772 commit 8f7c303

File tree

10 files changed

+336
-24
lines changed

10 files changed

+336
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,62 @@
11
package pl.jwizard.jwa.rest.route.status
22

33
import io.javalin.http.Context
4+
import io.javalin.http.pathParamAsClass
5+
import io.javalin.http.queryParamAsClass
46
import org.springframework.stereotype.Component
7+
import pl.jwizard.jwa.core.server.ValidatorChainFacade
58
import pl.jwizard.jwl.server.route.RestControllerBase
69
import pl.jwizard.jwl.server.route.RouteDefinitionBuilder
710

811
@Component
912
class StatusController(private val statusService: StatusService) : RestControllerBase {
1013
override val basePath = "/v1/status"
1114

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+
)
1452
ctx.json(resDto)
1553
}
1654

1755
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)
1961
.compositeRoutes()
2062
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package pl.jwizard.jwa.rest.route.status
22

3-
import pl.jwizard.jwa.rest.route.status.dto.GlobalStatusResDto
3+
import pl.jwizard.jwa.rest.route.status.dto.*
44

55
interface StatusService {
6-
fun getGlobalStatus(language: String?): GlobalStatusResDto
6+
fun getGlobalStatus(): GlobalStatusResDto
7+
8+
fun getInstancesStatus(avatarSize: Int?): List<InstanceStatusResDto>
9+
10+
fun getInstanceShardsStatus(instanceId: Int): List<ShardStatusResDto>
11+
12+
fun getAudioNodesStatus(): AudioNodesStatusResDto
13+
14+
fun checkIfShardForGuildNameOrIdIsUp(guildNameOrId: String, instanceId: Int?): ShardCheckResDto
715
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class AudioNodeStatus(
4+
val up: Boolean,
5+
val id: Int,
6+
val name: String,
7+
val pool: String,
8+
val region: String,
9+
val version: String? = "?",
10+
val lavaplayerVersion: String? = "?",
11+
val players: Int? = 0,
12+
val playingPlayers: Int? = 0,
13+
val uptime: String? = "?",
14+
val responseTime: Long? = 0
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class AudioNodesStatusResDto(
4+
val externalServicesUrl: String,
5+
val nodes: List<AudioNodeStatus>,
6+
)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pl.jwizard.jwa.rest.route.status.dto
22

33
data class GlobalStatusResDto(
4-
val operational: Boolean?,
5-
val description: String,
6-
val sourceUrl: String,
4+
val globalUp: Boolean?,
5+
val externalServicesWebsiteUrl: String,
76
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class InstanceStatusResDto(
4+
val id: Int,
5+
val name: String,
6+
val color: String,
7+
val avatarUrl: String,
8+
val shards: RunningStatusCount,
9+
val processes: RunningStatusCount,
10+
val avgShardGatewayPing: Long,
11+
val avgShardsPerProcess: Int,
12+
val servers: Int,
13+
val users: Int,
14+
val activeAudioPlayers: Int,
15+
val audioListeners: Int,
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class RunningStatusCount(
4+
val up: Int,
5+
val down: Int,
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class ShardCheckResDto(
4+
// true - shard with selected guild is active, false - shard is down or not assigned to
5+
// searched guild
6+
val status: Boolean,
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pl.jwizard.jwa.rest.route.status.dto
2+
3+
data class ShardStatusResDto(
4+
val up: Boolean,
5+
val globalShardId: Int,
6+
val processGroupId: Int,
7+
val gatewayPing: Long? = 0,
8+
val servers: Int? = 0,
9+
val users: Int? = 0,
10+
val activeAudioPlayers: Int? = 0,
11+
val audioListeners: Int? = 0,
12+
)

0 commit comments

Comments
 (0)