From 692660e8999747ab5e0a2c9018a6b923b83219ba Mon Sep 17 00:00:00 2001 From: anlicheng Date: Fri, 17 Feb 2023 00:39:55 +0800 Subject: [PATCH] fix --- apps/iot/include/iot.hrl | 2 +- apps/iot/src/iot_util.erl | 12 +++++++++++- apps/iot/src/model/host_model.erl | 12 +++++++++--- apps/iot/src/model/terminal_model.erl | 27 +++++++++++++++------------ 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/apps/iot/include/iot.hrl b/apps/iot/include/iot.hrl index 858c667..8a2353e 100644 --- a/apps/iot/include/iot.hrl +++ b/apps/iot/include/iot.hrl @@ -61,7 +61,7 @@ %% 当前最新值 last_value = 0 :: number(), %% 历史值序列, 队列;保存最近的100数值, 格式为: [{ts, value}] - serial_values = [] :: queue(), + queue = [] :: queus:queue(), %% 最后更新时间 update_ts = 0 :: integer() }). diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index 21ce0ff..261be32 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -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) || <> <= Bytes]), - string:to_lower(S). \ No newline at end of file + 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. \ No newline at end of file diff --git a/apps/iot/src/model/host_model.erl b/apps/iot/src/model/host_model.erl index 4cfb559..f991a03 100644 --- a/apps/iot/src/model/host_model.erl +++ b/apps/iot/src/model/host_model.erl @@ -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) -> diff --git a/apps/iot/src/model/terminal_model.erl b/apps/iot/src/model/terminal_model.erl index 88576c3..fdef7b5 100644 --- a/apps/iot/src/model/terminal_model.erl +++ b/apps/iot/src/model/terminal_model.erl @@ -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