Skip to content

Commit

Permalink
Implement ObservationsListDto return type
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed May 3, 2024
1 parent 3354d0a commit 2466b12
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 777 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +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 all configured users for a particular source-type use `GET */users?source-type={source-type}`
`GET */subject/{subjectId}/topic/{topicId}/observations`

## Installation

Expand Down
4 changes: 2 additions & 2 deletions data-dashboard-backend/dev/dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ service:

auth:
managementPortal:
url: http://localhost:8080/managementportal
url: http://127.0.0.1:8080/managementportal
clientId: data_dashboard_api
clientSecret: data_dashboard_api_secret
jwtResourceName: res_DataDashboardAPI

database:
url: jdbc:postgresql://localhost:5432/data
url: jdbc:postgresql://127.0.0.1:5432/data
user: radarbase
password: radarbase
dialect: org.hibernate.dialect.PostgreSQLDialect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ data class ObservationDto(
/** Unique observation ID. */
val id: Long?,

/** Unique identifier of study subject. */
val subject: String?,

/** Unique identifier of the kafka topic. */
val topic: String?,

/** Category of the observation (optional). */
val category: String?,

/** Date or date-time of the observation. */
val date: String?,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
package org.radarbase.datadashboard.api.api

/** List of variables. */
data class VariableListDto(
val variables: List<VariableDto>,
data class ObservationListDto(
val observations: List<ObservationDto>,
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ class ObservationRepository(
@Context em: Provider<EntityManager>,
) : HibernateRepository(em) {

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

return transact {
createQuery(
"SELECT o FROM Observation o WHERE o.subjectId = :subjectId ORDER BY o.date DESC",
"SELECT o FROM Observation o WHERE o.subject = :subjectId AND o.topic = :topicId ORDER BY o.date DESC",
Observation::class.java,
).apply {
setParameter("subjectId", subjectId)
setParameter("topicId", topicId)
}.resultList
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package org.radarbase.datadashboard.api.domain.mapper

import org.radarbase.datadashboard.api.api.ObservationDto
import org.radarbase.datadashboard.api.api.VariableDto
import org.radarbase.datadashboard.api.domain.model.Observation
import org.radarbase.datadashboard.api.domain.model.Variable
import java.time.Duration

fun Observation.toDto(): ObservationDto = ObservationDto(
id = id,
subject = subject,
topic = topic,
category = category,
date = date?.toString(),
period = if (date != null && endDate != null) {
Duration.between(date, endDate).toString()
Expand All @@ -35,22 +36,3 @@ fun Observation.toDto(): ObservationDto = ObservationDto(
value = valueNumeric ?: valueTextual,
)

fun Map.Entry<Variable, List<Observation>>.toDto(): VariableDto {
val (variable, observations) = this
return variable.toDto(
observations
.sortedBy { it.date }
.map { it.toDto() },
)
}

fun Variable.toDtoWithoutObservations(): VariableDto = toDto(null)

fun Variable.toDto(observations: List<ObservationDto>?): VariableDto = VariableDto(
id = id,
name = name,
type = type,
category = category,
observations = observations,
dateType = dateType,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,55 @@ import java.util.*

@Entity
@Table(name = "observation")
class Observation(
data class Observation(
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false)
@Id
val id: Long?,
@Column(name = "subject_id")
val subjectId: String,
@ManyToOne
@JoinColumn(name = "variable_id")
val variable: Variable,
val date: ZonedDateTime?,
val id: Long,

@Column(nullable = false)
@Id
val subject: String,

@Column(nullable = false)
@Id
val topic: String,

@Id
val category: String,

@Column(nullable = false)
@Id
val variable: String,

@Column(nullable = false)
@Id
val date: ZonedDateTime,

@Column(name = "end_date")
val endDate: ZonedDateTime?,

@Column(name = "value_textual")
val valueTextual: String?,

@Column(name = "value_numeric")
val valueNumeric: Double?,

) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Observation
if (id != null && other.id != null) {
return id == other.id
}

return subjectId == other.subjectId &&
return subject == other.subject &&
topic == other.topic &&
category == other.category &&
variable == other.variable &&
date == other.date &&
endDate == other.endDate
}

override fun hashCode(): Int = Objects.hash(subjectId, variable, date)

override fun toString(): String = "Observation(" +
"id=$id, " +
"variable=$variable, " +
"date=$date, " +
"endDate=$endDate, " +
"valueTextual=${valueTextual.toPrintString()}, " +
"valueNumeric=$valueNumeric)"
override fun hashCode(): Int = Objects.hash(subject, variable, date)

companion object {
internal fun String?.toPrintString() = if (this != null) "'$this'" else "null"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package org.radarbase.datadashboard.api.enhancer

import org.radarbase.datadashboard.api.config.DashboardApiConfig
import org.radarbase.datadashboard.api.domain.model.Observation
import org.radarbase.datadashboard.api.domain.model.Variable
import org.radarbase.jersey.enhancer.EnhancerFactory
import org.radarbase.jersey.enhancer.Enhancers
import org.radarbase.jersey.enhancer.JerseyResourceEnhancer
Expand All @@ -40,8 +39,7 @@ class DashBoardApiEnhancerFactory(
HibernateResourceEnhancer(
config.database.copy(
managedClasses = listOf(
Observation::class.jvmName,
Variable::class.jvmName
Observation::class.jvmName
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,36 @@ 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 org.radarbase.datadashboard.api.api.VariableListDto
import jakarta.ws.rs.core.Response
import org.radarbase.datadashboard.api.service.ObservationService
import org.radarbase.auth.authorization.Permission
import org.radarbase.datadashboard.api.api.ObservationDto
import org.radarbase.datadashboard.api.api.ObservationListDto
import org.radarbase.jersey.auth.Authenticated
import org.radarbase.jersey.auth.NeedsPermission

@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 observationService: ObservationService
) {
@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
): ObservationListDto {
// 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 @@ -19,19 +19,17 @@
package org.radarbase.datadashboard.api.service

import jakarta.ws.rs.core.Context
import org.radarbase.datadashboard.api.api.VariableListDto
import org.radarbase.datadashboard.api.api.ObservationListDto
import org.radarbase.datadashboard.api.domain.ObservationRepository
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): ObservationListDto {
val result = this.observationRepository.getObservations(topicId, subjectId)
return ObservationListDto(
result.map { it.toDto() },
)
}
}
Loading

0 comments on commit 2466b12

Please sign in to comment.