From 5df3a0481250584773accdb6548c2f2817e2136b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:47:38 +0900 Subject: [PATCH] =?UTF-8?q?watchdog-be=E3=81=AE=E3=82=A8=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watch-dog-be/build.gradle.kts | 15 +++++++ .../usbharu/unos/watchdog/be/ApiController.kt | 40 +++++++++++++++++-- .../usbharu/unos/watchdog/be/domain/Metric.kt | 3 +- .../watchdog/be/domain/MetricsRepository.kt | 1 + .../be/domain/MongoMetricsRepository.kt | 38 +++++++++++++++++- watch-dog-be/src/main/resources/logback.xml | 14 +++++++ 6 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 watch-dog-be/src/main/resources/logback.xml diff --git a/watch-dog-be/build.gradle.kts b/watch-dog-be/build.gradle.kts index 3bf10d2..bc80f8a 100644 --- a/watch-dog-be/build.gradle.kts +++ b/watch-dog-be/build.gradle.kts @@ -36,6 +36,7 @@ dependencies { implementation("io.ktor:ktor-server-content-negotiation-jvm") implementation("io.ktor:ktor-serialization-kotlinx-json-jvm") implementation("io.ktor:ktor-server-netty-jvm") + implementation("ch.qos.logback:logback-classic:1.5.3") } tasks.test { @@ -68,4 +69,18 @@ protobuf { } } } +} + +jib { + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + dockerClient.environment = mapOf( + "DOCKER_HOST" to "localhost:2375" + ) + } +} + +ktor { + docker { + localImageName.set("watchdog-be") + } } \ No newline at end of file diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/ApiController.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/ApiController.kt index c7ab6ab..a6af113 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/ApiController.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/ApiController.kt @@ -1,6 +1,8 @@ package dev.usbharu.unos.watchdog.be +import dev.usbharu.unos.watchdog.be.domain.Metric import dev.usbharu.unos.watchdog.be.domain.MetricsRepository +import dev.usbharu.unos.watchdog.be.domain.Status import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* @@ -9,14 +11,46 @@ import kotlinx.coroutines.flow.Flow fun Route.api(apiController: ApiController){ route("/api/v1"){ - get("/{object-id}/metrics"){ - call.respond(HttpStatusCode.OK,apiController.getMetrics(call.parameters["object-id"].orEmpty())) + route("/objects"){ + get("/{object-id}/metrics"){ + call.respond(HttpStatusCode.OK,apiController.getMetrics(call.parameters["object-id"].orEmpty())) + } + } + route("/metrics"){ + get("/ok") { + call.respond(HttpStatusCode.OK,apiController.getAlive()) + } + get("/dead") { + call.respond(HttpStatusCode.OK,apiController.getDead()) + } + get("/unknown") { + call.respond(HttpStatusCode.OK,apiController.getUnknown()) + } + get("/not-good"){ + call.respond(HttpStatusCode.OK,apiController.getNotGood()) + } } } } class ApiController(private val metricsRepository: MetricsRepository) { - fun getMetrics(objectId:String):Flow{ + fun getMetrics(objectId:String):Flow{ return metricsRepository.findByObjectId(objectId) } + + fun getAlive():Flow{ + return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.OK) + } + + fun getDead():Flow{ + return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.DEAD) + } + + fun getUnknown():Flow{ + return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.UNKNOWN) + } + + fun getNotGood():Flow{ + return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.NOT_GOOD) + } } \ No newline at end of file diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/Metric.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/Metric.kt index 208428b..bd75492 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/Metric.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/Metric.kt @@ -13,5 +13,6 @@ data class Metric( val domain: String, val status: Status, val value: String, - val timestamp: Instant + val timestamp: Instant, + val message:String? = "" ) diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MetricsRepository.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MetricsRepository.kt index c74e893..953d8e5 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MetricsRepository.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MetricsRepository.kt @@ -6,4 +6,5 @@ import watchdog.PushMetrics interface MetricsRepository { suspend fun save(metric: Metric):Metric fun findByObjectId(objectId:String): Flow + fun findByStatusAndGroupByObjectIdMaxTimestamp(status: Status): Flow } \ No newline at end of file diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MongoMetricsRepository.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MongoMetricsRepository.kt index 26b0abf..6532f0f 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MongoMetricsRepository.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/domain/MongoMetricsRepository.kt @@ -1,6 +1,9 @@ package dev.usbharu.unos.watchdog.be.domain +import com.mongodb.client.model.Accumulators +import com.mongodb.client.model.Aggregates import com.mongodb.client.model.Filters +import com.mongodb.client.model.Projections import com.mongodb.client.model.ReplaceOptions import com.mongodb.kotlin.client.coroutine.MongoDatabase import kotlinx.coroutines.Dispatchers @@ -16,7 +19,40 @@ class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository { metric } - override fun findByObjectId(objectId:String): Flow { + override fun findByObjectId(objectId: String): Flow { return collection.find(Filters.eq(Metric::objectId.name, objectId)).limit(20).flowOn(Dispatchers.IO) } + + override fun findByStatusAndGroupByObjectIdMaxTimestamp(status: Status): Flow { + return collection.aggregate( + listOf( + Aggregates.group( + "$"+Metric::objectId.name, + Accumulators.max(Metric::timestamp.name, "$" + Metric::timestamp.name), + Accumulators.first(Metric::id.name, "\$_id"), + Accumulators.first(Metric::objectId.name, "$" + Metric::objectId.name), + Accumulators.first(Metric::domain.name, "$" + Metric::domain.name), + Accumulators.first(Metric::status.name, "$" + Metric::status.name), + Accumulators.first(Metric::value.name, "$" + Metric::value.name), + Accumulators.first(Metric::name.name, "$" + Metric::name.name), + Accumulators.first(Metric::message.name,"$"+ Metric::message.name) + ), + Aggregates.project( + Projections.fields( + Projections.include( + Metric::objectId.name, + Metric::domain.name, + Metric::status.name, + Metric::value.name, + Metric::timestamp.name, + Metric::name.name, + Metric::message.name + ), + Projections.computed("_id","$"+ Metric::id.name) + ) + ), + Aggregates.match(Filters.eq(Metric::status.name, status)) + ) + ).flowOn(Dispatchers.IO) + } } \ No newline at end of file diff --git a/watch-dog-be/src/main/resources/logback.xml b/watch-dog-be/src/main/resources/logback.xml new file mode 100644 index 0000000..ddb12b4 --- /dev/null +++ b/watch-dog-be/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + + + + ${format} + + + + + + \ No newline at end of file