-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from FYP-2024-IQMA/SCRUM-164-Update-feedback-…
…details-to-S3 Scrum 164 update feedback details to s3
- Loading branch information
Showing
11 changed files
with
480 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# chromadb | ||
fastapi | ||
# langchain | ||
# langchain-chroma | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Request, Response } from "express"; | ||
import * as feedbackService from "../services/feedbackService"; | ||
import handleError from "../errors/errorHandling"; | ||
|
||
export const sendFeedback = async (req: Request, res: Response) => { | ||
const messageBody = req.body; | ||
|
||
try { | ||
const message = await feedbackService.sendMessage(messageBody); | ||
res.status(200).json({ message: 'Published message successfully' }); | ||
} catch (error: any) { | ||
const errorResponse = handleError(error); | ||
if (errorResponse) { | ||
res.status(errorResponse.status).json(errorResponse); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Feedback { | ||
userID: string; | ||
timestamp: Date; | ||
eventType: "feedback" | "bug" | "sugestion"; | ||
rating: number; | ||
message: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"timeTaken": 1818 | ||
}, | ||
{ | ||
"timeTaken": 1718 | ||
}, | ||
{ | ||
"feedback": 1618 | ||
}, | ||
{ | ||
"bug": 1518 | ||
}, | ||
{ | ||
"suggestion": 1418 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as feedbackController from '../controllers/feedbackController'; | ||
import { Router } from 'express'; | ||
import verifyToken from '../middleware/authMiddleware'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/sendFeedback', verifyToken, feedbackController.sendFeedback); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import amqp from "amqplib"; | ||
import { Feedback } from "../models/feedbackModel"; | ||
import { s3 } from "../config/awsConfig"; // Assuming you already have an AWS config for S3 | ||
|
||
// Create upload function | ||
async function uploadToS3(queue: string, newFeedback: Feedback) { | ||
const key = `${queue}/${newFeedback.userID}.json`; | ||
const params = { | ||
Bucket: "isb-raw-data-athena", | ||
Key: key, | ||
}; | ||
let existingFeedback: any[] = []; | ||
|
||
try { | ||
const existingData = await s3.getObject(params).promise(); | ||
let fileContent = existingData.Body!.toString("utf-8"); | ||
existingFeedback = fileContent | ||
.split("\n") | ||
.filter((line: string) => line.trim().length > 0) | ||
.map((line: string) => JSON.parse(line)); | ||
} catch (error: any) { | ||
if (error.code === "NoSuchKey") { | ||
console.log("Creating new file"); | ||
} else { | ||
console.error("Error uploading to S3", error); | ||
} | ||
} | ||
|
||
existingFeedback.push(newFeedback); | ||
const lineDelimitedJson = existingFeedback | ||
.map((item) => JSON.stringify(item)) | ||
.join("\n"); | ||
s3.putObject({ | ||
...params, | ||
Body: lineDelimitedJson, | ||
ContentType: "application/json", | ||
}).promise(); | ||
} | ||
|
||
// Define your queues (you can add more if necessary) | ||
const QUEUE_NAMES = ["feedback", "bug", "suggestion"]; | ||
|
||
// Create a function to consume messages | ||
async function consumeMessage() { | ||
try { | ||
const conn = await amqp.connect(process.env.RABBITMQ_URL!); | ||
const channel = await conn.createChannel(); | ||
|
||
for (const queue of QUEUE_NAMES) { | ||
await channel.assertQueue(queue); | ||
channel.consume(queue, async (message) => { | ||
if (message !== null) { | ||
const data = message.content.toString(); | ||
let parsedData: Feedback = JSON.parse(data); | ||
try { | ||
await uploadToS3(queue, parsedData); | ||
channel.ack(message); | ||
console.log(message) | ||
} catch (error) { | ||
console.error(`Error processing message from ${QUEUE_NAMES[0]}: `, error); | ||
channel.nack(message); | ||
} | ||
} | ||
}) | ||
} | ||
} catch (err) { | ||
console.error(`Error: ${err}`); | ||
} | ||
} | ||
|
||
export async function sendMessage(feedback: Feedback) { | ||
const queue = feedback.eventType; | ||
const conn = await amqp.connect(process.env.RABBITMQ_URL!); | ||
const channel = await conn.createChannel(); | ||
await channel.assertQueue(queue); | ||
channel.sendToQueue(queue, Buffer.from(JSON.stringify(feedback))); | ||
await consumeMessage(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.