Skip to content

Commit 7ef2706

Browse files
committed
perf: improve job page load speed
1 parent 181d73b commit 7ef2706

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

lib/algora/cloud.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ defmodule Algora.Cloud do
99
call(AlgoraCloud, :list_top_matches, [opts])
1010
end
1111

12+
def list_top_stargazers(opts \\ []) do
13+
call(AlgoraCloud, :list_top_stargazers, [opts])
14+
end
15+
1216
def truncate_matches(org, matches) do
1317
call(AlgoraCloud, :truncate_matches, [org, matches])
1418
end
1519

20+
def count_matches(job) do
21+
call(AlgoraCloud, :count_matches, [job])
22+
end
23+
1624
def get_contribution_score(job, user, contributions_map) do
1725
call(AlgoraCloud, :get_contribution_score, [job, user, contributions_map])
1826
end

lib/algora/settings/settings.ex

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ defmodule Algora.Settings do
102102
_ ->
103103
[
104104
tech_stack: job.tech_stack,
105-
limit: 50,
105+
limit: Algora.Cloud.count_matches(job),
106106
sort_by:
107107
case get_job_criteria(job) do
108108
criteria when map_size(criteria) > 0 -> criteria
@@ -116,31 +116,11 @@ defmodule Algora.Settings do
116116

117117
def get_top_stargazers(job) do
118118
[
119+
job: job,
119120
tech_stack: job.tech_stack,
120-
limit: 100,
121-
sort_by:
122-
case get_job_criteria(job) do
123-
criteria when map_size(criteria) > 0 -> criteria
124-
_ -> [{"solver", true}]
125-
end,
126-
users:
127-
from(u in User,
128-
where:
129-
fragment(
130-
"""
131-
exists (
132-
select 1
133-
from stargazers s
134-
inner join repositories r on s.repository_id = r.id
135-
where s.user_id = ? and r.user_id = ?
136-
)
137-
""",
138-
u.id,
139-
^job.user_id
140-
)
141-
)
121+
limit: 50
142122
]
143-
|> Algora.Cloud.list_top_matches()
123+
|> Algora.Cloud.list_top_stargazers()
144124
|> load_matches_2()
145125
end
146126

lib/algora/workspace/workspace.ex

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -643,23 +643,35 @@ defmodule Algora.Workspace do
643643
def list_user_contributions(ids, opts \\ []) do
644644
query =
645645
from uc in UserContribution,
646+
where: uc.user_id in ^ids,
646647
join: u in assoc(uc, :user),
647648
join: r in assoc(uc, :repository),
648649
join: repo_owner in assoc(r, :user),
649-
where: u.id in ^ids,
650-
where: not ilike(r.name, "%awesome%"),
651-
where: not ilike(r.name, "%algorithms%"),
652-
where: not ilike(r.name, "%exercises%"),
653-
where: not ilike(r.name, "%tutorials%"),
654-
where: not ilike(repo_owner.provider_login, "%algorithms%"),
655-
where: not ilike(repo_owner.provider_login, "%firstcontributions%"),
656650
where: repo_owner.type == :organization or r.stargazers_count > 200,
657651
# where: fragment("? && ?::citext[]", r.tech_stack, ^(opts[:tech_stack] || [])),
652+
where:
653+
not (ilike(r.name, "%awesome%") or
654+
ilike(r.name, "%algorithms%") or
655+
ilike(r.name, "%exercises%") or
656+
ilike(r.name, "%tutorials%")),
657+
where:
658+
not (ilike(repo_owner.provider_login, "%algorithms%") or
659+
ilike(repo_owner.provider_login, "%firstcontributions%")),
658660
order_by: [
659661
desc: fragment("CASE WHEN ? && ?::citext[] THEN 1 ELSE 0 END", r.tech_stack, ^(opts[:tech_stack] || [])),
660662
desc: r.stargazers_count
661663
],
662-
select_merge: %{user: u, repository: %{r | user: repo_owner}}
664+
select: %UserContribution{
665+
contribution_count: uc.contribution_count,
666+
user: map(u, [:id, :provider_login]),
667+
repository: %{
668+
id: r.id,
669+
name: r.name,
670+
stargazers_count: r.stargazers_count,
671+
tech_stack: r.tech_stack,
672+
user: map(repo_owner, [:id, :provider_login, :type, :name, :avatar_url, :stargazers_count])
673+
}
674+
}
663675

664676
query =
665677
case opts[:limit] do
@@ -748,11 +760,17 @@ defmodule Algora.Workspace do
748760
|> Oban.insert()
749761

750762
_ ->
763+
Logger.error("User not found for #{contribution.provider_login}")
751764
{:error, :user_not_found}
752765
end
753766
end)
754767

755-
if Enum.any?(results, &match?({:ok, _}, &1)), do: :ok, else: {:error, :failed}
768+
if Enum.any?(results, &match?({:ok, _}, &1)) do
769+
:ok
770+
else
771+
Logger.error("Failed to add contributions: #{inspect(results)}")
772+
{:error, :failed}
773+
end
756774
end
757775

758776
defp add_contributions(token, users, contributions) do

0 commit comments

Comments
 (0)