mirror of
https://github.com/karma-riuk/crab-webapp.git
synced 2025-07-05 14:18:12 +02:00
created a websocket to witness the progress of the processing
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import { Router } from "express";
|
||||
import multer from "multer";
|
||||
import { InvalidJsonFormatError } from "../utils/errors.js";
|
||||
import { io as socket } from "../socket.js";
|
||||
import { evaluate_comments } from "../utils/process_data.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@ -72,8 +74,9 @@ router.post("/submit/comments", upload.single("file"), async (req, res) => {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// TODO: Save the file or process it further
|
||||
// For now, just return success
|
||||
socket.emit("started-processing");
|
||||
evaluate_comments(validatedData);
|
||||
|
||||
res.status(200).json({
|
||||
message: "Answer submitted successfully",
|
||||
data: validatedData,
|
||||
@ -105,8 +108,8 @@ router.post("/submit/refinement", upload.single("file"), async (req, res) => {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// TODO: Save the file or process it further
|
||||
// For now, just return success
|
||||
socket.emit("started-processing");
|
||||
evaluate_comments(validatedData);
|
||||
res.status(200).json({
|
||||
message: "Answer submitted successfully",
|
||||
data: validatedData,
|
||||
|
@ -2,7 +2,7 @@ import express, { json } from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
import routes from "./routes/index.js";
|
||||
import createSocketServer from "./socket.js";
|
||||
import { createSocketServer } from "./socket.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
17
src/socket.js
Normal file
17
src/socket.js
Normal file
@ -0,0 +1,17 @@
|
||||
import http from "http";
|
||||
import { Server } from "socket.io";
|
||||
|
||||
function onConnect(socket) {
|
||||
console.log("Websocket client connected:", socket.id);
|
||||
}
|
||||
|
||||
let io;
|
||||
|
||||
function createSocketServer(app) {
|
||||
const httpServer = http.createServer(app);
|
||||
io = new Server(httpServer);
|
||||
io.on("connection", onConnect);
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
export { createSocketServer, io };
|
29
src/utils/process_data.js
Normal file
29
src/utils/process_data.js
Normal file
@ -0,0 +1,29 @@
|
||||
import { io as socket } from "../socket.js";
|
||||
|
||||
export const evaluate_comments = async (answers) => {
|
||||
const total = Object.keys(answers).length;
|
||||
let i = 0;
|
||||
for (const [key, value] of Object.entries(answers)) {
|
||||
console.log(`Processing ${key}: ${value}...`);
|
||||
await new Promise((res) => setTimeout(res, 500));
|
||||
console.log("Done");
|
||||
const data = {
|
||||
percent: Math.floor((++i / total) * 100),
|
||||
};
|
||||
socket.emit("progress", data);
|
||||
}
|
||||
};
|
||||
|
||||
export const evaluate_refinement = async (answers) => {
|
||||
const total = Object.keys(answers).length;
|
||||
let i = 0;
|
||||
for (const [key, value] of Object.entries(answers)) {
|
||||
console.log(`Processing ${key}: ${value}...`);
|
||||
await new Promise((res) => setTimeout(res, 500));
|
||||
console.log("Done");
|
||||
const data = {
|
||||
percent: Math.floor((++i / total) * 100),
|
||||
};
|
||||
socket.emit("progress", data);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user