Skip to content

Commit

Permalink
Merge pull request #74 from Eclipse-Dominator/link_execution_w_submis…
Browse files Browse the repository at this point in the history
…sion

Link execution w submission
  • Loading branch information
cheehongw authored Nov 15, 2023
2 parents f2a8240 + 9c1f030 commit 27390ba
Show file tree
Hide file tree
Showing 28 changed files with 580 additions and 424 deletions.
2 changes: 1 addition & 1 deletion api_gateway/src/proxy/service_addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const questionServiceUrl = isLocal ? "http://localhost:8080" : "http://qu
export const userServiceHostUrl = isLocal ? "http://localhost:8081" : "http://user-service:8081/";
export const matchServiceHostUrl = isLocal ? "http://localhost:8082" : "http://matching-service:8082"
export const collabServiceHostUrl = isLocal ? "http://localhost:8083" : "http://collab-service:8083"
export const executionServiceHostUrl = isLocal ? "http://localhost:8090" : "http://code-execution:8090";
export const executionServiceHostUrl = isLocal ? "http://localhost:8090" : "http://code-execution:8090/";
16 changes: 7 additions & 9 deletions code_execution/src/executor_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import axios from "axios";
import { createBatchSubmission, getQnStdInOut } from "./testcases";
import { callbacks, langToId } from "./shared";
import { judge0Result, judge0submission, submissionResult } from "./types";
import { submitSubmission } from "./submission_client";

const JUDGE_API_URL = "https://judge0-ce.p.rapidapi.com"; //"http://judge0-server:2358/submissions";
const JUDGE_API_URL = "http://peerprep-g10.com:2358"; //"https://judge0-ce.p.rapidapi.com";
const API_KEY = process.env.JUDGE0_API_KEY;

