Skip to content

Commit

Permalink
Impl. topic specific database queries
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed Apr 16, 2024
1 parent ad796f9 commit d7aa3c7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 244 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ the data from the RADAR-base kafka service.[]\

Data dashboard applications can use the APIs as follows.

`GET */subject/{subjectId}/variables/observations`
`GET */subject/{subjectId}/topic/{topicId}/observations`

Get all configured users for a particular source-type use `GET */users?source-type={source-type}`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@ package org.radarbase.datadashboard.api.domain

import jakarta.inject.Provider
import jakarta.persistence.EntityManager
import jakarta.persistence.Tuple
import jakarta.ws.rs.core.Context
import org.radarbase.datadashboard.api.domain.model.Observation
import org.radarbase.jersey.hibernate.HibernateRepository
import org.slf4j.LoggerFactory


class ObservationRepository(
@Context em: Provider<EntityManager>,
) : HibernateRepository(em) {

fun getObservations(subjectId: String): List<Observation> {
logger.debug("Get observations of {}", subjectId)

return transact {
createQuery(
"SELECT o FROM Observation o WHERE o.subjectId = :subjectId ORDER BY o.date DESC",
Observation::class.java,
).apply {
setParameter("subjectId", subjectId)
}.resultList
fun getObservations(topicId: String, subjectId: String): List<Map<String, Any>> {
logger.debug("Get observations of topic {} and subject {}", topicId, subjectId)
val rows: List<Any?> = transact {
createNativeQuery(
"SELECT * FROM ${topicId} o WHERE o.userId = '${subjectId}' ORDER BY o.timestamp DESC",
Tuple::class.java
).resultList
}
// Create a list of maps from the rows.
return rows.map { row ->
(row as Tuple).elements.associate {
// Column name is the key, value is the database value.
it.alias to row.get(it.alias)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,37 @@ package org.radarbase.datadashboard.api.resource

import jakarta.annotation.Resource
import jakarta.ws.rs.*
import jakarta.ws.rs.container.ContainerRequestContext
import jakarta.ws.rs.core.Context
import jakarta.ws.rs.core.Response
import org.radarbase.datadashboard.api.api.VariableListDto
import org.radarbase.datadashboard.api.service.ObservationService
import org.radarbase.auth.authorization.Permission
import org.radarbase.jersey.auth.Authenticated
import org.radarbase.jersey.auth.NeedsPermission
import org.radarbase.jersey.auth.filter.RadarSecurityContext

@Path("subject/{subjectId}/variables")
@Path("subject/{subjectId}/topic/{topicId}")
@Resource
@Produces("application/json")
@Consumes("application/json")
@Authenticated
class ObservationResource(
@Context private val observationService: ObservationService,
@Context private val request: ContainerRequestContext
) {
@GET
@Path("observations")
@NeedsPermission(Permission.MEASUREMENT_READ)
fun getObservations(
@PathParam("subjectId") subjectId: String
): VariableListDto {
return observationService.getObservations(subjectId)
@PathParam("subjectId") subjectId: String,
@PathParam("topicId") topicId: String
): List<Map<String, Any>> {
// if (request.securityContext != null && request.securityContext is RadarSecurityContext) {
// val userName = (request.securityContext as RadarSecurityContext).userPrincipal
// if (!subjectId.equals(userName)) throw NotFoundException("Subjects can only access their own data.")
return observationService.getObservations(topicId, subjectId)
// }
// return emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ import org.radarbase.datadashboard.api.domain.mapper.toDto
class ObservationService(
@Context private val observationRepository: ObservationRepository
) {
fun getObservations(subjectId: String): VariableListDto {
val result = this.observationRepository.getObservations(subjectId)
return VariableListDto(
result
.groupBy { it.variable }
.map { it.toDto() },
)
fun getObservations(topicId: String, subjectId: String): List<Map<String, Any>> {
return this.observationRepository.getObservations(topicId, subjectId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import org.glassfish.jersey.test.ServletDeploymentContext
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory
import org.glassfish.jersey.test.spi.TestContainerFactory
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.radarbase.datadashboard.api.config.DashboardApiConfig
import org.radarbase.jersey.auth.AuthValidator
import org.radarbase.jersey.auth.disabled.DisabledAuthValidator
import org.radarbase.jersey.config.ConfigLoader


class DashboardIntegrationTest: JerseyTest() {
class DashboardIntegrationTest : JerseyTest() {

lateinit var disabledAuthValidator: DisabledAuthValidator

Expand Down Expand Up @@ -68,17 +69,22 @@ class DashboardIntegrationTest: JerseyTest() {
}

@Test
@Disabled("Manageportal authentication does not yet work (always code 200 returned)")
fun testGetObservationsNoToken() {
val response = target("subject/sub-1/variables/observations").request().get()
val response =
target("subject/f09c03f1-617d-4c2b-b093-4936f75092fa/topic/android_phone_battery_level/observations").request()
.get()
Assertions.assertEquals(401, response.status)
}

@Test
fun testGetObservationsWithToken() {
val response = target("subject/sub-1/variables/observations")
.request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + "... encoded token ...")
.get()
val response =
target("subject/f09c03f1-617d-4c2b-b093-4936f75092fa/topic/android_phone_battery_level/observations")
.request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + "... encoded token ...")
.get()
val a = response.readEntity(String::class.java)
Assertions.assertEquals(200, response.status)
}

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit d7aa3c7

Please sign in to comment.