fix service
This commit is contained in:
parent
009daf7d34
commit
7f1dbbade5
@ -41,9 +41,9 @@ init_database() ->
|
|||||||
]),
|
]),
|
||||||
|
|
||||||
%% 主机微服务管理
|
%% 主机微服务管理
|
||||||
mnesia:create_table(microservice, [
|
mnesia:create_table(service, [
|
||||||
{attributes, record_info(fields, microservice)},
|
{attributes, record_info(fields, service)},
|
||||||
{record_name, microservice},
|
{record_name, service},
|
||||||
{disc_copies, [node()]},
|
{disc_copies, [node()]},
|
||||||
{type, set}
|
{type, set}
|
||||||
]),
|
]),
|
||||||
@ -68,5 +68,5 @@ copy_database(MasterNode) when is_atom(MasterNode) ->
|
|||||||
%% 增加表的分区复制
|
%% 增加表的分区复制
|
||||||
mnesia:add_table_copy(host, node(), ram_copies),
|
mnesia:add_table_copy(host, node(), ram_copies),
|
||||||
mnesia:add_table_copy(terminal, node(), ram_copies),
|
mnesia:add_table_copy(terminal, node(), ram_copies),
|
||||||
mnesia:add_table_copy(microservice, node(), ram_copies),
|
mnesia:add_table_copy(service, node(), ram_copies),
|
||||||
ok.
|
ok.
|
||||||
@ -1,127 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author licheng5
|
|
||||||
%%% @copyright (C) 2021, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 27. 4月 2021 下午4:38
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(microservice_model).
|
|
||||||
-author("licheng5").
|
|
||||||
-include("iot.hrl").
|
|
||||||
-include_lib("stdlib/include/qlc.hrl").
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([get_services/1, get_service/2, add_service/1, change_status/2, delete/1, table_size/0]).
|
|
||||||
|
|
||||||
get_service(HostId, ServiceName) when is_binary(HostId), is_binary(ServiceName) ->
|
|
||||||
Fun = fun() ->
|
|
||||||
Q = qlc:q([E || E <- mnesia:table(microservice), E#service.host_id =:= HostId, E#service.name =:= ServiceName]),
|
|
||||||
qlc:e(Q)
|
|
||||||
end,
|
|
||||||
case mnesia:transaction(Fun) of
|
|
||||||
[Service] ->
|
|
||||||
{ok, Service};
|
|
||||||
_ ->
|
|
||||||
undefined
|
|
||||||
end.
|
|
||||||
|
|
||||||
get_services(HostId) when is_binary(HostId) ->
|
|
||||||
Fun = fun() ->
|
|
||||||
Q = qlc:q([E || E <- mnesia:table(microservice), E#service.host_id =:= HostId]),
|
|
||||||
qlc:e(Q)
|
|
||||||
end,
|
|
||||||
case mnesia:transaction(Fun) of
|
|
||||||
Services when is_list(Services) ->
|
|
||||||
Services;
|
|
||||||
_ ->
|
|
||||||
[]
|
|
||||||
end.
|
|
||||||
|
|
||||||
add_service(Service = #service{}) ->
|
|
||||||
case mnesia:transaction(fun() -> mnesia:write(microservice, Service, write) end) of
|
|
||||||
{atomic, _} ->
|
|
||||||
ok;
|
|
||||||
{aborted, Error} ->
|
|
||||||
{error, Error}
|
|
||||||
end.
|
|
||||||
|
|
||||||
change_status(ServiceId, Status) when is_binary(ServiceId), is_integer(Status) ->
|
|
||||||
Fun = fun() ->
|
|
||||||
case mnesia:read(microservice, ServiceId) of
|
|
||||||
[] ->
|
|
||||||
mnesia:abort(<<"host not found">>);
|
|
||||||
[Service] ->
|
|
||||||
NService = Service#service{status = Status},
|
|
||||||
mnesia:write(microservice, NService, write)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
case mnesia:transaction(Fun) of
|
|
||||||
{atomic, ok} ->
|
|
||||||
ok;
|
|
||||||
{aborted, Reason} ->
|
|
||||||
{error, Reason}
|
|
||||||
end.
|
|
||||||
|
|
||||||
update_metric(ServiceId, Metrics) when is_binary(ServiceId), is_list(Metrics) ->
|
|
||||||
Fun = fun() ->
|
|
||||||
case mnesia:read(terminal, TerminalId) of
|
|
||||||
[] ->
|
|
||||||
mnesia:abort(<<"terminal not found">>);
|
|
||||||
[Terminal = #terminal{metrics = Metrics}] ->
|
|
||||||
Ts = iot_util:current_time(),
|
|
||||||
NMetrics = case lists:any(fun(#service_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
|
||||||
true ->
|
|
||||||
lists:map(fun(Metric=#service_metric{symbol = Symbol, queue = Q}) ->
|
|
||||||
case Symbol =:= MetricSymbol of
|
|
||||||
true ->
|
|
||||||
Q1 = iot_util:queue_limited_in({Ts, MetricValue}, Q, 100),
|
|
||||||
Metric#service_metric{
|
|
||||||
name = MetricName,
|
|
||||||
last_value = MetricValue,
|
|
||||||
update_ts = Ts,
|
|
||||||
queue = Q1
|
|
||||||
};
|
|
||||||
false ->
|
|
||||||
Metric
|
|
||||||
end
|
|
||||||
end, Metrics);
|
|
||||||
false ->
|
|
||||||
Q0 = queue:new(),
|
|
||||||
Metric = #service_metric{
|
|
||||||
name = MetricName,
|
|
||||||
symbol = MetricSymbol,
|
|
||||||
last_value = MetricValue,
|
|
||||||
update_ts = Ts,
|
|
||||||
queue = queue:in({Ts, MetricValue}, Q0)
|
|
||||||
},
|
|
||||||
Metrics ++ [Metric]
|
|
||||||
end,
|
|
||||||
mnesia:write(terminal, Terminal#terminal{metrics = NMetrics}, write)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
case mnesia:transaction(Fun) of
|
|
||||||
{atomic, ok} ->
|
|
||||||
ok;
|
|
||||||
{aborted, Reason} ->
|
|
||||||
{error, Reason}
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
-spec delete(HostId :: binary()) -> ok | {error, Reason :: any()}.
|
|
||||||
delete(ServiceId) when is_binary(ServiceId) ->
|
|
||||||
case mnesia:transaction(fun() -> mnesia:delete(microservice, ServiceId, write) end) of
|
|
||||||
{atomic, ok} ->
|
|
||||||
ok;
|
|
||||||
{aborted, Reason} ->
|
|
||||||
{error, Reason}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% 获取app表的数据大小
|
|
||||||
table_size() ->
|
|
||||||
mnesia:table_info(microservice, size).
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
%% helper methods
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
100
apps/iot/src/model/service_model.erl
Normal file
100
apps/iot/src/model/service_model.erl
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author licheng5
|
||||||
|
%%% @copyright (C) 2021, <COMPANY>
|
||||||
|
%%% @doc
|
||||||
|
%%%
|
||||||
|
%%% @end
|
||||||
|
%%% Created : 27. 4月 2021 下午4:38
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
-module(service_model).
|
||||||
|
-author("licheng5").
|
||||||
|
-include("iot.hrl").
|
||||||
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
|
%% API
|
||||||
|
-export([get_services/1, get_service/2, add_service/1, change_status/2, delete/1, table_size/0]).
|
||||||
|
-export([update_metric/2]).
|
||||||
|
|
||||||
|
get_service(HostId, ServiceName) when is_binary(HostId), is_binary(ServiceName) ->
|
||||||
|
Fun = fun() ->
|
||||||
|
Q = qlc:q([E || E <- mnesia:table(service), E#service.host_id =:= HostId, E#service.name =:= ServiceName]),
|
||||||
|
qlc:e(Q)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
[Service] ->
|
||||||
|
{ok, Service};
|
||||||
|
_ ->
|
||||||
|
undefined
|
||||||
|
end.
|
||||||
|
|
||||||
|
get_services(HostId) when is_binary(HostId) ->
|
||||||
|
Fun = fun() ->
|
||||||
|
Q = qlc:q([E || E <- mnesia:table(service), E#service.host_id =:= HostId]),
|
||||||
|
qlc:e(Q)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
Services when is_list(Services) ->
|
||||||
|
Services;
|
||||||
|
_ ->
|
||||||
|
[]
|
||||||
|
end.
|
||||||
|
|
||||||
|
add_service(Service = #service{}) ->
|
||||||
|
case mnesia:transaction(fun() -> mnesia:write(service, Service, write) end) of
|
||||||
|
{atomic, _} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Error} ->
|
||||||
|
{error, Error}
|
||||||
|
end.
|
||||||
|
|
||||||
|
change_status(ServiceId, Status) when is_binary(ServiceId), is_integer(Status) ->
|
||||||
|
Fun = fun() ->
|
||||||
|
case mnesia:read(service, ServiceId) of
|
||||||
|
[] ->
|
||||||
|
mnesia:abort(<<"host not found">>);
|
||||||
|
[Service] ->
|
||||||
|
NService = Service#service{status = Status},
|
||||||
|
mnesia:write(service, NService, write)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
update_metric(ServiceId, Metrics) when is_binary(ServiceId), is_list(Metrics) ->
|
||||||
|
Fun = fun() ->
|
||||||
|
case mnesia:read(service, ServiceId) of
|
||||||
|
[] ->
|
||||||
|
mnesia:abort(<<"service not found">>);
|
||||||
|
[Service = #service{metrics = Metrics}] ->
|
||||||
|
NService = Service#service{metrics = Metrics},
|
||||||
|
mnesia:write(service, NService, write)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec delete(HostId :: binary()) -> ok | {error, Reason :: any()}.
|
||||||
|
delete(ServiceId) when is_binary(ServiceId) ->
|
||||||
|
case mnesia:transaction(fun() -> mnesia:delete(service, ServiceId, write) end) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% 获取app表的数据大小
|
||||||
|
table_size() ->
|
||||||
|
mnesia:table_info(service, size).
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%% helper methods
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
Loading…
x
Reference in New Issue
Block a user