Helped solve a bug where it would get stuck
This commit is contained in:
@@ -14,6 +14,10 @@ import {speakingReverseMarking} from "@/utils/score";
|
|||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
|
function delay(ms: number) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
@@ -46,7 +50,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const backendRequest = await evaluate({answers: uploadingAudios});
|
const backendRequest = await evaluate({answers: uploadingAudios});
|
||||||
console.log("🌱 - Process complete");
|
console.log("🌱 - Process complete");
|
||||||
|
|
||||||
const correspondingStat = (await getDoc(doc(db, "stats", fields.id))).data() as Stat;
|
const correspondingStat = await getCorrespondingStat(fields.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data, solution: uploadingAudios}));
|
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data, solution: uploadingAudios}));
|
||||||
await setDoc(
|
await setDoc(
|
||||||
doc(db, "stats", fields.id),
|
doc(db, "stats", fields.id),
|
||||||
@@ -65,6 +70,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
|
const correspondingStat = await getDoc(doc(db, "stats", id));
|
||||||
|
|
||||||
|
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
||||||
|
await delay(3 * 10000);
|
||||||
|
return getCorrespondingStat(id, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
|
async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
|
||||||
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_3`, body, {
|
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_3`, body, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ import {speakingReverseMarking} from "@/utils/score";
|
|||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
|
function delay(ms: number) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
@@ -38,7 +42,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const backendRequest = await evaluate({answers: [{question: fields.question, answer: path}]});
|
const backendRequest = await evaluate({answers: [{question: fields.question, answer: path}]});
|
||||||
console.log("🌱 - Process complete");
|
console.log("🌱 - Process complete");
|
||||||
|
|
||||||
const correspondingStat = (await getDoc(doc(db, "stats", fields.id))).data() as Stat;
|
const correspondingStat = await getCorrespondingStat(fields.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({
|
const solutions = correspondingStat.solutions.map((x) => ({
|
||||||
...x,
|
...x,
|
||||||
@@ -63,6 +67,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
|
const correspondingStat = await getDoc(doc(db, "stats", id));
|
||||||
|
|
||||||
|
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
||||||
|
await delay(3 * 10000);
|
||||||
|
return getCorrespondingStat(id, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
|
async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
|
||||||
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_3`, body, {
|
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_3`, body, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ interface Body {
|
|||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function delay(ms: number) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -29,7 +33,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const backendRequest = await evaluate(req.body as Body);
|
const backendRequest = await evaluate(req.body as Body);
|
||||||
console.log("🌱 - Process complete");
|
console.log("🌱 - Process complete");
|
||||||
|
|
||||||
const correspondingStat = (await getDoc(doc(db, "stats", req.body.id))).data() as Stat;
|
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
|
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
|
||||||
await setDoc(
|
await setDoc(
|
||||||
doc(db, "stats", (req.body as Body).id),
|
doc(db, "stats", (req.body as Body).id),
|
||||||
@@ -47,6 +52,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
console.log("🌱 - Updated the DB");
|
console.log("🌱 - Updated the DB");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
|
const correspondingStat = await getDoc(doc(db, "stats", id));
|
||||||
|
|
||||||
|
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
||||||
|
await delay(3 * 10000);
|
||||||
|
return getCorrespondingStat(id, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
async function evaluate(body: Body): Promise<AxiosResponse> {
|
async function evaluate(body: Body): Promise<AxiosResponse> {
|
||||||
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task2`, body as Body, {
|
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task2`, body as Body, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Reference in New Issue
Block a user