This commit is contained in:
anlicheng 2023-02-17 00:39:55 +08:00
parent dad21ddf02
commit 692660e899
4 changed files with 36 additions and 17 deletions

View File

@ -61,7 +61,7 @@
%%
last_value = 0 :: number(),
%% , 100, : [{ts, value}]
serial_values = [] :: queue(),
queue = [] :: queus:queue(),
%%
update_ts = 0 :: integer()
}).

View File

@ -13,6 +13,7 @@
-export([timestamp/0, number_format/2, current_time/0]).
-export([step/3, chunks/2, rand_bytes/1]).
-export([json_data/1, json_error/2]).
-export([queue_limited_in/3]).
%%
timestamp() ->
@ -66,4 +67,13 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
rand_bytes(Size) when is_integer(Size) ->
Bytes = crypto:strong_rand_bytes(Size),
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.

View File

@ -12,13 +12,19 @@
-include_lib("stdlib/include/qlc.hrl").
%% 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信息
get_all_hosts() ->
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
Fun = fun() ->
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,
case mnesia:transaction(Fun) of
{atomic, Items} when is_list(Items) ->

View File

@ -13,7 +13,7 @@
%% API
-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信息
get_all_terminals() ->
@ -88,30 +88,33 @@ 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
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
true ->
NSerialValues = case queue:len(SerialValues) >= 100 of
true ->
SerialValues0 = queue:drop(SerialValues),
queue:in(MetricValue, SerialValues0);
false ->
queue:in(MetricValue, SerialValues)
end,
Q1 = iot_util:queue_limited_in({Ts, MetricValue}, Q, 100),
Metric#terminal_metric{
name = MetricName,
last_value = MetricValue,
update_ts = iot_util:current_time(),
serial_values = NSerialValues
update_ts = Ts,
queue = Q1
};
false ->
Metric
end
end, Metrics);
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,
mnesia:write(terminal, Terminal#terminal{metrics = NMetrics}, write)
end