From 6f7e5596c362e67f9a68482e189ab6eedc5c3e59 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 25 Feb 2019 14:59:35 +0000 Subject: [PATCH] Group session data can now be submitted and shows in session log --- .../uk/co/neviyn/observationdatabase/Api.kt | 4 +- .../GroupSessionManager.kt | 18 +------ .../controller/GroupSessionController.kt | 29 +++++++++-- frontend/src/views/GroupSession.vue | 49 ++++++++++--------- frontend/src/views/GroupSessionInput.vue | 49 +++++++++++-------- frontend/src/views/Home.vue | 2 +- 6 files changed, 83 insertions(+), 68 deletions(-) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Api.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Api.kt index e3f72f7..602eb3e 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Api.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Api.kt @@ -33,8 +33,8 @@ data class GroupObservationInit( ) data class GroupObservation( - val scenarios: List, - val person: String + val person: String, + val scenarios: List ) data class ObservationsRequest( diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt index 61ade0d..2fc44fb 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt @@ -2,8 +2,6 @@ package uk.co.neviyn.observationdatabase import org.slf4j.Logger import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.stereotype.Service import java.util.concurrent.ThreadLocalRandom @@ -12,12 +10,9 @@ object GroupSessionManager { private val logger: Logger = LoggerFactory.getLogger(javaClass)!! - @Autowired - lateinit var websocketMessenger: SimpMessagingTemplate - var sessionId = ThreadLocalRandom.current().nextInt(1000, 9999) var site: Site? = null - var tutors: Set? = null + var tutors: List? = null var trainingType: TrainingType? = null var scenarioTitles: List? = null var observations: MutableList = mutableListOf() @@ -31,7 +26,7 @@ object GroupSessionManager { } logger.debug("New ID is $sessionId") this.site = site - this.tutors = tutors + this.tutors = tutors.map { it.id } this.trainingType = trainingType this.scenarioTitles = scenarioTitles observations = mutableListOf() @@ -49,14 +44,5 @@ object GroupSessionManager { fun addObservation(observation: Observation) { observations.add(observation) - sendObservationsToSocket() - } - - private fun sendObservationsToSocket() { - if (::websocketMessenger.isInitialized) { - websocketMessenger.convertAndSend("/ws/observations", mapOf("observations" to observations)) - } else { - logger.warn("WebSocket messenger is not initialized. Not sending data to socket.") - } } } \ No newline at end of file diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt index 7f92d10..0963400 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.core.env.Environment import org.springframework.core.env.get +import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.web.bind.annotation.CrossOrigin import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable @@ -17,6 +18,7 @@ import uk.co.neviyn.observationdatabase.GroupObservation import uk.co.neviyn.observationdatabase.GroupObservationInit import uk.co.neviyn.observationdatabase.GroupSessionManager import uk.co.neviyn.observationdatabase.Observation +import uk.co.neviyn.observationdatabase.ObservationRepository import uk.co.neviyn.observationdatabase.Person import uk.co.neviyn.observationdatabase.PersonRepository import uk.co.neviyn.observationdatabase.SiteRepository @@ -39,9 +41,11 @@ class GroupSessionController { @Autowired lateinit var tutorRepository: TutorRepository @Autowired - lateinit var observationsController: ObservationsController + lateinit var observationRepository: ObservationRepository @Autowired lateinit var personRepository: PersonRepository + @Autowired + lateinit var websocketMessenger: SimpMessagingTemplate /** * Start a new Group Observation session @@ -95,11 +99,27 @@ class GroupSessionController { return GroupSessionManager.isValid() } + /** + * Save an Observation to the database + */ + fun saveObservation(observation: Observation): Observation { + logger.debug("Saving new Observation to database") + val committedObservation = observationRepository.save(observation) + logger.debug("Adding Observation data to Tutor records") + committedObservation.tutors.forEach { + it.observations.add(committedObservation) + tutorRepository.save(it) + } + logger.debug("Observation addition completed") + return committedObservation + } + /** * Submit an observation to be added to the session state and actual database */ @PostMapping("/submit") - fun addGroupObservation(observationData: GroupObservation) { + fun addGroupObservation(@Valid @RequestBody observationData: GroupObservation) { + val tutors = tutorRepository.findAllById(GroupSessionManager.tutors!!).toSet() if (GroupSessionManager.isValid()) { var observation = Observation( site = GroupSessionManager.site!!, @@ -115,11 +135,12 @@ class GroupSessionController { teamworkWorkload = observationData.scenarios.map { it.teamworkWorkload.rating }.average(), knowledge = observationData.scenarios.map { it.knowledge.rating }.average(), scenarios = observationData.scenarios, - tutors = GroupSessionManager.tutors!!, + tutors = tutors, person = personRepository.findFirstByNameLike(observationData.person.toUpperCase()) ?: personRepository.save(Person(name = observationData.person.toUpperCase())) ) - observation = observationsController.saveObservation(observation) + observation = saveObservation(observation) GroupSessionManager.addObservation(observation) + websocketMessenger.convertAndSend("/ws/observations", mapOf("observations" to GroupSessionManager.observations)) } } diff --git a/frontend/src/views/GroupSession.vue b/frontend/src/views/GroupSession.vue index d4b6253..aad1462 100644 --- a/frontend/src/views/GroupSession.vue +++ b/frontend/src/views/GroupSession.vue @@ -8,6 +8,11 @@ {{ qrdata }}

+ + + {{ item.person.name }} + + @@ -118,7 +123,7 @@ export default { active: false, stompclient: null, qrdata: "N/A", - data: [], + data: [{person:{name:"No data yet received."}}], site: null, tutors: null, siteOptions: [], @@ -176,7 +181,7 @@ export default { ) .then(function(response) { console.log(response); - if ('error' in response.data) { + if ("error" in response.data) { } else { let rdata = response.data; self.qrdata = `http://${rdata.ip}:${rdata.port}/#/groupsession/${ @@ -185,19 +190,18 @@ export default { self.active = true; self.stompclient = webstomp.over( new SockJS("http://127.0.0.1:8080/websocket", { - heartbeat: false + heartbeat: true }) ); - self.stompclient.connect( - [], - function() { - self.stompclient.subscribe("/ws/observations", function( - incomingData - ) { - self.data = incomingData.content.observations; - }); - } - ); + self.stompclient.connect([], function() { + self.stompclient.subscribe("/ws/observations", function( + incomingData + ) { + self.data = JSON.parse(incomingData.body).observations; + console.log("data #") + console.log(self.data) + }); + }); self.$refs.submissionModal.hide(); self.clearPassword(); } @@ -213,7 +217,7 @@ export default { .get("/grpob/recover") .then(function(response) { console.log(response); - if ('error' in response.data) { + if ("error" in response.data) { } else { let rdata = response.data; self.data = rdata.observations; @@ -228,16 +232,13 @@ export default { heartbeat: false }) ); - self.stompclient.connect( - [], - function() { - self.stompclient.subscribe("/ws/observations", function( - incomingData - ) { - self.data = incomingData.content.observations; - }); - } - ); + self.stompclient.connect([], function() { + self.stompclient.subscribe("/ws/observations", function( + incomingData + ) { + self.data = JSON.parse(incomingData.body).observations; + }); + }); self.$refs.submissionModal.hide(); self.clearPassword(); } diff --git a/frontend/src/views/GroupSessionInput.vue b/frontend/src/views/GroupSessionInput.vue index 54cf40d..3a72b21 100644 --- a/frontend/src/views/GroupSessionInput.vue +++ b/frontend/src/views/GroupSessionInput.vue @@ -44,7 +44,7 @@ - +
Monitoring
@@ -75,7 +75,7 @@
- +
Control Procedural
@@ -106,9 +106,7 @@
-
- - +
Control
@@ -139,7 +137,7 @@
- +
Conservatism
@@ -170,9 +168,7 @@
-
- - +
Teamwork Communications
@@ -203,7 +199,7 @@
- +
Teamwork Leadership
@@ -234,9 +230,7 @@
-
- - +
Teamwork Workload
@@ -267,7 +261,7 @@
- +
Knowledge
@@ -305,7 +299,7 @@ Submit - +

Once submitted, data cannot be changed.

Are you sure you wish to submit?

@@ -411,15 +405,17 @@ export default { showModal() { this.$refs.submissionModal.show(); }, - handleSubmit() { + actuallySubmit() { + var self = this; + var payload = { + person: self.participant, + scenarios: self.scenarios + } var form = document.getElementById("submission-form"); if (form.checkValidity()) { - var self = this; + console.log(payload) Vue.axios - .post("/grpob/submit", { - person: self.participant, - scenarios: JSON.parse(JSON.stringify(self.scenarios)) - }) + .post("/grpob/submit", payload) .then(function(response) { self.complete = true; console.log(response); @@ -434,3 +430,14 @@ export default { } }; + diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue index cf1503a..2782d23 100644 --- a/frontend/src/views/Home.vue +++ b/frontend/src/views/Home.vue @@ -22,7 +22,7 @@ - Start a Group Session Coming Soon! + Start a Group Session