Refactored CSV utilities

This commit is contained in:
neviyn 2019-07-15 12:51:15 +01:00
parent 319e59354a
commit 0778586e51
3 changed files with 40 additions and 20 deletions

View File

@ -0,0 +1,19 @@
package uk.co.neviyn.observationdatabase
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 observationsToCSV(data: List<Observation>): String {
val builder = StringBuilder(csvHeaderString())
data.forEach {
builder.append('\n')
builder.append(it.toCsvFormat())
}
return builder.toString()
}

View File

@ -1,5 +1,7 @@
package uk.co.neviyn.observationdatabase.controller package uk.co.neviyn.observationdatabase.controller
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
import uk.co.neviyn.observationdatabase.Email
import uk.co.neviyn.observationdatabase.GroupObservation import uk.co.neviyn.observationdatabase.GroupObservation
import uk.co.neviyn.observationdatabase.GroupObservationInit import uk.co.neviyn.observationdatabase.GroupObservationInit
import uk.co.neviyn.observationdatabase.GroupSessionManager import uk.co.neviyn.observationdatabase.GroupSessionManager
@ -48,6 +51,8 @@ class GroupSessionController {
lateinit var personRepository: PersonRepository lateinit var personRepository: PersonRepository
@Autowired @Autowired
lateinit var websocketMessenger: SimpMessagingTemplate lateinit var websocketMessenger: SimpMessagingTemplate
@Autowired
lateinit var mailer: Email
/** /**
* Start a new Group Observation session * Start a new Group Observation session
@ -124,8 +129,8 @@ class GroupSessionController {
* Get the current observation data for a user with [name] in the current session. * Get the current observation data for a user with [name] in the current session.
*/ */
@GetMapping("/participant/{name}") @GetMapping("/participant/{name}")
fun getParticipantData(@PathVariable name: String): GroupObservation{ fun getParticipantData(@PathVariable name: String): GroupObservation {
if(GroupSessionManager.participantExistsInSession(name)) if (GroupSessionManager.participantExistsInSession(name))
return GroupSessionManager.getObservationDataForParticipant(name)!! return GroupSessionManager.getObservationDataForParticipant(name)!!
throw ResponseStatusException(HttpStatus.NOT_FOUND, "No participant with the name:'$name'") throw ResponseStatusException(HttpStatus.NOT_FOUND, "No participant with the name:'$name'")
} }
@ -149,8 +154,9 @@ class GroupSessionController {
if (GroupSessionManager.isValid() && GroupSessionManager.dataComplete()) { if (GroupSessionManager.isValid() && GroupSessionManager.dataComplete()) {
logger.info("Completing session ${GroupSessionManager.sessionId}") logger.info("Completing session ${GroupSessionManager.sessionId}")
val tutors = tutorRepository.findAllById(GroupSessionManager.tutors!!).toSet() val tutors = tutorRepository.findAllById(GroupSessionManager.tutors!!).toSet()
val observations = mutableListOf<Observation>()
GroupSessionManager.observations.values.forEach { x -> GroupSessionManager.observations.values.forEach { x ->
saveObservation(Observation( val observation = Observation(
site = GroupSessionManager.site!!, site = GroupSessionManager.site!!,
date = LocalDate.now(), date = LocalDate.now(),
type = GroupSessionManager.trainingType!!, type = GroupSessionManager.trainingType!!,
@ -167,9 +173,17 @@ class GroupSessionController {
tutors = tutors, tutors = tutors,
person = personRepository.findFirstByNameLike(x.person.toUpperCase()) person = personRepository.findFirstByNameLike(x.person.toUpperCase())
?: personRepository.save(Person(name = x.person.toUpperCase())) ?: personRepository.save(Person(name = x.person.toUpperCase()))
)) )
saveObservation(observation)
observations.add(observation)
} }
GroupSessionManager.invalidate() GroupSessionManager.invalidate()
GlobalScope.launch {
if (::mailer.isInitialized)
mailer.sendObservationData(observations)
else
logger.error("Mailer has not been initialized.")
}
websocketMessenger.convertAndSend("/ws/status", mapOf("status" to "complete")) websocketMessenger.convertAndSend("/ws/status", mapOf("status" to "complete"))
return mapOf("success" to "The submission was successfully completed.") return mapOf("success" to "The submission was successfully completed.")
} else if (!GroupSessionManager.dataComplete()) { } else if (!GroupSessionManager.dataComplete()) {
@ -194,7 +208,7 @@ class GroupSessionController {
Thread.sleep(1_000) // Sleep for 1 second Thread.sleep(1_000) // Sleep for 1 second
} }
if (ipv4 != null && this::environment.isInitialized) if (ipv4 != null && this::environment.isInitialized)
return mapOf("ip" to ipv4, "port" to environment["local.server.port"]) return mapOf("ip" to ipv4, "port" to environment["local.server.port"]!!)
else if (ipv4 == null) { else if (ipv4 == null) {
logger.error("IP Address could not be determined") logger.error("IP Address could not be determined")
return mapOf("error" to "Could not determine IP Address") return mapOf("error" to "Could not determine IP Address")

View File

@ -32,6 +32,7 @@ import uk.co.neviyn.observationdatabase.Site
import uk.co.neviyn.observationdatabase.SiteRepository import uk.co.neviyn.observationdatabase.SiteRepository
import uk.co.neviyn.observationdatabase.Tutor import uk.co.neviyn.observationdatabase.Tutor
import uk.co.neviyn.observationdatabase.TutorRepository import uk.co.neviyn.observationdatabase.TutorRepository
import uk.co.neviyn.observationdatabase.observationsToCSV
import javax.validation.Valid import javax.validation.Valid
@RestController @RestController
@ -229,24 +230,10 @@ class ObservationsController {
*/ */
@PostMapping("/observations/csv") @PostMapping("/observations/csv")
fun getObservationsCsvDump(@Valid @RequestBody observationsRequest: ObservationsRequest): String? { fun getObservationsCsvDump(@Valid @RequestBody observationsRequest: ObservationsRequest): String? {
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\""
}
val data = getObservations(observationsRequest) val data = getObservations(observationsRequest)
if (data.isEmpty()) return null if (data.isEmpty()) return null
logger.debug("Building CSV") logger.debug("Building CSV")
val builder = StringBuilder(csvHeaderString()) return observationsToCSV(data)
data.forEach {
builder.append('\n')
builder.append(it.toCsvFormat())
}
logger.debug("Returning constructed CSV")
return builder.toString()
} }
val afiPieThreshold = 3 val afiPieThreshold = 3