Skip to content

Commit

Permalink
IS-2831: Skip creating oppgave when all sykmelding fields are irrelevant
Browse files Browse the repository at this point in the history
  • Loading branch information
andersrognstad committed Nov 29, 2024
1 parent f3ddee0 commit 6588fd1
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/syfo/KafkaModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.nav.syfo

import no.nav.syfo.behandler.kafka.sykmelding.launchKafkaTaskSykmelding
import no.nav.syfo.sykmelding.launchKafkaTaskSykmelding
import no.nav.syfo.behandlerdialog.AvvistMeldingService
import no.nav.syfo.behandlerdialog.MeldingFraBehandlerService
import no.nav.syfo.behandlerdialog.UbesvartMeldingService
Expand Down
62 changes: 31 additions & 31 deletions src/main/kotlin/no/nav/syfo/sykmelding/KafkaSykmeldingConsumer.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.syfo.behandler.kafka.sykmelding
package no.nav.syfo.sykmelding

import no.nav.syfo.ApplicationState
import no.nav.syfo.Environment
Expand All @@ -11,7 +11,6 @@ import no.nav.syfo.kafka.launchKafkaTask
import no.nav.syfo.personoppgave.domain.PersonOppgave
import no.nav.syfo.personoppgave.domain.PersonOppgaveType
import no.nav.syfo.personoppgave.getPersonOppgaverByReferanseUuid
import no.nav.syfo.sykmelding.*
import no.nav.syfo.util.configuredJacksonMapper
import org.apache.kafka.clients.consumer.*
import org.apache.kafka.common.serialization.Deserializer
Expand Down Expand Up @@ -56,55 +55,56 @@ class KafkaSykmeldingConsumer(
) {
val records = kafkaConsumer.poll(Duration.ofMillis(1000))
if (records.count() > 0) {
processSykmelding(
processSykmeldingRecords(
database = database,
consumerRecords = records,
)
kafkaConsumer.commitSync()
}
}

private fun processSykmelding(
private fun processSykmeldingRecords(
database: DatabaseInterface,
consumerRecords: ConsumerRecords<String, ReceivedSykmeldingDTO>,
) {
database.connection.use { connection ->
consumerRecords.forEach {
it.value()?.let { receivedSykmeldingDTO ->
COUNT_MOTTATT_SYKMELDING.increment()
val sykmelding = receivedSykmeldingDTO.sykmelding
if (!sykmelding.meldingTilNAV?.beskrivBistand.isNullOrEmpty() ||
!sykmelding.tiltakNAV.isNullOrEmpty() ||
!sykmelding.andreTiltak.isNullOrEmpty()
) {
createPersonoppgave(
connection = connection,
receivedSykmeldingDTO = receivedSykmeldingDTO,
)
countIrrelevantSykmeldingFelter(sykmelding)
}
consumerRecords.forEach { sykmeldingRecord ->
sykmeldingRecord.value()?.let { receivedSykmeldingDTO ->
processSykmelding(receivedSykmeldingDTO, connection)
}
}
connection.commit()
}
}

private fun countIrrelevantSykmeldingFelter(sykmelding: Sykmelding) {
val beskrivBistandNav = sykmelding.meldingTilNAV?.beskrivBistand
val tiltakNav = sykmelding.tiltakNAV
val andreTiltak = sykmelding.andreTiltak
if (!beskrivBistandNav.isNullOrEmpty() && isIrrelevant(beskrivBistandNav)) {
COUNT_MOTTATT_SYKMELDING_BESKRIV_BISTAND_NAV_IRRELEVANT.increment()
}
if (!tiltakNav.isNullOrEmpty() && isIrrelevant(tiltakNav)) {
COUNT_MOTTATT_SYKMELDING_TILTAK_NAV_IRRELEVANT.increment()
}
if (!andreTiltak.isNullOrEmpty() && isIrrelevant(andreTiltak)) {
COUNT_MOTTATT_SYKMELDING_ANDRE_TILTAK_IRRELEVANT.increment()
private fun processSykmelding(
receivedSykmeldingDTO: ReceivedSykmeldingDTO,
connection: Connection
) {
COUNT_MOTTATT_SYKMELDING.increment()
val sykmelding = receivedSykmeldingDTO.sykmelding
val relevantFields = listOfNotNull(
sykmelding.meldingTilNAV?.beskrivBistand,
sykmelding.tiltakNAV,
sykmelding.andreTiltak,
).filter { it.isNotEmpty() }

if (relevantFields.isNotEmpty()) {
if (relevantFields.all { hasIrrelevantContent(it) }) {
COUNT_MOTTATT_SYKMELDING_SKIPPED_IRRELEVANT_TEXT.increment()
} else {
if (relevantFields.all { it.length < 10 }) {
COUNT_MOTTATT_SYKMELDING_SHORT_TEXT.increment()
}
createPersonoppgave(
connection = connection,
receivedSykmeldingDTO = receivedSykmeldingDTO,
)
}
}
}

private fun isIrrelevant(content: String): Boolean =
private fun hasIrrelevantContent(content: String): Boolean =
irrelevantSykmeldingFelterContent.contains(content.lowercase().trim())

private fun createPersonoppgave(
Expand Down
22 changes: 8 additions & 14 deletions src/main/kotlin/no/nav/syfo/sykmelding/SykmeldingMetrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import no.nav.syfo.metric.*

const val MOTTATT_SYKMELDING = "${METRICS_NS}_mottatt_sykmelding_count"
const val MOTTATT_SYKMELDING_CREATED_PERSONOPPGAVE = "${METRICS_NS}_mottatt_sykmelding_personoppgave_count"
const val MOTTATT_SYKMELDING_BESKRIV_BISTAND_NAV_IRRELEVANT = "${METRICS_NS}_mottatt_sykmelding_beskriv_bistand_nav_irrelevant_count"
const val MOTTATT_SYKMELDING_TILTAK_NAV_IRRELEVANT = "${METRICS_NS}_mottatt_sykmelding_tiltak_nav_irrelevant_count"
const val MOTTATT_SYKMELDING_ANDRE_TILTAK_IRRELEVANT = "${METRICS_NS}_mottatt_sykmelding_andre_tiltak_irrelevant_count"
const val MOTTATT_SYKMELDING_SKIPPED_IRRELEVANT_TEXT = "${METRICS_NS}_mottatt_sykmelding_skipped_personoppgave_irrelevant_count"
const val MOTTATT_SYKMELDING_SHORT_TEXT = "${METRICS_NS}_mottatt_sykmelding_personoppgave_short_text_count"

val COUNT_MOTTATT_SYKMELDING: Counter = Counter
.builder(MOTTATT_SYKMELDING)
Expand All @@ -19,17 +18,12 @@ val COUNT_MOTTATT_SYKMELDING_SUCCESS: Counter = Counter
.description("Counts the number of received sykmelding that created personoppgave")
.register(METRICS_REGISTRY)

val COUNT_MOTTATT_SYKMELDING_BESKRIV_BISTAND_NAV_IRRELEVANT: Counter = Counter
.builder(MOTTATT_SYKMELDING_BESKRIV_BISTAND_NAV_IRRELEVANT)
.description("Counts the number of received sykmelding with beskriv bistand Nav that is irrelevant for oppgave")
val COUNT_MOTTATT_SYKMELDING_SKIPPED_IRRELEVANT_TEXT: Counter = Counter
.builder(MOTTATT_SYKMELDING_SKIPPED_IRRELEVANT_TEXT)
.description("Counts the number of received sykmelding that skipped creating personoppgave due to irrelevant text")
.register(METRICS_REGISTRY)

val COUNT_MOTTATT_SYKMELDING_TILTAK_NAV_IRRELEVANT: Counter = Counter
.builder(MOTTATT_SYKMELDING_TILTAK_NAV_IRRELEVANT)
.description("Counts the number of received sykmelding with tiltak Nav that is irrelevant for oppgave")
.register(METRICS_REGISTRY)

val COUNT_MOTTATT_SYKMELDING_ANDRE_TILTAK_IRRELEVANT: Counter = Counter
.builder(MOTTATT_SYKMELDING_ANDRE_TILTAK_IRRELEVANT)
.description("Counts the number of received sykmelding with andre tiltak that is irrelevant for oppgave")
val COUNT_MOTTATT_SYKMELDING_SHORT_TEXT: Counter = Counter
.builder(MOTTATT_SYKMELDING_SHORT_TEXT)
.description("Counts the number of received sykmelding that created personoppgave with short text")
.register(METRICS_REGISTRY)
181 changes: 179 additions & 2 deletions src/test/kotlin/no/nav/syfo/sykmelding/SykmeldingConsumerSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import no.nav.syfo.behandler.kafka.sykmelding.KafkaSykmeldingConsumer
import no.nav.syfo.behandler.kafka.sykmelding.SYKMELDING_TOPIC
import no.nav.syfo.domain.PersonIdent
import no.nav.syfo.database.PersonOppgaveRepository
import no.nav.syfo.personoppgave.domain.PersonOppgaveType
Expand Down Expand Up @@ -130,6 +128,185 @@ class SykmeldingConsumerSpek : Spek({
personOppgave.behandletTidspunkt shouldBe null
personOppgave.referanseUuid shouldBeEqualTo sykmeldingId
}
it("creates one oppgave if more than one relevant field has text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = null,
andreTiltak = "Jeg synes NAV skal gjøre dette",
tiltakNAV = "Jeg synes NAV skal gjøre dette også",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

val personOppgaver = database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).map { it.toPersonOppgave() }
personOppgaver.size shouldBeEqualTo 1
personOppgaver.first().type shouldBeEqualTo PersonOppgaveType.BEHANDLER_BER_OM_BISTAND
}
it("Creates oppgave if andreTiltak has relevant text and tiltakNAV has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = null,
andreTiltak = "Jeg synes NAV skal gjøre dette",
tiltakNAV = "-",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

val personOppgaver = database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).map { it.toPersonOppgave() }
personOppgaver.size shouldBeEqualTo 1
personOppgaver.first().type shouldBeEqualTo PersonOppgaveType.BEHANDLER_BER_OM_BISTAND
}
it("Creates oppgave if meldingTilNAV has relevant text and andreTiltak has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = MeldingTilNAV(
bistandUmiddelbart = false,
beskrivBistand = "Sjekk ut saken",
),
andreTiltak = ".",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

val personOppgaver = database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).map { it.toPersonOppgave() }
personOppgaver.size shouldBeEqualTo 1
personOppgaver.first().type shouldBeEqualTo PersonOppgaveType.BEHANDLER_BER_OM_BISTAND
}
it("Creates oppgave if tiltakNAV has relevant text and meldingTilNAV has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = MeldingTilNAV(
bistandUmiddelbart = false,
beskrivBistand = "nei",
),
tiltakNAV = "Jeg synes NAV skal gjøre dette",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

val personOppgaver = database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).map { it.toPersonOppgave() }
personOppgaver.size shouldBeEqualTo 1
personOppgaver.first().type shouldBeEqualTo PersonOppgaveType.BEHANDLER_BER_OM_BISTAND
}
it("Does not create oppgave if meldingTilNAV has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = MeldingTilNAV(
bistandUmiddelbart = false,
beskrivBistand = ".",
),
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).shouldBeEmpty()
}
it("Does not create oppgave if tiltakNAV has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = null,
tiltakNAV = "-",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).shouldBeEmpty()
}
it("Does not create oppgave if andreTiltak has irrelevant text") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
sykmeldingId = sykmeldingId,
meldingTilNAV = null,
tiltakNAV = "nei",
)
kafkaConsumer.mockPollConsumerRecords(
recordValue = sykmelding,
topic = topic,
)

kafkaSykmeldingConsumer.pollAndProcessRecords(
kafkaConsumer = kafkaConsumer,
)
verify(exactly = 1) {
kafkaConsumer.commitSync()
}

database.getPersonOppgaver(
personIdent = PersonIdent(sykmelding.personNrPasient),
).shouldBeEmpty()
}
it("Does not create oppgave if meldingTilNAV, tiltakNAV, and andreTiltak is null") {
val sykmeldingId = UUID.randomUUID()
val sykmelding = generateKafkaSykmelding(
Expand Down

0 comments on commit 6588fd1

Please sign in to comment.