fix: support custom count function

This commit is contained in:
zhongwencool 2022-08-04 10:23:26 +08:00
parent 6a1a784ee1
commit 1d9ee25c92
2 changed files with 21 additions and 4 deletions

View File

@ -132,7 +132,11 @@
list_clientid(_Bindings, Params) ->
SortFun = fun(#{created_at := C1}, #{created_at := C2}) -> C1 > C2 end,
return({ok, emqx_mgmt_api:node_query(node(), Params, ?CLIENTID_SCHEMA, ?query_clientid, SortFun)}).
CountFun = fun() ->
MatchSpec = [{{?TABLE, {clientid, '_'}, '$1', '$2'}, [], [true]}],
ets:select_count(?TABLE, MatchSpec)
end,
return({ok, emqx_mgmt_api:node_query(node(), Params, ?CLIENTID_SCHEMA, ?query_clientid, SortFun, CountFun)}).
lookup_clientid(#{clientid := Clientid}, _Params) ->
return({ok, format(emqx_auth_mnesia_cli:lookup_user({clientid, urldecode(Clientid)}))}).
@ -182,7 +186,11 @@ delete_clientid(#{clientid := Clientid}, _) ->
list_username(_Bindings, Params) ->
SortFun = fun(#{created_at := C1}, #{created_at := C2}) -> C1 > C2 end,
return({ok, emqx_mgmt_api:node_query(node(), Params, ?USERNAME_SCHEMA, ?query_username, SortFun)}).
CountFun = fun() ->
MatchSpec = [{{?TABLE, {username, '_'}, '$1', '$2'}, [], [true]}],
ets:select_count(?TABLE, MatchSpec)
end,
return({ok, emqx_mgmt_api:node_query(node(), Params, ?USERNAME_SCHEMA, ?query_username, SortFun, CountFun)}).
lookup_username(#{username := Username}, _Params) ->
return({ok, format(emqx_auth_mnesia_cli:lookup_user({username, urldecode(Username)}))}).

View File

@ -24,6 +24,7 @@
-export([ params2qs/2
, node_query/4
, node_query/5
, node_query/6
, cluster_query/3
, traverse_table/5
, select_table/5
@ -59,6 +60,11 @@ query_handle([Table]) when is_atom(Table) ->
query_handle(Tables) ->
qlc:append([qlc:q([E || E <- ets:table(T)]) || T <- Tables]).
count_size(Table, undefined) ->
count(Table);
count_size(_Table, CountFun) ->
CountFun().
count(Table) when is_atom(Table) ->
ets:info(Table, size);
count([Table]) when is_atom(Table) ->
@ -83,9 +89,12 @@ limit(Params) ->
%%--------------------------------------------------------------------
node_query(Node, Params, {Tab, QsSchema}, QueryFun) ->
node_query(Node, Params, {Tab, QsSchema}, QueryFun, undefined).
node_query(Node, Params, {Tab, QsSchema}, QueryFun, undefined, undefined).
node_query(Node, Params, {Tab, QsSchema}, QueryFun, SortFun) ->
node_query(Node, Params, {Tab, QsSchema}, QueryFun, SortFun, undefined).
node_query(Node, Params, {Tab, QsSchema}, QueryFun, SortFun, CountFun) ->
{CodCnt, Qs} = params2qs(Params, QsSchema),
Limit = limit(Params),
Page = page(Params),
@ -95,7 +104,7 @@ node_query(Node, Params, {Tab, QsSchema}, QueryFun, SortFun) ->
{_, Rows} = do_query(Node, Qs, QueryFun, Start, Limit+1),
Meta = #{page => Page, limit => Limit},
NMeta = case CodCnt =:= 0 of
true -> Meta#{count => count(Tab), hasnext => length(Rows) > Limit};
true -> Meta#{count => count_size(Tab, CountFun), hasnext => length(Rows) > Limit};
_ -> Meta#{count => -1, hasnext => length(Rows) > Limit}
end,
Data0 = lists:sublist(Rows, Limit),