From e25469d1c5b57f9abf0b8000d00f55eb768bf57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Wed, 19 Apr 2023 14:55:34 +0800 Subject: [PATCH] fix model --- apps/iot/include/iot.hrl | 4 +- .../iot/src/http_handler/http_api_handler.erl | 23 ---- apps/iot/src/model/host_model.erl | 113 +++++++++++------- apps/iot/src/model/id_generator_model.erl | 2 + apps/iot/src/model/issue_model.erl | 29 ++++- apps/iot/src/model/router_model.erl | 34 ++++-- apps/iot/src/model/scenario_deploy_model.erl | 21 ++-- apps/iot/src/model/scenario_model.erl | 29 +++-- apps/iot/src/model/terminal_model.erl | 52 ++++---- 9 files changed, 181 insertions(+), 126 deletions(-) delete mode 100644 apps/iot/src/http_handler/http_api_handler.erl diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index c82720e..3c334ec 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -122,9 +122,11 @@ %% 终端设备表 -record(terminal, { %% 设备ID - terminal_id :: binary(), + terminal_id :: integer(), %% 关联主机 host_id :: binary(), + %% 序列号 + serial_number = <<>> :: binary(), %% 名称 name :: binary(), %% 设备编码 diff --git a/apps/iot/src/http_handler/http_api_handler.erl b/apps/iot/src/http_handler/http_api_handler.erl deleted file mode 100644 index 24c3e96..0000000 --- a/apps/iot/src/http_handler/http_api_handler.erl +++ /dev/null @@ -1,23 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author licheng5 -%%% @copyright (C) 2020, -%%% @doc -%%% -%%% @end -%%% Created : 26. 4月 2020 3:36 下午 -%%%------------------------------------------------------------------- --module(http_api_handler). --author("licheng5"). - -%% API --export([handle_request/4]). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -handle_request("GET", "/api/booking", _, _Params) -> - {ok, 200, iot_util:json_data(<<"success">>)}; -handle_request("POST", _, _, Params) -> - lager:debug("body is: ~p", [Params]), - {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index 32ef9af..74c3213 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -30,7 +30,7 @@ get_all_hosts() -> end, case mnesia:transaction(Fun) of {atomic, Items} when is_list(Items) -> - {ok, Items}; + {ok, sort(Items)}; {aborted, Error} -> {error, Error} end. @@ -40,10 +40,16 @@ get_all_hosts() -> {ok, Items :: list(), TotalNum :: integer()} | {error, Reason :: any()}. get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> - Items0 = mnesia:dirty_select(host, [Spec]), - Items = lists:reverse(Items0), - NItems = lists:sublist(Items, Start + 1, Limit), - {ok, NItems, length(Items)}. + Hosts0 = mnesia:dirty_select(host, [Spec]), + %% TODO 处理host的排序方式 + SortedHosts = sort(Hosts0), + Len = length(SortedHosts), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(SortedHosts, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end. %% 按照状态分组统计 get_stat() -> @@ -75,19 +81,25 @@ find_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), is_ end, case mnesia:transaction(Fun) of - {atomic, Items} when is_list(Items) -> - Items1 = lists:reverse(Items), - {ok, lists:sublist(Items1, Start + 1, Limit), length(Items1)}; + {atomic, Hosts0} when is_list(Hosts0) -> + Hosts = sort(Hosts0), + Len = length(Hosts), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(Hosts, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end; {aborted, Error} -> {error, Error} end. --spec add_host(Host :: #host{}) -> ok | {error, Reason :: any()}. +-spec add_host(Host :: #host{}) -> ok | {error, Reason :: binary()}. add_host(Host = #host{serial_number = SerialNumber}) -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(host), E#host.serial_number =:= SerialNumber]), case qlc:e(Q) of - [_|_] -> + [_SomeHost|_] -> mnesia:abort(<<"serial_number exists">>); [] -> mnesia:write(host, Host, write) @@ -95,7 +107,7 @@ add_host(Host = #host{serial_number = SerialNumber}) -> end, case mnesia:transaction(Fun) of - {atomic, _} -> + {atomic, ok} -> ok; {aborted, Error} -> {error, Error} @@ -108,8 +120,7 @@ change_status(HostId, Status) when is_binary(HostId), is_integer(Status) -> [] -> mnesia:abort(<<"host not found">>); [Host] -> - NHost = Host#host{status = Status}, - mnesia:write(host, NHost, write) + mnesia:write(host, Host#host{status = Status}, write) end end, case mnesia:transaction(Fun) of @@ -184,13 +195,12 @@ table_size() -> %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -to_map(#host{host_id = HostId, name = Name, model = Model, cell_id = CellId, activated_ts = ActivatedTs, update_ts = UpdateTs, status = Status, - metric = #host_metric{ - cpus = Cpus, - cpu_temperature = CpuTemperature, - memory = #memory_metric{used = MemoryUsed, total = MemoryTotal}, - disk = #disk_metric{used = DiskUsed, total = DiskTotal}, - interfaces = Interfaces}}) -> +%% 主机信息的排序 +sort(Hosts) when is_list(Hosts) -> + lists:sort(fun(#host{name = N1}, #host{name = N2}) -> N1 < N2 end, Hosts). + +%% 将数据转换成hash +to_map(#host{host_id = HostId, name = Name, model = Model, cell_id = CellId, activated_ts = ActivatedTs, update_ts = UpdateTs, status = Status, metric = Metric}) -> #{ <<"host_id">> => HostId, <<"name">> => Name, @@ -199,27 +209,44 @@ to_map(#host{host_id = HostId, name = Name, model = Model, cell_id = CellId, act <<"activated_ts">> => ActivatedTs, <<"update_ts">> => UpdateTs, <<"status">> => Status, - <<"metric">> => #{ - <<"cpus">> => lists:map(fun(#cpu_metric{num = Num, load = Load}) -> - #{<<"num">> => Num, <<"load">> => Load } - end, - Cpus), - <<"cpu_temperature">> => CpuTemperature, - <<"memory">> => #{ - <<"used">> => MemoryUsed, - <<"total">> => MemoryTotal - }, - <<"disk">> => #{ - <<"used">> => DiskUsed, - <<"total">> => DiskTotal - }, - <<"interfaces">> => lists:map(fun(#interface_metric{name = IName, desc = IDesc, status = IStatus, detail = IDetail}) -> - #{ - <<"name">> => IName, - <<"desc">> => IDesc, - <<"status">> => IStatus, - <<"detatil">> => IDetail - } - end, Interfaces) + <<"metric">> => host_metric(Metric) + }. + +host_metric(undefined) -> + #{}; +host_metric(#host_metric{cpus = Cpus, cpu_temperature = CpuTemperature, memory = Memory, disk = Disk, interfaces = Interfaces}) -> + #{ + <<"cpus">> => host_cpus(Cpus), + <<"cpu_temperature">> => CpuTemperature, + <<"memory">> => host_memory(Memory), + <<"disk">> => host_disk(Disk), + <<"interfaces">> => host_interfaces(Interfaces) + }. + +host_cpus(Cpus) when is_list(Cpus) -> + lists:map(fun(#cpu_metric{num = Num, load = Load}) -> #{<<"num">> => Num, <<"load">> => Load } end, Cpus); +host_cpus(_) -> + []. + +host_memory(#memory_metric{used = MemoryUsed, total = MemoryTotal}) -> + #{<<"used">> => MemoryUsed, <<"total">> => MemoryTotal}; +host_memory(_) -> + #{}. + +host_disk(#disk_metric{used = DiskUsed, total = DiskTotal}) -> + #{<<"used">> => DiskUsed, <<"total">> => DiskTotal}; +host_disk(_) -> + #{}. + +%% 转换接口信息 +host_interfaces(Interfaces) when is_list(Interfaces) -> + lists:map(fun(#interface_metric{name = IName, desc = IDesc, status = IStatus, detail = IDetail}) -> + #{ + <<"name">> => IName, + <<"desc">> => IDesc, + <<"status">> => IStatus, + <<"detatil">> => IDetail } - }. \ No newline at end of file + end, Interfaces); +host_interfaces(_) -> + []. \ No newline at end of file diff --git a/apps/iot/src/model/id_generator_model.erl b/apps/iot/src/model/id_generator_model.erl index 2590ec2..8a72b2b 100644 --- a/apps/iot/src/model/id_generator_model.erl +++ b/apps/iot/src/model/id_generator_model.erl @@ -13,6 +13,8 @@ %% API -export([generate/1]). +%% 生成对应的自增id +-spec generate(Name :: string()) -> {ok, Id :: integer()} | {error, Reason :: any()}. generate(Name) when is_list(Name) -> case mnesia:transaction(fun() -> mnesia:dirty_update_counter(id_generator, Name, 1) end) of {atomic, Id} -> diff --git a/apps/iot/src/model/issue_model.erl b/apps/iot/src/model/issue_model.erl index 7ec209e..6c2ed47 100644 --- a/apps/iot/src/model/issue_model.erl +++ b/apps/iot/src/model/issue_model.erl @@ -30,9 +30,15 @@ get_issue(IssueId) when is_integer(IssueId) -> {ok, Items :: list(), TotalNum :: integer()} | {error, Reason :: any()}. get_issues(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> - Items = mnesia:dirty_select(?TAB_NAME, [Spec]), - NItems = lists:sublist(Items, Start + 1, Limit), - {ok, NItems, length(Items)}. + Issues0 = mnesia:dirty_select(?TAB_NAME, [Spec]), + Issues = sort(Issues0), + Len = length(Issues), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(Issues, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end. -spec get_user_issues(UserId :: integer(), Start :: integer(), Limit :: integer()) -> {ok, Items :: [#host{}], Num :: integer()} | @@ -43,8 +49,15 @@ get_user_issues(UserId, Start, Limit) when is_integer(UserId), is_integer(Limit) qlc:e(Q) end, case mnesia:transaction(Fun) of - {atomic, Items} when is_list(Items) -> - {ok, lists:sublist(Items, Start + 1, Limit), length(Items)}; + {atomic, Issues0} when is_list(Issues0) -> + Issues = sort(Issues0), + Len = length(Issues), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(Issues, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end; {aborted, Error} -> {error, Error} end. @@ -52,7 +65,7 @@ get_user_issues(UserId, Start, Limit) when is_integer(UserId), is_integer(Limit) -spec add_issue(Issue :: #issue{}) -> ok | {error, Reason :: any()}. add_issue(Issue = #issue{}) -> case mnesia:transaction(fun() -> mnesia:write(?TAB_NAME, Issue, write) end) of - {atomic, _} -> + {atomic, ok} -> ok; {aborted, Error} -> {error, Error} @@ -92,6 +105,10 @@ table_size() -> %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% 默认按照id倒序进行排序 +sort(Issues) when is_list(Issues) -> + lists:sort(fun(#issue{issue_id = Id0}, #issue{issue_id = Id1}) -> Id0 < Id1 end, Issues). + to_map(#issue{issue_id = IssueId, name = Name, uid = Uid, deploy_type = DeployType, assoc_id = AssocId, hosts = Hosts, timeout = Timeout, create_ts = CreateTs, results = Results, status = Status}) -> diff --git a/apps/iot/src/model/router_model.erl b/apps/iot/src/model/router_model.erl index 1b27afb..657317b 100644 --- a/apps/iot/src/model/router_model.erl +++ b/apps/iot/src/model/router_model.erl @@ -17,14 +17,14 @@ get_router(RouterId) when is_integer(RouterId) -> case mnesia:dirty_read(router, RouterId) of - [Router = #router{}] -> + [Router] -> {ok, Router}; _ -> undefined end. %% 获取app信息 --spec get_all_routers() -> {ok, Items :: list()} | {error, Reason :: any()}. +-spec get_all_routers() -> {ok, Routers :: list()} | {error, Reason :: any()}. get_all_routers() -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(router)]), @@ -32,13 +32,13 @@ get_all_routers() -> end, case mnesia:transaction(Fun) of {atomic, Routers} -> - {ok, Routers}; - {aborted, _} -> - error + {ok, sort(Routers)}; + {aborted, Reason} -> + {error, Reason} end. %% 获取app信息 --spec get_all_valid_routers() -> {ok, Items :: list()} | {error, Reason :: any()}. +-spec get_all_valid_routers() -> {ok, Routers :: list()} | {error, Reason :: any()}. get_all_valid_routers() -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(router), E#router.status =:= 1]), @@ -46,9 +46,9 @@ get_all_valid_routers() -> end, case mnesia:transaction(Fun) of {atomic, Routers} -> - {ok, Routers}; - {aborted, _} -> - error + {ok, sort(Routers)}; + {aborted, Reason} -> + {error, Reason} end. %% 获取app信息 @@ -56,10 +56,15 @@ get_all_valid_routers() -> {ok, Items :: list(), TotalNum :: integer()} | {error, Reason :: any()}. get_routers(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> - Items0 = mnesia:dirty_select(router, [Spec]), - Items = lists:reverse(Items0), - NItems = lists:sublist(Items, Start + 1, Limit), - {ok, NItems, length(Items)}. + Routers0 = mnesia:dirty_select(router, [Spec]), + Routers = sort(Routers0), + Len = length(Routers), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(Routers, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end. -spec add_router(Router :: #router{}) -> ok | {error, Reason :: any()}. add_router(Router = #router{}) -> @@ -121,6 +126,9 @@ table_size() -> %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +sort(Routers) when is_list(Routers) -> + lists:sort(fun(#router{router_id = Id0}, #router{router_id = Id1}) -> Id0 < Id1 end, Routers). + to_map(#router{router_id = RouterId, name = Name, status = Status, endpoint = Endpoint}) -> EndpointInfo = case Endpoint of undefined -> diff --git a/apps/iot/src/model/scenario_deploy_model.erl b/apps/iot/src/model/scenario_deploy_model.erl index f99ea3f..7f20cb3 100644 --- a/apps/iot/src/model/scenario_deploy_model.erl +++ b/apps/iot/src/model/scenario_deploy_model.erl @@ -2,7 +2,7 @@ %%% @author licheng5 %%% @copyright (C) 2021, %%% @doc -%%% +%%% 场景部署关系表 %%% @end %%% Created : 27. 4月 2021 下午4:38 %%%------------------------------------------------------------------- @@ -17,7 +17,7 @@ -export([get_host_deploy_list/1, get_scenario_deploy_list/1, add_deploy/1, change_status/2, delete/1, table_size/0]). -export([to_map/1]). --spec get_host_deploy_list(HostId :: binary()) -> {ok, List :: [#scenario_deploy{}]} | error. +-spec get_host_deploy_list(HostId :: binary()) -> {ok, List :: [#scenario_deploy{}]} | {error, Reason :: any()}. get_host_deploy_list(HostId) when is_binary(HostId) -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(?TAB_NAME), E#scenario_deploy.host_id =:= HostId]), @@ -25,12 +25,12 @@ get_host_deploy_list(HostId) when is_binary(HostId) -> end, case mnesia:transaction(Fun) of {atomic, Items} -> - {ok, Items}; - {aborted, _} -> - error + {ok, sort(Items)}; + {aborted, Reason} -> + {error, Reason} end. --spec get_scenario_deploy_list(ScenarioId :: integer()) -> {ok, List :: [#scenario_deploy{}]} | error. +-spec get_scenario_deploy_list(ScenarioId :: integer()) -> {ok, List :: [#scenario_deploy{}]} | {error, Reason :: any()}. get_scenario_deploy_list(ScenarioId) when is_integer(ScenarioId) -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(?TAB_NAME), E#scenario_deploy.scenario_id =:= ScenarioId]), @@ -38,9 +38,9 @@ get_scenario_deploy_list(ScenarioId) when is_integer(ScenarioId) -> end, case mnesia:transaction(Fun) of {atomic, Items} -> - {ok, Items}; - {aborted, _} -> - error + {ok, sort(Items)}; + {aborted, Reason} -> + {error, Reason} end. -spec add_deploy(Deploy :: #scenario_deploy{}) -> ok | {error, Reason :: any()}. @@ -86,6 +86,9 @@ table_size() -> %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +sort(DeployList) when is_list(DeployList) -> + lists:sort(fun(#scenario_deploy{deploy_id = Id0}, #scenario_deploy{deploy_id = Id1}) -> Id0 < Id1 end, DeployList). + to_map(#scenario_deploy{deploy_id = DeployId, scenario_id = ScenarioId, host_id = HostId, create_ts = CreateTs, update_ts = UpdateTs, status = Status}) -> #{ <<"deploy_id">> => DeployId, diff --git a/apps/iot/src/model/scenario_model.erl b/apps/iot/src/model/scenario_model.erl index 00b844f..2b38aae 100644 --- a/apps/iot/src/model/scenario_model.erl +++ b/apps/iot/src/model/scenario_model.erl @@ -11,12 +11,15 @@ -include("iot.hrl"). -include_lib("stdlib/include/qlc.hrl"). +-define(TAB_NAME, scenario). + %% API -export([get_scenario/1, get_scenario_list/3, add_scenario/1, change_status/2, delete/1, table_size/0]). -export([to_map/1, match_spec/1]). +-spec get_scenario(ScenarioId :: integer()) -> {ok, #scenario{}} | undefined. get_scenario(ScenarioId) when is_integer(ScenarioId) -> - case mnesia:dirty_read(scenario, ScenarioId) of + case mnesia:dirty_read(?TAB_NAME, ScenarioId) of [Scenario = #scenario{}] -> {ok, Scenario}; _ -> @@ -28,15 +31,20 @@ get_scenario(ScenarioId) when is_integer(ScenarioId) -> {ok, Items :: list(), TotalNum :: integer()} | {error, Reason :: any()}. get_scenario_list(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 -> - Items0 = mnesia:dirty_select(scenario, [Spec]), - Items = lists:reverse(Items0), - NItems = lists:sublist(Items, Start + 1, Limit), - {ok, NItems, length(Items)}. + Scenarios0 = mnesia:dirty_select(?TAB_NAME, [Spec]), + Scenarios = sort(Scenarios0), + Len = length(Scenarios), + case Len >= Start + 1 of + true -> + {ok, lists:sublist(Scenarios, Start + 1, Limit), Len}; + false -> + {ok, [], Len} + end. -spec add_scenario(Scenario :: #scenario{}) -> ok | {error, Reason :: any()}. add_scenario(Scenario = #scenario{}) -> - case mnesia:transaction(fun() -> mnesia:write(scenario, Scenario, write) end) of - {atomic, _} -> + case mnesia:transaction(fun() -> mnesia:write(?TAB_NAME, Scenario, write) end) of + {atomic, ok} -> ok; {aborted, Error} -> {error, Error} @@ -49,7 +57,7 @@ change_status(ScenarioId, Status) when is_integer(ScenarioId), is_integer(Status [] -> mnesia:abort(<<"scenario not found">>); [Scenario] -> - mnesia:write(scenario, Scenario#scenario{status = Status}, write) + mnesia:write(?TAB_NAME, Scenario#scenario{status = Status}, write) end end, case mnesia:transaction(Fun) of @@ -61,7 +69,7 @@ change_status(ScenarioId, Status) when is_integer(ScenarioId), is_integer(Status -spec delete(ScenarioId :: binary()) -> ok | {error, Reason :: any()}. delete(ScenarioId) when is_integer(ScenarioId) -> - case mnesia:transaction(fun() -> mnesia:delete(scenario, ScenarioId, write) end) of + case mnesia:transaction(fun() -> mnesia:delete(?TAB_NAME, ScenarioId, write) end) of {atomic, ok} -> ok; {aborted, Reason} -> @@ -93,6 +101,9 @@ table_size() -> %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +sort(Scenarios) when is_list(Scenarios) -> + lists:sort(fun(#scenario{scenario_id = Id0}, #scenario{scenario_id = Id1}) -> Id0 < Id1 end, Scenarios). + to_map(#scenario{scenario_id = ScenarioId, name = Name, desc = Desc, rule = Rule, update_ts = UpdateTs, status = Status}) -> #{ <<"scenario_id">> => ScenarioId, diff --git a/apps/iot/src/model/terminal_model.erl b/apps/iot/src/model/terminal_model.erl index 73ad996..b7d8fe6 100644 --- a/apps/iot/src/model/terminal_model.erl +++ b/apps/iot/src/model/terminal_model.erl @@ -11,43 +11,47 @@ -include("iot.hrl"). -include_lib("stdlib/include/qlc.hrl"). +-define(TAB_NAME, terminal). + %% API -export([get_all_terminals/0, get_host_terminals/1, get_status_stat/0, table_size/0]). -export([change_status/2, delete/1, to_map/1, add_terminal/1]). %% 获取app信息 +-spec get_all_terminals() -> {ok, Terminals :: [#terminal{}]} | {error, Reason :: any()}. get_all_terminals() -> Fun = fun() -> - Q = qlc:q([E || E <- mnesia:table(terminal)]), + Q = qlc:q([E || E <- mnesia:table(?TAB_NAME)]), qlc:e(Q) end, case mnesia:transaction(Fun) of - {atomic, Items} when is_list(Items) -> - {ok, Items}; + {atomic, Terminals} when is_list(Terminals) -> + {ok, sort(Terminals)}; {aborted, Error} -> {error, Error} end. +-spec get_host_terminals(HostId :: binary()) -> {ok, Terminals :: [#terminal{}]} | {error, Reason :: any()}. get_host_terminals(HostId) when is_binary(HostId) -> Fun = fun() -> - Q = qlc:q([E || E <- mnesia:table(terminal), E#terminal.host_id =:= HostId]), + Q = qlc:q([E || E <- mnesia:table(?TAB_NAME), E#terminal.host_id =:= HostId]), qlc:e(Q) end, case mnesia:transaction(Fun) of - {atomic, Items} when is_list(Items) -> - {ok, Items}; + {atomic, Terminals} when is_list(Terminals) -> + {ok, sort(Terminals)}; {aborted, Error} -> {error, Error} end. %% 获取状态分组统计信息 --spec get_status_stat() -> {ok, #{}} | {error, Reason :: any()}. +-spec get_status_stat() -> {ok, Stat :: #{}} | {error, Reason :: any()}. get_status_stat() -> Fun = fun() -> mnesia:foldl(fun(#terminal{status = Status}, Acc) -> Num = maps:get(Status, Acc, 0), Acc#{Status => Num + 1} - end, #{}, terminal) + end, #{}, ?TAB_NAME) end, case mnesia:transaction(Fun) of @@ -57,22 +61,23 @@ get_status_stat() -> {error, Reason} end. +-spec add_terminal(Terminal :: #terminal{}) -> ok | {error, Reason :: any()}. add_terminal(Terminal = #terminal{}) -> - case mnesia:transaction(fun() -> mnesia:write(terminal, Terminal, write) end) of + case mnesia:transaction(fun() -> mnesia:write(?TAB_NAME, Terminal, write) end) of {atomic, _} -> ok; {aborted, Error} -> {error, Error} end. -change_status(TerminalId, Status) when is_binary(TerminalId), is_integer(Status) -> +-spec change_status(TerminalId :: integer(), Status :: integer()) -> ok | {error, Reason :: any()}. +change_status(TerminalId, Status) when is_integer(TerminalId), is_integer(Status) -> Fun = fun() -> - case mnesia:read(terminal, TerminalId) of + case mnesia:read(?TAB_NAME, TerminalId) of [] -> mnesia:abort(<<"terminal not found">>); [Terminal] -> - NTerminal = Terminal#host{status = Status}, - mnesia:write(terminal, NTerminal, write) + mnesia:write(?TAB_NAME, Terminal#host{status = Status}, write) end end, case mnesia:transaction(Fun) of @@ -82,8 +87,9 @@ change_status(TerminalId, Status) when is_binary(TerminalId), is_integer(Status) {error, Reason} end. -delete(TerminalId) when is_binary(TerminalId) -> - case mnesia:transaction(fun() -> mnesia:delete(terminal, TerminalId, write) end) of +-spec delete(TerminalId :: integer()) -> ok | {error, Reason :: any()}. +delete(TerminalId) when is_integer(TerminalId) -> + case mnesia:transaction(fun() -> mnesia:delete(?TAB_NAME, TerminalId, write) end) of {atomic, ok} -> ok; {aborted, Reason} -> @@ -94,11 +100,19 @@ delete(TerminalId) when is_binary(TerminalId) -> table_size() -> mnesia:table_info(host, size). -to_map(#terminal{terminal_id = TerminalId, host_id = HostId, name = Name, code = Code, access_protocol = AccessProtocol, +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% helper methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +sort(Terminals) when is_list(Terminals) -> + lists:sort(fun(#terminal{terminal_id = Id0}, #terminal{terminal_id = Id1}) -> Id0 < Id1 end, Terminals). + +to_map(#terminal{terminal_id = TerminalId, host_id = HostId, serial_number = SerialNumber, name = Name, code = Code, access_protocol = AccessProtocol, product_id = ProductId, vendor_id = VendorId, model = Model, cell_id = CellId, status = Status, update_ts = UpdateTs}) -> #{ <<"terminal_id">> => TerminalId, <<"host_id">> => HostId, + <<"serial_number">> => SerialNumber, <<"name">> => Name, <<"code">> => Code, <<"access_protocol">> => AccessProtocol, @@ -109,9 +123,3 @@ to_map(#terminal{terminal_id = TerminalId, host_id = HostId, name = Name, code = <<"status">> => Status, <<"update_ts">> => UpdateTs }. - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% helper methods -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file