This commit is contained in:
安礼成 2023-04-19 20:08:58 +08:00
parent 996540fa21
commit 9bed8c8622
4 changed files with 98 additions and 1 deletions

View File

@ -245,3 +245,16 @@
%%
status = 0
}).
%%
-record(log, {
log_id :: integer(),
%%
action_name = <<>>,
%%
assoc_name = <<>>,
%% ID
assoc_id :: term(),
%%
create_ts = 0 :: integer()
}).

View File

@ -88,6 +88,14 @@ init_database() ->
{type, ordered_set}
]),
%%
mnesia:create_table(log, [
{attributes, record_info(fields, log)},
{record_name, log},
{disc_copies, [node()]},
{type, ordered_set}
]),
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_deploy, node(), ram_copies),
mnesia:add_table_copy(issue, node(), ram_copies),
mnesia:add_table_copy(log, node(), ram_copies),
ok.

View File

@ -38,7 +38,7 @@ get_all_hosts() ->
end.
%% app信息
-spec get_hosts(Filter :: any(), Start :: integer(), Limit :: integer()) ->
-spec get_hosts(Spec :: tuple(), Start :: integer(), Limit :: integer()) ->
{ok, Items :: list(), TotalNum :: integer()} |
{error, Reason :: any()}.
get_hosts(Spec, Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->

View 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
}.