Merge pull request #10341 from JimMoen/fix-undef-fun
fix: make emqx_api_lib compatible
This commit is contained in:
commit
f71e576bdd
|
@ -28,17 +28,17 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% exported API
|
%% exported API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec with_node(binary(), fun((atom()) -> {ok, term()} | {error, term()})) ->
|
-spec with_node(binary() | atom(), fun((atom()) -> {ok, term()} | {error, term()})) ->
|
||||||
?OK(term()) | ?NOT_FOUND(binary()) | ?BAD_REQUEST(term()).
|
?OK(term()) | ?NOT_FOUND(binary()) | ?BAD_REQUEST(term()).
|
||||||
with_node(BinNode, Fun) ->
|
with_node(Node0, Fun) ->
|
||||||
case lookup_node(BinNode) of
|
case lookup_node(Node0) of
|
||||||
{ok, Node} ->
|
{ok, Node} ->
|
||||||
handle_result(Fun(Node));
|
handle_result(Fun(Node));
|
||||||
not_found ->
|
not_found ->
|
||||||
?NODE_NOT_FOUND(BinNode)
|
?NODE_NOT_FOUND(Node0)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec with_node_or_cluster(binary(), fun((atom()) -> {ok, term()} | {error, term()})) ->
|
-spec with_node_or_cluster(binary() | atom(), fun((atom()) -> {ok, term()} | {error, term()})) ->
|
||||||
?OK(term()) | ?NOT_FOUND(iolist()) | ?BAD_REQUEST(term()).
|
?OK(term()) | ?NOT_FOUND(iolist()) | ?BAD_REQUEST(term()).
|
||||||
with_node_or_cluster(<<"all">>, Fun) ->
|
with_node_or_cluster(<<"all">>, Fun) ->
|
||||||
handle_result(Fun(all));
|
handle_result(Fun(all));
|
||||||
|
@ -49,18 +49,24 @@ with_node_or_cluster(Node, Fun) ->
|
||||||
%% Internal
|
%% Internal
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec lookup_node(binary()) -> {ok, atom()} | not_found.
|
-spec lookup_node(atom() | binary()) -> {ok, atom()} | not_found.
|
||||||
lookup_node(BinNode) ->
|
lookup_node(BinNode) when is_binary(BinNode) ->
|
||||||
case emqx_misc:safe_to_existing_atom(BinNode, utf8) of
|
case emqx_misc:safe_to_existing_atom(BinNode, utf8) of
|
||||||
{ok, Node} ->
|
{ok, Node} ->
|
||||||
|
is_running_node(Node);
|
||||||
|
_Error ->
|
||||||
|
not_found
|
||||||
|
end;
|
||||||
|
lookup_node(Node) when is_atom(Node) ->
|
||||||
|
is_running_node(Node).
|
||||||
|
|
||||||
|
-spec is_running_node(atom()) -> {ok, atom()} | not_found.
|
||||||
|
is_running_node(Node) ->
|
||||||
case lists:member(Node, mria:running_nodes()) of
|
case lists:member(Node, mria:running_nodes()) of
|
||||||
true ->
|
true ->
|
||||||
{ok, Node};
|
{ok, Node};
|
||||||
false ->
|
false ->
|
||||||
not_found
|
not_found
|
||||||
end;
|
|
||||||
_Error ->
|
|
||||||
not_found
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_result({ok, Result}) ->
|
handle_result({ok, Result}) ->
|
||||||
|
|
|
@ -133,7 +133,7 @@ dashboard_samplers_fun(Latest) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
monitor_current(get, #{bindings := []}) ->
|
monitor_current(get, #{bindings := []}) ->
|
||||||
with_node(erlang:node(), fun emqx_dashboard_monitor:current_rate/1);
|
emqx_api_lib:with_node_or_cluster(erlang:node(), fun emqx_dashboard_monitor:current_rate/1);
|
||||||
monitor_current(get, #{bindings := Bindings}) ->
|
monitor_current(get, #{bindings := Bindings}) ->
|
||||||
RawNode = maps:get(node, Bindings, <<"all">>),
|
RawNode = maps:get(node, Bindings, <<"all">>),
|
||||||
emqx_api_lib:with_node_or_cluster(RawNode, fun current_rate/1).
|
emqx_api_lib:with_node_or_cluster(RawNode, fun current_rate/1).
|
||||||
|
|
Loading…
Reference in New Issue