diff --git a/apps/iot/src/influxdb/influx_client.erl b/apps/iot/src/influxdb/influx_client.erl index c6cf0c2..4b950c1 100644 --- a/apps/iot/src/influxdb/influx_client.erl +++ b/apps/iot/src/influxdb/influx_client.erl @@ -35,6 +35,12 @@ %%% API %%%=================================================================== +%% 数据过滤器 +data_filter(#{<<"key">> := Key}) when is_binary(Key), Key /= <<>> -> + true; +data_filter(_) -> + false. + %% 获取时间标识符号 -spec get_precision(Timestamp :: integer()) -> binary(). get_precision(Timestamp) when is_integer(Timestamp) -> @@ -53,15 +59,22 @@ get_precision(Timestamp) when is_integer(Timestamp) -> -spec write_data(Measurement :: binary(), Tags :: map(), FieldsList :: list(), Timestamp :: integer()) -> no_return(). write_data(Measurement, Tags, FieldsList, Timestamp) when is_binary(Measurement), is_map(Tags), is_list(FieldsList), is_integer(Timestamp) -> - %% 按照设备的uuid进行分组 - Points = lists:map(fun(Fields = #{<<"key">> := Key}) -> - Values = maps:remove(<<"key">>, Fields), - NFields = #{Key => Values}, - influx_point:new(Measurement, Tags, NFields, Timestamp) - end, FieldsList), - Precision = influx_client:get_precision(Timestamp), + %% 过来掉没有key的选项 + NFieldsList = lists:filter(fun data_filter/1, FieldsList), + case length(NFieldsList) > 0 of + true -> + %% 按照设备的uuid进行分组 + Points = lists:map(fun(Fields = #{<<"key">> := Key}) -> + Values = maps:remove(<<"key">>, Fields), + NFields = #{Key => Values}, + influx_point:new(Measurement, Tags, NFields, Timestamp) + end, NFieldsList), + Precision = influx_client:get_precision(Timestamp), - poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, ?DEFAULT_BUCKET, ?DEFAULT_ORG, Precision, Points) end). + poolboy:transaction(influx_pool, fun(Pid) -> influx_client:write(Pid, ?DEFAULT_BUCKET, ?DEFAULT_ORG, Precision, Points) end); + false -> + ok + end. -spec write(Pid :: pid(), Bucket :: binary(), Org :: binary(), Points :: list()) -> no_return(). write(Pid, Bucket, Org, Points) when is_pid(Pid), is_binary(Bucket), is_binary(Org), is_list(Points) -> diff --git a/apps/iot/src/iot.app.src b/apps/iot/src/iot.app.src index d35662d..a5667ff 100644 --- a/apps/iot/src/iot.app.src +++ b/apps/iot/src/iot.app.src @@ -21,6 +21,7 @@ public_key, ssl, erts, + observer, kernel, stdlib ]}, diff --git a/apps/iot/src/iot_observer.erl b/apps/iot/src/iot_observer.erl new file mode 100644 index 0000000..7d12b2a --- /dev/null +++ b/apps/iot/src/iot_observer.erl @@ -0,0 +1,22 @@ +%%%------------------------------------------------------------------- +%%% @author anlicheng +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 21. 12月 2023 11:08 +%%%------------------------------------------------------------------- +-module(iot_observer). +-author("anlicheng"). + +%% API +-export([memory_top/1, cpu_top/1, stop/0]). + +memory_top(Interval) when is_integer(Interval) -> + spawn(fun()->etop:start([{output, text}, {interval, Interval}, {lines, 20}, {sort, memory}])end). + +cpu_top(Interval) when is_integer(Interval) -> + spawn(fun()->etop:start([{output, text}, {interval, Interval}, {lines, 20}, {sort, runtime}])end). + +stop() -> + etop:stop(). \ No newline at end of file