Skip to content

Commit

Permalink
improve processing result condition handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig committed Nov 4, 2024
1 parent 6f7c757 commit 59f7328
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.json.exists
import org.jetbrains.exposed.sql.kotlin.datetime.datetime
import org.jetbrains.exposed.sql.max
import org.jetbrains.exposed.sql.not
import org.jetbrains.exposed.sql.or
import org.jetbrains.exposed.sql.wrapAsExpression
import org.loculus.backend.api.AccessionVersionInterface
import org.loculus.backend.api.Organism
import org.loculus.backend.api.OriginalData
import org.loculus.backend.api.PreprocessingAnnotation
import org.loculus.backend.api.ProcessedData
import org.loculus.backend.api.ProcessingResult
import org.loculus.backend.api.Status
import org.loculus.backend.api.toPairs
import org.loculus.backend.service.jacksonSerializableJsonb
Expand Down Expand Up @@ -64,8 +67,22 @@ object SequenceEntriesView : Table(SEQUENCE_ENTRIES_VIEW_NAME) {

fun organismIs(organism: Organism) = organismColumn eq organism.name

val hasWarnings: Op<Boolean> = warningsColumn.isNotNull() and warningsColumn.exists("[0]")
val hasErrors: Op<Boolean> = errorsColumn.isNotNull() and errorsColumn.exists("[0]")
private val hasWarnings: Op<Boolean> = warningsColumn.isNotNull() and warningsColumn.exists("[0]")
private val hasErrors: Op<Boolean> = errorsColumn.isNotNull() and errorsColumn.exists("[0]")

fun processingResultIs(processingResult: ProcessingResult) = when (processingResult) {
ProcessingResult.ERRORS -> SequenceEntriesView.hasErrors
ProcessingResult.NO_ISSUES -> not(
SequenceEntriesView.hasErrors or SequenceEntriesView.hasWarnings,
) and SequenceEntriesView.statusIs(Status.PROCESSED)
ProcessingResult.WARNINGS -> not(SequenceEntriesView.hasErrors) and
SequenceEntriesView.hasWarnings
}

fun processingResultIsOneOf(processingResults: List<ProcessingResult>) = processingResults.map {
SequenceEntriesView.processingResultIs(it)
}
.fold(Op.FALSE as Op<Boolean>) { acc, condition -> acc or condition }

fun statusIs(status: Status) = statusColumn eq status.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,6 @@ class SubmissionDatabaseService(

val statusCondition = SequenceEntriesView.statusIs(Status.PROCESSED)

val errorCondition = not(SequenceEntriesView.hasErrors)

val accessionCondition = if (accessionVersionsFilter !== null) {
SequenceEntriesView.accessionVersionIsIn(accessionVersionsFilter)
} else if (authenticatedUser.isSuperUser) {
Expand All @@ -485,11 +483,11 @@ class SubmissionDatabaseService(
SequenceEntriesView.groupIsOneOf(groupManagementDatabaseService.getGroupIdsOfUser(authenticatedUser))
}

val scopeCondition = if (scope == ApproveDataScope.WITHOUT_WARNINGS) {
not(SequenceEntriesView.hasWarnings)
} else {
Op.TRUE
var includedProcessingResults = mutableListOf(ProcessingResult.NO_ISSUES)
if (scope == ApproveDataScope.ALL) {
includedProcessingResults.add(ProcessingResult.WARNINGS)
}
val scopeCondition = SequenceEntriesView.processingResultIsOneOf(includedProcessingResults)

val groupCondition = getGroupCondition(groupIdsFilter, authenticatedUser)

Expand All @@ -504,7 +502,7 @@ class SubmissionDatabaseService(
val accessionVersionsToUpdate = SequenceEntriesView
.selectAll()
.where {
statusCondition and errorCondition and accessionCondition and scopeCondition and groupCondition and
statusCondition and accessionCondition and scopeCondition and groupCondition and
organismCondition and submitterCondition
}
.map { AccessionVersion(it[SequenceEntriesView.accessionColumn], it[SequenceEntriesView.versionColumn]) }
Expand Down Expand Up @@ -690,24 +688,10 @@ class SubmissionDatabaseService(
SequenceEntriesView.statusIsOneOf(listOfStatuses)
}

processingResultFilter?.let {
processingResultFilter?.let { processingResultsToInclude ->
filteredQuery.andWhere {
// Filter for has errors or not, but only if status is processed
not(SequenceEntriesView.statusIs(Status.PROCESSED)) or
(
SequenceEntriesView.statusIs(Status.PROCESSED) and
it.map { processingResult ->
when (processingResult) {
ProcessingResult.ERRORS -> SequenceEntriesView.hasErrors
ProcessingResult.NO_ISSUES -> not(
SequenceEntriesView.hasErrors or SequenceEntriesView.hasWarnings,
)
ProcessingResult.WARNINGS -> not(SequenceEntriesView.hasErrors) and
SequenceEntriesView.hasWarnings
}
}
.fold(Op.FALSE as Op<Boolean>) { acc, condition -> acc or condition }
)
SequenceEntriesView.processingResultIsOneOf(processingResultsToInclude) or
not(SequenceEntriesView.statusIs(Status.PROCESSED))
}
}

Expand Down Expand Up @@ -768,12 +752,14 @@ class SubmissionDatabaseService(
countBaseQuery.andWhere { SequenceEntriesView.organismIs(organism) }
}

val errorCount = countBaseQuery.copy().andWhere { SequenceEntriesView.hasErrors }.count()
val errorCount = countBaseQuery.copy().andWhere {
SequenceEntriesView.processingResultIs(ProcessingResult.ERRORS)
}.count()
val warningCount = countBaseQuery.copy().andWhere {
SequenceEntriesView.hasWarnings and not(SequenceEntriesView.hasErrors)
SequenceEntriesView.processingResultIs(ProcessingResult.WARNINGS)
}.count()
val noIssuesCount = countBaseQuery.copy().andWhere {
not(SequenceEntriesView.hasWarnings or SequenceEntriesView.hasErrors)
SequenceEntriesView.processingResultIs(ProcessingResult.NO_ISSUES)
}.count()
val processingResultCounts = mapOf(
ProcessingResult.ERRORS to errorCount.toInt(),
Expand Down Expand Up @@ -848,7 +834,9 @@ class SubmissionDatabaseService(
(SequenceEntriesView.accessionColumn inList accessions) and
SequenceEntriesView.isMaxVersion and
SequenceEntriesView.statusIs(Status.PROCESSED) and
not(SequenceEntriesView.hasErrors)
SequenceEntriesView.processingResultIsOneOf(
listOf(ProcessingResult.WARNINGS, ProcessingResult.NO_ISSUES)
)
}
.orderBy(SequenceEntriesView.accessionColumn)
.map {
Expand Down Expand Up @@ -901,9 +889,9 @@ class SubmissionDatabaseService(

val scopeCondition = when (scope) {
DeleteSequenceScope.PROCESSED_WITH_ERRORS -> SequenceEntriesView.statusIs(Status.PROCESSED) and
SequenceEntriesView.hasErrors
SequenceEntriesView.processingResultIs(ProcessingResult.ERRORS)
DeleteSequenceScope.PROCESSED_WITH_WARNINGS -> SequenceEntriesView.statusIs(Status.PROCESSED) and
SequenceEntriesView.hasWarnings
SequenceEntriesView.processingResultIs(ProcessingResult.WARNINGS)

DeleteSequenceScope.ALL -> SequenceEntriesView.statusIsOneOf(listOfDeletableStatuses)
}
Expand Down

0 comments on commit 59f7328

Please sign in to comment.