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

Return no observations when table does not (yet) exist #10

Merged
merged 2 commits into from
Oct 15, 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
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@Suppress("ConstPropertyName")
object Versions {
const val project = "0.1.8"
const val project = "0.2.2"

const val java = 17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.radarbase.datadashboard.api.domain
import jakarta.inject.Provider
import jakarta.persistence.EntityManager
import jakarta.ws.rs.core.Context
import org.hibernate.exception.SQLGrammarException
import org.radarbase.datadashboard.api.domain.model.Observation
import org.radarbase.jersey.hibernate.HibernateRepository
import org.radarbase.jersey.service.AsyncCoroutineService
Expand All @@ -31,22 +32,40 @@ class ObservationRepositoryImpl(
@Context asyncService: AsyncCoroutineService,
) : HibernateRepository(em, asyncService), ObservationRepository {

private val tableExistsRegex = Regex("relation \".*\" does not exist")

override suspend fun getObservations(projectId: String, subjectId: String, topicId: String): List<Observation> {
logger.debug("Get observations in topic {} of subject {} in project {}", topicId, subjectId, projectId)

return transact {
createQuery(
"SELECT o FROM Observation o WHERE o.project = :projectId AND o.subject = :subjectId AND o.topic = :topicId ORDER BY o.observationTime DESC",
Observation::class.java,
).apply {
setParameter("projectId", projectId)
setParameter("subjectId", subjectId)
setParameter("topicId", topicId)
}.resultList
try {
createQuery(
"SELECT o FROM Observation o WHERE o.project = :projectId AND o.subject = :subjectId AND o.topic = :topicId ORDER BY o.observationTime DESC",
Observation::class.java,
).apply {
setParameter("projectId", projectId)
setParameter("subjectId", subjectId)
setParameter("topicId", topicId)
}.resultList
} catch (ex: SQLGrammarException) {
if (tableDoesNotExist(ex)) {
logger.info(
"Observations table has not been created by JDBC connector yet " +
"(will be created upon first data ingestion). Returning empty result...",
)
emptyList()
} else {
throw ex
}
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(ObservationRepositoryImpl::class.java)
}

private fun tableDoesNotExist(ex: SQLGrammarException): Boolean {
return tableExistsRegex.containsMatchIn(ex.message ?: "")
}
}
Loading