Skip to content

Commit

Permalink
Sender meldinger om avbrutt behandling til statistikk der det mangler
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindsh committed Dec 13, 2024
1 parent af381ef commit 4b5ea7e
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class BehandlingFactory(
gruppeId = persongalleri.avdoed.firstOrNull(),
)
return BehandlingOgOppgave(behandling, oppgave) {
behandlingHendelser.sendMeldingForHendelseStatisitkk(
behandlingHendelser.sendMeldingForHendelseStatistikk(
behandling.toStatistikkBehandling(persongalleri),
BehandlingHendelseType.OPPRETTET,
)
Expand Down Expand Up @@ -351,7 +351,7 @@ class BehandlingFactory(
}
}

behandlingHendelser.sendMeldingForHendelseStatisitkk(
behandlingHendelser.sendMeldingForHendelseStatistikk(
behandlingerForOmgjoering.nyFoerstegangsbehandling.toStatistikkBehandling(persongalleri),
BehandlingHendelseType.OPPRETTET,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ internal class BehandlingServiceImpl(
val persongalleri =
runBlocking { grunnlagKlient.hentPersongalleri(behandlingId, saksbehandler) }

behandlingHendelser.sendMeldingForHendelseStatisitkk(
behandlingHendelser.sendMeldingForHendelseStatistikk(
behandling.toStatistikkBehandling(persongalleri = persongalleri!!.opplysning),
BehandlingHendelseType.AVBRUTT,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import org.slf4j.LoggerFactory
import java.util.UUID

interface BehandlingHendelserKafkaProducer {
fun sendMeldingForHendelseStatisitkk(
fun sendMeldingForHendelseStatistikk(
statistikkBehandling: StatistikkBehandling,
hendelseType: BehandlingHendelseType,
overstyrtTekniskTid: Tidspunkt?,
)

fun sendMeldingForHendelsePaaVent(
Expand All @@ -44,9 +45,10 @@ class BehandlingsHendelserKafkaProducerImpl(
) : BehandlingHendelserKafkaProducer {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)

override fun sendMeldingForHendelseStatisitkk(
override fun sendMeldingForHendelseStatistikk(
statistikkBehandling: StatistikkBehandling,
hendelseType: BehandlingHendelseType,
overstyrtTekniskTid: Tidspunkt?,
) {
val correlationId = getCorrelationId()

Expand All @@ -58,7 +60,7 @@ class BehandlingsHendelserKafkaProducerImpl(
hendelseType.lagEventnameForType(),
mapOf(
CORRELATION_ID_KEY to correlationId,
TEKNISK_TID_KEY to Tidspunkt.now(),
TEKNISK_TID_KEY to (overstyrtTekniskTid ?: Tidspunkt.now()),
STATISTIKKBEHANDLING_RIVER_KEY to statistikkBehandling,
),
).toJson(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package no.nav.etterlatte.behandling

import no.nav.etterlatte.behandling.hendelse.getUUID
import no.nav.etterlatte.behandling.jobs.BehandlingSomIkkeErAvbruttIStatistikk
import no.nav.etterlatte.common.ConnectionAutoclosing
import no.nav.etterlatte.libs.common.feilhaandtering.checkInternFeil
import no.nav.etterlatte.libs.common.sak.SakId
import no.nav.etterlatte.libs.database.toList
import java.util.UUID

class SendManglendeMeldingerDao(
private val connectionAutoclosing: ConnectionAutoclosing,
) {
fun hentManglendeAvslagBehandling(): List<BehandlingSomIkkeErAvbruttIStatistikk> =
connectionAutoclosing.hentConnection {
val statement =
it.prepareStatement(
"""
SELECT behandling_id, sak_id, mangler_hendelse from behandling_mangler_avbrudd_statistikk where sendt_melding = false limit 100
""".trimIndent(),
)
return@hentConnection statement
.executeQuery()
.toList {
BehandlingSomIkkeErAvbruttIStatistikk(
behandlingId = getUUID("behandling_id"),
sakId = SakId(getLong("sak_id")),
manglerHendelse = getBoolean("mangler_hendelse"),
)
}
}

fun oppdaterSendtMelding(behandlingId: UUID) {
connectionAutoclosing.hentConnection {
val statement =
it.prepareStatement(
"""
UPDATE behandling_mangler_avbrudd_statistikk SET sendt_melding = true WHERE behandling_id = ?
""".trimIndent(),
)
statement.setObject(1, behandlingId)
checkInternFeil(statement.executeUpdate() == 1) {
"Fikk ikke oppdatert raden for behandling har sendt melding for behandling $behandlingId"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class HendelseDao(
saksbehandler: String,
kommentar: String? = null,
valgtBegrunnelse: String? = null,
overstyrtInntruffet: Tidspunkt? = null,
) = lagreHendelse(
UlagretHendelse(
hendelse = "BEHANDLING:AVBRUTT",
inntruffet = Tidspunkt.now(),
inntruffet = overstyrtInntruffet ?: Tidspunkt.now(),
vedtakId = null,
behandlingId = behandling.id,
sakId = behandling.sak.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package no.nav.etterlatte.behandling.jobs

import kotlinx.coroutines.runBlocking
import no.nav.etterlatte.behandling.BehandlingDao
import no.nav.etterlatte.behandling.BehandlingHendelserKafkaProducer
import no.nav.etterlatte.behandling.SendManglendeMeldingerDao
import no.nav.etterlatte.behandling.domain.toStatistikkBehandling
import no.nav.etterlatte.behandling.hendelse.HendelseDao
import no.nav.etterlatte.behandling.klienter.GrunnlagKlient
import no.nav.etterlatte.inTransaction
import no.nav.etterlatte.jobs.LoggerInfo
import no.nav.etterlatte.jobs.fixedRateCancellableTimer
import no.nav.etterlatte.libs.common.TimerJob
import no.nav.etterlatte.libs.common.behandling.BehandlingHendelseType
import no.nav.etterlatte.libs.common.feilhaandtering.InternfeilException
import no.nav.etterlatte.libs.common.sak.SakId
import no.nav.etterlatte.libs.common.tidspunkt.toTidspunkt
import no.nav.etterlatte.libs.jobs.LeaderElection
import no.nav.etterlatte.libs.ktor.token.HardkodaSystembruker
import org.slf4j.LoggerFactory
import java.time.Duration
import java.time.LocalDate
import java.util.Timer
import java.util.UUID

class ResendManglendeAvbrytJob(
private val leaderElection: LeaderElection,
private val kafkaProducer: BehandlingHendelserKafkaProducer,
private val sendManglendeMeldingerDao: SendManglendeMeldingerDao,
private val behandlingDao: BehandlingDao,
private val grunnlagKlient: GrunnlagKlient,
private val hendelseDao: HendelseDao,
) : TimerJob {
private val logger = LoggerFactory.getLogger(ResendManglendeAvbrytJob::class.java)
private val jobbNavn = ResendManglendeAvbrytJob::class.java.simpleName
private val initialDelay = Duration.ofMinutes(3L)
private val period = Duration.ofMinutes(3L)

override fun schedule(): Timer {
logger.info("Jobb $jobbNavn startet med initialDelay $initialDelay med periode $period")
return fixedRateCancellableTimer(
name = jobbNavn,
initialDelay = initialDelay.toMillis(),
period = period.toMillis(),
loggerInfo =
LoggerInfo(
logger = logger,
loggTilSikkerLogg = false,
),
) {
SendManglendeAvbrytMelding(
leaderElection,
sendManglendeMeldingerDao,
kafkaProducer,
behandlingDao,
grunnlagKlient,
hendelseDao,
).send()
}
}

class SendManglendeAvbrytMelding(
private val leaderElection: LeaderElection,
private val sendManglendeMeldingerDao: SendManglendeMeldingerDao,
private val kafkaProducer: BehandlingHendelserKafkaProducer,
private val behandlingDao: BehandlingDao,
private val grunnlagKlient: GrunnlagKlient,
private val hendelseDao: HendelseDao,
) {
private val logger = LoggerFactory.getLogger(SendManglendeAvbrytMelding::class.java)

fun send() {
if (!leaderElection.isLeader()) {
logger.info("Er ikke leader, kjører ikke ${SendManglendeAvbrytMelding::class.java.simpleName}-jobb")
}
try {
val behandlinger = inTransaction { sendManglendeMeldingerDao.hentManglendeAvslagBehandling() }
behandlinger.forEach {
try {
inTransaction {
val behandling =
behandlingDao.hentBehandling(it.behandlingId)
?: throw InternfeilException("Behandling ${it.behandlingId} ble ikke funnet")
val inntruffet = behandling.sistEndret.toTidspunkt()
val persongalleri =
runBlocking {
grunnlagKlient.hentPersongalleri(
behandling.id,
HardkodaSystembruker.oppgave,
)
}
?: throw InternfeilException("Kunne ikke hente persongalleri for behandling ${behandling.id}")
if (it.manglerHendelse) {
hendelseDao.behandlingAvbrutt(
behandling = behandling,
saksbehandler = "EY",
kommentar =
"Behandling ble avbrutt igjennom flyt som ikke lagret hendelse. " +
"Hendelse ble lagt til ${LocalDate.now()}",
valgtBegrunnelse = "RYDDING_HENDELSER",
)
}
kafkaProducer.sendMeldingForHendelseStatistikk(
statistikkBehandling = behandling.toStatistikkBehandling(persongalleri.opplysning),
hendelseType = BehandlingHendelseType.AVBRUTT,
overstyrtTekniskTid = inntruffet,
)
sendManglendeMeldingerDao.oppdaterSendtMelding(behandling.id)
}
} catch (e: Exception) {
logger.warn(
"Kunne ikke sende sende hendelse for behandling med id = ${it.behandlingId} i " +
"sak ${it.sakId}",
)
}
}
} catch (e: Exception) {
logger.warn("Kunne ikke sende avbryt melding for behandling som mangler", e)
}
}
}
}

data class BehandlingSomIkkeErAvbruttIStatistikk(
val behandlingId: UUID,
val sakId: SakId,
val manglerHendelse: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class RevurderingService(
}
},
sendMeldingForHendelse = {
behandlingHendelser.sendMeldingForHendelseStatisitkk(
behandlingHendelser.sendMeldingForHendelseStatistikk(
it.toStatistikkBehandling(persongalleri),
BehandlingHendelseType.OPPRETTET,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
create table behandling_mangler_avbrudd_statistikk
(
behandling_id UUID primary key,
sak_id BIGINT,
mangler_hendelse BOOL default false,
sendt_melding BOOL default false
);

-- Populerer tabellen med behandlinger som ikke har fått en behandlinghendelse med avbrutt,
-- siden de har blitt avbrutt utenfor standard flyt og dermed har de heller ikke sendt melding
-- til statistikk
insert into behandling_mangler_avbrudd_statistikk (behandling_id, sak_id, mangler_hendelse)
select b.id, sak_id, true
from behandling b
where b.status = 'AVBRUTT'
and not exists(select 1 from behandlinghendelse where behandlingid = b.id and hendelse = 'BEHANDLING:AVBRUTT');
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
-- 108 behandlinger ble avbrutt uten at melding ble sendt til statistikk før flyten for avbryt
-- ble fikset slik at vi alltid fikk med meldinger i disse sakene.
insert into behandling_mangler_avbrudd_statistikk (behandling_id, sak_id, mangler_hendelse)
VALUES ('1982524d-c174-40fb-94b5-b80e91ed4a36', 1, false),
('3b43eede-2141-4c7c-9085-ada4bff1ff8d', 1, false),
('66e1e608-35e2-4ba9-9def-b83273cb2bc2', 1, false),
('24262de2-9193-47f7-88a4-8d4caa780177', 8947, false),
('219ed713-c5d5-4e84-8aef-2abf9e97b621', 8947, false),
('06348342-9b25-42f0-8bca-be5bf61df191', 10433, false),
('0843b8da-fb46-4684-b331-395f38412a25', 8967, false),
('526be63f-d434-48f8-8a03-1ae49731b1b6', 178, false),
('4abd1dc4-aede-4a00-ad62-e73bfe2958cb', 8981, false),
('9ee76f1a-4b3a-42b7-aa1b-b556d105cd1b', 8995, false),
('a9ed0fa2-b576-4fac-afc5-15bf961e6c24', 8996, false),
('0e034546-e7fb-4261-b132-263a7b7e4b72', 8998, false),
('b13ad320-bf63-420f-b4c1-f1b872776aea', 9018, false),
('74e293e4-8af9-44b1-884d-01dc2f83f929', 356, false),
('0d955355-9f9c-4459-8df0-c61169f6e495', 164, false),
('6889d9fd-5660-439c-b7db-30b43e0e4453', 166, false),
('f30e2483-0008-4b1d-a583-814ceb0a050b', 165, false),
('0d35b856-7b0f-4943-8abb-3be44e8f7add', 9036, false),
('9c1f1d1e-9235-461a-a88b-d247d53d92c6', 9036, false),
('7bd52cd0-69b6-4c77-9a3b-3f3408a0a193', 810, false),
('6572ace3-8e34-4ab1-a2df-365e09fa6a34', 1166, false),
('c7d16428-26f2-4716-9f2b-fc58fda4eb03', 11444, false),
('dcfd743a-47d0-462c-b7b0-dacde81611eb', 1590, false),
('fa28a63d-2a7e-4836-96ca-6c25909559b7', 1327, false),
('1cb6fffc-f614-43dd-8f99-941ae733a2e3', 11444, false),
('c08fdb38-7ba2-4d54-8ff6-39b73fbf2a58', 11464, false),
('0af9943d-4239-4c06-b1d2-c12e2b4f6618', 11466, false),
('e928b908-cc1a-45bc-b840-ec8479342652', 8956, false),
('a01b51e9-1da8-48e7-88b8-9aef1d59fbf4', 11821, false),
('4ab47c67-b060-4f5a-86d5-1e6323883ee5', 11848, false),
('49668cd8-6e97-474b-9f0d-050907220d0b', 11489, false),
('4a284f20-d2dc-45e6-80c1-01966c7d0c23', 8956, false),
('d49dc348-6fd8-47b7-a64e-94aced00a4c6', 2177, false),
('a5608f67-def5-42f1-b8a0-44996dc4569e', 11491, false),
('d004406b-f9a4-4cb4-ad38-01f75979417d', 11492, false),
('cdcd5ca1-b806-48c9-b6dd-7f995f9f181f', 11507, false),
('a9dfc906-be99-4d33-a67d-c64bb7877187', 9350, false),
('3bb67090-183c-4408-b1ad-72a1cc02eb73', 11496, false),
('dfe58c79-204f-4830-bf1c-8f482439f666', 11494, false),
('b7dd8b83-242a-4bcb-a33f-613078b0dafe', 11073, false),
('0571139e-4d71-4aa7-8b21-973ff3d59282', 11909, false),
('fccfd49e-7663-4422-b3bf-cb8232747bb1', 11533, false),
('f644f0c3-f76f-4086-94b3-ef9693cdbb8e', 11935, false),
('2d907ee8-bb05-4123-9ec0-fb97bc0a08b5', 4544, false),
('d5cd85a9-3f5b-431c-8bff-34a830cec325', 3990, false),
('5c7a0197-7303-4836-b7d4-e7eaef66b52c', 11994, false),
('c23b1487-8fa7-4011-8898-783100776155', 11588, false),
('10c4a9b7-fd2b-42c0-94bc-8f66928540cc', 11598, false),
('75dc1ae3-1246-4c8f-9738-77b84a016ba2', 11598, false),
('cf32f30c-5364-4d07-a16d-7d81f052507b', 4718, false),
('402db6ea-2cb0-41d7-9b48-3e0496f80698', 8968, false),
('eeb3327a-c719-48aa-9409-8ff84004c49f', 218, false),
('f7338908-f90d-4870-aaf9-4ed06cf6d54f', 222, false),
('7cd00d6c-271a-4962-a450-4da1409ea64e', 4930, false),
('d6ae7191-115e-4d3d-8713-dae0bb7b016b', 757, false),
('7e621523-fbb7-49d9-912c-e380c0e453ad', 11076, false),
('0f1e9e18-da0c-4b58-afb1-d3776dc17074', 885, false),
('9109d520-9fe2-4492-9e0c-10957dc3cbcd', 12081, false),
('5f553baf-95ef-4386-af00-7df1128f22ec', 1242, false),
('3979ab41-c591-4029-a3b4-6fd2fd1fe446', 2794, false),
('68031c75-69b8-429a-ae4d-e329de48e5e9', 12092, false),
('74240d4a-0a79-40a7-a6ec-db5132adc065', 12098, false),
('fc87d2e0-b2c2-4234-91d3-b1915c3dbeca', 2822, false),
('db24c532-9046-4e1e-ba0b-e768efa4acee', 1590, false),
('d2d9c810-5411-48c9-aae2-e2e17d180ac9', 12105, false),
('1a90401e-5d02-4740-a32e-5f1269b30451', 12121, false),
('7d0396f5-d68f-4768-943e-347f4eb664b6', 12121, false),
('27b19f7f-23df-4473-a454-90d2ac848303', 12121, false),
('136ed682-49dd-426d-a2bd-a3b9e2e695ca', 12117, false),
('03f57619-ee72-4da0-a62f-f6181ddd96e5', 10784, false),
('e28ed504-3dcb-4033-bfdd-fb3c8f607027', 12117, false),
('cb67f5f9-401e-416c-9557-ac5c30a79c6a', 9564, false),
('d73a03bc-90aa-41e3-bf81-bc9ea0c77c4a', 5297, false),
('c9d1a917-2fc3-48c5-9a4a-3aae0f9df597', 5729, false),
('c41692d5-cc20-4a69-87dd-d6e057e97df1', 6283, false),
('3bdb75aa-e515-4e8d-8c2d-8ecccca07741', 6360, false),
('a61c45c6-e153-4cb9-9c5f-2a73cac1f9fa', 10143, false),
('7d2b8b6c-3bcd-43a0-8f46-50d66d067279', 9911, false),
('81779256-fc4a-4db0-b96d-e4e7f1fb5b50', 12280, false),
('f65cdc2f-cace-4c26-b87d-e823e2f63afe', 7739, false),
('41fdc6b4-af88-46fc-9ffe-a51ae9fe4756', 8585, false),
('bb272c8f-bc27-4cc3-96e8-58e2ab2ee56d', 7917, false),
('a965ed86-7657-483f-a8d5-54eef7e3d22a', 9054, false),
('e298fdbe-5404-4af5-b653-336eda56b56c', 7576, false),
('43a57c8f-e82b-4af5-83e8-564694b182c4', 8919, false),
('846cfd19-6b13-4afc-b970-8825910eec6d', 12354, false),
('7e874611-a254-44ec-89e7-65042f20fc53', 12117, false),
('945d90cb-c5fb-4fe6-ba53-7d0e0436b17e', 6040, false),
('84feb86c-73c0-47fa-9a52-332a232ec4d8', 121, false),
('a0025a5e-b150-4f5e-bf8b-67c73f4ad3c2', 190, false),
('cb675372-fc3d-419a-9e8c-b7d61db3249d', 42, false),
('87dae975-b676-4ea6-b08c-96617fa9067b', 12452, false),
('d4d7969e-ddd1-4aa5-b7a3-e315c812ba59', 12454, false),
('0a315413-2871-41c5-83e6-e12c4525fab2', 12455, false),
('874423b9-d696-4f07-9c38-24084d3276c1', 12457, false),
('07c37f98-63af-4914-bbab-7d9e7bfb99c5', 11576, false),
('64e74fd1-95af-4521-b728-282ca46b5d00', 11070, false),
('018c7e82-34d1-4ee5-acfb-d4bae0ce895e', 11085, false),
('f2d2d460-7542-4496-a351-311a0ef39a7c', 11085, false),
('a3a89bc0-fcf2-4f45-be0e-d02f4b103508', 10784, false),
('5d673779-ed74-4561-8bd3-0d7c58fe2beb', 8910, false),
('c5621b79-c67e-4134-900a-bd629d63ab22', 8924, false),
('5f5a9990-3889-42e1-818e-df9e6b377ae9', 8926, false),
('2ab8657b-5725-45d8-9558-79f6b9272ee1', 8925, false),
('dadbfd7a-e031-43bc-8606-e9a2cbcc1e46', 8934, false),
('39eb2392-4208-4411-92ea-5c9ae2c0b55a', 8940, false),
('e20b27d3-faf1-4a3e-a3a9-5b6d8356473c', 8941, false),
('4806458b-1672-43ab-b988-de1c6e4dd7e2', 8947, false);
Loading

0 comments on commit 4b5ea7e

Please sign in to comment.