-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathapps.ex
More file actions
185 lines (157 loc) · 4.83 KB
/
apps.ex
File metadata and controls
185 lines (157 loc) · 4.83 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
defmodule Plexus.Apps do
@moduledoc """
The Apps context.
"""
import Ecto.Query
alias Plexus.PaginationHelpers
alias Plexus.QueryHelpers
alias Plexus.Repo
alias Plexus.Schemas.App
alias Plexus.Schemas.Rating
alias Plexus.Schemas.Score
@doc """
Fetches a page of Apps.
## Options
- See `Plexus.QueryHelpers.merge_opts/2`
- See `Plexus.PaginationHelpers.page_opts/2`
- `:scores` :: boolean (default: `false`)
"""
@spec list_apps(Keyword.t()) :: Repo.page(App.t())
def list_apps(opts \\ []) do
page_opts = PaginationHelpers.page_opts(opts)
App
|> with_scores(opts)
|> QueryHelpers.merge_opts(opts)
|> filter(opts)
|> Repo.paginate(page_opts)
end
@doc """
Fetches the most recently added app
"""
@spec fetch_most_recently_added_app! :: App.t()
def fetch_most_recently_added_app! do
App
|> order_by(:inserted_at)
|> limit(1)
|> Repo.one!()
end
@doc """
Fetches an App.
## Options
- See `Plexus.QueryHelpers.merge_opts/2`
- `:scores` :: boolean (default: `false`)
"""
@spec get_app!(String.t(), Keyword.t()) :: App.t()
def get_app!(package, opts \\ []) do
App
|> where([a], a.package == ^package)
|> with_scores(opts)
|> QueryHelpers.merge_opts(opts)
|> Repo.one!()
end
@spec fetch_app(String.t()) :: {:ok, App.t()} | {:error, :not_found}
def fetch_app(package) do
case Repo.get(App, package) do
%App{} = app -> {:ok, app}
nil -> {:error, :not_found}
end
end
@spec create_app(%{
optional(:icon_url) => String.t(),
package: String.t(),
name: String.t()
}) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
def create_app(params) do
%App{}
|> App.changeset(params)
|> Repo.insert()
|> broadcast(:app_created)
end
@spec update_app(App.t(), %{
optional(:updated_at) => DateTime.t(),
optional(:icon_url) => String.t(),
optional(:name) => String.t()
}) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
def update_app(%App{} = app, params) do
app
|> App.changeset(params)
|> Repo.update()
|> broadcast(:app_updated)
end
@spec change_app(App.t(), map()) :: Ecto.Changeset.t()
def change_app(app, params \\ %{}) do
App.changeset(app, params)
end
@spec delete_app(App.t()) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
def delete_app(%App{} = app) do
app
|> Repo.delete()
|> broadcast(:app_deleted)
end
defp with_scores(query, opts) do
if Keyword.get(opts, :scores, false) do
with_scores(query)
else
query
end
end
defp with_scores(query) do
micro_g = from Rating, where: [rating_type: :micro_g]
native = from Rating, where: [rating_type: :native]
query
|> join(:left, [a], r_micro_g in subquery(micro_g), on: a.package == r_micro_g.app_package)
|> join(:left, [a, _], r_native in subquery(native), on: a.package == r_native.app_package)
|> group_by([a, r_micro_g, r_native], [a.package, r_micro_g.rating_type, r_native.rating_type])
|> select_merge([a, r_micro_g, r_native], %{
scores: %{
native: %Score{
app_package: a.package,
rating_type: :native,
numerator:
fragment("CAST(ROUND(AVG(COALESCE(?, 0))::numeric, 2) AS FLOAT)", r_native.score),
total_count: count(r_native.id, :distinct)
},
micro_g: %Score{
app_package: a.package,
rating_type: :micro_g,
numerator:
fragment("CAST(ROUND(AVG(COALESCE(?, 0))::numeric, 2) AS FLOAT)", r_micro_g.score),
total_count: count(r_micro_g.id, :distinct)
}
}
})
end
defp filter(query, opts) do
Enum.reduce(opts, query, fn
{_, ""}, query ->
query
{:updated_at_greater_than_or_equal_to, dt}, query ->
from q in query, where: q.updated_at >= ^dt
{:search_term, search_term}, query ->
pattern = "%#{search_term}%"
from q in query,
where:
fragment("SIMILARITY(?, ?) > .30", q.name, ^search_term) or
ilike(q.name, ^pattern) or
fragment("SIMILARITY(?, ?) > .30", q.package, ^search_term) or
ilike(q.package, ^pattern),
order_by: fragment("LEVENSHTEIN(?, ?)", q.name, ^search_term)
_, query ->
query
end)
end
@spec subscribe :: :ok
def subscribe do
Phoenix.PubSub.subscribe(Plexus.PubSub, "apps")
end
@spec subscribe(String.t()) :: :ok
def subscribe(package) do
Phoenix.PubSub.subscribe(Plexus.PubSub, "apps:#{package}")
end
defp broadcast({:error, _reason} = error, _event), do: error
defp broadcast({:ok, app}, event) do
Phoenix.PubSub.broadcast!(Plexus.PubSub, "apps", {event, app})
Phoenix.PubSub.broadcast!(Plexus.PubSub, "apps:#{app.package}", {event, app})
{:ok, app}
end
end