Skip to content

Commit

Permalink
Add p95 and p99 latency percentiles to MonitorDetailsDto (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkobor authored Aug 20, 2020
1 parent 603ff1e commit 2eb1444
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/api-doc/kuvasz-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ components:
type: integer
format: int32
nullable: true
p95LatencyInMs:
type: integer
format: int32
nullable: true
p99LatencyInMs:
type: integer
format: int32
nullable: true
UptimeStatus:
type: string
enum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ data class MonitorDetailsDto(
val uptimeStatus: UptimeStatus?,
val uptimeStatusStartedAt: OffsetDateTime?,
val uptimeError: String?,
val averageLatencyInMs: Int?
val averageLatencyInMs: Int?,
val p95LatencyInMs: Int?,
val p99LatencyInMs: Int?
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ class LatencyLogRepository @Inject constructor(jooqConfig: Configuration) : Late
dsl.delete(LATENCY_LOG)
.where(LATENCY_LOG.CREATED_AT.lessThan(limit))
.execute()

fun getLatencyPercentileForMonitor(monitorId: Int, percentile: Int): Int? =
dsl.fetch(
"""
with ranked as (
select latency_log.latency, ntile(100) over (order by latency_log.latency) as quartile
from latency_log where monitor_id = ?
)
select min(latency) from ranked
where quartile = ?
group by quartile""", monitorId, percentile
).into(Int::class.java).firstOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kuvaszuptime.kuvasz.models.MonitorNotFoundError
import com.kuvaszuptime.kuvasz.models.dto.MonitorCreateDto
import com.kuvaszuptime.kuvasz.models.dto.MonitorDetailsDto
import com.kuvaszuptime.kuvasz.models.dto.MonitorUpdateDto
import com.kuvaszuptime.kuvasz.repositories.LatencyLogRepository
import com.kuvaszuptime.kuvasz.repositories.MonitorRepository
import com.kuvaszuptime.kuvasz.tables.pojos.MonitorPojo
import javax.inject.Inject
Expand All @@ -14,13 +15,30 @@ import javax.inject.Singleton
@Singleton
class MonitorCrudService @Inject constructor(
private val monitorRepository: MonitorRepository,
private val latencyLogRepository: LatencyLogRepository,
private val checkScheduler: CheckScheduler
) {

fun getMonitorDetails(monitorId: Int): Option<MonitorDetailsDto> = monitorRepository.getMonitorDetails(monitorId)
companion object {
private const val P95 = 95
private const val P99 = 99
}

fun getMonitorDetails(monitorId: Int): Option<MonitorDetailsDto> =
monitorRepository.getMonitorDetails(monitorId).map { detailsDto ->
detailsDto.copy(
p95LatencyInMs = latencyLogRepository.getLatencyPercentileForMonitor(monitorId, P95),
p99LatencyInMs = latencyLogRepository.getLatencyPercentileForMonitor(monitorId, P99)
)
}

fun getMonitorsWithDetails(enabledOnly: Boolean): List<MonitorDetailsDto> =
monitorRepository.getMonitorsWithDetails(enabledOnly)
monitorRepository.getMonitorsWithDetails(enabledOnly).map { detailsDto ->
detailsDto.copy(
p95LatencyInMs = latencyLogRepository.getLatencyPercentileForMonitor(detailsDto.id, P95),
p99LatencyInMs = latencyLogRepository.getLatencyPercentileForMonitor(detailsDto.id, P99)
)
}

fun getMonitor(monitorId: Int): Option<MonitorPojo> = monitorRepository.findById(monitorId).toOption()

Expand Down

0 comments on commit 2eb1444

Please sign in to comment.