fix service
This commit is contained in:
parent
375db51b9c
commit
009daf7d34
@ -60,6 +60,8 @@
|
|||||||
name :: binary(),
|
name :: binary(),
|
||||||
%% 当前最新值
|
%% 当前最新值
|
||||||
last_value = 0 :: number(),
|
last_value = 0 :: number(),
|
||||||
|
%% 单位, 如电压: a、v
|
||||||
|
unit :: any(),
|
||||||
%% 历史值序列, 队列;保存最近的100数值, 格式为: [{ts, value}]
|
%% 历史值序列, 队列;保存最近的100数值, 格式为: [{ts, value}]
|
||||||
queue = [] :: queus:queue(),
|
queue = [] :: queus:queue(),
|
||||||
%% 最后更新时间
|
%% 最后更新时间
|
||||||
@ -129,7 +131,7 @@
|
|||||||
}).
|
}).
|
||||||
|
|
||||||
%% 微服务表
|
%% 微服务表
|
||||||
-record(microservice, {
|
-record(service, {
|
||||||
%% ID, 基于UUID生成
|
%% ID, 基于UUID生成
|
||||||
service_id :: binary(),
|
service_id :: binary(),
|
||||||
%% 关联主机
|
%% 关联主机
|
||||||
|
|||||||
@ -88,10 +88,11 @@ handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) ->
|
|||||||
lager:debug("the data is: ~p", [Infos]),
|
lager:debug("the data is: ~p", [Infos]),
|
||||||
|
|
||||||
%% 一次可能提前多组数据
|
%% 一次可能提前多组数据
|
||||||
lists:foreach(fun(#{<<"service_name">> := ServiceName, <<"time">> := Time, <<"data">> := Items}) ->
|
lists:foreach(fun(#{<<"service_name">> := ServiceName, <<"data">> := Items}) ->
|
||||||
case lists:search(fun(#microservice{name = Name}) -> Name =:= ServiceName end, Services) of
|
case lists:search(fun(#service{name = Name}) -> Name =:= ServiceName end, Services) of
|
||||||
{value, Service=#microservice{metrics = Metrics}} ->
|
{value, Service=#service{metrics = Metrics}} ->
|
||||||
%% 更新数据
|
%% 更新数据
|
||||||
|
NMetrics = lists:foldl(fun(MetricData, MetricsAcc) -> append_metric(MetricsAcc, MetricData) end, Metrics, Items),
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -119,16 +120,21 @@ handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) ->
|
|||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
||||||
append_metric(Metrics, MetricName, MetricSymbol, MetricValue, Ts) ->
|
append_metric(Metrics, MetricData) when is_map(MetricData) ->
|
||||||
case lists:any(fun(#service_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
Name = maps:get(<<"name">>, MetricData, <<"">>),
|
||||||
|
Symbol = maps:get(<<"symbol">>, MetricData, <<"">>),
|
||||||
|
Value = maps:get(<<"value">>, MetricData),
|
||||||
|
Ts = maps:get(<<"time">>, MetricData, 0),
|
||||||
|
|
||||||
|
case lists:any(fun(#service_metric{symbol = Symbol0}) -> Symbol =:= Symbol0 end, Metrics) of
|
||||||
true ->
|
true ->
|
||||||
lists:map(fun(Metric=#service_metric{symbol = Symbol, queue = Q}) ->
|
lists:map(fun(Metric=#service_metric{symbol = Symbol0, queue = Q}) ->
|
||||||
case Symbol =:= MetricSymbol of
|
case Symbol =:= Symbol0 of
|
||||||
true ->
|
true ->
|
||||||
Q1 = iot_util:queue_limited_in({Ts, MetricValue}, Q, 100),
|
Q1 = iot_util:queue_limited_in({Ts, Value}, Q, 100),
|
||||||
Metric#service_metric{
|
Metric#service_metric{
|
||||||
name = MetricName,
|
name = Name,
|
||||||
last_value = MetricValue,
|
last_value = Value,
|
||||||
update_ts = Ts,
|
update_ts = Ts,
|
||||||
queue = Q1
|
queue = Q1
|
||||||
};
|
};
|
||||||
@ -139,11 +145,11 @@ append_metric(Metrics, MetricName, MetricSymbol, MetricValue, Ts) ->
|
|||||||
false ->
|
false ->
|
||||||
Q0 = queue:new(),
|
Q0 = queue:new(),
|
||||||
Metric = #service_metric{
|
Metric = #service_metric{
|
||||||
name = MetricName,
|
name = Name,
|
||||||
symbol = MetricSymbol,
|
symbol = Symbol,
|
||||||
last_value = MetricValue,
|
last_value = Value,
|
||||||
update_ts = Ts,
|
update_ts = Ts,
|
||||||
queue = queue:in({Ts, MetricValue}, Q0)
|
queue = queue:in({Ts, Value}, Q0)
|
||||||
},
|
},
|
||||||
Metrics ++ [Metric]
|
Metrics ++ [Metric]
|
||||||
end.
|
end.
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
get_service(HostId, ServiceName) when is_binary(HostId), is_binary(ServiceName) ->
|
get_service(HostId, ServiceName) when is_binary(HostId), is_binary(ServiceName) ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(microservice), E#microservice.host_id =:= HostId, E#microservice.name =:= ServiceName]),
|
Q = qlc:q([E || E <- mnesia:table(microservice), E#service.host_id =:= HostId, E#service.name =:= ServiceName]),
|
||||||
qlc:e(Q)
|
qlc:e(Q)
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
@ -28,7 +28,7 @@ get_service(HostId, ServiceName) when is_binary(HostId), is_binary(ServiceName)
|
|||||||
|
|
||||||
get_services(HostId) when is_binary(HostId) ->
|
get_services(HostId) when is_binary(HostId) ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(microservice), E#microservice.host_id =:= HostId]),
|
Q = qlc:q([E || E <- mnesia:table(microservice), E#service.host_id =:= HostId]),
|
||||||
qlc:e(Q)
|
qlc:e(Q)
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
@ -38,7 +38,7 @@ get_services(HostId) when is_binary(HostId) ->
|
|||||||
[]
|
[]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
add_service(Service = #microservice{}) ->
|
add_service(Service = #service{}) ->
|
||||||
case mnesia:transaction(fun() -> mnesia:write(microservice, Service, write) end) of
|
case mnesia:transaction(fun() -> mnesia:write(microservice, Service, write) end) of
|
||||||
{atomic, _} ->
|
{atomic, _} ->
|
||||||
ok;
|
ok;
|
||||||
@ -52,7 +52,7 @@ change_status(ServiceId, Status) when is_binary(ServiceId), is_integer(Status) -
|
|||||||
[] ->
|
[] ->
|
||||||
mnesia:abort(<<"host not found">>);
|
mnesia:abort(<<"host not found">>);
|
||||||
[Service] ->
|
[Service] ->
|
||||||
NService = Service#microservice{status = Status},
|
NService = Service#service{status = Status},
|
||||||
mnesia:write(microservice, NService, write)
|
mnesia:write(microservice, NService, write)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
@ -63,7 +63,7 @@ change_status(ServiceId, Status) when is_binary(ServiceId), is_integer(Status) -
|
|||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
update_metric(TerminalId, MetricName, MetricSymbol, MetricValue) when is_binary(TerminalId), is_binary(MetricName), is_binary(MetricSymbol), is_number(MetricValue) ->
|
update_metric(ServiceId, Metrics) when is_binary(ServiceId), is_list(Metrics) ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
case mnesia:read(terminal, TerminalId) of
|
case mnesia:read(terminal, TerminalId) of
|
||||||
[] ->
|
[] ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user