Skip to content

Commit f99af62

Browse files
committed
feat: sort stargazers by contributions
1 parent 6ba377a commit f99af62

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

lib/algora/settings/settings.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ defmodule Algora.Settings do
22
@moduledoc false
33
use Ecto.Schema
44

5+
import Ecto.Query
6+
57
alias Algora.Accounts
8+
alias Algora.Accounts.User
69
alias Algora.Repo
710

811
@primary_key {:key, :string, []}
@@ -111,6 +114,36 @@ defmodule Algora.Settings do
111114
end
112115
end
113116

117+
def get_top_stargazers(job) do
118+
[
119+
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+
)
142+
]
143+
|> Algora.Cloud.list_top_matches()
144+
|> load_matches_2()
145+
end
146+
114147
def set_job_criteria(job_id, criteria) when is_binary(job_id) and is_map(criteria) do
115148
set("job_criteria:#{job_id}", %{"criteria" => criteria})
116149
end

lib/algora/workspace/workspace.ex

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -570,27 +570,35 @@ defmodule Algora.Workspace do
570570
)
571571
end
572572

573-
def list_stargazers(id) do
574-
Repo.all(
575-
from(s in Stargazer,
576-
join: r in assoc(s, :repository),
577-
where: r.provider == "github",
578-
join: ro in assoc(r, :user),
579-
where: ro.id == ^id,
580-
join: u in assoc(s, :user),
581-
where: u.type != :bot,
582-
where: not ilike(u.provider_login, "%bot"),
583-
left_join: m in Member,
584-
on: m.user_id == u.id and m.org_id == r.user_id,
585-
where: is_nil(m.id),
586-
distinct: [s.user_id],
587-
select_merge: %{user: u},
588-
order_by: [desc: s.inserted_at, asc: s.id],
589-
limit: 100
590-
)
573+
def stargazers_query(id) do
574+
from(s in Stargazer,
575+
join: r in assoc(s, :repository),
576+
where: r.provider == "github",
577+
join: ro in assoc(r, :user),
578+
where: ro.id == ^id,
579+
join: u in assoc(s, :user),
580+
where: u.type != :bot,
581+
where: not ilike(u.provider_login, "%bot"),
582+
left_join: m in Member,
583+
on: m.user_id == u.id and m.org_id == r.user_id,
584+
where: is_nil(m.id),
585+
distinct: [s.user_id]
591586
)
592587
end
593588

589+
def list_stargazers(id) do
590+
id
591+
|> stargazers_query()
592+
|> select_merge([s, r, ro, u], %{user: u})
593+
|> limit(100)
594+
|> order_by([s], desc: s.inserted_at, asc: s.id)
595+
|> Repo.all()
596+
end
597+
598+
def count_stargazers(id) do
599+
id |> stargazers_query() |> Repo.aggregate(:count)
600+
end
601+
594602
def fetch_contributor(repository_id, user_id) do
595603
Repo.fetch_by(Contributor, repository_id: repository_id, user_id: user_id)
596604
end
@@ -641,6 +649,8 @@ defmodule Algora.Workspace do
641649
where: u.id in ^ids,
642650
where: not ilike(r.name, "%awesome%"),
643651
where: not ilike(r.name, "%algorithms%"),
652+
where: not ilike(r.name, "%exercises%"),
653+
where: not ilike(r.name, "%tutorials%"),
644654
where: not ilike(repo_owner.provider_login, "%algorithms%"),
645655
where: not ilike(repo_owner.provider_login, "%firstcontributions%"),
646656
where: repo_owner.type == :organization or r.stargazers_count > 200,

lib/algora_web/live/org/job_live.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule AlgoraWeb.Org.JobLive do
77
alias Algora.Markdown
88
alias Algora.Repo
99
alias Algora.Settings
10+
alias Algora.Workspace
1011
alias AlgoraWeb.Forms.BountyForm
1112
alias AlgoraWeb.Forms.ContractForm
1213
alias AlgoraWeb.Forms.TipForm
@@ -242,7 +243,7 @@ defmodule AlgoraWeb.Org.JobLive do
242243
<%= for {tab, label, count} <- [
243244
{"applicants", "Applicants", length(@applicants)},
244245
{"imports", "Imports", length(@imports)},
245-
if(length(@stargazers) > 0, do: {"stargazers", "Stargazers", length(@stargazers)}, else: nil),
246+
if(@stargazers_count > 0, do: {"stargazers", "Stargazers", @stargazers_count}, else: nil),
246247
{"matches", "Matches", length(@matches)}
247248
] |> Enum.reject(& is_nil(&1)) do %>
248249
<label class={[
@@ -262,7 +263,7 @@ defmodule AlgoraWeb.Org.JobLive do
262263
<span class="flex items-center justify-between w-full gap-2">
263264
<span class="text-sm font-medium">{label}</span>
264265
<span class="text-xs text-muted-foreground">
265-
{count}{if count == 100, do: "+", else: ""}
266+
{Algora.Util.format_number_compact(count)}
266267
</span>
267268
</span>
268269
</label>
@@ -894,7 +895,8 @@ defmodule AlgoraWeb.Org.JobLive do
894895

895896
defp assign_applicants(socket) do
896897
all_applicants = Jobs.list_job_applications(socket.assigns.job)
897-
stargazers = Algora.Workspace.list_stargazers(socket.assigns.current_org.id)
898+
stargazers = Settings.get_top_stargazers(socket.assigns.job)
899+
stargazers_count = Workspace.count_stargazers(socket.assigns.job.user_id)
898900
applicants = Enum.reject(all_applicants, & &1.imported_at)
899901
imports = Enum.filter(all_applicants, & &1.imported_at)
900902
matches = Settings.get_job_matches(socket.assigns.job)
@@ -914,6 +916,7 @@ defmodule AlgoraWeb.Org.JobLive do
914916
|> assign(:applicants, sort_by_contributions(socket.assigns.job, applicants, contributions_map))
915917
|> assign(:imports, sort_by_contributions(socket.assigns.job, imports, contributions_map))
916918
|> assign(:stargazers, sort_by_contributions(socket.assigns.job, stargazers, contributions_map))
919+
|> assign(:stargazers_count, stargazers_count)
917920
|> assign(:matches, matches)
918921
|> assign(:truncated_matches, truncated_matches)
919922
|> assign(:contributions_map, contributions_map)

0 commit comments

Comments
 (0)