[go: nahoru, domu]

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

Fix DB crash due to timeout #1031

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Migrate to more optimised query
* Use subquery instead of join for answers
* Split main model query and view model generation
* Split queries into 3
* Use ORM where possible
  • Loading branch information
RichDom2185 committed Nov 6, 2023
commit 97890a6c1bd2ab0caf130fed77e9a9c1826628d1
79 changes: 79 additions & 0 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ defmodule Cadet.Assessments do
# We bypass Ecto here and use a raw query to generate JSON directly from
# PostgreSQL, because doing it in Elixir/Erlang is too inefficient.

# TODO: Remove old query
case Repo.query(
"""
select json_agg(q)::TEXT from
Expand Down Expand Up @@ -1346,6 +1347,84 @@ defmodule Cadet.Assessments do
{:ok, %{rows: [[nil]]}} -> {:ok, "[]"}
{:ok, %{rows: [[json]]}} -> {:ok, json}
end

# TODO: Implement filtering
submissions =
case Repo.query("""
SELECT
s.id,
s.status,
s.unsubmitted_at,
s.unsubmitted_by_id,
s_ans.xp,
s_ans.xp_adjustment,
s.xp_bonus,
s_ans.graded_count,
s.student_id,
s.assessment_id
FROM
submissions AS s
LEFT JOIN (
SELECT
ans.submission_id,
SUM(ans.xp) AS xp,
SUM(ans.xp_adjustment) AS xp_adjustment,
COUNT(ans.id) FILTER (
WHERE
ans.grader_id IS NOT NULL
) AS graded_count
FROM
answers AS ans
GROUP BY
ans.submission_id
) AS s_ans ON s_ans.submission_id = s.id
WHERE
s.assessment_id IN (
SELECT
id
FROM
assessments
WHERE
assessments.course_id = #{course_id}
);
""") do
{:ok, %{columns: columns, rows: result}} ->
result
|> Enum.map(
&(columns
|> Enum.map(fn c -> String.to_atom(c) end)
|> Enum.zip(&1)
|> Enum.into(%{}))
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we put a default case here? in case of error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think it was necessary as I don't think the SQL will ever error (unless the DB server itself is down, in which case it would give the same error messages as the ones we have been having recently).

What do you think?

end

{:ok, generate_grading_summary_view_model(submissions, course_id)}
end

defp generate_grading_summary_view_model(submissions, course_id) do
users =
CourseRegistration
|> where([cr], cr.course_id == ^course_id)
|> join(:inner, [cr], u in assoc(cr, :user))
|> join(:left, [cr, u], g in assoc(cr, :group))
|> preload([cr, u, g], user: u, group: g)
|> Repo.all()

assessment_ids = submissions |> Enum.map(& &1.assessment_id) |> Enum.uniq()

assessments =
Assessment
|> where([a], a.id in ^assessment_ids)
|> join(:left, [a], q in assoc(a, :questions))
|> join(:inner, [a], ac in assoc(a, :config))
|> preload([a, q, ac], questions: q, config: ac)
|> Repo.all()

%{
users: users,
assessments: assessments,
submissions: submissions
}
end

@spec get_answers_in_submission(integer() | String.t()) ::
Expand Down
4 changes: 2 additions & 2 deletions lib/cadet_web/admin_controllers/admin_grading_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ defmodule CadetWeb.AdminGradingController do
group = String.to_atom(group)

case Assessments.all_submissions_by_grader_for_index(course_reg, group) do
{:ok, submissions} ->
{:ok, view_model} ->
conn
|> put_status(:ok)
|> put_resp_content_type("application/json")
|> text(submissions)
|> render("gradingsummaries.json", view_model)
end
end

Expand Down