-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
La til initiell funksjonalitet for håndtering bekreftelse-ansvar
- Loading branch information
Showing
24 changed files
with
526 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...bekreftelse-tjeneste/src/main/kotlin/no/nav/paw/bekreftelsetjeneste/ansvar/AnsvarSerde.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package no.nav.paw.bekreftelsetjeneste.ansvar | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import org.apache.kafka.common.serialization.Deserializer | ||
import org.apache.kafka.common.serialization.Serde | ||
import org.apache.kafka.common.serialization.Serializer | ||
|
||
private val ansvarObjectMapper = ObjectMapper() | ||
.registerKotlinModule() | ||
.registerModules(JavaTimeModule()) | ||
|
||
class AnsvarSerde: Serde<Ansvar> { | ||
private val ansvarSerializer = AnsvarSerializer() | ||
private val ansvarDeserializer = AnsvarDeserializer() | ||
|
||
override fun serializer(): Serializer<Ansvar> = ansvarSerializer | ||
|
||
override fun deserializer(): Deserializer<Ansvar> = ansvarDeserializer | ||
} | ||
|
||
class AnsvarSerializer: Serializer<Ansvar> { | ||
override fun serialize(topic: String?, data: Ansvar): ByteArray { | ||
return ansvarObjectMapper.writeValueAsBytes(data) | ||
} | ||
} | ||
|
||
class AnsvarDeserializer: Deserializer<Ansvar> { | ||
override fun deserialize(topic: String?, data: ByteArray): Ansvar { | ||
return ansvarObjectMapper.readValue(data) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...lse-tjeneste/src/main/kotlin/no/nav/paw/bekreftelsetjeneste/ansvar/handterAnsvarEndret.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package no.nav.paw.bekreftelsetjeneste.ansvar | ||
|
||
import no.nav.paw.bekreftelse.ansvar.v1.AnsvarEndret | ||
import no.nav.paw.bekreftelse.ansvar.v1.vo.AvslutterAnsvar | ||
import no.nav.paw.bekreftelse.ansvar.v1.vo.TarAnsvar | ||
import no.nav.paw.bekreftelse.internehendelser.AndreHarOvertattAnsvar | ||
import no.nav.paw.bekreftelse.internehendelser.BekreftelseHendelse | ||
import no.nav.paw.bekreftelsetjeneste.tilstand.InternTilstand | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.* | ||
|
||
fun haandterAnsvarEndret( | ||
tilstand: InternTilstand?, | ||
ansvar: Ansvar?, | ||
ansvarEndret: AnsvarEndret | ||
): List<Handling> { | ||
return when (val handling = ansvarEndret.handling) { | ||
is TarAnsvar -> tarAnsvar( | ||
tilstand = tilstand, | ||
ansvar = ansvar, | ||
ansvarEndret = ansvarEndret, | ||
handling = handling | ||
) | ||
is AvslutterAnsvar -> avslutterAnsvar( | ||
ansvar = ansvar, | ||
ansvarEndret = ansvarEndret | ||
) | ||
else -> emptyList() | ||
} | ||
} | ||
|
||
fun avslutterAnsvar( | ||
ansvar: Ansvar?, | ||
ansvarEndret: AnsvarEndret | ||
): List<Handling> { | ||
val oppdatertAnsvar = ansvar - Loesning.from(ansvarEndret.bekreftelsesloesning) | ||
val ansvarsHandling = when { | ||
ansvar != null && oppdatertAnsvar == null -> SlettAnsvar(ansvarEndret.periodeId) | ||
ansvar != null && oppdatertAnsvar != null -> SkrivAnsvar(ansvarEndret.periodeId, oppdatertAnsvar) | ||
else -> null | ||
} | ||
return listOfNotNull(ansvarsHandling) | ||
} | ||
|
||
fun tarAnsvar( | ||
tilstand: InternTilstand?, | ||
ansvar: Ansvar?, | ||
ansvarEndret: AnsvarEndret, | ||
handling: TarAnsvar | ||
): List<Handling> { | ||
val oppdatertAnsvar = | ||
(ansvar ?: ansvar(ansvarEndret.periodeId)) + | ||
Ansvarlig( | ||
loesning = Loesning.from(ansvarEndret.bekreftelsesloesning), | ||
intervall = Duration.ofMillis(handling.intervalMS), | ||
gracePeriode = Duration.ofMillis(handling.graceMS) | ||
) | ||
val hendelse = tilstand?.let { | ||
AndreHarOvertattAnsvar( | ||
hendelseId = UUID.randomUUID(), | ||
periodeId = ansvarEndret.periodeId, | ||
arbeidssoekerId = tilstand.periode.arbeidsoekerId, | ||
hendelseTidspunkt = Instant.now(), | ||
) | ||
} | ||
return listOfNotNull( | ||
if (ansvar != oppdatertAnsvar) SkrivAnsvar(ansvarEndret.periodeId, oppdatertAnsvar) else null, | ||
hendelse?.let(::SendHendelse) | ||
) | ||
} | ||
|
||
|
||
sealed interface Handling | ||
data class SlettAnsvar(val id: UUID) : Handling | ||
data class SkrivAnsvar(val id: UUID, val value: Ansvar) : Handling | ||
data class SendHendelse(val hendelse: BekreftelseHendelse) : Handling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...reftelse-tjeneste/src/main/kotlin/no/nav/paw/bekreftelsetjeneste/topology/AnsvarStroem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package no.nav.paw.bekreftelsetjeneste.topology | ||
|
||
import io.micrometer.core.instrument.Tag | ||
import io.micrometer.core.instrument.Tags | ||
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry | ||
import no.nav.paw.bekreftelse.ansvar.v1.AnsvarEndret | ||
import no.nav.paw.bekreftelse.ansvar.v1.vo.AvslutterAnsvar | ||
import no.nav.paw.bekreftelse.ansvar.v1.vo.TarAnsvar | ||
import no.nav.paw.bekreftelse.internehendelser.BekreftelseHendelse | ||
import no.nav.paw.bekreftelse.internehendelser.BekreftelseHendelseSerde | ||
import no.nav.paw.bekreftelsetjeneste.ansvar.* | ||
import no.nav.paw.bekreftelsetjeneste.config.KafkaTopologyConfig | ||
import no.nav.paw.bekreftelsetjeneste.tilstand.InternTilstand | ||
import no.nav.paw.config.kafka.streams.mapNonNull | ||
import org.apache.kafka.common.serialization.Serdes | ||
import org.apache.kafka.streams.StreamsBuilder | ||
import org.apache.kafka.streams.kstream.Produced | ||
import org.apache.kafka.streams.state.KeyValueStore | ||
import java.util.* | ||
|
||
fun StreamsBuilder.byggAnsvarsStroem( | ||
registry: PrometheusMeterRegistry, | ||
kafkaTopologyConfig: KafkaTopologyConfig, | ||
bekreftelseHendelseSerde: BekreftelseHendelseSerde | ||
) { | ||
stream<Long, AnsvarEndret>(kafkaTopologyConfig.ansvarsTopic) | ||
.peek { _, message -> count(registry, message) } | ||
.mapNonNull( | ||
name = "endre_ansvar", | ||
kafkaTopologyConfig.ansvarStateStoreName, | ||
kafkaTopologyConfig.internStateStoreName | ||
) { message -> | ||
val internStateStore = | ||
getStateStore<KeyValueStore<UUID, InternTilstand>>(kafkaTopologyConfig.internStateStoreName) | ||
val ansvarStateStore = getStateStore<KeyValueStore<UUID, Ansvar>>(kafkaTopologyConfig.ansvarStateStoreName) | ||
val internTilstand = internStateStore[message.periodeId] | ||
val ansvar = ansvarStateStore[message.periodeId] | ||
haandterAnsvarEndret( | ||
tilstand = internTilstand, | ||
ansvar = ansvar, | ||
ansvarEndret = message | ||
).map { handling -> | ||
when (handling) { | ||
is SendHendelse -> handling.hendelse | ||
is SkrivAnsvar -> ansvarStateStore.put(handling.id, handling.value) | ||
is SlettAnsvar -> ansvarStateStore.delete(handling.id) | ||
} | ||
}.filterIsInstance<BekreftelseHendelse>() | ||
} | ||
.flatMapValues { _, value -> value } | ||
.to( | ||
kafkaTopologyConfig.bekreftelseHendelseloggTopic, | ||
Produced.with(Serdes.Long(), bekreftelseHendelseSerde) | ||
) | ||
} | ||
|
||
fun count( | ||
registry: PrometheusMeterRegistry, | ||
message: AnsvarEndret | ||
) { | ||
val action = when (message.handling) { | ||
is TarAnsvar -> "tar_ansvar" | ||
is AvslutterAnsvar -> "avslutter_ansvar" | ||
else -> "ukjent" | ||
} | ||
val bekreftelsesloesning = message.bekreftelsesloesning | ||
registry.counter( | ||
"paw_bekreftelse_ansvar_endret", | ||
Tags.of( | ||
Tag.of("bekreftelsesloesing", bekreftelsesloesning.name), | ||
Tag.of("handling", action) | ||
) | ||
) | ||
} |
Oops, something went wrong.