async function submitIndividual(submission: judge0submission) {
Expand All @@ -13,7 +14,6 @@ async function submitIndividual(submission: judge0submission) {
{
headers: {
"Content-Type": "application/json",
"X-Rapidapi-Key": API_KEY,
},
params: { base64_encoded: "true" },
}
Expand All @@ -29,7 +29,6 @@ async function submitCode(submission: judge0submission[]) {
{
headers: {
"Content-Type": "application/json",
"X-Rapidapi-Key": API_KEY,
},
params: { base64_encoded: "true" },
}
Expand All @@ -52,9 +51,6 @@ function pollIndividualTillComplete(
) {
setTimeout(() => {
const options = {
headers: {
"X-Rapidapi-Key": API_KEY,
},
params: {
fields: "status",
},
Expand Down Expand Up @@ -114,7 +110,6 @@ async function delSubmissions(tokens: string[]) {
params: { fields: "status_id" },
headers: {
"X-Judge0-User": "Auth-Judge0-User",
"X-Rapidapi-Key": API_KEY,
},
})
)
Expand Down Expand Up @@ -155,8 +150,9 @@ export async function runSubmission(
lang: string,
qn__id: string,
source_code: string,
userid: string
userid: number
) {
const submissionTime = new Date();
const resDat: submissionResult = {
completed: false,
evaluated: 0,
Expand Down Expand Up @@ -190,4 +186,6 @@ export async function runSubmission(

resDat.verdict = resDat.verdict === "Unknown" ? "Accepted" : resDat.verdict;
resDat.completed = true;
}

submitSubmission(resDat.verdict, lang, qn__id, userid, source_code, submissionTime); // runs in the bg
}
2 changes: 1 addition & 1 deletion code_execution/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ app.get("/api/code/result/:token", async (req, res) => {
return res.status(400).json({ error: "id not found" });
}

res.json(response);
res.status(200).json(response);
} catch (error) {
console.error(error);
res.status(500).json({ error: "An error occurred" });
Expand Down
6 changes: 3 additions & 3 deletions code_execution/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const callbacks: callbackMapping = {};

export const langToId: { [key: string]: number } = {
"c++17": 76,
"python3": 92,
"java": 91,
}
python3: 71,
java: 62,
};
45 changes: 45 additions & 0 deletions code_execution/src/submission_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import axios from "axios";
import { judge0Result, submissionResult } from "./types";

type Question = {
id: number;
_id: string;
title: string;
description: string;
topics: string[];
difficulty: number;
};

const fetchQn = async (id: string) => {
const response = await axios.get<Question>(
`http://question-service:8080/api/questions/${id}`
);
return response.data;
};

export const submitSubmission = async (
verdict: string,
language: string,
qid: string,
userId: number,
sourceCode: string,
time: Date
) => {
const question = await fetchQn(qid);
const submission = {
question__id: question._id,
userId: userId,
questionTitle: question.title,
questionId: question.id,
difficulty: question.difficulty,
topics: question.topics,
verdict: verdict,
sourceCode: sourceCode,
language: language,
answeredAt: time.toISOString(),
};
await axios.post(
`http://user-service:8081/api/users/${userId}/addquestions`,
submission
);
};
2 changes: 1 addition & 1 deletion frontend/src/api/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function submitCodeForExecution(data_payload: any) {
}

export async function getExecutionResult(token : string) {
const res = await apiGatewayClient.post(`/api/code/result/${token}`);
const res = await apiGatewayClient.get(`/api/code/result/${token}`);

return res;
}
104 changes: 54 additions & 50 deletions frontend/src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { apiGatewayClient } from "./gateway";
* @returns An array of completed questions.
*/
export async function fetchUserCompletedQuestions(
userId: String
): Promise<SolvedQuestion[]> {
userId: number
): Promise<SolvedQuestion[]> {
try {
const response: AxiosResponse = await apiGatewayClient.get(
`/api/users/${userId}/questions`
Expand All @@ -19,14 +19,16 @@ export async function fetchUserCompletedQuestions(
const completedQuestions: SolvedQuestion[] = resData.map(
(q: any) =>
new SolvedQuestion(
q._id,
q.id,
q.title,
q.description,
q.question__id,
q.questionId,
q.questionTitle,
"", // not impt
q.topics,
q.difficulty,
false, // Default value for solved
undefined // Default value for solvedDate
q.verdict,
q.sourceCode,
q.language,
q.answeredAt // Default value for solvedDate
)
);

Expand All @@ -38,50 +40,52 @@ export async function fetchUserCompletedQuestions(
}
}

/**
* Add a new question to the user's completed questions.
* @param userId - The ID of the user to whom you want to add the question.
* @param questionId - The ID of the question you want to add.
* @param complexity - The complexity of the question.
* @param category - The category of the question.
* @returns The added question.
*/
export async function addUserQuestion(
userId: number,
questionId: string,
complexity: number,
category: string[]
): Promise<SolvedQuestion> {
try {
const response: AxiosResponse = await apiGatewayClient.post(
`/api/users/${userId}/addquestions`,
{
userId,
questionId,
complexity,
category,
}
);
// /**
// * Add a new question to the user's completed questions.
// * @param userId - The ID of the user to whom you want to add the question.
// * @param questionId - The ID of the question you want to add.
// * @param complexity - The complexity of the question.
// * @param category - The category of the question.
// * @returns The added question.
// */
// export async function addUserQuestion(
// userId: number,
// questionId: string,
// complexity: number,
// category: string[]
// ): Promise<SolvedQuestion> {
// try {
// const response: AxiosResponse = await apiGatewayClient.post(
// `/api/users/${userId}/addquestions`,
// {
// userId,
// questionId,
// complexity,
// category,
// }
// );

const resData = response.data;
const addedQuestion: SolvedQuestion = new SolvedQuestion(
resData._id,
resData.id,
resData.title,
resData.description,
resData.topics,
resData.difficulty,
false, // Default value for solved
undefined // Default value for solvedDate
);
// const resData = response.data;
// const addedQuestion: SolvedQuestion = new SolvedQuestion(
// resData._id,
// resData.id,
// resData.title,
// resData.description,
// resData.topics,
// resData.difficulty,
// resData.verdict,
// resData.sourceCode,
// resData.language,
// undefined // Default value for solvedDate
// );

return addedQuestion;
} catch (error) {
// Handle errors appropriately, e.g., log the error or throw it to be handled by the caller.
console.error(error);
throw error;
}
}
// return addedQuestion;
// } catch (error) {
// // Handle errors appropriately, e.g., log the error or throw it to be handled by the caller.
// console.error(error);
// throw error;
// }
// }


/**
Expand Down
32 changes: 9 additions & 23 deletions frontend/src/components/MatchMakeBtn/MatchMakeBtn.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ import { useMatchmake } from "../../contexts/matchmake.context";
import { AiOutlineDisconnect as DisconnectIcon } from "react-icons/ai";
import { useSelector } from "react-redux";
import { selectUser } from "../../reducers/authSlice";

const diffRange = [
[0, 2.9],
[3, 5.9],
[6, 7.9],
[8, 9.9],
[0, 9.9],
];
import { diffRanges } from "../../helper/DifficultyFilterHelper";

const MatchMakeBtn = () => {
const user = useSelector(selectUser);
Expand Down Expand Up @@ -79,21 +72,14 @@ const MatchMakeBtn = () => {
)}

<MenuList>
<MenuItem onClick={() => findMatch(diffRange[0][0], diffRange[0][1])}>
Basic
</MenuItem>
<MenuItem onClick={() => findMatch(diffRange[1][0], diffRange[1][1])}>
Simple
</MenuItem>
<MenuItem onClick={() => findMatch(diffRange[2][0], diffRange[2][1])}>
Medium
</MenuItem>
<MenuItem onClick={() => findMatch(diffRange[3][0], diffRange[3][1])}>
Hard
</MenuItem>
<MenuItem onClick={() => findMatch(diffRange[4][0], diffRange[4][1])}>
All
</MenuItem>
{diffRanges.map((dr) => (
<MenuItem
onClick={() => findMatch(dr.range[0], dr.range[1])}
id={dr.difficulty}
>
{dr.difficulty}
</MenuItem>
))}
</MenuList>
</Menu>
);
Expand Down
Loading

0 comments on commit 27390ba

Please sign in to comment.