Merge branch 'master' of github.com:dgiot/dgiot

This commit is contained in:
lsxredrain 2021-08-26 20:24:54 +08:00
commit 347272fd2d
6 changed files with 238 additions and 46 deletions

View File

@ -63,14 +63,21 @@
]
},
"put": {
"summary": "更新日志配置",
"description": "更新日志配置",
"summary": "更新日志级别",
"description": "更新日志级别",
"parameters": [
{
"in": "query",
"name": "Handle",
"name": "type",
"type": "string",
"description": "日志模块",
"description": "日志类型",
"required": true
},
{
"in": "query",
"name": "name",
"type": "string",
"description": "模块名称",
"required": true
},
{
@ -450,13 +457,70 @@
]
}
},
"/app": {
"/traces": {
"get": {
"summary": "获取App",
"description": "获取App",
"summary": "traces列表",
"description": "获取traces列表",
"parameters": [],
"responses": {
"201": {
"200": {
"description": "Returns operation status"
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"500": {
"description": "Server Internal error"
}
},
"tags": [
"System"
]
},
"post": {
"summary": "traces操作",
"description": "启动停止traces",
"parameters": [
{
"in": "body",
"name": "body",
"schema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"description": "操作类型",
"example": "start"
},
"topic": {
"type": "string",
"description": "topic名称",
"example": "test"
},
"level": {
"type": "string",
"description": "日志等级",
"example": "info"
},
"logfile": {
"type": "string",
"description": "日志文件",
"example": "test.txt"
}
}
},
"description": "相关参数",
"required": true
}
],
"responses": {
"200": {
"description": "Returns operation status"
},
"400": {

View File

@ -210,19 +210,53 @@ do_request(post_cluster, #{<<"action">> := Action, <<"node">> := N}, _Context, _
{400, #{<<"error">> => dgiot_utils:format("~p", [Reason])}}
end;
%% Log : :
do_request(put_log_level, _Args, _Context, _Req) ->
{error, <<"TO DO">>};
%% Log : :
do_request(put_log_level, #{<<"type">> := Type, <<"name">> := Name, <<"level">> := Level}, _Context, _Req) ->
case dgiot_parse:set_loglevel(Type, Name, Level) of
ok ->
LoglevelId = dgiot_parse:get_loglevelid(Name, Type),
dgiot_parse:update_object(<<"LogLevel">>, LoglevelId,#{<<"level">> => Level}),
{200, #{<<"code">> => 200, <<"msg">> => <<"SUCCESS">>}};
{error, Reason} ->
{400, #{<<"code">> => 400, <<"error">> => dgiot_utils:format("~p", [Reason])}}
end;
%% Log : :
do_request(get_log_level, _Args, _Context, _Req) ->
{error, <<"TO DO">>};
%% System : App :App
do_request(get_app, _Args, _Context, _Req) ->
{ok, get_applist()};
%% traces : traces :traces列表
do_request(get_traces, _Args, _Context, _Req) ->
Data = emqx_tracer:lookup_traces(),
NewData =
lists:foldl(fun(X, Acc) ->
case X of
{{topic, Name}, {Level, _}} ->
Acc ++ [#{<<"name">> => dgiot_utils:to_binary(Name), <<"level">> => dgiot_utils:to_binary(Level)}];
_ ->
Acc
end
end, [], Data),
?LOG(info, "NewData ~p", [NewData]),
{200, #{<<"code">> => 200, <<"data">> => NewData}};
%% traces : traces :traces
do_request(post_traces, #{<<"action">> := Action, <<"topic">> := Topic, <<"level">> := Level, <<"logfile">> := LogFile}, _Context, _Req) ->
Rtn =
case Action of
<<"start">> ->
emqx_tracer:start_trace({topic, Topic}, binary_to_atom(Level), binary_to_list(LogFile));
<<"stop">> ->
emqx_tracer:stop_trace({topic, Topic});
_Other ->
{error, _Other}
end,
case Rtn of
ok ->
{200, #{<<"code">> => 200, <<"msg">> => <<"SUCCESS">>}};
{error, Reason} ->
{400, #{<<"code">> => 400, <<"error">> => dgiot_utils:format("~p", [Reason])}}
end;
%% API接口
do_request(OperationId, Args, _Context, _Req) ->
@ -287,28 +321,3 @@ format_val(Mod, Schema) ->
end, [], Paths),
{Tpl, [{mod, Mod}, {apis, Apis}], [{api, record_info(fields, api)}]}.
%% [{<<"appname">> => <<"ads">>,<<"modules">>=>[]}]
get_applist() ->
Apps = code:all_loaded(),
lists:foldl(fun({Appname, AppPath}, Acc) ->
case atom_to_binary(Appname) of
<<"dgiot_", _/binary>> ->
case file:list_dir_all(filename:dirname(AppPath)) of
{ok, Modules} ->
NewModules =
lists:foldl(fun(Mod, Mods) ->
case binary:split(dgiot_utils:to_binary(Mod), <<$.>>, [global, trim]) of
[Module, <<"bean">>] ->
Mods ++ [Module];
_ ->
Mods
end
end, [], Modules),
Acc ++ [#{<<"appname">> => Appname, <<"modules">> => NewModules}];
_ ->
Acc
end;
_ ->
Acc
end
end, [], Apps).

View File

@ -183,7 +183,6 @@ init(Req0, {swagger, Name} = Opts) ->
%% install
init(Req0, install) ->
io:format("Req0 install ~p",[Req0]),
Product = dgiot_req:binding(<<"Product">>, Req0),
#{peer := {IpPeer, _}} = Req0,
Req =
@ -193,7 +192,6 @@ init(Req0, install) ->
{ok, Name} = dgiot_data:lookup({Port, httpd}),
case catch (dgiot_install:start(#{product => Product, webserver => Name})) of
{Type, Reason} when Type == 'EXIT'; Type == error ->
?LOG(info,"Reason ~p ", [Reason]),
dgiot_req:reply(500, ?HEADER#{
<<"content-type">> => <<"application/json; charset=utf-8">>
}, jsx:encode(#{error => list_to_binary(io_lib:format("~p", [Reason]))}), Req0);

View File

@ -320,9 +320,7 @@ generate_users(Result) ->
%%
generate_rule(Result) ->
?LOG(info,"Result ~p",[Result]),
#{name := ServerName} = proplists:get_value(<<"webname">>, Result, #{name => dgiot_rest}),
?LOG(info,"ServerName ~p",[ServerName]),
Rules =
case dgiot_swagger:read(ServerName, #{}) of
{ok, Schema} ->

View File

@ -91,9 +91,11 @@
get_productid/3,
get_maintenanceid/2,
get_articleid/2,
get_loglevelid/2,
get_userids/1,
get_roleids/1,
get_notificationid/1
get_notificationid/1,
load_LogLevel/0
]).
-export([
@ -183,6 +185,10 @@ get_articleid(ProjectId, Timestamp) ->
<<Pid:10/binary, _/binary>> = dgiot_utils:to_md5(<<"Article", ProjectId/binary, Timestamp/binary>>),
Pid.
get_loglevelid(Name, Type) ->
<<Pid:10/binary, _/binary>> = dgiot_utils:to_md5(<<"LogLevel", Name/binary, Type/binary>>),
Pid.
get_objectid(Class, Map) ->
case Class of
<<"post_classes_article">> ->
@ -237,6 +243,15 @@ get_objectid(Class, Map) ->
Map#{
<<"objectId">> => Did
};
<<"post_classes_loglevel">> ->
get_objectid(<<"LogLevel">>, Map);
<<"LogLevel">> ->
Name = maps:get(<<"name">>, Map, <<"">>),
Type = maps:get(<<"type">>, Map, <<"">>),
<<Did:10/binary, _/binary>> = dgiot_utils:to_md5(<<"LogLevel", Name/binary, Type/binary>>),
Map#{
<<"objectId">> => Did
};
<<"post_classes_evidence">> ->
get_objectid(<<"Evidence">>, Map);
<<"Evidence">> ->
@ -1270,3 +1285,110 @@ get_roleids(Userid) ->
[]
end.
load_LogLevel() ->
Level = emqx_logger:get_primary_log_level(),
case create_logconfig(Level, <<"0">>, <<"dgiot">>, <<"system">>, 0) of
{ok, #{<<"objectId">> := DgiotlogId}} ->
create_applog(DgiotlogId);
_Ot ->
pass
end.
create_applog(DgiotlogId) ->
Apps = application:loaded_applications(),
lists:foldl(fun({Appname, _, _}, Acc) ->
BinAppname = atom_to_binary(Appname),
case BinAppname of
<<"dgiot_", _/binary>> ->
case create_logconfig(<<"info">>, DgiotlogId, BinAppname, <<"app">>, Acc) of
{ok, #{<<"objectId">> := ApplogId}} ->
AppPath = code:lib_dir(Appname) ++ "/ebin",
case file:list_dir_all(AppPath) of
{ok, Modules} ->
lists:foldl(fun(Mod, Mods) ->
BinMod = dgiot_utils:to_binary(Mod),
case binary:split(BinMod, <<$.>>, [global, trim]) of
[Module, <<"beam">>] ->
AtomMod = binary_to_atom(Module),
Modlevel =
case logger:get_module_level(AtomMod) of
[{AtomMod, Level} | _] ->
Level;
_ ->
<<"debug">>
end,
timer:sleep(1000),
case create_logconfig(Modlevel, ApplogId, BinMod, <<"module">>, Mods) of
{ok, #{<<"objectId">> := ModlogId}} ->
Functions = AtomMod:module_info(exports),
lists:foldl(fun({Fun, Num}, Funs) ->
BinFun = dgiot_utils:to_binary(Fun),
BinNum = dgiot_utils:to_binary(Num),
create_logconfig(Modlevel, ModlogId, <<BinFun/binary, "/", BinNum/binary>>, <<"function">>, Funs),
Funs + 1
end, 1, Functions);
_ ->
Mods
end,
Mods + 1;
_ ->
Mods
end
end, 1, Modules);
_Ot ->
?LOG(info, "_Ot ~p", [_Ot]),
Acc
end;
_ ->
Acc
end,
Acc + 1;
_ ->
Acc
end
end, 1, Apps).
create_logconfig(Level, Parent, Name, Type, Order) ->
create_loglevel(#{
<<"level">> => Level,
<<"parent">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"LogLevel">>,
<<"objectId">> => Parent
},
<<"name">> => Name,
<<"type">> => Type,
<<"order">> => Order
}).
create_loglevel(LogLevel) ->
Name1 = maps:get(<<"name">>, LogLevel),
Type1 = maps:get(<<"type">>, LogLevel),
LoglevelId = dgiot_parse:get_loglevelid(Name1, Type1),
case dgiot_parse:get_object(<<"LogLevel">>, LoglevelId) of
{ok, #{<<"objectId">> := LoglevelId, <<"type">> := Type, <<"name">> := Name, <<"level">> := Level}} ->
set_loglevel(Type, Name, Level),
{ok, #{<<"objectId">> => LoglevelId}};
_ ->
dgiot_parse:create_object(<<"LogLevel">>, LogLevel)
end.
%% emqx_logger:get_primary_log_level().
%% emqx_logger:set_log_level(debug).
%% app日志等级 emqx_logger:get_primary_log_level().
%% app日志等级 logger:set_application_level(dgiot,debug).
%% module日志等级 logger:get_module_level(dgiot)
%% module日志等级 logger:set_module_level(dgiot,debug)
set_loglevel(<<"system">>, <<"dgiot">>, Level) ->
emqx_logger:set_log_level(Level);
set_loglevel(<<"app">>, Name, Level) ->
logger:set_application_level(Name, Level);
set_loglevel(<<"module">>, Name, Level) ->
logger:set_module_level(Name, Level);
set_loglevel(Type, _Name, _Level) ->
{error, <<Type/binary, " error">>}.

View File

@ -166,6 +166,7 @@ init(?TYPE, Channel, Cfg) ->
Specs = [
{dgiot_dcache, {dgiot_dcache, start_link, Opts}, permanent, 5000, worker, [dgiot_dcache]}
],
dgiot_parse:load_LogLevel(),
{ok, State, Specs}.
%%