代码简化
This commit is contained in:
parent
eea16209ef
commit
1b5dcd5411
@ -30,34 +30,15 @@ handle_request(_, "/host/list", Params, PostParams) ->
|
|||||||
Model = maps:get(<<"model">>, PostParams, <<"">>),
|
Model = maps:get(<<"model">>, PostParams, <<"">>),
|
||||||
CellId = maps:get(<<"cell_id">>, PostParams, <<"">>),
|
CellId = maps:get(<<"cell_id">>, PostParams, <<"">>),
|
||||||
CellId1 = case CellId =/= <<>> of
|
CellId1 = case CellId =/= <<>> of
|
||||||
true ->
|
true -> binary_to_integer(CellId);
|
||||||
binary_to_integer(CellId);
|
false -> 0
|
||||||
false ->
|
|
||||||
0
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
MatchHead = #host{model = '$1', cell_id = '$2', _ = '_'},
|
MatchSpec = host_model:match_spec([{model, Model}, {cell, CellId1}]),
|
||||||
Guard = [],
|
case host_model:get_hosts(MatchSpec, Start, Size) of
|
||||||
Guard1 = case Model =/= <<"">> of
|
|
||||||
true ->
|
|
||||||
[{'=:=', '$1', Model}|Guard];
|
|
||||||
false ->
|
|
||||||
Guard
|
|
||||||
end,
|
|
||||||
|
|
||||||
Guard2 = case CellId1 > 0 of
|
|
||||||
true ->
|
|
||||||
[{'=:=', '$2', CellId1}|Guard1];
|
|
||||||
false ->
|
|
||||||
Guard1
|
|
||||||
end,
|
|
||||||
|
|
||||||
Result = ['$_'],
|
|
||||||
|
|
||||||
case host_model:get_hosts({MatchHead, Guard2, Result}, Start, Size) of
|
|
||||||
{ok, Hosts, TotalNum} ->
|
{ok, Hosts, TotalNum} ->
|
||||||
Response = #{
|
Response = #{
|
||||||
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
|
<<"hosts">> => lists:map(fun host_model:to_map/1, Hosts),
|
||||||
<<"stat">> => host_model:get_stat(),
|
<<"stat">> => host_model:get_stat(),
|
||||||
<<"total_num">> => TotalNum
|
<<"total_num">> => TotalNum
|
||||||
},
|
},
|
||||||
@ -79,16 +60,16 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) ->
|
|||||||
HostInfo = host_model:to_map(Host),
|
HostInfo = host_model:to_map(Host),
|
||||||
%% 获取终端信息
|
%% 获取终端信息
|
||||||
{ok, Terminals0} = terminal_model:get_host_terminals(HostId),
|
{ok, Terminals0} = terminal_model:get_host_terminals(HostId),
|
||||||
Terminals = lists:map(fun(E) -> terminal_model:to_map(E) end, Terminals0),
|
Terminals = lists:map(fun terminal_model:to_map/1, Terminals0),
|
||||||
HostInfo1 = maps:put(<<"terminals">>, Terminals, HostInfo),
|
HostInfo1 = maps:put(<<"terminals">>, Terminals, HostInfo),
|
||||||
%% 获取微服务信息
|
%% 获取微服务信息
|
||||||
{ok, Services0} = service_model:get_host_services(HostId),
|
{ok, Services0} = service_model:get_host_services(HostId),
|
||||||
Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0),
|
Services = lists:map(fun service_model:to_map/1, Services0),
|
||||||
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1),
|
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1),
|
||||||
|
|
||||||
%% 获取部署应用场景
|
%% 获取部署应用场景
|
||||||
{ok, DeployList0} = scenario_deploy_model:get_host_deploy_list(HostId),
|
{ok, DeployList0} = scenario_deploy_model:get_host_deploy_list(HostId),
|
||||||
DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0),
|
DeployList = lists:map(fun scenario_deploy_model:to_map/1, DeployList0),
|
||||||
%% 获取部署的场景信息
|
%% 获取部署的场景信息
|
||||||
DeployList1 = lists:map(fun(DeployInfo = #{<<"scenario_id">> := ScenarioId}) ->
|
DeployList1 = lists:map(fun(DeployInfo = #{<<"scenario_id">> := ScenarioId}) ->
|
||||||
case scenario_model:get_scenario(ScenarioId) of
|
case scenario_model:get_scenario(ScenarioId) of
|
||||||
|
|||||||
@ -13,9 +13,6 @@
|
|||||||
%% API
|
%% API
|
||||||
-export([handle_request/4]).
|
-export([handle_request/4]).
|
||||||
|
|
||||||
handle_request("GET", "/api/booking", _, _Params) ->
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)};
|
|
||||||
|
|
||||||
%% 下发参数
|
%% 下发参数
|
||||||
handle_request("POST", "/iot/send_params", _, PostParams = #{<<"host_id">> := HostId, <<"service_name">> := ServiceName, <<"params">> := Params}) ->
|
handle_request("POST", "/iot/send_params", _, PostParams = #{<<"host_id">> := HostId, <<"service_name">> := ServiceName, <<"params">> := Params}) ->
|
||||||
lager:debug("body is: ~p", [PostParams]),
|
lager:debug("body is: ~p", [PostParams]),
|
||||||
|
|||||||
@ -59,12 +59,7 @@ init(Req0, Opts = [Mod|_]) ->
|
|||||||
|
|
||||||
%% 判断是否支持gzip
|
%% 判断是否支持gzip
|
||||||
supported_gzip(AcceptEncoding) when is_binary(AcceptEncoding) ->
|
supported_gzip(AcceptEncoding) when is_binary(AcceptEncoding) ->
|
||||||
case binary:match(AcceptEncoding, <<"gzip">>) of
|
binary:match(AcceptEncoding, <<"gzip">>) =/= nomatch.
|
||||||
nomatch ->
|
|
||||||
false;
|
|
||||||
_ ->
|
|
||||||
true
|
|
||||||
end.
|
|
||||||
|
|
||||||
parse_body(Req0) ->
|
parse_body(Req0) ->
|
||||||
ContentType = cowboy_req:header(<<"content-type">>, Req0),
|
ContentType = cowboy_req:header(<<"content-type">>, Req0),
|
||||||
|
|||||||
@ -29,18 +29,8 @@ handle_request(_, "/router/list", Params, PostParams) ->
|
|||||||
%% 处理查询条件
|
%% 处理查询条件
|
||||||
Name = maps:get(<<"name">>, PostParams, <<"">>),
|
Name = maps:get(<<"name">>, PostParams, <<"">>),
|
||||||
|
|
||||||
MatchHead = #router{name = '$1', _ = '_'},
|
MatchSpec = router_model:match_spec([{name, Name}]),
|
||||||
Guard = [],
|
case router_model:get_routers(MatchSpec, Start, Size) of
|
||||||
Guard1 = case Name =/= <<"">> of
|
|
||||||
true ->
|
|
||||||
[{'=:=', '$1', Name}|Guard];
|
|
||||||
false ->
|
|
||||||
Guard
|
|
||||||
end,
|
|
||||||
|
|
||||||
Result = ['$_'],
|
|
||||||
|
|
||||||
case router_model:get_routers({MatchHead, Guard1, Result}, Start, Size) of
|
|
||||||
{ok, Routers, TotalNum} ->
|
{ok, Routers, TotalNum} ->
|
||||||
Response = #{
|
Response = #{
|
||||||
<<"routers">> => lists:map(fun(R) -> router_model:to_map(R) end, Routers),
|
<<"routers">> => lists:map(fun(R) -> router_model:to_map(R) end, Routers),
|
||||||
|
|||||||
@ -29,20 +29,11 @@ handle_request(_, "/scenario/list", Params, PostParams) ->
|
|||||||
%% 处理查询条件
|
%% 处理查询条件
|
||||||
Name = maps:get(<<"name">>, PostParams, <<"">>),
|
Name = maps:get(<<"name">>, PostParams, <<"">>),
|
||||||
|
|
||||||
MatchHead = #scenario{name = '$1', _ = '_'},
|
MatchSpec = scenario_model:match_spec([{name, Name}]),
|
||||||
Guard = [],
|
case scenario_model:get_scenario_list(MatchSpec, Start, Size) of
|
||||||
Guard1 = case Name =/= <<"">> of
|
|
||||||
true ->
|
|
||||||
[{'=:=', '$1', Name}|Guard];
|
|
||||||
false ->
|
|
||||||
Guard
|
|
||||||
end,
|
|
||||||
Result = ['$_'],
|
|
||||||
|
|
||||||
case scenario_model:get_scenario_list({MatchHead, Guard1, Result}, Start, Size) of
|
|
||||||
{ok, ScenarioList, TotalNum} ->
|
{ok, ScenarioList, TotalNum} ->
|
||||||
Response = #{
|
Response = #{
|
||||||
<<"scenarios">> => lists:map(fun(Host) -> scenario_model:to_map(Host) end, ScenarioList),
|
<<"scenarios">> => lists:map(fun scenario_model:to_map/1, ScenarioList),
|
||||||
<<"total_num">> => TotalNum
|
<<"total_num">> => TotalNum
|
||||||
},
|
},
|
||||||
{ok, 200, iot_util:json_data(Response)};
|
{ok, 200, iot_util:json_data(Response)};
|
||||||
@ -60,7 +51,7 @@ handle_request("GET", "/scenario/detail", #{<<"id">> := ScenarioId0}, _) ->
|
|||||||
ScenarioInfo = scenario_model:to_map(Scenario),
|
ScenarioInfo = scenario_model:to_map(Scenario),
|
||||||
%% 获取场景下部署的主机列表
|
%% 获取场景下部署的主机列表
|
||||||
{ok, DeployList0} = scenario_deploy_model:get_scenario_deploy_list(ScenarioId),
|
{ok, DeployList0} = scenario_deploy_model:get_scenario_deploy_list(ScenarioId),
|
||||||
DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0),
|
DeployList = lists:map(fun scenario_deploy_model:to_map/1, DeployList0),
|
||||||
ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList),
|
ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList),
|
||||||
%% 获取部署的主机信息
|
%% 获取部署的主机信息
|
||||||
ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) ->
|
ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) ->
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_host/1, get_hosts/3, get_all_hosts/0, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3, activate/1]).
|
-export([get_host/1, get_hosts/3, get_all_hosts/0, get_stat/0, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3, activate/1]).
|
||||||
-export([to_map/1]).
|
-export([to_map/1, match_spec/1]).
|
||||||
|
|
||||||
get_host(HostId) when is_binary(HostId) ->
|
get_host(HostId) when is_binary(HostId) ->
|
||||||
case mnesia:dirty_read(host, HostId) of
|
case mnesia:dirty_read(host, HostId) of
|
||||||
@ -148,6 +148,24 @@ delete(HostId) when is_binary(HostId) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% 获取查询的过滤条件
|
||||||
|
match_spec(Specs) when is_list(Specs) ->
|
||||||
|
MatchHead = #host{model = '$1', cell_id = '$2', _ = '_'},
|
||||||
|
Guard = guard(Specs),
|
||||||
|
Result = ['$_'],
|
||||||
|
{MatchHead, Guard, Result}.
|
||||||
|
|
||||||
|
guard(Specs) when is_list(Specs) ->
|
||||||
|
guard(Specs, []).
|
||||||
|
guard([], Guard) ->
|
||||||
|
Guard;
|
||||||
|
guard([{model, Model}|Tail], Guard) when Model =/= <<"">> ->
|
||||||
|
guard(Tail, [{'=:=', '$1', Model}|Guard]);
|
||||||
|
guard([{cell, CellId}|Tail], Guard) when CellId > 0 ->
|
||||||
|
guard(Tail, [{'=:=', '$2', CellId}|Guard]);
|
||||||
|
guard([_|Tail], Guard) ->
|
||||||
|
guard(Tail, Guard).
|
||||||
|
|
||||||
%% 获取app表的数据大小
|
%% 获取app表的数据大小
|
||||||
table_size() ->
|
table_size() ->
|
||||||
mnesia:table_info(host, size).
|
mnesia:table_info(host, size).
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0, get_all_routers/0, get_all_valid_routers/0]).
|
-export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0, get_all_routers/0, get_all_valid_routers/0]).
|
||||||
-export([to_map/1]).
|
-export([to_map/1, match_spec/1]).
|
||||||
|
|
||||||
get_router(RouterId) when is_integer(RouterId) ->
|
get_router(RouterId) when is_integer(RouterId) ->
|
||||||
case mnesia:dirty_read(router, RouterId) of
|
case mnesia:dirty_read(router, RouterId) of
|
||||||
@ -96,6 +96,23 @@ delete(RouterId) when is_integer(RouterId) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% 获取查询的过滤条件
|
||||||
|
match_spec(Specs) when is_list(Specs) ->
|
||||||
|
MatchHead = #router{name = '$1', _ = '_'},
|
||||||
|
Result = ['$_'],
|
||||||
|
Guard = guard(Specs),
|
||||||
|
Result = ['$_'],
|
||||||
|
{MatchHead, Guard, Result}.
|
||||||
|
|
||||||
|
guard(Specs) when is_list(Specs) ->
|
||||||
|
guard(Specs, []).
|
||||||
|
guard([], Guard) ->
|
||||||
|
Guard;
|
||||||
|
guard([{name, Name}|Tail], Guard) when Name =/= <<"">> ->
|
||||||
|
guard(Tail, [{'=:=', '$1', Name}|Guard]);
|
||||||
|
guard([_|Tail], Guard) ->
|
||||||
|
guard(Tail, Guard).
|
||||||
|
|
||||||
%% 获取app表的数据大小
|
%% 获取app表的数据大小
|
||||||
table_size() ->
|
table_size() ->
|
||||||
mnesia:table_info(router, size).
|
mnesia:table_info(router, size).
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_scenario/1, get_scenario_list/3, add_scenario/1, change_status/2, delete/1, table_size/0]).
|
-export([get_scenario/1, get_scenario_list/3, add_scenario/1, change_status/2, delete/1, table_size/0]).
|
||||||
-export([to_map/1]).
|
-export([to_map/1, match_spec/1]).
|
||||||
|
|
||||||
get_scenario(ScenarioId) when is_integer(ScenarioId) ->
|
get_scenario(ScenarioId) when is_integer(ScenarioId) ->
|
||||||
case mnesia:dirty_read(scenario, ScenarioId) of
|
case mnesia:dirty_read(scenario, ScenarioId) of
|
||||||
@ -68,6 +68,23 @@ delete(ScenarioId) when is_integer(ScenarioId) ->
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% 获取查询的过滤条件
|
||||||
|
match_spec(Specs) when is_list(Specs) ->
|
||||||
|
MatchHead = #scenario{name = '$1', _ = '_'},
|
||||||
|
Result = ['$_'],
|
||||||
|
Guard = guard(Specs),
|
||||||
|
Result = ['$_'],
|
||||||
|
{MatchHead, Guard, Result}.
|
||||||
|
|
||||||
|
guard(Specs) when is_list(Specs) ->
|
||||||
|
guard(Specs, []).
|
||||||
|
guard([], Guard) ->
|
||||||
|
Guard;
|
||||||
|
guard([{name, Name}|Tail], Guard) when Name =/= <<"">> ->
|
||||||
|
guard(Tail, [{'=:=', '$1', Name}|Guard]);
|
||||||
|
guard([_|Tail], Guard) ->
|
||||||
|
guard(Tail, Guard).
|
||||||
|
|
||||||
%% 获取app表的数据大小
|
%% 获取app表的数据大小
|
||||||
table_size() ->
|
table_size() ->
|
||||||
mnesia:table_info(scenario, size).
|
mnesia:table_info(scenario, size).
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user