-
Notifications
You must be signed in to change notification settings - Fork 28
/
ecto_psql_extras.ex
378 lines (290 loc) · 10.8 KB
/
ecto_psql_extras.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
defmodule EctoPSQLExtras do
@moduledoc """
The entry point for each function.
"""
@callback info :: %{
required(:title) => binary,
required(:columns) => [%{name: atom, type: atom}],
optional(:order_by) => [{atom, :asc | :desc}],
optional(:default_args) => list,
optional(:args_for_select) => list
}
@callback query :: binary
@type repo :: module() | {module(), node()}
@doc """
Returns all queries and their modules.
If a repository is given, it will be queried for extensions support
and special queries will be included if available.
"""
@spec queries(repo() | nil) :: map()
def queries(repo \\ nil) do
%{
bloat: EctoPSQLExtras.Bloat,
blocking: EctoPSQLExtras.Blocking,
cache_hit: EctoPSQLExtras.CacheHit,
db_settings: EctoPSQLExtras.DbSettings,
extensions: EctoPSQLExtras.Extensions,
table_cache_hit: EctoPSQLExtras.TableCacheHit,
index_cache_hit: EctoPSQLExtras.IndexCacheHit,
index_size: EctoPSQLExtras.IndexSize,
index_usage: EctoPSQLExtras.IndexUsage,
locks: EctoPSQLExtras.Locks,
all_locks: EctoPSQLExtras.AllLocks,
long_running_queries: EctoPSQLExtras.LongRunningQueries,
mandelbrot: EctoPSQLExtras.Mandelbrot,
records_rank: EctoPSQLExtras.RecordsRank,
seq_scans: EctoPSQLExtras.SeqScans,
table_indexes_size: EctoPSQLExtras.TableIndexesSize,
table_size: EctoPSQLExtras.TableSize,
total_index_size: EctoPSQLExtras.TotalIndexSize,
total_table_size: EctoPSQLExtras.TotalTableSize,
unused_indexes: EctoPSQLExtras.UnusedIndexes,
null_indexes: EctoPSQLExtras.NullIndexes,
vacuum_stats: EctoPSQLExtras.VacuumStats,
kill_all: EctoPSQLExtras.KillAll
}
|> Map.merge(pg_stat_statements_queries(repo))
end
@pg_stat_statements_query "select installed_version from pg_available_extensions where name='pg_stat_statements'"
defp pg_stat_statements_queries(repo) do
case repo && pg_stat_statements_version(repo) do
nil ->
%{}
vsn when vsn < {1, 8, 0} ->
%{calls: EctoPSQLExtras.CallsLegacy, outliers: EctoPSQLExtras.OutliersLegacy}
_vsn ->
%{calls: EctoPSQLExtras.Calls, outliers: EctoPSQLExtras.Outliers}
end
end
defp pg_stat_statements_version(repo) do
case query!(repo, @pg_stat_statements_query) do
%{rows: [[value]]} when is_binary(value) -> Postgrex.Utils.parse_version(value)
_ -> nil
end
end
@doc """
Run a query with `name`, on `repo`, in the given `format`.
The `repo` can be a module name or a tuple like `{module, node}`.
## Options
* `:format` - The format that results will return. Accepts `:ascii` or `:raw`.
If `:raw` a result struct will be returned. Otherwise it returns a nice
table printed in ASCII - a string. This option is required.
* `:args` - Overwrites the default arguments for the given query. You can
check the defaults of each query in its modules defined in this project.
"""
def query(name, repo, opts \\ []) do
query_module = Map.fetch!(queries(repo), name)
opts = prepare_opts(opts, query_module.info[:default_args])
result = query!(repo, query_module.query(Keyword.fetch!(opts, :args)))
format(
Keyword.fetch!(opts, :format),
query_module.info,
result
)
end
defp query!({repo, node}, query) do
case :rpc.call(node, repo, :query!, [query]) do
{:badrpc, {:EXIT, {:undef, _}}} ->
raise "repository is not defined on remote node"
{:badrpc, error} ->
raise "cannot send query to remote node #{inspect(node)}. Reason: #{inspect(error)}"
result ->
result
end
end
defp query!(repo, query) do
repo.query!(query)
end
@doc """
Run `bloat` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def bloat(repo, opts \\ []), do: query(:bloat, repo, opts)
@doc """
Run `blocking` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def blocking(repo, opts \\ []), do: query(:blocking, repo, opts)
@doc """
Run `cache_hit` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def cache_hit(repo, opts \\ []), do: query(:cache_hit, repo, opts)
@doc """
Run `db_settings` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def db_settings(repo, opts \\ []), do: query(:db_settings, repo, opts)
@doc """
Run `extensions` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def extensions(repo, opts \\ []), do: query(:extensions, repo, opts)
@doc """
Run `table_cache_hit` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def table_cache_hit(repo, opts \\ []), do: query(:table_cache_hit, repo, opts)
@doc """
Run `index_cache_hit` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def index_cache_hit(repo, opts \\ []), do: query(:index_cache_hit, repo, opts)
@doc """
Run `index_size` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def index_size(repo, opts \\ []), do: query(:index_size, repo, opts)
@doc """
Run `index_usage` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def index_usage(repo, opts \\ []), do: query(:index_usage, repo, opts)
@doc """
Run `locks` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def locks(repo, opts \\ []), do: query(:locks, repo, opts)
@doc """
Run `all_locks` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def all_locks(repo, opts \\ []), do: query(:all_locks, repo, opts)
@doc """
Run `long_running_queries` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def long_running_queries(repo, opts \\ []), do: query(:long_running_queries, repo, opts)
@doc """
Run `mandelbrot` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def mandelbrot(repo, opts \\ []), do: query(:mandelbrot, repo, opts)
@doc """
Run `records_rank` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def records_rank(repo, opts \\ []), do: query(:records_rank, repo, opts)
@doc """
Run `seq_scans` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def seq_scans(repo, opts \\ []), do: query(:seq_scans, repo, opts)
@doc """
Run `table_indexes_size` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def table_indexes_size(repo, opts \\ []), do: query(:table_indexes_size, repo, opts)
@doc """
Run `table_size` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def table_size(repo, opts \\ []), do: query(:table_size, repo, opts)
@doc """
Run `total_index_size` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def total_index_size(repo, opts \\ []), do: query(:total_index_size, repo, opts)
@doc """
Run `total_table_size` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def total_table_size(repo, opts \\ []), do: query(:total_table_size, repo, opts)
@doc """
Run `unused_indexes` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def unused_indexes(repo, opts \\ []), do: query(:unused_indexes, repo, opts)
@doc """
Run `null_indexes` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def null_indexes(repo, opts \\ []), do: query(:null_indexes, repo, opts)
@doc """
Run `vacuum_stats` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def vacuum_stats(repo, opts \\ []), do: query(:vacuum_stats, repo, opts)
@doc """
Run `kill_all` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def kill_all(repo, opts \\ []), do: query(:kill_all, repo, opts)
@doc """
Run `calls` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def calls(repo, opts \\ []), do: query(:calls, repo, opts)
@doc """
Run `outliers` query on `repo`, in the given `format`.
`format` is either `:ascii` or `:raw`
"""
def outliers(repo, opts \\ []), do: query(:outliers, repo, opts)
defp format(:ascii, info, result) do
names = Enum.map(info.columns, & &1.name)
types = Enum.map(info.columns, & &1.type)
rows =
if result.rows == [] do
[["No results", nil]]
else
Enum.map(result.rows, &parse_row(&1, types))
end
rows
|> TableRex.quick_render!(names, info.title)
|> IO.puts()
end
defp format(:raw, _info, result) do
result
end
defp parse_row(list, types) do
list
|> Enum.zip(types)
|> Enum.map(&format_value/1)
end
@doc false
def format_value({%struct{} = value, _}) when struct in [Decimal, Postgrex.Interval],
do: struct.to_string(value)
def format_value({nil, _}), do: ""
def format_value({number, :percent}), do: format_percent(number)
def format_value({integer, :bytes}) when is_integer(integer), do: format_bytes(integer)
def format_value({string, :string}), do: String.replace(string, "\n", "")
def format_value({binary, _}) when is_binary(binary), do: binary
def format_value({other, _}), do: inspect(other)
defp format_percent(number) do
number |> Kernel.*(100.0) |> Float.round(1) |> Float.to_string()
end
defp format_bytes(bytes) do
cond do
bytes >= memory_unit(:TB) -> format_bytes(bytes, :TB)
bytes >= memory_unit(:GB) -> format_bytes(bytes, :GB)
bytes >= memory_unit(:MB) -> format_bytes(bytes, :MB)
bytes >= memory_unit(:KB) -> format_bytes(bytes, :KB)
true -> format_bytes(bytes, :B)
end
end
defp format_bytes(bytes, :B) when is_integer(bytes), do: "#{bytes} bytes"
defp format_bytes(bytes, unit) when is_integer(bytes) do
value = bytes / memory_unit(unit)
"#{:erlang.float_to_binary(value, decimals: 1)} #{unit}"
end
defp memory_unit(:TB), do: 1024 * 1024 * 1024 * 1024
defp memory_unit(:GB), do: 1024 * 1024 * 1024
defp memory_unit(:MB), do: 1024 * 1024
defp memory_unit(:KB), do: 1024
defp prepare_opts(format, default_args) when is_atom(format) do
IO.warn "This API is deprecated. Please pass format value as a keyword list: `format: :raw`",
Macro.Env.stacktrace(__ENV__)
prepare_opts([format: format], default_args)
end
defp prepare_opts(opts, default_args) do
format = Keyword.get(opts, :format, :ascii)
args = Keyword.merge(
default_args || [],
opts[:args] || []
)
[
format: format,
args: args
]
end
end