Skip to content

Commit

Permalink
Merge pull request #50 from Killerrekt/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Emerald-Wolf authored Sep 22, 2023
2 parents 6c03d22 + 9a21a0f commit 37976c9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
93 changes: 68 additions & 25 deletions api/controllers/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require("dotenv").config();
const Judge0 = process.env.JUDGE_URI;

class submission {
async create(req, user, score, max, result) {
async create(req, user, score, max, result,time) {
//console.log(result);
const { language_id, code, question_id } = req.body;
const check = await submission_db.findOne({
Expand All @@ -26,7 +26,8 @@ class submission {
score: score,
max_score: max,
lastResults: result,
},
runtime : time
}
)
.then(() => "Submission record has been updated")
.catch(() => "Error faced during updating the sub DB");
Expand All @@ -40,6 +41,7 @@ class submission {
score: score,
max_score: max,
lastResults: result,
runtime : time
})
.then(() => "Submission record has been saved")
.catch((err) => "Error faced during creating the entry");
Expand Down Expand Up @@ -76,7 +78,7 @@ class submission {
}
const check = await submission_db.findOne(
{ regNo: reg_no, question_id: question_id },
"code score lastResults allPassesAt",
"code score lastResults allPassesAt"
);
//console.log(!check.allPassesAt);
if (check && check.code == code) {
Expand Down Expand Up @@ -144,11 +146,10 @@ class submission {
language_id: language_id,
stdin: Buffer.from(current.input, "binary").toString("base64"),
expected_output: Buffer.from(current.expectedOutput, "binary").toString(
"base64",
"base64"
),
cpu_time_limit: current.time * multipler < 15
? current.time * multipler
: 15,
cpu_time_limit:
current.time * multipler < 15 ? current.time * multipler : 15,
//redirect_stderr_to_stdout: true,
});
const group = current.group;
Expand Down Expand Up @@ -176,7 +177,7 @@ class submission {
header: {
"Content-Type": "application/JSON",
},
},
}
)
.then((response) => response.data)
.catch((err) => {
Expand All @@ -192,10 +193,11 @@ class submission {
tokens.forEach((element) => {
str.push(element.token);
});
const url = Judge0 +
const url =
Judge0 +
"/submissions/batch?tokens=" +
str.toString() +
"&base64_encoded=true&fields=status_id,stderr,compile_output,expected_output,stdout";
"&base64_encoded=true&fields=status_id,stderr,compile_output,expected_output,stdout,time";
console.log(url);
let completion = false;
let data_sent_back = {
Expand All @@ -205,6 +207,7 @@ class submission {
Score: "",
};
while (!completion) {
let runtime = 0;
let score = 0;
completion = true;
let failed = [];
Expand All @@ -220,20 +223,21 @@ class submission {
break;
case 3:
//console.log(Buffer.from(element.expected_output,"base64").toString("utf-8"));
runtime += parseFloat(element.time);
break;
case 4:
//console.log(Buffer.from(element.expected_output,"base64").toString("utf-8"));
//console.log(Buffer.from(element.stdout,"base64").toString("utf-8"));
if (
Buffer.from(element.stdout, "base64").toString("utf-8") + "\n" ==
Buffer.from(element.expected_output, "base64").toString(
"utf-8",
"utf-8"
) ||
Buffer.from(element.stdout, "base64").toString("utf-8") ==
Buffer.from(element.expected_output, "base64").toString("utf-8")
) {
continue;
} else {
)
runtime += parseFloat(element.time);
else {
data_sent_back.error[3] = true;
failed.push(i);
}
Expand All @@ -260,6 +264,11 @@ class submission {
break;
}
}
//console.log(runtime);
runtime = runtime/(tests.length-failed.length);
//console.log(tests.length,failed.length);
runtime = runtime/multipler;
//console.log("runtime = ", runtime);
if (completion) {
//Checking whether complilation error or runtime error
if (failed.length != tests.length && data_sent_back.error[0]) {
Expand All @@ -269,11 +278,12 @@ class submission {

//In case of complilation error the following code will run
if (data_sent_back.error[0]) {
const msg = result[0].compile_output != null
? Buffer.from(result[0].compile_output, "base64").toString(
"utf-8",
)
: Buffer.from(result[0].stderr, "base64").toString("utf-8");
const msg =
result[0].compile_output != null
? Buffer.from(result[0].compile_output, "base64").toString(
"utf-8"
)
: Buffer.from(result[0].stderr, "base64").toString("utf-8");
data_sent_back.Sub_db = "Not saved in Sub DB(complilation error)";
res.status(201).json({
error: data_sent_back.error,
Expand All @@ -298,9 +308,8 @@ class submission {
});

//comparing the score with the existing score and picking the best one
data_sent_back.Score = !check || score >= check.score
? score
: check.score;
data_sent_back.Score =
!check || score >= check.score ? score : check.score;
console.log(data_sent_back.Score, score);

//If new score is higher or equal to the existing score, then sub DB is updated
Expand All @@ -311,6 +320,7 @@ class submission {
score,
Object.keys(grp).length,
data_sent_back.error,
runtime
);
await this.create_score(reg_no);
} else {
Expand Down Expand Up @@ -382,10 +392,10 @@ class submission {
}

async get_all(req, res) {
const { regno } = req.params;
const { regno } = req.body;
const record = await submission_db.find(
{ regNo: regno },
"code score question_id lastResults",
"code score question_id lastResults"
);
console.log(record);
if (record.length == 0) {
Expand Down Expand Up @@ -424,7 +434,7 @@ class submission {
regNo: user,
question_id: question_id,
},
{ allPassesAt: curr_time },
{ allPassesAt: curr_time }
)
.then(() => "Updated all testcases timestamp")
.catch(() => "Failed to update the timestamp");
Expand All @@ -447,5 +457,38 @@ class submission {
return res.status(500).json({ status: false, error: error });
}
}

async round_lb(req,res){
const {round} = req.params;
const QID = await questiondb.find({round : round},"_id");
let question = [];
QID.forEach((ele) => question.push(ele._id.toString()));
const all_submit = await submission_db.find({question_id : {"$in" : question}}
,"regNo score question_id runtime allPassesAt");
let leaderboard = {};
all_submit.forEach((ele) => {
if (ele.regNo in leaderboard) {
let data = leaderboard[ele.regNo];
data[0] += ele.score;
data[1] = data[1]>ele.allPassesAt?ele.allPassesAt:data[1];
data[2] += ele.runtime;
//console.log(data);
leaderboard[ele.regNo] = data;
} else {
leaderboard[ele.regNo] = [ele.score,ele.allPassesAt,ele.runtime];
}
});
let items = Object.keys(leaderboard).map(function(key) {
return [key, leaderboard[key]];
});
items.sort(function(a,b){
console.log(a[1][0]);
if(a[1][0]>b[1][0]) return -1;
if(a[1][0]<b[1][0]) return 1;
if(a[1][1]>b[1][1]) return 1;
if(a[1][1]<b[1][1]) return -1;
});
res.status(200).json(items);
}
}
module.exports = submission;
1 change: 1 addition & 0 deletions api/models/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const submission_schema = new Schema({
score: { type: Number },
lastResults: [{ type: Boolean }],
allPassesAt: { type: Date },
runtime: {type: Number, required: true}
}, { timestamps: true });

const submission_db = model("Submissions", submission_schema);
Expand Down
5 changes: 5 additions & 0 deletions api/routes/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ router.get("/endtest", verifyAccessToken, (req, res) => {
submit.endtest(req, res);
});

router.get("/round_lb/:round", verifyAdminToken, (req, res)=> {
submit.round_lb(req,res);
});


module.exports = router;

0 comments on commit 37976c9

Please sign in to comment.