-
Notifications
You must be signed in to change notification settings - Fork 1
/
workspace.ex
85 lines (74 loc) · 2.03 KB
/
workspace.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
defmodule Algora.Workspace do
import Ecto.Query
alias Algora.Repo
alias Algora.Workspace.Issue
def issue_search_job_name, do: "issue_search"
def create_issue!(attrs \\ %{}) do
{:ok, issue} = create_issue(attrs)
issue
end
def create_issue(attrs \\ %{}) do
%Issue{}
|> Issue.changeset(attrs)
|> Repo.insert(on_conflict: :nothing, conflict_target: :path)
end
def get_issue(id), do: Repo.get(Issue, id)
def get_issue_by_path(path), do: Repo.get_by(Issue, path: path)
def search_issues_like(issue, opts \\ []) do
limit = opts[:limit] || 10
query = "##{issue.title}\n\n#{issue.body}\n\nComments: #{Jason.encode!(issue.comments)}"
Repo.all(
from(i in Issue,
join:
s in fragment(
"""
SELECT (search_results->>'id') as issue_id, row_number() OVER () as rank
FROM vectorize.search(
job_name => ?,
query => ?,
return_columns => ARRAY['id'],
num_results => ?
)
""",
^issue_search_job_name(),
^query,
^limit
),
on: i.id == fragment("issue_id"),
order_by: [asc: s.rank]
)
)
end
def get_random_issues(opts \\ []) do
limit = opts[:limit] || 2
result =
Repo.transaction(fn ->
if seed = opts[:seed] do
:rand.seed(:exsplus, {seed, seed, seed})
seed = :rand.uniform()
Repo.query!("SELECT setseed($1)", [seed])
end
Repo.all(
from(i in Issue,
where:
fragment(
"""
id IN (
SELECT issues.id
FROM issues
WHERE bounty IS NOT NULL
ORDER BY RANDOM()
LIMIT ?
)
""",
^limit
)
)
)
end)
case result do
{:ok, issues} -> {:ok, issues}
{:error, reason} -> {:error, reason}
end
end
end