fix handler
This commit is contained in:
parent
a425947db4
commit
148773241c
@ -86,7 +86,23 @@ handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) ->
|
|||||||
Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0),
|
Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0),
|
||||||
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1),
|
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1),
|
||||||
|
|
||||||
{ok, 200, iot_util:json_data(HostInfo2)}
|
%% 获取部署应用场景
|
||||||
|
{ok, DeployList0} = scenario_deploy_model:get_host_deploy_list(HostId),
|
||||||
|
DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0),
|
||||||
|
%% 获取部署的场景信息
|
||||||
|
DeployList1 = lists:map(fun(DeployInfo = #{<<"scenario_id">> := ScenarioId}) ->
|
||||||
|
case scenario_model:get_scenario(ScenarioId) of
|
||||||
|
{ok, Scenario} ->
|
||||||
|
ScenarioInfo = scenario_model:to_map(Scenario),
|
||||||
|
DeployInfo#{<<"scenario_info">> => ScenarioInfo};
|
||||||
|
undefined ->
|
||||||
|
DeployInfo#{<<"scenario_info">> => #{}}
|
||||||
|
end
|
||||||
|
end, DeployList),
|
||||||
|
|
||||||
|
HostInfo3 = maps:put(<<"deploy_list">>, HostInfo2, DeployList1),
|
||||||
|
|
||||||
|
{ok, 200, iot_util:json_data(HostInfo3)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_request("POST", "/host/update", _, _Params) ->
|
handle_request("POST", "/host/update", _, _Params) ->
|
||||||
|
|||||||
@ -31,58 +31,76 @@ handle_request(_, "/scenario/list", Params, PostParams) ->
|
|||||||
|
|
||||||
MatchHead = #scenario{name = '$1', _ = '_'},
|
MatchHead = #scenario{name = '$1', _ = '_'},
|
||||||
Guard = [],
|
Guard = [],
|
||||||
Guard1 = case Model =/= <<"">> of
|
Guard1 = case Name =/= <<"">> of
|
||||||
true ->
|
true ->
|
||||||
[{'=:=', '$1', Model}|Guard];
|
[{'=:=', '$1', Name}|Guard];
|
||||||
false ->
|
false ->
|
||||||
Guard
|
Guard
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Guard2 = case CellId1 > 0 of
|
|
||||||
true ->
|
|
||||||
[{'=:=', '$2', CellId1}|Guard1];
|
|
||||||
false ->
|
|
||||||
Guard1
|
|
||||||
end,
|
|
||||||
|
|
||||||
Result = ['$_'],
|
Result = ['$_'],
|
||||||
|
|
||||||
case host_model:get_hosts({MatchHead, Guard2, Result}, Start, Size) of
|
case scenario_model:get_scenario_list({MatchHead, Guard1, Result}, Start, Size) of
|
||||||
{ok, Hosts, TotalNum} ->
|
{ok, ScenarioList, TotalNum} ->
|
||||||
Response = #{
|
Response = #{
|
||||||
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
|
<<"scenarios">> => lists:map(fun(Host) -> scenario_model:to_map(Host) end, ScenarioList),
|
||||||
<<"stat">> => host_model:get_stat(),
|
|
||||||
<<"total_num">> => TotalNum
|
<<"total_num">> => TotalNum
|
||||||
},
|
},
|
||||||
|
|
||||||
lager:debug("resp is: ~p", [Response]),
|
|
||||||
|
|
||||||
{ok, 200, iot_util:json_data(Response)};
|
{ok, 200, iot_util:json_data(Response)};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:warning("[host_handler] get a error: ~p", [Reason]),
|
lager:warning("[http_scenario_handler] get a error: ~p", [Reason]),
|
||||||
{ok, 200, iot_util:json_error(404, <<"database error">>)}
|
{ok, 200, iot_util:json_error(404, <<"database error">>)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_request("GET", "/host/detail", #{<<"id">> := HostId}, _) ->
|
handle_request("GET", "/scenario/detail", #{<<"id">> := ScenarioId0}, _) ->
|
||||||
lager:debug("[host_handler] detail id is: ~p", [HostId]),
|
ScenarioId = binary_to_integer(ScenarioId0),
|
||||||
case host_model:get_host(HostId) of
|
case scenario_model:get_scenario(ScenarioId) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{ok, 200, iot_util:json_error(404, <<"host not found">>)};
|
{ok, 200, iot_util:json_error(404, <<"scenario not found">>)};
|
||||||
|
{ok, Scenario} ->
|
||||||
|
ScenarioInfo = scenario_model:to_map(Scenario),
|
||||||
|
%% 获取场景下部署的主机列表
|
||||||
|
{ok, DeployList0} = scenario_deploy_model:get_scenario_deploy_list(ScenarioId),
|
||||||
|
DeployList = lists:map(fun(E) -> scenario_deploy_model:to_map(E) end, DeployList0),
|
||||||
|
ScenarioInfo1 = maps:put(<<"deploy_list">>, ScenarioInfo, DeployList),
|
||||||
|
%% 获取部署的主机信息
|
||||||
|
ScenarioInfo2 = lists:map(fun(Info = #{<<"host_id">> := HostId}) ->
|
||||||
|
case host_model:get_host(HostId) of
|
||||||
{ok, Host} ->
|
{ok, Host} ->
|
||||||
HostInfo = host_model:to_map(Host),
|
HostInfo = host_model:to_map(Host),
|
||||||
%% 获取终端信息
|
Info#{<<"host_info">> => HostInfo};
|
||||||
{ok, Terminals0} = terminal_model:get_host_terminals(HostId),
|
undefined ->
|
||||||
Terminals = lists:map(fun(E) -> terminal_model:to_map(E) end, Terminals0),
|
Info#{<<"host_info">> => #{}}
|
||||||
HostInfo1 = maps:put(<<"terminals">>, Terminals, HostInfo),
|
end
|
||||||
%% 获取微服务信息
|
end, ScenarioInfo1),
|
||||||
{ok, Services0} = service_model:get_host_services(HostId),
|
|
||||||
Services = lists:map(fun(S) -> service_model:to_map(S) end, Services0),
|
|
||||||
HostInfo2 = maps:put(<<"services">>, Services, HostInfo1),
|
|
||||||
|
|
||||||
{ok, 200, iot_util:json_data(HostInfo2)}
|
{ok, 200, iot_util:json_data(ScenarioInfo2)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_request("POST", "/host/update", _, _Params) ->
|
handle_request("POST", "/scenario/change_status", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) ->
|
||||||
lager:debug("[host_handler] post params is: ~p", [_Params]),
|
case scenario_model:change_status(ScenarioId, Status) of
|
||||||
|
ok ->
|
||||||
|
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||||
|
{error, Reason} when is_binary(Reason) ->
|
||||||
|
lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]),
|
||||||
|
{ok, 200, iot_util:json_error(404, Reason)}
|
||||||
|
end;
|
||||||
|
|
||||||
{ok, 200, iot_util:json_data(<<"success">>)}.
|
%% 部署
|
||||||
|
handle_request("POST", "/scenario/deploy", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) ->
|
||||||
|
case scenario_model:change_status(ScenarioId, Status) of
|
||||||
|
ok ->
|
||||||
|
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||||
|
{error, Reason} when is_binary(Reason) ->
|
||||||
|
lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]),
|
||||||
|
{ok, 200, iot_util:json_error(404, Reason)}
|
||||||
|
end;
|
||||||
|
|
||||||
|
%% 删除??
|
||||||
|
handle_request("POST", "/scenario/change_status", _, #{<<"scenario_id">> := ScenarioId, <<"status">> := Status}) when is_integer(ScenarioId), is_integer(Status) ->
|
||||||
|
case scenario_model:change_status(ScenarioId, Status) of
|
||||||
|
ok ->
|
||||||
|
{ok, 200, iot_util:json_data(<<"success">>)};
|
||||||
|
{error, Reason} when is_binary(Reason) ->
|
||||||
|
lager:warning("[http_scenario_handler] change_status get a error: ~p", [Reason]),
|
||||||
|
{ok, 200, iot_util:json_error(404, Reason)}
|
||||||
|
end.
|
||||||
@ -47,7 +47,7 @@ change_status(ScenarioId, Status) when is_integer(ScenarioId), is_integer(Status
|
|||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
case mnesia:read(scenario, ScenarioId) of
|
case mnesia:read(scenario, ScenarioId) of
|
||||||
[] ->
|
[] ->
|
||||||
mnesia:abort(<<"host not found">>);
|
mnesia:abort(<<"scenario not found">>);
|
||||||
[Scenario] ->
|
[Scenario] ->
|
||||||
mnesia:write(scenario, Scenario#scenario{status = Status}, write)
|
mnesia:write(scenario, Scenario#scenario{status = Status}, write)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user