From ea80da90d2123fcd1e0b04b1d5b96228f4237a3f Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:00:54 +0900 Subject: [PATCH] =?UTF-8?q?watchdog-be:=20=E5=90=84=E7=8A=B6=E6=85=8B?= =?UTF-8?q?=E3=81=AE=E3=82=AA=E3=83=96=E3=82=B8=E3=82=A7=E3=82=AF=E3=83=88?= =?UTF-8?q?=E3=81=AE=E6=95=B0=E3=82=92=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/unos/watchdog/be/ApiController.kt | 7 +++ .../watchdog/be/domain/MetricsRepository.kt | 2 + .../be/domain/MongoMetricsRepository.kt | 43 +++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) 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 a6af113..c68c7c1 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 @@ -29,6 +29,9 @@ fun Route.api(apiController: ApiController){ get("/not-good"){ call.respond(HttpStatusCode.OK,apiController.getNotGood()) } + get { + call.respond(HttpStatusCode.OK,apiController.statusCount()) + } } } } @@ -53,4 +56,8 @@ class ApiController(private val metricsRepository: MetricsRepository) { fun getNotGood():Flow{ return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.NOT_GOOD) } + + suspend fun statusCount():Map{ + return metricsRepository.countyGroupByObjectIdMaxTimestamp() + } } \ No newline at end of file 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 953d8e5..4eba638 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 @@ -7,4 +7,6 @@ interface MetricsRepository { suspend fun save(metric: Metric):Metric fun findByObjectId(objectId:String): Flow fun findByStatusAndGroupByObjectIdMaxTimestamp(status: Status): Flow + + suspend fun countyGroupByObjectIdMaxTimestamp():Map } \ 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 6532f0f..1936b8f 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 @@ -9,7 +9,9 @@ import com.mongodb.kotlin.client.coroutine.MongoDatabase import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.toList import kotlinx.coroutines.withContext +import kotlinx.serialization.Serializable class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository { @@ -27,15 +29,15 @@ class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository { return collection.aggregate( listOf( Aggregates.group( - "$"+Metric::objectId.name, + "$" + 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) + Accumulators.last(Metric::id.name, "\$_id"), + Accumulators.last(Metric::objectId.name, "$" + Metric::objectId.name), + Accumulators.last(Metric::domain.name, "$" + Metric::domain.name), + Accumulators.last(Metric::status.name, "$" + Metric::status.name), + Accumulators.last(Metric::value.name, "$" + Metric::value.name), + Accumulators.last(Metric::name.name, "$" + Metric::name.name), + Accumulators.last(Metric::message.name, "$" + Metric::message.name) ), Aggregates.project( Projections.fields( @@ -48,11 +50,34 @@ class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository { Metric::name.name, Metric::message.name ), - Projections.computed("_id","$"+ Metric::id.name) + Projections.computed("_id", "$" + Metric::id.name) ) ), Aggregates.match(Filters.eq(Metric::status.name, status)) ) ).flowOn(Dispatchers.IO) } + + override suspend fun countyGroupByObjectIdMaxTimestamp(): Map { + return collection.aggregate( + listOf( + Aggregates.group( + "$" + Metric::objectId.name, + Accumulators.max(Metric::timestamp.name, "$" + Metric::timestamp.name), + Accumulators.last(Metric::status.name, "$" + Metric::status.name), + ), + Aggregates.group( + "\$status", + Accumulators.first("status","\$status"), + Accumulators.sum("count", 1) + ) + ) + ).flowOn(Dispatchers.IO).toList().associate { it.status to it.count } + } + + @Serializable + data class StatusIntMap( + val status: Status, + val count: Int + ) } \ No newline at end of file