Skip to content
Closed

Dev #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"http://192.168.0.5:3000",
"http://192.168.0.11:5173",
"http://192.168.68.194:5173",
"https://grader.kanonkc.com",
FRONEND_URL
]

Expand Down
19 changes: 19 additions & 0 deletions api/controllers/problem/import_elabsheet_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from api.utility import passwordEncryption
from rest_framework.response import Response
from rest_framework.decorators import api_view
from api.sandbox.grader import PythonGrader
from ...constant import GET,POST,PUT,DELETE
from ...models import *
from rest_framework import status
from django.forms.models import model_to_dict
from ...serializers import *

def import_elabsheet_problem(request, problem: Problem):
print("importing elabsheet problem")
print(request.data)
# Get file
file = request.data.get('file')
problem.pdf_url = file
print(file)
print(problem.pdf_url)
return Response(status=status.HTTP_204_NO_CONTENT)
6 changes: 5 additions & 1 deletion api/controllers/submission/submit_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ def submit_problem_function(account_id:str,problem_id:str,topic_id:str,request):
return Response(testser.data,status=status.HTTP_201_CREATED)

def submit_problem(account_id:str,problem_id:str,request):
return submit_problem_function(account_id,problem_id,None,request)
try:
return submit_problem_function(account_id,problem_id,None,request)
except Exception as e:
print(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
2 changes: 1 addition & 1 deletion api/difficulty_predictor/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
def predict(avg_first_passed_total_attempts,avg_first_passed_time_used):
if not difficulty:
return 0
return int(difficulty.predict([[avg_first_passed_total_attempts,avg_first_passed_time_used]])[0])
return 0 # int(difficulty.predict([[avg_first_passed_total_attempts,avg_first_passed_time_used]])[0])
91 changes: 46 additions & 45 deletions api/difficulty_predictor/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,66 @@

def modelgrader_preprocessor(submission_df):

if not success:
return [-1,-1]
# if not success:
# return [-1,-1]

submission_df = submission_df[['account_id', 'problem_id','is_passed','language','date','submission_code','passed_ratio']]
# submission_df = submission_df[['account_id', 'problem_id','is_passed','language','date','submission_code','passed_ratio']]

# Change submission_code to string
submission_df['submission_code'] = submission_df['submission_code'].astype(str)
# # Change submission_code to string
# submission_df['submission_code'] = submission_df['submission_code'].astype(str)

# Sort by account_id, problem_id and date
submission_df.sort_values(['account_id','problem_id','date'])
# # Sort by account_id, problem_id and date
# submission_df.sort_values(['account_id','problem_id','date'])

# Calculate the difference time between the current submission and the previous submission
submission_df['date'] = pd.to_datetime(submission_df['date'])
submission_df['diff_time'] = submission_df.groupby(['account_id','problem_id'])['date'].diff()
submission_df['diff_time'] = submission_df['diff_time'].dt.total_seconds()
# # Calculate the difference time between the current submission and the previous submission
# submission_df['date'] = pd.to_datetime(submission_df['date'])
# submission_df['diff_time'] = submission_df.groupby(['account_id','problem_id'])['date'].diff()
# submission_df['diff_time'] = submission_df['diff_time'].dt.total_seconds()

# Set all diff_time which is NaN to 0
submission_df['diff_time'].fillna(0, inplace=True)
# # Set all diff_time which is NaN to 0
# submission_df['diff_time'].fillna(0, inplace=True)

# Set all diff_time which has more than 3 hours to 3
submission_df.loc[submission_df['diff_time'] > 10800, 'diff_time'] = 10801
# # Set all diff_time which has more than 3 hours to 3
# submission_df.loc[submission_df['diff_time'] > 10800, 'diff_time'] = 10801

# Loop each row
current_problem_id = None
current_account_id = None
already_passed = False
previous_diff_time = -1
# # Loop each row
# current_problem_id = None
# current_account_id = None
# already_passed = False
# previous_diff_time = -1


for index, row in submission_df.iterrows():
if not current_problem_id and not current_account_id:
current_problem_id = row['problem_id']
current_account_id = row['account_id']
# for index, row in submission_df.iterrows():
# if not current_problem_id and not current_account_id:
# current_problem_id = row['problem_id']
# current_account_id = row['account_id']

if current_problem_id != row['problem_id'] or current_account_id != row['account_id']:
current_problem_id = row['problem_id']
current_account_id = row['account_id']
already_passed = False
elif already_passed:
# row['diff_time'] = 0
submission_df.drop(index, inplace=True)
elif row['is_passed'] == 1:
already_passed = True
if row['diff_time'] > 10800:
row['diff_time'] = previous_diff_time
# if current_problem_id != row['problem_id'] or current_account_id != row['account_id']:
# current_problem_id = row['problem_id']
# current_account_id = row['account_id']
# already_passed = False
# elif already_passed:
# # row['diff_time'] = 0
# submission_df.drop(index, inplace=True)
# elif row['is_passed'] == 1:
# already_passed = True
# if row['diff_time'] > 10800:
# row['diff_time'] = previous_diff_time

grouped_submission_df = submission_df.groupby(['account_id','problem_id']).agg({'is_passed':'sum','date':'count','diff_time':'sum'}).reset_index()
# grouped_submission_df = submission_df.groupby(['account_id','problem_id']).agg({'is_passed':'sum','date':'count','diff_time':'sum'}).reset_index()

# Rename columns
grouped_submission_df.rename(columns={'date':'first_passed_total_attempts','is_passed':'passed_submission','diff_time':'first_passed_time_used'}, inplace=True)
# # Rename columns
# grouped_submission_df.rename(columns={'date':'first_passed_total_attempts','is_passed':'passed_submission','diff_time':'first_passed_time_used'}, inplace=True)

grouped_submission_df = grouped_submission_df.groupby(['problem_id']).agg({'passed_submission':'sum','first_passed_total_attempts':'sum','first_passed_time_used':'mean'}).reset_index()
# grouped_submission_df = grouped_submission_df.groupby(['problem_id']).agg({'passed_submission':'sum','first_passed_total_attempts':'sum','first_passed_time_used':'mean'}).reset_index()

# grouped_submission_df['first_pf_ratio'] = grouped_submission_df['passed_submission'] / grouped_submission_df['first_passed_total_attempts']
df = grouped_submission_df.drop(['passed_submission'], axis=1)
# # grouped_submission_df['first_pf_ratio'] = grouped_submission_df['passed_submission'] / grouped_submission_df['first_passed_total_attempts']
# df = grouped_submission_df.drop(['passed_submission'], axis=1)

df.rename(columns={'first_passed_total_attempts':'avg_first_passed_total_attempts','first_passed_time_used':'avg_first_passed_time_used'}, inplace=True)
# df.rename(columns={'first_passed_total_attempts':'avg_first_passed_total_attempts','first_passed_time_used':'avg_first_passed_time_used'}, inplace=True)

# Change problem_id to index
df.set_index('problem_id', inplace=True)
# # Change problem_id to index
# df.set_index('problem_id', inplace=True)

return [df['avg_first_passed_total_attempts'][0],df['avg_first_passed_time_used'][0]]
# return [df['avg_first_passed_total_attempts'][0],df['avg_first_passed_time_used'][0]]
return [0,0]
2 changes: 1 addition & 1 deletion api/sandbox/grader.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def runtime(self) -> list[RuntimeResult]:
'r'
),stderr=subprocess.DEVNULL,timeout=float(self.timeout))
result.append(RuntimeResult(self.testcases[i],runner.decode(),"OK"))
except subprocess.CalledProcessError:
except subprocess.CalledProcessError as e:
result.append(RuntimeResult(self.testcases[i],None,"ERROR"))
except subprocess.TimeoutExpired:
result.append(RuntimeResult(self.testcases[i],None,"TIMEOUT"))
Expand Down
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
path('problems',problem.all_problems_view),
path('problems/validate',problem.validation_view),
path('problems/<str:problem_id>',problem.one_problem_view),
path('problems/<str:problem_id>/import/pdf',problem.import_pdf_view),
path("problems/<str:problem_id>/accounts/<str:account_id>/submissions",submission.account_problem_submission_view),
path('topics/<str:topic_id>/problems/<str:problem_id>/accounts/<str:account_id>',problem.problem_in_topic_account_view),

Expand Down
3 changes: 3 additions & 0 deletions api/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def formParser(querydict):
def uploadTopic(instance,filename):
return f"topics/{filename}"

def uploadProblemImportPDF(instance,filename):
return f"import-pdfs/{filename}"

def regexMatching(regex:str,code:str)->bool:
code = ";".join([i.strip() for i in code.split("\n") if i != ""])
return re.search(regex, code)
14 changes: 13 additions & 1 deletion api/views/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ..controllers.problem.get_problem_in_topic_with_best_submission import *
from ..controllers.problem.update_group_permission_to_problem import *
from ..controllers.problem.get_problem_public import *
from ..controllers.problem.import_elabsheet_problem import *


# Create your views here.
Expand Down Expand Up @@ -76,4 +77,15 @@ def problem_in_topic_account_view(request,account_id:str,topic_id:str,problem_id
def problem_group_view(request,account_id:int,problem_id:int):
problem = Problem.objects.get(problem_id=problem_id)
if request.method == PUT:
return update_group_permission_to_problem(problem,request)
return update_group_permission_to_problem(problem,request)

# @api_view([POST])
# def import_elabsheet_problem_view(request):
# if request.method == POST:
# print(request)

@api_view([PUT])
def import_pdf_view(request,problem_id:int):
problem = Problem.objects.get(problem_id=problem_id)
if request.method == PUT:
return import_elabsheet_problem(request, problem)
6 changes: 5 additions & 1 deletion api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
@api_view([POST,GET])
def account_problem_submission_view(request,problem_id,account_id):
if request.method == POST:
return submit_problem(account_id,problem_id,request)
try:
return submit_problem(account_id,problem_id,request)
except Exception as e:
print(e)
return Response({"error":str(e)},status=status.HTTP_400_BAD_REQUEST)
if request.method == GET:
return get_submissions_by_account_problem(account_id,problem_id)

Expand Down