Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use highest version of concept in elastic #397

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/deploy-prod&demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
environment: prod
secrets:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GCP_SA_DIGDIR_FDK_GCR_KEY: ${{ secrets.GCP_SA_DIGDIR_FDK_GCR_KEY }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

deploy-prod:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/deploy-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
environment: staging
secrets:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GCP_SA_DIGDIR_FDK_GCR_KEY: ${{ secrets.GCP_SA_DIGDIR_FDK_GCR_KEY }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

deploy:
Expand All @@ -41,5 +40,3 @@ jobs:
uses: Informasjonsforvaltning/workflows/.github/workflows/build.yaml@main
with:
java_version: '17'
secrets:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion deploy/base/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ resources:
- concept-catalog-service.yaml
images:
- name: concept-catalog
newName: eu.gcr.io/digdir-fdk-infra/concept-catalog
newName: ghcr.io/informasjonsforvaltning/concept-catalog
newTag: $(GIT_COMMIT_SHA)
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 @@
} catch (_: Exception) { }

conceptRepository.findAll<BegrepDBO>()
.groupBy { concept -> concept.originaltBegrep }
.mapNotNull { pair -> pair.value.maxByOrNull { concept -> concept.versjonsnr } }

Check warning on line 30 in src/main/kotlin/no/fdk/concept_catalog/elastic/ElasticUpdater.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/concept_catalog/elastic/ElasticUpdater.kt#L29-L30

Added lines #L29 - L30 were not covered by tests
.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))

Check warning on line 33 in src/main/kotlin/no/fdk/concept_catalog/elastic/ElasticUpdater.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/no/fdk/concept_catalog/elastic/ElasticUpdater.kt#L32-L33

Added lines #L32 - L33 were not covered by tests
}

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 sistPublisertId: 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),
sistPublisertId = 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,
sistPublisertId = 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,
sistPublisertId = "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", sistPublisertId = "id3-1", versjonsnr = SemVer(2, 10, 0), revisjonAv = "id3", status = Status.GODKJENT, erPublisert = false, revisjonAvSistPublisert = false)
val ok = BEGREP_3.copy(id = "id3-3", sistPublisertId = "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)) }
}
}
9 changes: 8 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,
sistPublisertId = "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,
sistPublisertId = "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,
sistPublisertId = "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,
sistPublisertId = "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,
sistPublisertId = "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 All @@ -428,6 +433,7 @@ val BEGREP_UNPUBLISHED_REVISION_MULTIPLE_FIRST = Begrep(
status = Status.UTKAST,
statusURI = "http://publications.europa.eu/resource/authority/concept-status/DRAFT",
erPublisert = false,
sistPublisertId = "id-has-multiple-revisions",
gjeldendeRevisjon = null,
versjonsnr = SemVer(0, 0, 2),
ansvarligVirksomhet = Virksomhet(
Expand All @@ -443,8 +449,9 @@ val BEGREP_UNPUBLISHED_REVISION_MULTIPLE_SECOND = Begrep(
status = Status.UTKAST,
statusURI = "http://publications.europa.eu/resource/authority/concept-status/DRAFT",
erPublisert = false,
sistPublisertId = "id-has-multiple-revisions",
gjeldendeRevisjon = null,
versjonsnr = SemVer(0, 0, 2),
versjonsnr = SemVer(0, 0, 3),
ansvarligVirksomhet = Virksomhet(
id = "222222222"
),
Expand Down
Loading