From b5968394d55f9299f465453b32e9487665194be4 Mon Sep 17 00:00:00 2001 From: Germaine Date: Mon, 16 Sep 2024 13:34:35 +0800 Subject: [PATCH] SCRUM-130 setPlaying false when user click continue --- backend/dist/services/clickstreamService.js | 29 ++++++++++--------- frontend/iQMA-Skills-Builder/app/Lesson.tsx | 6 ++-- .../app/SectionIntroduction.tsx | 6 ++-- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/backend/dist/services/clickstreamService.js b/backend/dist/services/clickstreamService.js index 630e600..5c424c4 100644 --- a/backend/dist/services/clickstreamService.js +++ b/backend/dist/services/clickstreamService.js @@ -20,32 +20,33 @@ const s3 = new aws_sdk_1.default.S3({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }); -function uploadToS3(queue, userID, newClickstream) { +function uploadToS3(queue, newClickstream) { return __awaiter(this, void 0, void 0, function* () { - const key = `${queue}/${userID}.json`; + const key = `${queue}/${newClickstream.userID}.json`; const params = { - Bucket: `isb-raw-data-athena`, + Bucket: 'isb-raw-data-athena', Key: key, }; + let existingClickstream = []; try { const existingData = yield s3.getObject(params).promise(); - let existingClickstream = JSON.parse(existingData.Body.toString('utf-8')); - if (Array.isArray(existingClickstream)) { - existingClickstream.push(JSON.parse(newClickstream)); - } - else { - existingClickstream = [existingClickstream, JSON.parse(newClickstream)]; - } - yield s3.putObject(Object.assign(Object.assign({}, params), { Body: JSON.stringify(existingClickstream), ContentType: "application/json" })).promise(); + let fileContent = existingData.Body.toString('utf-8'); + existingClickstream = fileContent + .split('\n') + .filter((line) => line.trim().length > 0) + .map((line) => JSON.parse(line)); } catch (error) { if (error.code === 'NoSuchKey') { - yield s3.upload(Object.assign(Object.assign({}, params), { Body: JSON.stringify([JSON.parse(newClickstream)]), ContentType: "application/json" })).promise(); + console.log("Creating new file"); } else { console.error("Error uploading to S3", error); } } + existingClickstream.push(newClickstream); + const lineDelimitedJson = existingClickstream.map(item => JSON.stringify(item)).join('\n'); + s3.putObject(Object.assign(Object.assign({}, params), { Body: lineDelimitedJson, ContentType: "application/json" })).promise(); }); } const QUEUE_NAMES = ['timeTaken', 'attemptsTaken']; @@ -59,9 +60,9 @@ function consumeMessage() { channel.consume(queue, (message) => __awaiter(this, void 0, void 0, function* () { if (message !== null) { const data = message.content.toString(); - const parsedData = JSON.parse(data); + let parsedData = JSON.parse(data); try { - yield uploadToS3(queue, parsedData.userID, data); + yield uploadToS3(queue, parsedData); channel.ack(message); console.log(message); } diff --git a/frontend/iQMA-Skills-Builder/app/Lesson.tsx b/frontend/iQMA-Skills-Builder/app/Lesson.tsx index fd7a467..fe7b34e 100644 --- a/frontend/iQMA-Skills-Builder/app/Lesson.tsx +++ b/frontend/iQMA-Skills-Builder/app/Lesson.tsx @@ -35,6 +35,7 @@ export default function Lesson() { }, [navigation]); const handlePress = () => { + setPlaying(false); router.push({ pathname: 'VideoQuiz', // params: {sectionID: sectionID, unitID: unitID, lessonID: '1a'}, @@ -75,15 +76,12 @@ export default function Lesson() { }, [sectionID, unitID]); const onStateChange = (state: string) => { - if (state === 'ended') { + if (state === 'ended' || state === 'paused') { setPlaying(false); } if (state === 'playing') { setPlaying(true); } - if (state === 'paused') { - setPlaying(false); - } }; return ( diff --git a/frontend/iQMA-Skills-Builder/app/SectionIntroduction.tsx b/frontend/iQMA-Skills-Builder/app/SectionIntroduction.tsx index c6d3826..d71b901 100644 --- a/frontend/iQMA-Skills-Builder/app/SectionIntroduction.tsx +++ b/frontend/iQMA-Skills-Builder/app/SectionIntroduction.tsx @@ -55,6 +55,7 @@ export default function SectionIntroduction() { const handlePress = () => { // router.push('UnitIntroduction'); + setPlaying(false); router.push({ pathname: 'UnitIntroduction', params: {sectionID, unitID, lessonID}, @@ -62,15 +63,12 @@ export default function SectionIntroduction() { }; const onStateChange = (state: string) => { - if (state === 'ended') { + if (state === 'ended' || state === 'paused') { setPlaying(false); } if (state === 'playing') { setPlaying(true); } - if (state === 'paused') { - setPlaying(false); - } }; return (