Skip to content

Commit

Permalink
Slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusroemer committed Nov 23, 2024
1 parent 7880ae1 commit bb23726
Showing 1 changed file with 12 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -618,22 +618,6 @@ class SubmissionDatabaseService(
)
}

/** Efficiently get the counts of sequences in each status for a given organism and group(s). */
fun getStatusCounts(organism: Organism, groupCondition: Op<Boolean>): Map<Status, Int> {
// Use group by to calculate counts efficiently
val statusCounts = SequenceEntriesView
.select(SequenceEntriesView.statusColumn, Count(SequenceEntriesView.statusColumn))
.where {
SequenceEntriesView.organismIs(organism) and groupCondition
}
.groupBy(SequenceEntriesView.statusColumn)
.associate {
Status.fromString(it[SequenceEntriesView.statusColumn]) to
it[Count(SequenceEntriesView.statusColumn)].toInt()
}
return Status.entries.associateWith { statusCounts[it] ?: 0 }
}

/**
* Returns a paginated list of sequences matching the given filters.
* Also returns status counts and processing result counts.
Expand All @@ -659,8 +643,6 @@ class SubmissionDatabaseService(

val groupCondition = getGroupCondition(groupIdsFilter, authenticatedUser)

log.info { "Defining base query" }

val baseQuery = SequenceEntriesView
.join(
DataUseTermsTable,
Expand Down Expand Up @@ -690,10 +672,6 @@ class SubmissionDatabaseService(
.andWhere { SequenceEntriesView.organismIs(organism) }
.orderBy(SequenceEntriesView.accessionColumn)

log.info { "Getting status counts" }

val statusCounts = getStatusCounts(organism, groupCondition)

val filteredQuery = baseQuery.andWhere {
SequenceEntriesView.statusIsOneOf(listOfStatuses)
}
Expand All @@ -711,8 +689,6 @@ class SubmissionDatabaseService(
filteredQuery
}

log.info { "Getting sequence entries" }

val entries = pagedQuery
.map { row ->
SequenceEntryStatus(
Expand All @@ -735,11 +711,9 @@ class SubmissionDatabaseService(
)
}

log.info { "Getting processing result counts" }

val processingResultCounts = getProcessingResultCounts(groupIdsFilter, authenticatedUser, organism)

log.info { "Returning response" }
val statusCounts = getStatusCounts(organism, groupCondition)

return GetSequenceResponse(
sequenceEntries = entries,
Expand All @@ -748,6 +722,17 @@ class SubmissionDatabaseService(
)
}

/** Efficiently get the counts of sequences in each status for a given organism and group(s). */
private fun getStatusCounts(organism: Organism, groupCondition: Op<Boolean>): Map<Status, Int> {
val statusColumn = SequenceEntriesView.statusColumn
val statusCounts = SequenceEntriesView
.select(statusColumn, Count(statusColumn))
.where { SequenceEntriesView.organismIs(organism) and groupCondition }
.groupBy(statusColumn)
.associate { Status.fromString(it[statusColumn]) to it[Count(statusColumn)].toInt() }
return Status.entries.associateWith { statusCounts[it] ?: 0 }
}

/**
* How many processing results have errors, just warnings, or none?
* Considers only SequenceEntries that are PROCESSED.
Expand Down

0 comments on commit bb23726

Please sign in to comment.