diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt index 7d6f95e..40ad8bd 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt @@ -189,6 +189,18 @@ class Controller { ) ) } + + @PostMapping("/observations/csv") + fun getObservationsCsvDump(@Valid @RequestBody observationsRequest: ObservationsRequest): String? { + val data = getObservations(observationsRequest) + if(data.isEmpty()) return null + val builder = StringBuilder(data[0].csvHeaderString()) + data.forEach { + builder.append('\n') + builder.append(it.toCsvFormat()) + } + return builder.toString() + } } data class AverageData( diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt index a18fb62..3ce80db 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt @@ -18,6 +18,7 @@ import javax.persistence.ManyToMany import javax.persistence.ManyToOne import javax.persistence.OneToMany import javax.persistence.Table +import kotlin.math.round /** * Defines a location at which observations occur. @@ -95,7 +96,45 @@ data class Observation( val tutors: Set, @ManyToMany val persons: Set -) +) { + fun csvHeaderString(): String { + return "Ob ID,Observerfull,Observer,obDate,Title,Type,Department,Station,Station Focus,Scores-Monitoring,Scores-Control Procedural," + + "Scores-Control,Scores-Conservatism,Scores-Teamwork Comms,Scores-Teamwork Leadership,Scores-Teamwork Workload,\"Scores-Knowledge, Skills and Attitudes\"," + + "Strengths-Monitoring,Strengths-Control Procedural,Strengths-Control,Strengths-Conservatism,Strengths-Teamwork Comms," + + "Strengths-Teamwork Leadership,Strengths-Teamwork Workload,\"Strengths-Knowledge, Skills and Attitudes\"," + + "Develop-Monitoring,Develop-Control Procedural,Develop-Control,Develop-Conservatism,Develop-Teamwork Comms," + + "Develop-Teamwork Leadership,Develop-Teamwork Workload,\"Develop-Knowledge, Skills and Attitudes\"" + } + fun toCsvFormat(): String { + return ",,${tutors.elementAt(0).name},$date,\"$observed\",\"Training\",\"Performance Improvement - Training\",\"${site.name}\",\"N/A\"," + + "${roundScore(monitoring)},${roundScore(controlProcedural)},${roundScore(control)}," + + "${roundScore(conservatism)},${roundScore(teamworkCommunications)},${roundScore(teamworkLeadership)}," + + "${roundScore(teamworkWorkload)},${roundScore(knowledge)},\"${getStrengths(RatingCategory.MONITORING)}\"," + + "\"${getStrengths(RatingCategory.CONTROL_PROCEDURAL)}\",\"${getStrengths(RatingCategory.CONTROL)}\",\"${getStrengths(RatingCategory.CONSERVATISM)}\"," + + "\"${getStrengths(RatingCategory.TEAMWORK_COMMUNICATIONS)}\",\"${getStrengths(RatingCategory.TEAMWORK_LEADERSHIP)}\"," + + "\"${getStrengths(RatingCategory.TEAMWORK_WORKLOAD)}\",\"${getStrengths(RatingCategory.KNOWLEDGE)}\"," + + "\"${getImprovements(RatingCategory.MONITORING)}\",\"${getImprovements(RatingCategory.CONTROL_PROCEDURAL)}\"," + + "\"${getImprovements(RatingCategory.CONTROL)}\",\"${getImprovements(RatingCategory.CONSERVATISM)}\"," + + "\"${getImprovements(RatingCategory.TEAMWORK_COMMUNICATIONS)}\",\"${getImprovements(RatingCategory.TEAMWORK_LEADERSHIP)}\"," + + "\"${getImprovements(RatingCategory.TEAMWORK_WORKLOAD)}\",\"${getImprovements(RatingCategory.KNOWLEDGE)}\"" + } + + private fun getStrengths(category: RatingCategory): String{ + return entries.asSequence().filter { it.type == category }.map{ it.strengths }.joinToString() + } + + private fun getImprovements(category: RatingCategory): String{ + return entries.asSequence().filter { it.type == category }.map{ it.improvements }.joinToString() + } + + private fun roundScore(input: Double?): String { + if (input != null) { + return round(input).toString() + } + return "" + } +} + /** * Entry giving specific details on observation performance for a tutor defined constraint.