Skip to content

Commit

Permalink
Optimalisere siste_behandlinger ved å kun lagre de siste 10 (#2827)
Browse files Browse the repository at this point in the history
  • Loading branch information
tendestad authored Dec 19, 2024
1 parent 2cab810 commit 7afa9c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,98 @@
package no.nav.k9.los.domene.repository

import com.fasterxml.jackson.module.kotlin.readValue
import kotliquery.TransactionalSession
import kotliquery.queryOf
import kotliquery.sessionOf
import kotliquery.using
import no.nav.k9.los.domene.modell.BehandlingType
import no.nav.k9.los.domene.modell.FagsakYtelseType
import no.nav.k9.los.tjenester.avdelingsleder.nokkeltall.AlleOppgaverNyeOgFerdigstilte
import no.nav.k9.los.tjenester.avdelingsleder.nokkeltall.FerdigstiltBehandling
import no.nav.k9.los.tjenester.saksbehandler.oppgave.BehandledeOppgaver
import no.nav.k9.los.tjenester.saksbehandler.oppgave.BehandletOppgave
import no.nav.k9.los.utils.Cache
import no.nav.k9.los.utils.CacheObject
import no.nav.k9.los.utils.LosObjectMapper
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*
import java.util.concurrent.atomic.LongAdder
import javax.sql.DataSource

class StatistikkRepository(
private val dataSource: DataSource
) {
companion object {
val SISTE_8_UKER_I_DAGER = 55
val ANTALL = 10
}

fun lagreBehandling(brukerIdent: String, oppgave : BehandletOppgave) {
fun lagreBehandling(brukerIdent: String, oppgave: BehandletOppgave) {
using(sessionOf(dataSource)) {
it.transaction { tx ->
val resultat: MutableList<BehandletOppgave> = ArrayList()
val tidligere = hentBehandlinger(brukerIdent, tx, ANTALL - 1)
resultat.addAll(tidligere)
resultat.add(oppgave)

val json = LosObjectMapper.instance.writeValueAsString(oppgave)
tx.run(
queryOf(
"""
insert into siste_behandlinger as k (id, data)
values (:id, :dataInitial :: jsonb)
on conflict (id) do update
set data = jsonb_set(k.data, '{siste_behandlinger,999999}', :data :: jsonb, true)
""", mapOf("id" to brukerIdent, "dataInitial" to "{\"siste_behandlinger\": [$json]}", "data" to json)
).asUpdate
)
val json = LosObjectMapper.instance.writeValueAsString(resultat)

if (tidligere.isEmpty()) {
tx.run(
queryOf(
"""
insert into siste_behandlinger (id, data) values (:id, :data :: jsonb)
""", mapOf("id" to brukerIdent, "data" to "{\"siste_behandlinger\": $json}")
).asUpdate
)
} else {
tx.run(
queryOf(
"""
update siste_behandlinger set data = :data :: jsonb where id = :id
""", mapOf("id" to brukerIdent, "data" to "{\"siste_behandlinger\": $json}")
).asUpdate
)
}
}
}
}

fun hentBehandlinger(ident: String): List<BehandletOppgave> {
val json = using(sessionOf(dataSource)) {
it.run(
queryOf(
"""
select data, timestamp from (
select distinct on (saksnummer)
(data -> 'saksnummer') as saksnummer,
(data -> 'timestamp') as timestamp,
data from (select jsonb_array_elements(data -> 'siste_behandlinger') as data from siste_behandlinger where id = :id) as saker
order by saksnummer, timestamp desc
) as s
order by timestamp desc
limit 10
fun hentBehandlinger(ident: String, antall: Int = ANTALL): List<BehandletOppgave> {
return using(sessionOf(dataSource)) {
it.transaction { tx -> hentBehandlinger(ident, tx, antall) }
}
}

fun hentBehandlinger(ident: String, tx: TransactionalSession, antall: Int = ANTALL): List<BehandletOppgave> {
val jsonData = tx.run(
queryOf(
"""
select data from siste_behandlinger where id = :id
for update
""".trimIndent(),
mapOf("id" to ident)
mapOf(
"id" to ident,
"antall" to antall
)
.map { row ->
row.string("data")
}.asList
)
.map { row ->
row.string("data")
}
.asSingle
)
if (jsonData == null) {
return emptyList()
} else {
println(jsonData)
val medEvtDuplikater =
LosObjectMapper.instance.readValue(jsonData, BehandledeOppgaver::class.java).siste_behandlinger
val nyestePrSak: MutableList<BehandletOppgave> = mutableListOf()
medEvtDuplikater.groupBy { it.saksnummer }.entries.forEach {
nyestePrSak.add(it.value.toMutableList().sortedBy { it.timestamp }.last())
}
return nyestePrSak.sortedBy { it.timestamp }.reversed().stream().limit(antall.toLong()).toList()
}
return json.map { LosObjectMapper.instance.readValue(it, BehandletOppgave::class.java) }
}

fun lagreFerdigstilt(bt: String, eksternId: UUID, dato: LocalDate) {
Expand Down Expand Up @@ -155,7 +181,9 @@ class StatistikkRepository(
}
}

private val hentFerdigstilteOgNyeHistorikkMedYtelsetypeCache = Cache<String, List<AlleOppgaverNyeOgFerdigstilte>>(cacheSizeLimit = 1000)
private val hentFerdigstilteOgNyeHistorikkMedYtelsetypeCache =
Cache<String, List<AlleOppgaverNyeOgFerdigstilte>>(cacheSizeLimit = 1000)

fun hentFerdigstilteOgNyeHistorikkMedYtelsetypeSiste8Uker(
refresh: Boolean = false
): List<AlleOppgaverNyeOgFerdigstilte> {
Expand Down Expand Up @@ -221,7 +249,9 @@ class StatistikkRepository(
behandlingType = BehandlingType.fraKode(row.string("behandlingType")),
fagsakYtelseType = FagsakYtelseType.fraKode(row.string("fagsakYtelseType")),
dato = row.localDate("dato"),
ferdigstilte = LosObjectMapper.instance.readValue(row.stringOrNull("ferdigstilte") ?: "[]"),
ferdigstilte = LosObjectMapper.instance.readValue(
row.stringOrNull("ferdigstilte") ?: "[]"
),
nye = LosObjectMapper.instance.readValue(row.stringOrNull("nye") ?: "[]"),
ferdigstilteSaksbehandler = LosObjectMapper.instance.readValue(
row.stringOrNull("ferdigstiltesaksbehandler") ?: "[]"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package no.nav.k9.los.tjenester.saksbehandler.oppgave

data class BehandledeOppgaver (val siste_behandlinger : List<BehandletOppgave>)

0 comments on commit 7afa9c8

Please sign in to comment.