Skip to content

Commit

Permalink
unngå unnødvendige db queries
Browse files Browse the repository at this point in the history
Statistikk på statusoversikt på behovsmeldinger er ikke så viktig
at det må kjøres 3000 ganger om dagen. Endrer derfor til å kun
kjøre 1 gang hver natt, for å redusere CPU-load.
  • Loading branch information
olesls committed Feb 9, 2024
1 parent 578675a commit f6cd437
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/no/nav/hjelpemidler/soknad/db/metrics/Metrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import no.nav.hjelpemidler.soknad.db.db.SøknadStore
import no.nav.hjelpemidler.soknad.db.domain.Status
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Date
import java.util.Timer
import java.util.UUID
import java.util.concurrent.TimeUnit
import kotlin.concurrent.timerTask

private val logg = KotlinLogging.logger {}

Expand All @@ -15,6 +22,20 @@ internal class Metrics(
private val aivenMetrics: AivenMetrics = AivenMetrics(),
) {

init {
Timer("metrics", true).schedule(
timerTask {
runBlocking {
launch {
countApplicationsByStatus()
}
}
},
midnatt(),
TimeUnit.DAYS.toMillis(1),
)
}

fun measureElapsedTimeBetweenStatusChanges(soknadsId: UUID, status: Status) {
runBlocking {
launch(Job()) {
Expand Down Expand Up @@ -125,3 +146,9 @@ internal class Metrics(
const val COUNT_OF_SOKNAD_BY_STATUS = "hm-soknadsbehandling.event.count_of_soknad_by_status"
}
}

private fun midnatt() = LocalDate.now().plusDays(1).atStartOfDay().toDate()

private fun LocalDateTime.toDate(): Date {
return Date.from(this.atZone(ZoneId.systemDefault()).toInstant())
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ internal fun Route.azureAdRoutes(
call.respond(rowsUpdated)

metrics.measureElapsedTimeBetweenStatusChanges(soknadsId, newStatus)
metrics.countApplicationsByStatus()
} catch (e: Exception) {
logger.error(e) { "Feilet ved oppdatering av søknad" }
call.respond(HttpStatusCode.BadRequest, "Feilet ved oppdatering av søknad")
Expand All @@ -362,7 +361,6 @@ internal fun Route.azureAdRoutes(
call.respond(rowsUpdated)

metrics.measureElapsedTimeBetweenStatusChanges(statusMedÅrsak.søknadId, statusMedÅrsak.status)
metrics.countApplicationsByStatus()
} catch (e: Exception) {
logger.error(e) { "Feilet ved oppdatering av søknad" }
call.respond(HttpStatusCode.BadRequest, "Feilet ved oppdatering av søknad")
Expand Down

0 comments on commit f6cd437

Please sign in to comment.