add observer
This commit is contained in:
parent
ef419f551c
commit
5f12140565
@ -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) ->
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
public_key,
|
||||
ssl,
|
||||
erts,
|
||||
observer,
|
||||
kernel,
|
||||
stdlib
|
||||
]},
|
||||
|
||||
22
apps/iot/src/iot_observer.erl
Normal file
22
apps/iot/src/iot_observer.erl
Normal file
@ -0,0 +1,22 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2023, <COMPANY>
|
||||
%%% @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().
|
||||
Loading…
x
Reference in New Issue
Block a user