watchdog-fe #5

Merged
usbharu merged 6 commits from watchdog-fe into master 2024-03-13 07:54:25 +00:00
3 changed files with 43 additions and 9 deletions
Showing only changes of commit ea80da90d2 - Show all commits

View File

@ -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<Metric>{
return metricsRepository.findByStatusAndGroupByObjectIdMaxTimestamp(Status.NOT_GOOD)
}
suspend fun statusCount():Map<Status,Int>{
return metricsRepository.countyGroupByObjectIdMaxTimestamp()
}
}

View File

@ -7,4 +7,6 @@ interface MetricsRepository {
suspend fun save(metric: Metric):Metric
fun findByObjectId(objectId:String): Flow<Metric>
fun findByStatusAndGroupByObjectIdMaxTimestamp(status: Status): Flow<Metric>
suspend fun countyGroupByObjectIdMaxTimestamp():Map<Status,Int>
}

View File

@ -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 {
@ -29,13 +31,13 @@ class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository {
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)
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(
@ -55,4 +57,27 @@ class MongoMetricsRepository(database: MongoDatabase) : MetricsRepository {
)
).flowOn(Dispatchers.IO)
}
override suspend fun countyGroupByObjectIdMaxTimestamp(): Map<Status, Int> {
return collection.aggregate<StatusIntMap>(
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
)
}