From 3cca7a6bd014522ffbaa9a71d685d5a09acf6f32 Mon Sep 17 00:00:00 2001 From: rrachea Date: Sun, 20 Oct 2024 15:46:17 +0800 Subject: [PATCH] added chat response time --- .../components/MiniChatbot.tsx | 39 ++++++++++-------- .../helpers/chatInteractions.tsx | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/frontend/iQMA-Skills-Builder/components/MiniChatbot.tsx b/frontend/iQMA-Skills-Builder/components/MiniChatbot.tsx index d721ff9..c8c8d9c 100644 --- a/frontend/iQMA-Skills-Builder/components/MiniChatbot.tsx +++ b/frontend/iQMA-Skills-Builder/components/MiniChatbot.tsx @@ -1,4 +1,5 @@ import * as chatInputFunctions from '@/components/ChatInput'; +import * as chatInteractionsEndpoints from '@/helpers/chatInteractions'; import React, {useContext, useEffect, useRef, useState} from 'react'; import {ScrollView, StyleSheet, View} from 'react-native'; @@ -132,9 +133,15 @@ const MiniChatbot: React.FC = ({ if (response) { const end = Date.now(); const timeTaken = end - start; + // track time taken for chatbot to respond (rabbitmq) setResponseTime(timeTaken); console.log(`Time taken for chatbot to respond: ${timeTaken}ms`); - await sendToRabbitMQ(timeTaken); + // await sendToRabbitMQ(timeTaken); + await chatInteractionsEndpoints.chatResponseTime( + sectionID, + unitID, + timeTaken + ); // Add the chatbot response to the chat const botReply = {text: response.content, isUser: false}; @@ -158,21 +165,21 @@ const MiniChatbot: React.FC = ({ } }; - const sendToRabbitMQ = async (timeTaken: number) => { - try { - await fetch(`${process.env.EXPO_PUBLIC_LOCALHOST_URL}/rabbitmq`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - timeTaken: timeTaken, - }), - }); - } catch (error) { - console.error('Error sending time to RabbitMQ:', error); - } - }; + // const sendToRabbitMQ = async (timeTaken: number) => { + // try { + // await fetch(`${process.env.EXPO_PUBLIC_LOCALHOST_URL}/rabbitmq`, { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/json', + // }, + // body: JSON.stringify({ + // timeTaken: timeTaken, + // }), + // }); + // } catch (error) { + // console.error('Error sending time to RabbitMQ:', error); + // } + // }; useEffect(() => { if (sectionID && unitID) { diff --git a/frontend/iQMA-Skills-Builder/helpers/chatInteractions.tsx b/frontend/iQMA-Skills-Builder/helpers/chatInteractions.tsx index 11e9a09..e37fb0f 100644 --- a/frontend/iQMA-Skills-Builder/helpers/chatInteractions.tsx +++ b/frontend/iQMA-Skills-Builder/helpers/chatInteractions.tsx @@ -42,3 +42,43 @@ export const chatInteractions = async ( } } }; + +export const chatResponseTime = async ( + section: string, + unit: string, + duration: number +) => { + const userID = await AsyncStorage.getItem('userID'); + let age = await AsyncStorage.getItem('age'); + + if (age === null) { + try { + const ageResponse = await axios.get( + `${process.env.EXPO_PUBLIC_LOCALHOST_URL}/accounts/getaccountbyid/${userID}` + ); + await AsyncStorage.setItem('age', ageResponse.data['age']); + } catch (e) { + console.error(e); + } + } else { + try { + const response = await axios.post( + `${process.env.EXPO_PUBLIC_LOCALHOST_URL}/clickstream/sendMessage`, + { + userID: userID, + age: age, + eventType: 'chatResponseTime', + section: section, + event: `Section: ${section}, Unit: ${unit} Chat Response Time`, + timestamp: new Date().toISOString(), + duration: duration, + } + ); + console.log(response.data); + console.log('chat response time event type'); + console.log('added chat response time:', duration); + } catch (e) { + console.error(e); + } + } +};