-
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 basis funksjoner for id håndtering
- Loading branch information
Showing
15 changed files
with
325 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
apps/kafka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/PdlConsumer.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,50 @@ | ||
package no.nav.paw.kafkakeymaintenance | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import no.nav.paw.kafkakeygenerator.client.KafkaKeysClient | ||
import no.nav.paw.kafkakeygenerator.client.LokaleAlias | ||
import no.nav.paw.kafkakeymaintenance.functions.genererIdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.functions.harAvvik | ||
import no.nav.paw.kafkakeymaintenance.functions.hentData | ||
import no.nav.paw.kafkakeymaintenance.functions.hentPerioder | ||
import no.nav.paw.kafkakeymaintenance.kafka.Topic | ||
import no.nav.paw.kafkakeymaintenance.kafka.TransactionContext | ||
import no.nav.paw.kafkakeymaintenance.kafka.updateHwm | ||
import no.nav.paw.kafkakeymaintenance.vo.IdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.vo.avviksMelding | ||
import no.nav.person.pdl.aktor.v2.Aktor | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.jetbrains.exposed.sql.Transaction | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import java.time.Instant | ||
|
||
const val ANTALL_PARTISJONER = 6 | ||
|
||
fun KafkaKeysClient.hentAlias(identiteter: List<String>): List<LokaleAlias> = runBlocking { | ||
getAlias(ANTALL_PARTISJONER, identiteter).alias | ||
} | ||
|
||
fun process( | ||
txContextFactory: Transaction.() -> TransactionContext, | ||
hentAlias: (List<String>) -> List<LokaleAlias>, | ||
record: ConsumerRecord<String, Aktor> | ||
): IdOppdatering? = | ||
transaction { | ||
val txContext = txContextFactory() | ||
val valid = txContext.updateHwm( | ||
topic = Topic(record.topic()), | ||
partition = record.partition(), | ||
offset = record.offset(), | ||
time = Instant.ofEpochMilli(record.timestamp()), | ||
lastUpdated = Instant.now() | ||
) | ||
if (valid) { | ||
hentData(hentAlias, record) | ||
.takeIf(::harAvvik) | ||
?.let(::avviksMelding) | ||
?.let(txContext::hentPerioder) | ||
?.let(::genererIdOppdatering) | ||
} else { | ||
null | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...rc/main/kotlin/no/nav/paw/kafkakeymaintenance/functions/GenererAutomatiskIdOppdatering.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,48 @@ | ||
package no.nav.paw.kafkakeymaintenance.functions | ||
|
||
import no.nav.paw.kafkakeymaintenance.vo.AutomatiskIdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.vo.IdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.perioder.PeriodeRad | ||
import no.nav.paw.kafkakeymaintenance.vo.AvviksMelding | ||
import no.nav.paw.kafkakeymaintenance.vo.IdMap | ||
|
||
fun genererAutomatiskIdOppdatering(avvik: AvviksMelding, periodeRad: PeriodeRad): IdOppdatering { | ||
requireNotNull(avvik.lokaleAlias.find { it.identitetsnummer == periodeRad.identitetsnummer }) { | ||
"Intern logiskfeil, lokal data for identietetsnummer mangler" | ||
}.let { alias -> | ||
return AutomatiskIdOppdatering( | ||
oppdatertData = IdMap( | ||
gjeldeneIdentitetsnummer = avvik.gjeldeneIdentitetsnummer, | ||
arbeidsoekerId = alias.arbeidsoekerId, | ||
recordKey = alias.recordKey, | ||
partisjon = alias.partition, | ||
identiteter = avvik.lokaleAliasSomSkalPekePaaPdlPerson() | ||
), | ||
frieIdentiteter = avvik.lokaleAliasSomIkkeSkalPekePaaPdlPerson() | ||
) | ||
} | ||
|
||
} | ||
|
||
fun genererAutomatiskIdOppdatering(avvik: AvviksMelding): IdOppdatering { | ||
val frieIdentiteter = avvik.lokaleAliasSomIkkeSkalPekePaaPdlPerson() | ||
val identerSomSkalPekePaaPdlPerson = avvik.lokaleAliasSomSkalPekePaaPdlPerson() | ||
return if (identerSomSkalPekePaaPdlPerson.isEmpty()) { | ||
AutomatiskIdOppdatering( | ||
oppdatertData = null, | ||
frieIdentiteter = frieIdentiteter | ||
) | ||
} else { | ||
val data = identerSomSkalPekePaaPdlPerson.maxBy { it.arbeidsoekerId } | ||
AutomatiskIdOppdatering( | ||
oppdatertData = IdMap( | ||
gjeldeneIdentitetsnummer = avvik.gjeldeneIdentitetsnummer, | ||
arbeidsoekerId = data.arbeidsoekerId, | ||
recordKey = data.recordKey, | ||
partisjon = data.partition, | ||
identiteter = identerSomSkalPekePaaPdlPerson | ||
), | ||
frieIdentiteter = frieIdentiteter | ||
) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ntenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/functions/GenererIdOppdatering.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,35 @@ | ||
package no.nav.paw.kafkakeymaintenance.functions | ||
|
||
import arrow.core.NonEmptyList | ||
import arrow.core.nonEmptyListOf | ||
import arrow.core.tail | ||
import no.nav.paw.kafkakeygenerator.client.Alias | ||
import no.nav.paw.kafkakeymaintenance.vo.IdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.vo.ManuellIdOppdatering | ||
import no.nav.paw.kafkakeymaintenance.perioder.PeriodeRad | ||
import no.nav.paw.kafkakeymaintenance.vo.AvviksMelding | ||
import no.nav.paw.kafkakeymaintenance.vo.AvvvikOgPerioder | ||
|
||
fun genererIdOppdatering(avvikOgPerioder: AvvvikOgPerioder): IdOppdatering { | ||
val (avvik, perioder) = avvikOgPerioder | ||
val periode = perioder.firstOrNull() | ||
return if (periode == null) { | ||
genererAutomatiskIdOppdatering(avvik) | ||
} else { | ||
genererIdOppdatering(avvik, nonEmptyListOf(periode, *perioder.tail().toTypedArray())) | ||
} | ||
} | ||
|
||
fun genererIdOppdatering(avvik: AvviksMelding, perioder: NonEmptyList<PeriodeRad>): IdOppdatering { | ||
val aktivPerioder = perioder.filter(PeriodeRad::erAktiv) | ||
return when (aktivPerioder.size) { | ||
0 -> genererAutomatiskIdOppdatering(avvik, perioder.maxBy(PeriodeRad::fra)) | ||
1 -> genererAutomatiskIdOppdatering(avvik, aktivPerioder.first()) | ||
else -> ManuellIdOppdatering( | ||
gjeldeneIdentitetsnummer = avvik.gjeldeneIdentitetsnummer, | ||
pdlIdentitetsnummer = avvik.lokaleAliasSomSkalPekePaaPdlPerson().map(Alias::identitetsnummer), | ||
lokaleAlias = avvik.lokaleAlias, | ||
perioder = perioder | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...afka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/functions/HarAvvik.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,9 @@ | ||
package no.nav.paw.kafkakeymaintenance.functions | ||
|
||
import no.nav.paw.kafkakeymaintenance.vo.Data | ||
|
||
fun harAvvik(data: Data): Boolean = | ||
data.alias | ||
.flatMap { it.kobliner } | ||
.map { it.arbeidsoekerId } | ||
.distinct().size > 1 |
17 changes: 17 additions & 0 deletions
17
...afka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/functions/HentData.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,17 @@ | ||
package no.nav.paw.kafkakeymaintenance.functions | ||
|
||
import no.nav.paw.kafkakeygenerator.client.LokaleAlias | ||
import no.nav.paw.kafkakeymaintenance.vo.Data | ||
import no.nav.person.pdl.aktor.v2.Aktor | ||
import no.nav.person.pdl.aktor.v2.Type | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
|
||
fun hentData( | ||
hentAlias: (List<String>) -> List<LokaleAlias>, | ||
record: ConsumerRecord<String, Aktor>, | ||
): Data = | ||
record.value().identifikatorer | ||
.filter { it.type == Type.FOLKEREGISTERIDENT } | ||
.map { it.idnummer } | ||
.let(hentAlias) | ||
.let { Data(record, it) } |
15 changes: 15 additions & 0 deletions
15
...-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/functions/HentPerioder.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,15 @@ | ||
package no.nav.paw.kafkakeymaintenance.functions | ||
|
||
import no.nav.paw.kafkakeymaintenance.kafka.TransactionContext | ||
import no.nav.paw.kafkakeymaintenance.perioder.periodeRad | ||
import no.nav.paw.kafkakeymaintenance.vo.AvviksMelding | ||
import no.nav.paw.kafkakeymaintenance.vo.AvvvikOgPerioder | ||
|
||
fun TransactionContext.hentPerioder(avviksMelding: AvviksMelding): AvvvikOgPerioder { | ||
val perioder = avviksMelding | ||
.pdlIdentitetsnummer | ||
.plus(avviksMelding.lokaleAlias.map { it.identitetsnummer }) | ||
.distinct() | ||
.mapNotNull(::periodeRad) | ||
return AvvvikOgPerioder(avviksMelding, perioder) | ||
} |
17 changes: 17 additions & 0 deletions
17
...fka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/perioder/PeriodeRad.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,17 @@ | ||
package no.nav.paw.kafkakeymaintenance.perioder | ||
|
||
import no.nav.paw.arbeidssokerregisteret.api.v1.Periode | ||
import java.time.Instant | ||
import java.util.* | ||
|
||
data class PeriodeRad( | ||
val periodeId: UUID, | ||
val identitetsnummer: String, | ||
val fra: Instant, | ||
val til: Instant? | ||
) { | ||
val erAktiv: Boolean = til == null | ||
} | ||
|
||
fun periodeRad(periodeId: UUID, identitetsnummer: String, fra: Instant, til: Instant) = PeriodeRad(periodeId, identitetsnummer, fra, til) | ||
fun periodeRad(periode: Periode) = PeriodeRad(periode.id, periode.identitetsnummer, periode.startet.tidspunkt, periode.avsluttet?.tidspunkt) |
45 changes: 45 additions & 0 deletions
45
...-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/perioder/PerioderFunctions.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,45 @@ | ||
package no.nav.paw.kafkakeymaintenance.perioder | ||
|
||
import no.nav.paw.kafkakeymaintenance.kafka.TransactionContext | ||
import org.jetbrains.exposed.sql.and | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.update | ||
|
||
fun TransactionContext.insertOrUpdate(periode: PeriodeRad) { | ||
periodeRad(periode.identitetsnummer) | ||
.let { lagretDate -> | ||
if (lagretDate == null) { | ||
PerioderTable.insert { | ||
it[version] = appContext.consumerVersion | ||
it[periodeId] = periode.periodeId | ||
it[identitetsnummer] = periode.identitetsnummer | ||
it[fra] = periode.fra | ||
it[til] = periode.til | ||
} | ||
} else { | ||
PerioderTable.update( | ||
where = { (PerioderTable.version eq appContext.consumerVersion) and (PerioderTable.periodeId eq periode.periodeId) } | ||
) { | ||
it[identitetsnummer] = periode.identitetsnummer | ||
it[fra] = periode.fra | ||
it[til] = periode.til | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun TransactionContext.periodeRad(identitetsnummer: String): PeriodeRad? = | ||
PerioderTable | ||
.selectAll() | ||
.where { | ||
(PerioderTable.version eq appContext.consumerVersion) and | ||
(PerioderTable.identitetsnummer eq identitetsnummer) | ||
}.firstOrNull()?.let { | ||
PeriodeRad( | ||
periodeId = it[PerioderTable.periodeId], | ||
identitetsnummer = it[PerioderTable.identitetsnummer], | ||
fra = it[PerioderTable.fra], | ||
til = it[PerioderTable.til] | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
...-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/perioder/PerioderTable.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,14 @@ | ||
package no.nav.paw.kafkakeymaintenance.perioder | ||
|
||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.javatime.timestamp | ||
|
||
object PerioderTable: Table("perioder") { | ||
val version = integer("version") | ||
val periodeId = uuid("periode_id") | ||
val identitetsnummer = varchar("identitetsnummer", 11) | ||
val fra = timestamp("fra") | ||
val til = timestamp("til").nullable() | ||
|
||
override val primaryKey = PrimaryKey(version, periodeId) | ||
} |
28 changes: 28 additions & 0 deletions
28
.../kafka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/vo/AvviksMelding.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,28 @@ | ||
package no.nav.paw.kafkakeymaintenance.vo | ||
|
||
import no.nav.paw.kafkakeygenerator.client.Alias | ||
import no.nav.person.pdl.aktor.v2.Type | ||
|
||
fun avviksMelding(data: Data): AvviksMelding { | ||
val fregIder = data.record.value() | ||
.identifikatorer | ||
.filter { it.type == Type.FOLKEREGISTERIDENT } | ||
return AvviksMelding( | ||
gjeldeneIdentitetsnummer = fregIder | ||
.filter { it.gjeldende } | ||
.map { it.idnummer } | ||
.firstOrNull(), | ||
pdlIdentitetsnummer = fregIder.map { it.idnummer }, | ||
lokaleAlias = data.alias.flatMap { it.kobliner } | ||
) | ||
} | ||
|
||
data class AvviksMelding( | ||
val gjeldeneIdentitetsnummer: String?, | ||
val pdlIdentitetsnummer: List<String>, | ||
val lokaleAlias: List<Alias> | ||
) { | ||
fun lokaleAliasSomSkalPekePaaPdlPerson() = lokaleAlias.filter { it.identitetsnummer in pdlIdentitetsnummer } | ||
|
||
fun lokaleAliasSomIkkeSkalPekePaaPdlPerson() = lokaleAlias.filter { it.identitetsnummer !in pdlIdentitetsnummer } | ||
} |
8 changes: 8 additions & 0 deletions
8
...fka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/vo/AvvvikOgPerioder.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,8 @@ | ||
package no.nav.paw.kafkakeymaintenance.vo | ||
|
||
import no.nav.paw.kafkakeymaintenance.perioder.PeriodeRad | ||
|
||
data class AvvvikOgPerioder( | ||
val avviksMelding: AvviksMelding, | ||
val perioder: List<PeriodeRad> | ||
) |
10 changes: 10 additions & 0 deletions
10
apps/kafka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/vo/Data.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,10 @@ | ||
package no.nav.paw.kafkakeymaintenance.vo | ||
|
||
import no.nav.paw.kafkakeygenerator.client.LokaleAlias | ||
import no.nav.person.pdl.aktor.v2.Aktor | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
|
||
data class Data( | ||
val record: ConsumerRecord<String, Aktor>, | ||
val alias: List<LokaleAlias> | ||
) |
11 changes: 11 additions & 0 deletions
11
apps/kafka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/vo/IdMap.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,11 @@ | ||
package no.nav.paw.kafkakeymaintenance.vo | ||
|
||
import no.nav.paw.kafkakeygenerator.client.Alias | ||
|
||
data class IdMap( | ||
val gjeldeneIdentitetsnummer: String?, | ||
val arbeidsoekerId: Long, | ||
val recordKey: Long, | ||
val partisjon: Int, | ||
val identiteter: List<Alias> | ||
) |
17 changes: 17 additions & 0 deletions
17
.../kafka-key-maintenance/src/main/kotlin/no/nav/paw/kafkakeymaintenance/vo/IdOppdatering.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,17 @@ | ||
package no.nav.paw.kafkakeymaintenance.vo | ||
|
||
import no.nav.paw.kafkakeygenerator.client.Alias | ||
import no.nav.paw.kafkakeymaintenance.perioder.PeriodeRad | ||
|
||
interface IdOppdatering {} | ||
data class ManuellIdOppdatering( | ||
val gjeldeneIdentitetsnummer: String?, | ||
val pdlIdentitetsnummer: List<String>, | ||
val lokaleAlias: List<Alias>, | ||
val perioder: List<PeriodeRad> | ||
) : IdOppdatering | ||
|
||
data class AutomatiskIdOppdatering( | ||
val oppdatertData: IdMap?, | ||
val frieIdentiteter: List<Alias> | ||
) : IdOppdatering |