1 |
|
-module(mongoose_graphql_metric_admin_query). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
|
6 |
|
-ignore_xref([execute/4]). |
7 |
|
|
8 |
|
execute(_Ctx, _Obj, <<"getMetrics">>, Args) -> |
9 |
22 |
Name = get_name(Args), |
10 |
22 |
mongoose_metrics_api:get_metrics(Name); |
11 |
|
execute(_Ctx, _Obj, <<"getMetricsAsDicts">>, Args) -> |
12 |
18 |
Name = get_name(Args), |
13 |
18 |
Keys = get_keys2(Args), |
14 |
18 |
mongoose_metrics_api:get_metrics_as_dicts(Name, Keys); |
15 |
|
execute(_Ctx, _Obj, <<"getClusterMetricsAsDicts">>, Args) -> |
16 |
24 |
Name = get_name(Args), |
17 |
24 |
Keys = get_keys2(Args), |
18 |
24 |
Nodes = get_nodes(Args), |
19 |
24 |
mongoose_metrics_api:get_cluster_metrics_as_dicts(Name, Keys, Nodes). |
20 |
|
|
21 |
|
%% get_keys is a BIF, so we have a name conflict |
22 |
|
get_keys2(Args) -> |
23 |
42 |
Keys = get_list(<<"keys">>, Args), |
24 |
42 |
lists:map(fun prepare_key/1, Keys). |
25 |
|
|
26 |
|
prepare_key(X) when is_binary(X) -> |
27 |
22 |
binary_to_atom(X); |
28 |
|
prepare_key(X) when is_integer(X) -> %% For percentiles |
29 |
:-( |
X. |
30 |
|
|
31 |
|
get_name(Args) -> |
32 |
64 |
Segments = get_list(<<"name">>, Args), |
33 |
64 |
lists:map(fun binary_to_atom/1, Segments). |
34 |
|
|
35 |
|
get_nodes(Args) -> |
36 |
24 |
Nodes = get_list(<<"nodes">>, Args), |
37 |
24 |
lists:map(fun binary_to_atom/1, Nodes). |
38 |
|
|
39 |
|
get_list(Key, Map) -> |
40 |
130 |
null_as_empty(maps:get(Key, Map, [])). |
41 |
|
|
42 |
42 |
null_as_empty(null) -> []; |
43 |
88 |
null_as_empty(X) -> X. |