Skip to content

Commit 80daf50

Browse files
committed
fix N+1 query
1 parent 71bc492 commit 80daf50

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

lib/algora/workspace/workspace.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,14 @@ defmodule Algora.Workspace do
609609
end
610610
end
611611

612-
@spec list_user_contributions(String.t(), map()) :: {:ok, list(map())} | {:error, term()}
613-
def list_user_contributions(github_handle, opts \\ []) do
612+
@spec list_user_contributions(list(String.t()), map()) :: {:ok, list(map())} | {:error, term()}
613+
def list_user_contributions(ids, opts \\ []) do
614614
query =
615615
from uc in UserContribution,
616616
join: u in assoc(uc, :user),
617617
join: r in assoc(uc, :repository),
618618
join: repo_owner in assoc(r, :user),
619-
where: u.provider == "github",
620-
where: u.provider_login == ^github_handle,
619+
where: u.id in ^ids,
621620
where: not ilike(r.name, "%awesome%"),
622621
where: not ilike(r.name, "%algorithms%"),
623622
where: not ilike(repo_owner.provider_login, "%algorithms%"),
@@ -627,7 +626,7 @@ defmodule Algora.Workspace do
627626
desc: fragment("CASE WHEN ? && ?::citext[] THEN 1 ELSE 0 END", r.tech_stack, ^(opts[:tech_stack] || [])),
628627
desc: r.stargazers_count
629628
],
630-
select_merge: %{repository: %{r | user: repo_owner}}
629+
select_merge: %{user: u, repository: %{r | user: repo_owner}}
631630

632631
query =
633632
case opts[:limit] do

lib/algora_web/live/org/job_live.ex

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,14 +1197,10 @@ defmodule AlgoraWeb.Org.JobLive do
11971197

11981198
# Fetch contributions for all applicants and create a map for quick lookup
11991199
defp fetch_applicants_contributions(users, tech_stack) do
1200-
Enum.reduce(users, %{}, fn user, acc ->
1201-
if user.provider_login do
1202-
contributions = Algora.Workspace.list_user_contributions(user.provider_login, limit: 20, tech_stack: tech_stack)
1203-
Map.put(acc, user.id, contributions)
1204-
else
1205-
acc
1206-
end
1207-
end)
1200+
users
1201+
|> Enum.map(& &1.id)
1202+
|> Algora.Workspace.list_user_contributions(tech_stack: tech_stack)
1203+
|> Enum.group_by(& &1.user.id)
12081204
end
12091205

12101206
# Sort applicants by their total number of contributions

lib/algora_web/live/user/profile_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule AlgoraWeb.User.ProfileLive do
1414
{:ok, user} ->
1515
transactions = Payments.list_received_transactions(user.id, limit: page_size())
1616

17-
contributions = Algora.Workspace.list_user_contributions(user.provider_login, limit: 20)
17+
contributions = Algora.Workspace.list_user_contributions([user.id], limit: 20)
1818

1919
{:ok,
2020
socket

0 commit comments

Comments
 (0)