fix
This commit is contained in:
parent
dad21ddf02
commit
692660e899
@ -61,7 +61,7 @@
|
|||||||
%% 当前最新值
|
%% 当前最新值
|
||||||
last_value = 0 :: number(),
|
last_value = 0 :: number(),
|
||||||
%% 历史值序列, 队列;保存最近的100数值, 格式为: [{ts, value}]
|
%% 历史值序列, 队列;保存最近的100数值, 格式为: [{ts, value}]
|
||||||
serial_values = [] :: queue(),
|
queue = [] :: queus:queue(),
|
||||||
%% 最后更新时间
|
%% 最后更新时间
|
||||||
update_ts = 0 :: integer()
|
update_ts = 0 :: integer()
|
||||||
}).
|
}).
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
-export([timestamp/0, number_format/2, current_time/0]).
|
-export([timestamp/0, number_format/2, current_time/0]).
|
||||||
-export([step/3, chunks/2, rand_bytes/1]).
|
-export([step/3, chunks/2, rand_bytes/1]).
|
||||||
-export([json_data/1, json_error/2]).
|
-export([json_data/1, json_error/2]).
|
||||||
|
-export([queue_limited_in/3]).
|
||||||
|
|
||||||
%% 时间,精确到毫秒
|
%% 时间,精确到毫秒
|
||||||
timestamp() ->
|
timestamp() ->
|
||||||
@ -67,3 +68,12 @@ rand_bytes(Size) when is_integer(Size) ->
|
|||||||
Bytes = crypto:strong_rand_bytes(Size),
|
Bytes = crypto:strong_rand_bytes(Size),
|
||||||
S = lists:flatten([integer_to_list(E, 16) || <<E:4>> <= Bytes]),
|
S = lists:flatten([integer_to_list(E, 16) || <<E:4>> <= Bytes]),
|
||||||
string:to_lower(S).
|
string:to_lower(S).
|
||||||
|
|
||||||
|
queue_limited_in(Item, Q, Num) when is_integer(Num) ->
|
||||||
|
case queue:len(Q) >= Num of
|
||||||
|
true ->
|
||||||
|
Q1 = queue:drop(Q),
|
||||||
|
queue:in(Item, Q1);
|
||||||
|
false ->
|
||||||
|
queue:in(Item, Q)
|
||||||
|
end.
|
||||||
@ -12,13 +12,19 @@
|
|||||||
-include_lib("stdlib/include/qlc.hrl").
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_all_hosts/0, insert/4, change_status/2, delete/1, table_size/0]).
|
-export([get_hosts/2, insert/4, change_status/2, delete/1, table_size/0]).
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
get_all_hosts() ->
|
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
||||||
Fun = fun() ->
|
Fun = fun() ->
|
||||||
Q = qlc:q([E || E <- mnesia:table(host)]),
|
Q = qlc:q([E || E <- mnesia:table(host)]),
|
||||||
qlc:e(Q)
|
QC = qlc:cursor(Q),
|
||||||
|
case qlc:next_answers(QC, Start + Limit) of
|
||||||
|
Answers when is_list(Answers) ->
|
||||||
|
lists:sublist(Answers, Start + 1, Limit);
|
||||||
|
_ ->
|
||||||
|
[]
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
case mnesia:transaction(Fun) of
|
case mnesia:transaction(Fun) of
|
||||||
{atomic, Items} when is_list(Items) ->
|
{atomic, Items} when is_list(Items) ->
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_all_terminals/0, get_status_stat/0]).
|
-export([get_all_terminals/0, get_status_stat/0]).
|
||||||
-export([change_status/2, delete/1]).
|
-export([change_status/2, delete/1, update_metric/4]).
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
get_all_terminals() ->
|
get_all_terminals() ->
|
||||||
@ -88,30 +88,33 @@ update_metric(TerminalId, MetricName, MetricSymbol, MetricValue) when is_binary(
|
|||||||
[] ->
|
[] ->
|
||||||
mnesia:abort(<<"terminal not found">>);
|
mnesia:abort(<<"terminal not found">>);
|
||||||
[Terminal = #terminal{metrics = Metrics}] ->
|
[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(#terminal_metric{symbol = Symbol}) -> Symbol =:= MetricSymbol end, Metrics) of
|
||||||
true ->
|
true ->
|
||||||
lists:map(fun(Metric=#terminal_metric{symbol = Symbol, serial_values = SerialValues}) ->
|
lists:map(fun(Metric=#terminal_metric{symbol = Symbol, queue = Q}) ->
|
||||||
case Symbol =:= MetricSymbol of
|
case Symbol =:= MetricSymbol of
|
||||||
true ->
|
true ->
|
||||||
NSerialValues = case queue:len(SerialValues) >= 100 of
|
Q1 = iot_util:queue_limited_in({Ts, MetricValue}, Q, 100),
|
||||||
true ->
|
|
||||||
SerialValues0 = queue:drop(SerialValues),
|
|
||||||
queue:in(MetricValue, SerialValues0);
|
|
||||||
false ->
|
|
||||||
queue:in(MetricValue, SerialValues)
|
|
||||||
end,
|
|
||||||
Metric#terminal_metric{
|
Metric#terminal_metric{
|
||||||
name = MetricName,
|
name = MetricName,
|
||||||
last_value = MetricValue,
|
last_value = MetricValue,
|
||||||
update_ts = iot_util:current_time(),
|
update_ts = Ts,
|
||||||
serial_values = NSerialValues
|
queue = Q1
|
||||||
};
|
};
|
||||||
false ->
|
false ->
|
||||||
Metric
|
Metric
|
||||||
end
|
end
|
||||||
end, Metrics);
|
end, Metrics);
|
||||||
false ->
|
false ->
|
||||||
Metrics#terminal_metric{}
|
Q0 = queue:new(),
|
||||||
|
Metric = #terminal_metric{
|
||||||
|
name = MetricName,
|
||||||
|
symbol = MetricSymbol,
|
||||||
|
last_value = MetricValue,
|
||||||
|
update_ts = Ts,
|
||||||
|
queue = queue:in({Ts, MetricValue}, Q0)
|
||||||
|
},
|
||||||
|
Metrics ++ [Metric]
|
||||||
end,
|
end,
|
||||||
mnesia:write(terminal, Terminal#terminal{metrics = NMetrics}, write)
|
mnesia:write(terminal, Terminal#terminal{metrics = NMetrics}, write)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user