watchdog-fe #5
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
}
|
|
@ -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
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue