Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Folder Support to Autograding #1110

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2297,17 +2297,29 @@ defmodule Cadet.Assessments do
_requesting_user = %CourseRegistration{id: grader_id}
)
when is_ecto_id(submission_id) and is_ecto_id(question_id) do
answer =
questions =
Question
|> where(question_id: ^question_id)
|> order_by(:display_order)
|> Repo.all()

answers =
Answer
|> where(submission_id: ^submission_id, question_id: ^question_id)
|> where(submission_id: ^submission_id)
|> preload([:question, :submission])
|> Repo.one()
|> order_by(:question_id)
|> Repo.all()

# the answer we want to target
answer =
answers
|> Enum.find(fn answer -> answer.question_id == question_id end)

with {:get, answer} when not is_nil(answer) <- {:get, answer},
{:status, true} <-
{:status,
answer.submission.student_id == grader_id or answer.submission.status == :submitted} do
GradingJob.grade_answer(answer, answer.question, true)
GradingJob.grade_answer(answer.question, answers, answer, questions, true)
{:ok, nil}
else
{:get, nil} ->
Expand Down
14 changes: 10 additions & 4 deletions lib/cadet/jobs/autograder/grading_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ defmodule Cadet.Autograder.GradingJob do
end
end

def grade_answer(answer = %Answer{}, question = %Question{type: type}, overwrite \\ false) do
def grade_answer(
question = %Question{type: type},
answers,
answer = %Answer{},
questions,
overwrite \\ false
) do
case type do
:programming ->
Utilities.dispatch_programming_answer(answer, question, overwrite)
Utilities.dispatch_programming_answer(question, answers, answer, questions, overwrite)

:mcq ->
grade_mcq_answer(answer, question)
Expand Down Expand Up @@ -210,15 +216,15 @@ defmodule Cadet.Autograder.GradingJob do

defp grade_submission_question_answer_lists(
submission_id,
[question = %Question{} | question_tail],
questions = [question = %Question{} | question_tail],
answers = [answer = %Answer{} | answer_tail],
regrade,
overwrite
)
when is_boolean(regrade) and is_boolean(overwrite) and is_ecto_id(submission_id) do
if question.id == answer.question_id do
if regrade || answer.autograding_status in [:none, :failed] do
grade_answer(answer, question, overwrite)
grade_answer(question, answers, answer, questions, overwrite)
end

grade_submission_question_answer_lists(
Expand Down
67 changes: 54 additions & 13 deletions lib/cadet/jobs/autograder/lambda_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@
alias Cadet.Autograder.ResultStoreWorker
alias Cadet.Assessments.{Answer, Question}

@type entrypoint_file :: String.t()

@doc """
This Que callback transforms an input of %{question: %Question{}, answer: %Answer{}} into
the correct shape to dispatch to lambda, waits for the response, parses it, and enqueues a
storage job.
"""
def perform(params = %{answer: answer = %Answer{}, question: %Question{}}) do
def perform(%{
base_question: base_question = %Question{},
questions: questions,
answers: answers,
answer: answer = %Answer{}
}) do
params = %{
base_question: base_question,
questions: questions,
answers: answers
}

lambda_params = build_request_params(params)

if Enum.empty?(lambda_params.testcases) do
Expand All @@ -42,7 +55,15 @@
end
end

def on_failure(%{answer: answer = %Answer{}, question: %Question{}}, error) do
def on_failure(
%{
base_question: base_question = %Question{},
questions: questions,
answers: answers,
answer: answer = %Answer{}
},
error
) do
error_message =
"Failed to get autograder result. answer_id: #{answer.id}, error: #{inspect(error, pretty: true)}"

Expand Down Expand Up @@ -74,28 +95,48 @@
)
end

def build_request_params(%{question: question = %Question{}, answer: answer = %Answer{}}) do
question_content = question.question

# base_question is the actual question that this request grades on
def build_request_params(%{
base_question: base_question = %Question{},
questions: questions,
answers: answers
}) do
{_, upcased_name_external} =
question.grading_library.external
base_question.grading_library.external
|> Map.from_struct()
|> Map.get_and_update(
:name,
&{&1, &1 |> String.upcase()}
)

filesContent =

Check warning on line 112 in lib/cadet/jobs/autograder/lambda_worker.ex

View workflow job for this annotation

GitHub Actions / Run CI

Variable names should be written in snake_case.
Enum.zip(questions, answers)
|> Enum.reduce(%{}, fn {question, answer}, acc ->
question_content = question.question
file_name = Integer.to_string(question.display_order) <> ".js"

final_answer_content =
(Map.get(question_content, "prepend") || "") <>
(Map.get(answer.answer, "code") || "") <>
(Map.get(question_content, "postpend") || "")

Map.put(acc, file_name, final_answer_content)
end)

base_question_content = base_question.question
# test
%{
prependProgram: Map.get(question_content, "prepend", ""),
studentProgram: Map.get(answer.answer, "code"),
postpendProgram: Map.get(question_content, "postpend", ""),
files: filesContent,
# entrypointFile is the base question's question number
entrypointFile: Integer.to_string(base_question_content.display_order) <> ".js",
testcases:
Map.get(question_content, "public", []) ++
Map.get(question_content, "opaque", []) ++ Map.get(question_content, "secret", []),
Map.get(base_question_content, "public", []) ++
Map.get(base_question_content, "opaque", []) ++
Map.get(base_question_content, "secret", []),
library: %{
chapter: question.grading_library.chapter,
chapter: base_question.grading_library.chapter,
external: upcased_name_external,
globals: Enum.map(question.grading_library.globals, fn {k, v} -> [k, v] end)
globals: Enum.map(base_question.grading_library.globals, fn {k, v} -> [k, v] end)
}
}
end
Expand Down
12 changes: 10 additions & 2 deletions lib/cadet/jobs/autograder/utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ defmodule Cadet.Autograder.Utilities do
alias Cadet.Accounts.CourseRegistration
alias Cadet.Assessments.{Answer, Assessment, Question, Submission}

def dispatch_programming_answer(answer = %Answer{}, question = %Question{}, overwrite \\ false) do
def dispatch_programming_answer(
base_question = %Question{},
answers,
answer = %Answer{},
questions,
overwrite \\ false
) do
# This should never fail
answer =
answer
|> Answer.autograding_changeset(%{autograding_status: :processing})
|> Repo.update!()

Que.add(Cadet.Autograder.LambdaWorker, %{
question: question,
base_question: base_question,
answer: answer,
questions: questions,
answers: answers,
overwrite: overwrite
})
end
Expand Down
Loading