fix handler
This commit is contained in:
parent
70386fbed2
commit
35041b9708
@ -8,6 +8,7 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(http_host_handler).
|
-module(http_host_handler).
|
||||||
-author("licheng5").
|
-author("licheng5").
|
||||||
|
-include("iot.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([handle_request/4]).
|
-export([handle_request/4]).
|
||||||
@ -16,7 +17,7 @@
|
|||||||
%% helper methods
|
%% helper methods
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
handle_request(_, "/host/list", Params, _) ->
|
handle_request(_, "/host/list", Params, PostParams) ->
|
||||||
Page0 = maps:get(<<"page">>, Params, <<"1">>),
|
Page0 = maps:get(<<"page">>, Params, <<"1">>),
|
||||||
Size0 = maps:get(<<"size">>, Params, <<"10">>),
|
Size0 = maps:get(<<"size">>, Params, <<"10">>),
|
||||||
Page = binary_to_integer(Page0),
|
Page = binary_to_integer(Page0),
|
||||||
@ -25,16 +26,42 @@ handle_request(_, "/host/list", Params, _) ->
|
|||||||
true = Page > 0 andalso Size > 0,
|
true = Page > 0 andalso Size > 0,
|
||||||
Start = (Page - 1) * Size,
|
Start = (Page - 1) * Size,
|
||||||
|
|
||||||
case host_model:get_hosts(Start, Size) of
|
%% 处理查询条件
|
||||||
{ok, Hosts, Stat} ->
|
Model = maps:get(<<"model">>, PostParams, <<"">>),
|
||||||
|
CellId = maps:get(<<"cell_id">>, PostParams, <<"">>),
|
||||||
|
CellId1 = case CellId =/= <<>> of
|
||||||
|
true ->
|
||||||
|
binary_to_integer(CellId);
|
||||||
|
false ->
|
||||||
|
0
|
||||||
|
end,
|
||||||
|
|
||||||
|
%% 过滤函数
|
||||||
|
Filter = fun(#host{model = Model0, cell_id = CellId0}) ->
|
||||||
|
lists:all(fun(E) ->
|
||||||
|
case E of
|
||||||
|
{model, M} ->
|
||||||
|
lager:debug("m: ~p, mid: ~p", [M, Model0]),
|
||||||
|
case M =/= <<"">> of
|
||||||
|
true -> M =:= Model0;
|
||||||
|
_ -> true
|
||||||
|
end;
|
||||||
|
{cell_id, C} ->
|
||||||
|
lager:debug("c: ~p, cid: ~p", [C, CellId0]),
|
||||||
|
case C > 0 of
|
||||||
|
true -> C == CellId0;
|
||||||
|
_ -> true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end, [{model, Model}, {cell_id, CellId1}])
|
||||||
|
end,
|
||||||
|
|
||||||
|
case host_model:get_hosts(Filter, Start, Size) of
|
||||||
|
{ok, Hosts, TotalNum} ->
|
||||||
Response = #{
|
Response = #{
|
||||||
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
|
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
|
||||||
<<"stat">> => lists:map(fun({Status, Num}) ->
|
<<"stat">> => host_model:get_stat(),
|
||||||
#{
|
<<"total_num">> => TotalNum
|
||||||
<<"status">> => Status,
|
|
||||||
<<"num">> => Num
|
|
||||||
}
|
|
||||||
end, maps:to_list(Stat))
|
|
||||||
},
|
},
|
||||||
|
|
||||||
lager:debug("resp is: ~p", [Response]),
|
lager:debug("resp is: ~p", [Response]),
|
||||||
|
|||||||
@ -21,7 +21,7 @@ insert_hosts() ->
|
|||||||
host_id = integer_to_binary(Id0),
|
host_id = integer_to_binary(Id0),
|
||||||
name = <<"N1000_0001">>,
|
name = <<"N1000_0001">>,
|
||||||
model = <<"N1000">>,
|
model = <<"N1000">>,
|
||||||
cell_id = <<"公共教学楼"/utf8>>
|
cell_id = rand:uniform(100)
|
||||||
},
|
},
|
||||||
|
|
||||||
host_model:add_host(Host)
|
host_model:add_host(Host)
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
-include_lib("stdlib/include/qlc.hrl").
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
|
-export([get_host/1, get_hosts/3, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
|
||||||
-export([to_map/1]).
|
-export([to_map/1]).
|
||||||
|
|
||||||
get_host(HostId) when is_binary(HostId) ->
|
get_host(HostId) when is_binary(HostId) ->
|
||||||
@ -24,32 +24,42 @@ get_host(HostId) when is_binary(HostId) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
-spec get_hosts(Start :: integer(), Limit :: integer()) ->
|
-spec get_hosts(Filter :: fun(), Start :: integer(), Limit :: integer()) ->
|
||||||
{ok, Items :: list(), Stat :: maps:map()} |
|
{ok, Items :: list(), TotalNum :: integer()} |
|
||||||
{error, Reason :: any()}.
|
{error, Reason :: any()}.
|
||||||
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
get_hosts(Filter, Start, Limit) when is_function(Filter, 1), is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(host)]),
|
mnesia:foldl(fun(Host, Acc) ->
|
||||||
QC = qlc:cursor(Q),
|
case Filter(Host) of
|
||||||
case qlc:next_answers(QC, Start + Limit) of
|
true -> [Host|Acc];
|
||||||
Answers when is_list(Answers) ->
|
false -> Acc
|
||||||
lists:sublist(Answers, Start + 1, Limit);
|
end
|
||||||
_ ->
|
end, [], host)
|
||||||
[]
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
{atomic, Items} when is_list(Items) ->
|
{atomic, Items0} when is_list(Items0) ->
|
||||||
%% 按照状态分组统计
|
Items = lists:reverse(Items0),
|
||||||
Stat = lists:foldl(fun(#host{status = Status}, Acc) ->
|
NItems = lists:sublist(Items, Start + 1, Limit),
|
||||||
Num = maps:get(Status, Acc, 0),
|
{ok, NItems, length(Items)};
|
||||||
Acc#{Status => Num + 1}
|
|
||||||
end, #{}, Items),
|
|
||||||
{ok, Items, Stat};
|
|
||||||
{aborted, Error} ->
|
{aborted, Error} ->
|
||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% 按照状态分组统计
|
||||||
|
get_stat() ->
|
||||||
|
Fun = fun() ->
|
||||||
|
mnesia:foldl(fun(#host{status = Status}, Acc) ->
|
||||||
|
Num = maps:get(Status, Acc, 0),
|
||||||
|
Acc#{Status => Num + 1}
|
||||||
|
end, #{}, host)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, Stat} when is_map(Stat) ->
|
||||||
|
lists:map(fun({Status, Num}) -> #{<<"status">> => Status, <<"num">> => Num} end, maps:to_list(Stat));
|
||||||
|
{aborted, _} ->
|
||||||
|
#{}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec find_hosts(Pred :: fun((#host{}) -> boolean()), Start :: integer(), Limit :: integer()) ->
|
-spec find_hosts(Pred :: fun((#host{}) -> boolean()), Start :: integer(), Limit :: integer()) ->
|
||||||
{ok, Items :: [#host{}], Num :: integer()} |
|
{ok, Items :: [#host{}], Num :: integer()} |
|
||||||
{error, Reason :: any()}.
|
{error, Reason :: any()}.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user