Skip to content

Commit

Permalink
feat: use highest version of concept in elastic
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Nov 12, 2024
1 parent f99bc8a commit 145fb0a
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 32 deletions.
18 changes: 4 additions & 14 deletions src/main/kotlin/no/fdk/concept_catalog/elastic/ElasticUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,15 @@ class ElasticUpdater(
} catch (_: Exception) { }

conceptRepository.findAll<BegrepDBO>()
.groupBy { concept -> concept.originaltBegrep }
.mapNotNull { pair -> pair.value.maxByOrNull { concept -> concept.versjonsnr } }
.forEach {
if (it.shouldBeCurrent(currentConceptRepository.findByIdOrNull(it.originaltBegrep))) {
logger.debug("reindexing ${it.id}, ${it.ansvarligVirksomhet.id}")
currentConceptRepository.save(CurrentConcept(it))
} else {
logger.debug("skipping not current ${it.id}, ${it.ansvarligVirksomhet.id}")
}
logger.debug("reindexing ${it.id}, ${it.ansvarligVirksomhet.id}")
currentConceptRepository.save(CurrentConcept(it))
}

logger.info("finished reindexing elastic")
}
}

}

fun BegrepDBO.shouldBeCurrent(current: CurrentConcept?): Boolean =
when {
current == null -> true
erPublisert && !current.erPublisert -> true
!erPublisert && current.erPublisert -> false
else -> versjonsnr > current.versjonsnr
}
1 change: 1 addition & 0 deletions src/main/kotlin/no/fdk/concept_catalog/model/Begrep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ data class Begrep (
val originaltBegrep: String? = null,
val versjonsnr: SemVer? = null,
val erSistPublisert: Boolean = false,
val sistPublisert: String? = null,
val revisjonAvSistPublisert: Boolean = false,
val revisjonAv: String? = null,
val status: Status? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.fdk.concept_catalog.service

import no.fdk.concept_catalog.elastic.CurrentConceptRepository
import no.fdk.concept_catalog.model.*
import java.util.UUID
import no.fdk.concept_catalog.repository.ChangeRequestRepository
Expand All @@ -21,7 +20,6 @@ private val logger = LoggerFactory.getLogger(ChangeRequestService::class.java)
class ChangeRequestService(
private val changeRequestRepository: ChangeRequestRepository,
private val conceptRepository: ConceptRepository,
private val currentConceptRepository: CurrentConceptRepository,
private val conceptService: ConceptService,
private val mapper: ObjectMapper
) {
Expand Down Expand Up @@ -91,7 +89,6 @@ class ChangeRequestService(
val conceptToUpdate = when {
dbConcept == null -> createNewConcept(Virksomhet(id=catalogId), user)
.updateLastChangedAndByWhom(user)
.also { currentConceptRepository.save(CurrentConcept(it)) }
.let { conceptRepository.save(it) }
dbConcept.erPublisert -> dbConcept.createNewRevision()
.updateLastChangedAndByWhom(user)
Expand All @@ -105,9 +102,6 @@ class ChangeRequestService(
logger.error("update of concept failed when accepting ${changeRequest.id}, reverting acceptation", ex)
changeRequest.copy(status = ChangeRequestStatus.OPEN).run { changeRequestRepository.save(this) }
if (conceptToUpdate.id != dbConcept?.id) {
if (conceptToUpdate.id == conceptToUpdate.originaltBegrep) {
currentConceptRepository.delete(CurrentConcept(conceptToUpdate))
}
conceptRepository.delete(conceptToUpdate)
}
throw ex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fun BegrepDBO.toDTO(highestPublishedVersion: SemVer?, highestPublishedId: String
originaltBegrep,
versjonsnr,
erSistPublisert = isHighestPublishedVersion(highestPublishedVersion),
sistPublisert = highestPublishedId,
revisjonAvSistPublisert = isRevisionOfHighestPublishedVersion(highestPublishedId),
revisjonAv,
status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class ConceptService(
val locations = conceptsAndOperations.map { historyService.updateHistory(it.key, it.value, user, jwt) }
try {
conceptsAndOperations.keys
.filter { it.id == it.originaltBegrep }
.map { CurrentConcept(it) }
.run { currentConceptRepository.saveAll(this) }
return conceptRepository.saveAll(conceptsAndOperations.keys).map { it.withHighestVersionDTO() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class ChangeRequests : ApiTestContext() {
versjonsnr = SemVer(1, 0, 2),
erPublisert = false,
erSistPublisert = false,
sistPublisert = BEGREP_0.id,
publiseringsTidspunkt = null,
revisjonAv = BEGREP_0.id,
revisjonAvSistPublisert = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class PublishConcept : ApiTestContext() {
assertEquals(BEGREP_TO_BE_UPDATED.copy(
erPublisert = true,
erSistPublisert = true,
sistPublisert = "id-to-be-updated",
publiseringsTidspunkt = result.publiseringsTidspunkt
), result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,6 @@ class SearchConcepts : ApiTestContext() {
assertEquals(HttpStatus.OK.value(), rsp["status"])

val result: Paginated = mapper.readValue(rsp["body"] as String)
assertEquals(listOf(BEGREP_HAS_MULTIPLE_REVISIONS), result.hits)
assertEquals(listOf(BEGREP_UNPUBLISHED_REVISION_MULTIPLE_SECOND), result.hits)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class LastPublished {
@Test
fun `Sets revision of last published for relevant concepts`() {
val newPublished = BEGREP_3.copy(id = "id3-1", versjonsnr = SemVer(1, 9, 1), revisjonAv = "id3")
val invalid = BEGREP_3.copy(id = "id3-2", versjonsnr = SemVer(2, 10, 0), revisjonAv = "id3", status = Status.GODKJENT, erPublisert = false, revisjonAvSistPublisert = false)
val ok = BEGREP_3.copy(id = "id3-3", versjonsnr = SemVer(2, 10, 0), revisjonAv = "id3-1", status = Status.UTKAST, erPublisert = false, revisjonAvSistPublisert = true)
val invalid = BEGREP_3.copy(id = "id3-2", sistPublisert = "id3-1", versjonsnr = SemVer(2, 10, 0), revisjonAv = "id3", status = Status.GODKJENT, erPublisert = false, revisjonAvSistPublisert = false)
val ok = BEGREP_3.copy(id = "id3-3", sistPublisert = "id3-1", versjonsnr = SemVer(2, 10, 0), revisjonAv = "id3-1", status = Status.UTKAST, erPublisert = false, revisjonAvSistPublisert = true)

whenever(conceptRepository.findById("id3-2"))
.thenReturn(Optional.of(invalid.toDBO()))
Expand Down
11 changes: 4 additions & 7 deletions src/test/kotlin/no/fdk/concept_catalog/utils/ResetElastic.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package no.fdk.concept_catalog.utils

import no.fdk.concept_catalog.elastic.CurrentConceptRepository
import no.fdk.concept_catalog.elastic.shouldBeCurrent
import no.fdk.concept_catalog.model.BegrepDBO
import no.fdk.concept_catalog.model.CurrentConcept
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.findAll
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component

@Component
Expand All @@ -21,10 +19,9 @@ class ResetElastic {
fun elasticReindex() {
val concepts = conceptRepository.findAll<BegrepDBO>()
currentConceptRepository.deleteAll()
concepts.forEach {
if (it.shouldBeCurrent(currentConceptRepository.findByIdOrNull(it.originaltBegrep))) {
currentConceptRepository.save(CurrentConcept(it))
}
}
concepts
.groupBy { concept -> concept.originaltBegrep }
.mapNotNull { pair -> pair.value.maxByOrNull { concept -> concept.versjonsnr } }
.forEach { currentConceptRepository.save(CurrentConcept(it)) }
}
}
7 changes: 6 additions & 1 deletion src/test/kotlin/no/fdk/concept_catalog/utils/TestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ val BEGREP_0_OLD = Begrep(
status = Status.PUBLISERT,
statusURI = "http://publications.europa.eu/resource/authority/concept-status/DEPRECATED",
erPublisert = true,
sistPublisert = "id0",
publiseringsTidspunkt = ZonedDateTime.of(2019, 1, 1, 12,0,0,0, ZoneId.of("Europe/Oslo")).toInstant(),
gjeldendeRevisjon = null,
anbefaltTerm = Term(navn = mapOf(Pair("nb", "old anbefaltTerm"))),
Expand Down Expand Up @@ -64,6 +65,7 @@ val BEGREP_0 = Begrep(
publiseringsTidspunkt = ZonedDateTime.of(2020, 1, 2, 12,0,0,0, ZoneId.of("Europe/Oslo")).toInstant(),
gjeldendeRevisjon = null,
erSistPublisert = true,
sistPublisert = "id0",
anbefaltTerm = Term(navn = mapOf(Pair("nb", "anbefaltTerm"))),
tillattTerm = mapOf(Pair("nn", listOf("tillattTerm", "tillattTerm2"))),
frarådetTerm = mapOf(Pair("nb", listOf("fraraadetTerm", "fraraadetTerm2", "Lorem ipsum"))),
Expand Down Expand Up @@ -274,6 +276,7 @@ val BEGREP_4 = Begrep(
statusURI = "http://publications.europa.eu/resource/authority/concept-status/CURRENT",
erPublisert = true,
erSistPublisert = true,
sistPublisert = "id4",
publiseringsTidspunkt = ZonedDateTime.of(2020, 1, 2, 12,0,0,0, ZoneId.of("Europe/Oslo")).toInstant(),
gjeldendeRevisjon = null,
anbefaltTerm = Term(navn = mapOf(Pair("en", "Begrep 4"))),
Expand Down Expand Up @@ -303,6 +306,7 @@ val BEGREP_5 = Begrep(
statusURI = "http://publications.europa.eu/resource/authority/concept-status/CURRENT",
erPublisert = true,
erSistPublisert = true,
sistPublisert = "id5",
publiseringsTidspunkt = ZonedDateTime.of(2020, 1, 2, 12,0,0,0, ZoneId.of("Europe/Oslo")).toInstant(),
gjeldendeRevisjon = null,
anbefaltTerm = Term(navn = mapOf(Pair("en", "Begrep 5"))),
Expand Down Expand Up @@ -412,6 +416,7 @@ val BEGREP_HAS_MULTIPLE_REVISIONS = Begrep(
statusURI = "http://publications.europa.eu/resource/authority/concept-status/CURRENT",
erPublisert = true,
erSistPublisert = true,
sistPublisert = "id-has-multiple-revisions",
publiseringsTidspunkt = ZonedDateTime.of(2020, 1, 2, 12,0,0,0, ZoneId.of("Europe/Oslo")).toInstant(),
gjeldendeRevisjon = "id-unpublished-revision-multiple-first",
versjonsnr = SemVer(0, 0, 1),
Expand Down Expand Up @@ -444,7 +449,7 @@ val BEGREP_UNPUBLISHED_REVISION_MULTIPLE_SECOND = Begrep(
statusURI = "http://publications.europa.eu/resource/authority/concept-status/DRAFT",
erPublisert = false,
gjeldendeRevisjon = null,
versjonsnr = SemVer(0, 0, 2),
versjonsnr = SemVer(0, 0, 3),
ansvarligVirksomhet = Virksomhet(
id = "222222222"
),
Expand Down

0 comments on commit 145fb0a

Please sign in to comment.