fix
This commit is contained in:
parent
e7ef3350f0
commit
9baa0e7d4d
@ -52,8 +52,8 @@
|
||||
status
|
||||
}).
|
||||
|
||||
%% 终端指标收集
|
||||
-record(terminal_metric, {
|
||||
%% 微服务指标收集
|
||||
-record(service_metric, {
|
||||
%% 标识
|
||||
symbol :: binary(),
|
||||
%% 名称
|
||||
@ -121,8 +121,7 @@
|
||||
model :: binary(),
|
||||
%% 所在单元ID,管理系统负责
|
||||
cell_id :: integer(),
|
||||
%% 指标, 终端指标涉及到查询效率和更新效率的问题,因此合并到一起
|
||||
metrics = [] :: [#terminal_metric{}],
|
||||
|
||||
%% 终端状态
|
||||
status = 0 :: integer(),
|
||||
%% 最后上线时间
|
||||
@ -146,5 +145,8 @@
|
||||
%% 执行次数
|
||||
execute_count = 0,
|
||||
%% 部署时间
|
||||
deploy_ts = 0 :: integer()
|
||||
deploy_ts = 0 :: integer(),
|
||||
%% 指标, 终端指标涉及到查询效率和更新效率的问题,因此合并到一起
|
||||
metrics = [] :: [#service_metric{}],
|
||||
status = 0
|
||||
}).
|
||||
81
apps/iot/src/model/microservice_model.erl
Normal file
81
apps/iot/src/model/microservice_model.erl
Normal file
@ -0,0 +1,81 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @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#microservice.host_id =:= HostId, E#microservice.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#microservice.host_id =:= HostId]),
|
||||
qlc:e(Q)
|
||||
end,
|
||||
case mnesia:transaction(Fun) of
|
||||
Services when is_list(Services) ->
|
||||
Services;
|
||||
_ ->
|
||||
[]
|
||||
end.
|
||||
|
||||
add_service(Service = #microservice{}) ->
|
||||
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#microservice{status = Status},
|
||||
mnesia:write(microservice, 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(microservice, ServiceId, write) end) of
|
||||
{atomic, ok} ->
|
||||
ok;
|
||||
{aborted, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
%% 获取app表的数据大小
|
||||
table_size() ->
|
||||
mnesia:table_info(microservice, size).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% helper methods
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@ -89,13 +89,13 @@ update_metric(TerminalId, MetricName, MetricSymbol, MetricValue) when is_binary(
|
||||
mnesia:abort(<<"terminal not found">>);
|
||||
[Terminal = #terminal{metrics = Metrics}] ->
|
||||
Ts = iot_util:current_time(),
|
||||
NMetrics = case lists:any(fun(#terminal_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
||||
NMetrics = case lists:any(fun(#service_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
||||
true ->
|
||||
lists:map(fun(Metric=#terminal_metric{symbol = Symbol, queue = Q}) ->
|
||||
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#terminal_metric{
|
||||
Metric#service_metric{
|
||||
name = MetricName,
|
||||
last_value = MetricValue,
|
||||
update_ts = Ts,
|
||||
@ -107,7 +107,7 @@ update_metric(TerminalId, MetricName, MetricSymbol, MetricValue) when is_binary(
|
||||
end, Metrics);
|
||||
false ->
|
||||
Q0 = queue:new(),
|
||||
Metric = #terminal_metric{
|
||||
Metric = #service_metric{
|
||||
name = MetricName,
|
||||
symbol = MetricSymbol,
|
||||
last_value = MetricValue,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user