add log
This commit is contained in:
parent
996540fa21
commit
9bed8c8622
@ -245,3 +245,16 @@
|
|||||||
%% 状态
|
%% 状态
|
||||||
status = 0
|
status = 0
|
||||||
}).
|
}).
|
||||||
|
|
||||||
|
%% 操作日志表
|
||||||
|
-record(log, {
|
||||||
|
log_id :: integer(),
|
||||||
|
%% 操作名称名称
|
||||||
|
action_name = <<>>,
|
||||||
|
%% 设备分类名称
|
||||||
|
assoc_name = <<>>,
|
||||||
|
%% 关联ID
|
||||||
|
assoc_id :: term(),
|
||||||
|
%% 创建时间
|
||||||
|
create_ts = 0 :: integer()
|
||||||
|
}).
|
||||||
@ -88,6 +88,14 @@ init_database() ->
|
|||||||
{type, ordered_set}
|
{type, ordered_set}
|
||||||
]),
|
]),
|
||||||
|
|
||||||
|
%% 操作日志表
|
||||||
|
mnesia:create_table(log, [
|
||||||
|
{attributes, record_info(fields, log)},
|
||||||
|
{record_name, log},
|
||||||
|
{disc_copies, [node()]},
|
||||||
|
{type, ordered_set}
|
||||||
|
]),
|
||||||
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% 加入集群
|
%% 加入集群
|
||||||
@ -112,4 +120,5 @@ copy_database(MasterNode) when is_atom(MasterNode) ->
|
|||||||
mnesia:add_table_copy(scenario, node(), ram_copies),
|
mnesia:add_table_copy(scenario, node(), ram_copies),
|
||||||
mnesia:add_table_copy(scenario_deploy, node(), ram_copies),
|
mnesia:add_table_copy(scenario_deploy, node(), ram_copies),
|
||||||
mnesia:add_table_copy(issue, node(), ram_copies),
|
mnesia:add_table_copy(issue, node(), ram_copies),
|
||||||
|
mnesia:add_table_copy(log, node(), ram_copies),
|
||||||
ok.
|
ok.
|
||||||
@ -38,7 +38,7 @@ get_all_hosts() ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
-spec get_hosts(Filter :: any(), Start :: integer(), Limit :: integer()) ->
|
-spec get_hosts(Spec :: tuple(), Start :: integer(), Limit :: integer()) ->
|
||||||
{ok, Items :: list(), TotalNum :: integer()} |
|
{ok, Items :: list(), TotalNum :: integer()} |
|
||||||
{error, Reason :: any()}.
|
{error, Reason :: any()}.
|
||||||
get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
|
||||||
|
|||||||
75
apps/iot/src/model/log_model.erl
Normal file
75
apps/iot/src/model/log_model.erl
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author licheng5
|
||||||
|
%%% @copyright (C) 2021, <COMPANY>
|
||||||
|
%%% @doc
|
||||||
|
%%%
|
||||||
|
%%% @end
|
||||||
|
%%% Created : 27. 4月 2021 下午4:38
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
-module(log_model).
|
||||||
|
-author("licheng5").
|
||||||
|
-include("iot.hrl").
|
||||||
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
|
-define(TAB_NAME, log).
|
||||||
|
|
||||||
|
%% API
|
||||||
|
-export([get_logs/1, add_log/1, delete/1, table_size/0]).
|
||||||
|
-export([to_map/1]).
|
||||||
|
|
||||||
|
%% 获取app信息
|
||||||
|
-spec get_logs(Limit :: integer()) -> {ok, Logs :: list()} | {error, Reason :: any()}.
|
||||||
|
get_logs(Limit) when is_integer(Limit), Limit > 0 ->
|
||||||
|
Fun = fun() ->
|
||||||
|
Q = qlc:q([E || E <- mnesia:table(?TAB_NAME)]),
|
||||||
|
%% 按照创建时间倒序排列
|
||||||
|
Order = fun(A, B) -> A#log.create_ts > B#log.create_ts end,
|
||||||
|
Q1 = qlc:sort(Q, [{order, Order}]),
|
||||||
|
|
||||||
|
QC = qlc:cursor(Q1),
|
||||||
|
qlc:next_answers(QC, Limit)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, Logs} when is_list(Logs) ->
|
||||||
|
{ok, Logs};
|
||||||
|
{aborted, Error} ->
|
||||||
|
{error, Error}
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec add_log(Log :: #log{}) -> ok | {error, Reason :: binary()}.
|
||||||
|
add_log(Log = #log{}) ->
|
||||||
|
case mnesia:transaction(fun() -> mnesia:write(?TAB_NAME, Log, write) end) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Error} ->
|
||||||
|
{error, Error}
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec delete(LogId :: integer()) -> ok | {error, Reason :: any()}.
|
||||||
|
delete(LogId) when is_binary(LogId) ->
|
||||||
|
case mnesia:transaction(fun() -> mnesia:delete(?TAB_NAME, LogId, write) end) of
|
||||||
|
{atomic, ok} ->
|
||||||
|
ok;
|
||||||
|
{aborted, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% 获取app表的数据大小
|
||||||
|
table_size() ->
|
||||||
|
mnesia:table_info(?TAB_NAME, size).
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%% helper methods
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
%% 主机信息的排序
|
||||||
|
sort(Logs) when is_list(Logs) ->
|
||||||
|
lists:sort(fun(#log{create_ts = Ts0}, #log{create_ts = Ts1}) -> Ts0 > Ts1 end, Logs).
|
||||||
|
|
||||||
|
%% 将数据转换成hash
|
||||||
|
to_map(#log{log_id = LogId, action_name = ActionName, assoc_name = AssocName, assoc_id = AssocId, create_ts = CreateTs}) ->
|
||||||
|
#{
|
||||||
|
<<"log_id">> => LogId,
|
||||||
|
<<"action_name">> => ActionName,
|
||||||
|
<<"create_ts">> => CreateTs
|
||||||
|
}.
|
||||||
Loading…
x
Reference in New Issue
Block a user