Merge pull request #10341 from JimMoen/fix-undef-fun

fix: make emqx_api_lib compatible
This commit is contained in:
JimMoen 2023-04-06 17:47:40 +08:00 committed by GitHub
commit f71e576bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 14 deletions

View File

@ -28,17 +28,17 @@
%%--------------------------------------------------------------------
%% 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()).
with_node(BinNode, Fun) ->
case lookup_node(BinNode) of
with_node(Node0, Fun) ->
case lookup_node(Node0) of
{ok, Node} ->
handle_result(Fun(Node));
not_found ->
?NODE_NOT_FOUND(BinNode)
?NODE_NOT_FOUND(Node0)
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()).
with_node_or_cluster(<<"all">>, Fun) ->
handle_result(Fun(all));
@ -49,18 +49,24 @@ with_node_or_cluster(Node, Fun) ->
%% Internal
%%--------------------------------------------------------------------
-spec lookup_node(binary()) -> {ok, atom()} | not_found.
lookup_node(BinNode) ->
-spec lookup_node(atom() | binary()) -> {ok, atom()} | not_found.
lookup_node(BinNode) when is_binary(BinNode) ->
case emqx_misc:safe_to_existing_atom(BinNode, utf8) of
{ok, Node} ->
case lists:member(Node, mria:running_nodes()) of
true ->
{ok, Node};
false ->
not_found
end;
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
true ->
{ok, Node};
false ->
not_found
end.
handle_result({ok, Result}) ->

View File

@ -133,7 +133,7 @@ dashboard_samplers_fun(Latest) ->
end.
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}) ->
RawNode = maps:get(node, Bindings, <<"all">>),
emqx_api_lib:with_node_or_cluster(RawNode, fun current_rate/1).