-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc
More file actions
594 lines (517 loc) · 20.3 KB
/
abc
File metadata and controls
594 lines (517 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import { db } from '@/config/db';
import { submissions, practicals, students, users, prac_io, prac_language, courses, batch_practical_access, batch, courses_faculty } from '@/models/schema';
import { eq, and, gt } from 'drizzle-orm';
import { AppError } from '@/utils/errors';
import axios from 'axios';
import redis from '@/config/redis';
// import { createClient } from 'redis';
// const redis = new Redis(process.env.REDIS_URL || 'redis://localhost:6380');
const JUDGE0_API_URL = process.env.JUDGE0_API_URL || 'http://localhost:2358';
const SUBMISSION_TIMEOUT = 30000; // 30 seconds
const RESULTS_EXPIRY = 3600; // 1 hour in seconds
const SUBMISSION_RATE_LIMIT = 3; // 30 seconds between submissions
const RUN_RATE_LIMIT = 1; // 15 seconds between code runs
const SUBMISSION_BATCH_SIZE = 5; // Process submissions in batches
const BATCH_SIZE = 5; // Process submissions in batches
const MAX_POLL_ATTEMPTS = 6; // Maximum number of polling attempts
const POLL_INTERVAL = 5000; // 5 seconds between polls
interface SubmissionResult {
token: string;
input: string;
expectedOutput: string;
status?: string;
actualOutput?: string;
}
interface SubmissionResult {
token: string;
input: string;
expectedOutput: string;
status?: string;
actualOutput?: string;
}
export async function getSubmissionResults(submissionId: string) {
const redisKey = `submission:${submissionId}`;
const submissionData = await redis.get(redisKey);
if (!submissionData) {
throw new AppError(404, 'Submission not found');
}
const data = JSON.parse(submissionData);
return {
status: data.status,
testResults: data.results,
allPassed: data.status === 'completed' &&
data.results.every((result: any) => result.status === 'Accepted')
};
}
export async function getPracticalWithSubmissionStatus(courseId: number, studentId: number) {
const result = await db.select({
practical_id: practicals.practical_id,
sr_no: practicals.sr_no,
practical_name: practicals.practical_name,
description: practicals.description,
pdf_url: practicals.pdf_url,
status: submissions.status,
marks: submissions.marks,
deadline: batch_practical_access.deadline,
lock: batch_practical_access.lock,
})
.from(practicals)
.leftJoin(submissions, and(
eq(submissions.practical_id, practicals.practical_id),
eq(submissions.student_id, studentId)
))
.leftJoin(batch_practical_access, eq(batch_practical_access.practical_id, practicals.practical_id))
.leftJoin(students, eq(students.student_id, studentId))
.leftJoin(batch, eq(batch.batch_id, students.batch_id))
.where(and(
eq(practicals.course_id, courseId),
eq(batch_practical_access.batch_id, batch.batch_id)
));
return result;
}
export async function getSubmissionsByPractical(practicalId: number, batchId: number, facultyId: number) {
try {
const submissionsList = await db
.select({
submission_id: submissions.submission_id,
roll_id: students.roll_id,
student_name: users.username,
code: submissions.code_submitted,
status: submissions.status,
submission_time: submissions.submission_time,
marks: submissions.marks,
batch: students.batch_id
})
.from(submissions)
.innerJoin(students, eq(submissions.student_id, students.student_id))
.innerJoin(users, eq(students.student_id, users.user_id))
.where(and(
eq(submissions.practical_id, practicalId),
eq(students.batch_id, batchId)
));
return submissionsList;
} catch (error) {
console.error('Error in getSubmissionsByPractical:', error);
throw new AppError(500, 'Failed to fetch submissions for practical');
}
}
export async function getSubmissionById(submissionId: number) {
try {
const submission = await db
.select({
submission_id: submissions.submission_id,
practical_sr_no: practicals.sr_no,
practical_name: practicals.practical_name,
course_name: courses.course_name,
prac_io: prac_io.input,
submission_status: submissions.status,
code_submitted: submissions.code_submitted,
marks: submissions.marks,
student_name: users.username,
roll_id: students.roll_id,
submission_time: submissions.submission_time,
batch_name: batch.batch
})
.from(submissions)
.innerJoin(practicals, eq(submissions.practical_id, practicals.practical_id))
.innerJoin(prac_io, eq(practicals.practical_id, prac_io.practical_id))
.innerJoin(courses, eq(practicals.course_id, courses.course_id))
.innerJoin(students, eq(submissions.student_id, students.student_id))
.innerJoin(users, eq(students.student_id, users.user_id))
.innerJoin(batch, eq(students.batch_id, batch.batch_id))
.where(eq(submissions.submission_id, submissionId))
.limit(1);
return submission[0];
} catch (error) {
console.error('Error in getSubmissionById:', error);
throw new AppError(500, 'Failed to fetch submission');
}
}
export async function updateSubmission(submissionId: number, updateData: { status: string; marks: number }) {
try {
await db.update(submissions)
.set({
// @ts-ignore
status: updateData.status,
marks: updateData.marks,
})
.where(eq(submissions.submission_id, submissionId));
// Fetch the updated submission to return
const updatedSubmission = await getSubmissionById(submissionId);
return updatedSubmission;
} catch (error) {
console.error('Error in updateSubmission:', error);
throw new AppError(500, 'Failed to update submission');
}
}
export async function getFacultyBatches(facultyId: number) {
try {
const facultyBatches = await db
.select({
batch_id: batch.batch_id,
division: batch.division,
batch_name: batch.batch
})
.from(batch)
.innerJoin(courses_faculty, eq(batch.batch_id, courses_faculty.batch_id))
.where(eq(courses_faculty.faculty_id, facultyId));
return facultyBatches;
} catch (error) {
console.error('Error in getFacultyBatches:', error);
throw new AppError(500, 'Failed to fetch faculty batches');
}
}
export async function getStudentSubmissions(studentId: number) {
try {
const studentSubmissions = await db
.select({
submission_id: submissions.submission_id,
practical_id: submissions.practical_id,
practical_sr_no: practicals.sr_no,
practical_name: practicals.practical_name,
course_name: courses.course_name,
submission_time: submissions.submission_time,
status: submissions.status,
marks: submissions.marks,
})
.from(submissions)
.innerJoin(practicals, eq(submissions.practical_id, practicals.practical_id))
.innerJoin(courses, eq(practicals.course_id, courses.course_id))
.where(eq(submissions.student_id, studentId));
return studentSubmissions;
} catch (error) {
console.error('Error in getStudentSubmissions:', error);
throw new AppError(500, 'Failed to fetch student submissions');
}
}
export async function getStudentDetails(studentId: number) {
try {
const studentDetails = await db
.select({
student_id: students.student_id,
name: users.username,
email: users.email,
roll_id: students.roll_id,
semester: batch.semester,
division: batch.division,
batch: batch.batch,
})
.from(students)
.innerJoin(users, eq(students.student_id, users.user_id))
.innerJoin(batch, eq(students.batch_id, batch.batch_id))
.where(eq(students.student_id, studentId))
.limit(1);
if (studentDetails.length === 0) {
throw new AppError(404, 'Student not found');
}
return studentDetails[0];
} catch (error) {
console.error('Error in getStudentDetails:', error);
throw error instanceof AppError ? error : new AppError(500, 'Failed to fetch student details');
}
}
export async function updateStudent(studentId: number, updateData: Partial<typeof students.$inferSelect>) {
try {
await db.update(students)
.set(updateData)
.where(eq(students.student_id, studentId));
// Fetch and return the updated student details
const updatedStudent = await getStudentDetails(studentId);
return updatedStudent;
} catch (error) {
console.error('Error in updateStudent:', error);
throw new AppError(500, 'Failed to update student');
}
}
export async function deleteStudent(studentId: number) {
try {
await db.delete(students)
.where(eq(students.student_id, studentId));
} catch (error) {
console.error('Error in deleteStudent:', error);
throw new AppError(500, 'Failed to delete student');
}
}
export async function getRunResult(token: string) {
try {
const response = await axios.get(`${JUDGE0_API_URL}/submissions/${token}?fields=status,stdout,stderr,time,memory`);
return response.data;
} catch (error) {
console.error('Error in getRunResult:', error);
throw new AppError(500, 'Failed to get run result');
}
}
export async function getSubmissionStatus_(submissionId: string) {
try {
const submissionData = await redis.get(`submission:${submissionId}`);
if (!submissionData) {
throw new AppError(404, 'Submission not found');
}
const data = JSON.parse(submissionData);
const completed = data.status === 'completed';
return {
status: completed ? data.results.every((r: any) => r.status === 'Accepted') ? 'Accepted' : 'Rejected' : 'Processing',
completed
};
} catch (error) {
console.error('Error in getSubmissionStatus:', error);
throw new AppError(500, 'Failed to get submission status');
}
}
async function storeSubmissionData(submissionData: any, results: SubmissionResult[]) {
const [result] = await db.insert(submissions).values({
practical_id: submissionData.practicalId,
student_id: submissionData.studentId,
code_submitted: submissionData.code,
status: 'Pending',
submission_time: new Date()
});
// In MySQL, the insertId is returned directly
const submissionId = result.insertId;
// Store additional data in Redis
const redisKey = `submission:${submissionId}`;
await redis.set(redisKey, JSON.stringify({
results,
status: 'processing',
practicalId: submissionData.practicalId,
studentId: submissionData.studentId,
code: submissionData.code
}), { EX: RESULTS_EXPIRY });
return submissionId;
}
async function checkExistingSubmission(submissionData: any) {
return db
.select()
.from(submissions)
.where(
and(
eq(submissions.practical_id, submissionData.practicalId),
eq(submissions.student_id, submissionData.studentId),
eq(submissions.status, 'Accepted')
)
)
.limit(1);
}
async function fetchAllTestCases(practicalId: number) {
return db
.select()
.from(prac_io)
.where(eq(prac_io.practical_id, practicalId));
}
async function saveSubmissionToDatabase(submissionData: any) {
await db.insert(submissions).values({
practical_id: submissionData.practicalId,
student_id: submissionData.studentId,
code_submitted: submissionData.code,
submission_time: new Date(),
status: 'Accepted',
marks: 15 // Assuming a fixed mark for accepted submissions
});
}
async function checkRateLimit(userId: number, action: 'submit' | 'run'): Promise<boolean> {
const key = `ratelimit:${action}:${userId}`;
const limit = action === 'submit' ? SUBMISSION_RATE_LIMIT : RUN_RATE_LIMIT;
try {
const lastAction = await redis.get(key);
if (lastAction) {
return false;
}
await redis.set(key, Date.now().toString(), { EX: limit });
return true;
} catch (error) {
console.error(`Rate limit check failed for ${action}:`, error);
return false;
}
}
export async function runCode(runData: { code: string; language: string; input: string; userId: number }) {
const canRun = await checkRateLimit(runData.userId, 'run');
if (!canRun) {
throw new AppError(429, `Please wait ${RUN_RATE_LIMIT} seconds before running code again`);
}
try {
const response = await axios.post(`${JUDGE0_API_URL}/submissions`, {
source_code: runData.code,
language_id: parseInt(runData.language, 10), // Ensure language_id is an integer
stdin: runData.input,
redirect_stderr_to_stdout: true
});
const { token } = response.data;
const result = await waitForResult(token);
return {
output: result.stdout || result.stderr || 'No output',
status: result.status.description,
time: result.time,
memory: result.memory
};
} catch (error: any) {
console.error('Error in runCode:', error);
if (error.response && error.response.status === 422) {
console.error('Judge0 API Error:', error.response.data);
throw new AppError(422, 'Invalid request payload to Judge0 API');
}
throw new AppError(500, 'Failed to run code');
}
}
async function waitForResult(token: string, timeout = SUBMISSION_TIMEOUT) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
const response = await axios.get(`${JUDGE0_API_URL}/submissions/${token}`);
if (response.data.status.id !== 1 && response.data.status.id !== 2) { // Not In Queue or Processing
return response.data;
}
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second before next attempt
} catch (error) {
console.error('Error fetching result:', error);
throw new AppError(500, 'Failed to get result');
}
}
throw new AppError(504, 'Submission processing timeout');
}
async function createBatchSubmissions(code: string, language: string, testCases: any[]): Promise<SubmissionResult[]> {
const results: SubmissionResult[] = [];
// Process test cases in batches
for (let i = 0; i < testCases.length; i += BATCH_SIZE) {
const batchTestCases = testCases.slice(i, i + BATCH_SIZE);
const submissions = batchTestCases.map(testCase => ({
source_code: code,
language_id: language,
stdin: testCase.input,
expected_output: testCase.output,
redirect_stderr_to_stdout: true
}));
const response = await axios.post(`${JUDGE0_API_URL}/submissions/batch`, { submissions });
results.push(...response.data.map((result: any, index: number) => ({
token: result.token,
input: batchTestCases[index].input,
expectedOutput: batchTestCases[index].output
})));
}
return results;
}
async function pollBatchResults(tokens: string[]): Promise<any[]> {
let attempts = 0;
const results: any[] = [];
while (attempts < MAX_POLL_ATTEMPTS) {
const batchTokens = tokens.join(',');
const response = await axios.get(`${JUDGE0_API_URL}/submissions/batch`, {
params: {
tokens: batchTokens,
fields: 'token,status,stdout,stderr'
}
});
const allCompleted = response.data.submissions.every((sub: any) =>
sub.status.id !== 1 && sub.status.id !== 2); // Not In Queue or Processing
if (allCompleted) {
return response.data.submissions;
}
attempts++;
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL));
}
throw new AppError(504, 'Submission processing timeout');
}
export async function submitCode(submissionData: {
code: string;
language: string;
practicalId: number;
studentId: number;
}) {
// Fetch all test cases
const testCases = await db
.select()
.from(prac_io)
.where(eq(prac_io.practical_id, submissionData.practicalId));
// Create batch submissions
const batchResults = await createBatchSubmissions(
submissionData.code,
submissionData.language,
testCases
);
// Store initial submission
const [result] = await db.insert(submissions).values({
practical_id: submissionData.practicalId,
student_id: submissionData.studentId,
code_submitted: submissionData.code,
status: 'Pending',
submission_time: new Date()
});
const submissionId = result.insertId;
// Start processing results asynchronously
processSubmissionResults(submissionId, batchResults).catch(console.error);
return { submissionId };
}
async function processSubmissionResults(submissionId: number, results: SubmissionResult[]) {
try {
const tokens = results.map(r => r.token);
const batchResults = await pollBatchResults(tokens);
console.log(batchResults)
const allPassed = batchResults.every(result => result.status.id === 3); // 3 = Accepted
const status = allPassed ? 'Accepted' : 'Rejected';
// Update database
await db.update(submissions)
.set({
status,
marks: allPassed ? 15 : 0
})
.where(eq(submissions.submission_id, submissionId));
// Store results in Redis (without test case details)
await redis.set(`submission:${submissionId}`, JSON.stringify({
status,
completed: true
}), { EX: RESULTS_EXPIRY });
} catch (error) {
console.error('Error processing submission results:', error);
await db.update(submissions)
.set({ status: 'Rejected' })
.where(eq(submissions.submission_id, submissionId));
}
}
export async function getSubmissionStatus(submissionId: string) {
const data = await redis.get(`submission:${submissionId}`);
console.log(data);
if (!data) {
const [submission] = await db
.select()
.from(submissions)
.where(eq(submissions.submission_id, parseInt(submissionId)))
.limit(1);
if (!submission) {
throw new AppError(404, 'Submission not found');
}
return {
status: submission.status,
completed: submission.status !== 'Pending'
};
}
return JSON.parse(data);
}
export async function updateSubmissionCode(submissionData: {
submissionId: number;
code: string;
language: string;
practicalId: number;
studentId: number;
}) {
// Fetch all test cases
const testCases = await db
.select()
.from(prac_io)
.where(eq(prac_io.practical_id, submissionData.practicalId));
// Create batch submissions
const batchResults = await createBatchSubmissions(
submissionData.code,
submissionData.language,
testCases
);
// Update existing submission
await db.update(submissions)
.set({
code_submitted: submissionData.code,
status: 'Pending',
submission_time: new Date()
})
.where(eq(submissions.submission_id, submissionData.submissionId));
// Process results asynchronously
processSubmissionResults(submissionData.submissionId, batchResults).catch(console.error);
return { submissionId: submissionData.submissionId };
}