Skip to content

Commit

Permalink
added chat response time
Browse files Browse the repository at this point in the history
  • Loading branch information
rrachea committed Oct 20, 2024
1 parent 5dd4b57 commit 3cca7a6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
39 changes: 23 additions & 16 deletions frontend/iQMA-Skills-Builder/components/MiniChatbot.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -132,9 +133,15 @@ const MiniChatbot: React.FC<MiniChatbotProps> = ({
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};
Expand All @@ -158,21 +165,21 @@ const MiniChatbot: React.FC<MiniChatbotProps> = ({
}
};

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) {
Expand Down
40 changes: 40 additions & 0 deletions frontend/iQMA-Skills-Builder/helpers/chatInteractions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};

0 comments on commit 3cca7a6

Please sign in to comment.