add parse api log

This commit is contained in:
lsxredrain 2021-08-30 11:49:06 +08:00
parent 43e962ba6e
commit 3e6623ec39
7 changed files with 298 additions and 37 deletions

View File

@ -41,11 +41,11 @@
test(N) ->
%% Test = <<"test">>,
?MLOG(info, #{<<"test">> => test}),
?MLOG(info, #{<<"test">> => <<"中文"/utf8>>}),
?MLOG(info, #{test1 => test1}),
lists:map(fun(X) ->
timer:sleep(2),
?MLOG(info, #{<<"test">> => X, <<"time">> => dgiot_datetime:now_microsecs()}, ['acl_test'])
?MLOG(info, #{<<"test">> => X, <<"name">> => <<"中文"/utf8>>, <<"time">> => dgiot_datetime:now_microsecs()}, ['acl_test'])
end,lists:seq(1,N)).
%%--------------------------------------------------------------------

View File

@ -38,10 +38,10 @@
%% Handlers
-export([handle_request/2]).
-export([handle_multipart/2]).
-export([call/4]).
-export([call/3]).
-dgiot_data("ets").
-export([init_ets/0]).
-export([init_ets/0,get_log/4]).
-record(state, {
operationid :: atom(),
@ -80,7 +80,7 @@ init(Req, #{logic_handler := LogicHandler} = Map) ->
<<"OPTIONS">> ->
default_init(#{operationid => options}, State, Req);
_ ->
case call(LogicHandler, init, [Req, Map], Req) of
case call(LogicHandler, init, [Req, Map]) of
{no_call, Req1} ->
Index = maps:get(Method, Map),
{ok, {_, Config}} = dgiot_router:get_state(Index),
@ -301,9 +301,9 @@ do_request(Populated, Req0, State = #state{
Result =
case IsMock of
true ->
call(LogicHandler, mock, Args, Req0);
call(LogicHandler, mock, Args);
false ->
call(LogicHandler, handle, Args, Req0)
call(LogicHandler, handle, Args)
end,
case Result of
no_call when IsMock ->
@ -356,7 +356,7 @@ handle_multipart({file, Name, Filename, ContentType}, Req, Acc, State) ->
do_authorized(LogicHandler, OperationID, Args, Req) ->
case call(LogicHandler, check_auth, [OperationID, Args, Req], Req) of
case call(LogicHandler, check_auth, [OperationID, Args, Req]) of
no_call ->
dgiot_auth:check_auth(OperationID, Args, Req);
{true, NContext, Req1} ->
@ -366,7 +366,7 @@ do_authorized(LogicHandler, OperationID, Args, Req) ->
{false, Err, Req1} ->
{false, Err, Req1};
{switch_handler, NLogicHandler, Req1} ->
call(NLogicHandler, check_auth, [OperationID, Args, Req1], Req1)
call(NLogicHandler, check_auth, [OperationID, Args, Req1])
end.
default_mock_handler(_OperationID, _Populated, Context, Req) ->
@ -397,50 +397,56 @@ do_response(Status, Headers, Body, Req0, State) when is_binary(Body) ->
end,
{stop, Req, State}.
call(Mod, Fun, Args, Req) ->
call(Mod, Fun, Args) ->
case erlang:function_exported(Mod, Fun, length(Args)) of
true ->
{Time, Result} = timer:tc(Mod, Fun, Args),
get_log(Req, Time, Result),
get_log(Fun, Args, Time, Result),
Result;
false ->
BinMod = dgiot_utils:to_binary(Mod),
BinFun = dgiot_utils:to_binary(Fun),
BinLen = dgiot_utils:to_binary(length(Args)),
get_log(Req, 0, {not_fun, <<BinMod/binary, ":", BinFun/binary, "/", BinLen/binary, " no_call">>}),
no_call
end.
init_ets() ->
dgiot_data:init(?DGIOT_SWAGGER).
get_log(#{peer := {PeerName, _}} = Req, Time, Result) ->
Ip = dgiot_utils:get_ip(PeerName),
SessionToken = maps:get(<<"sessionToken">>, Req, <<"">>),
NewReq = maps:with([method, path], Req),
get_log(handle, [_OperationID, Body, #{<<"sessionToken">> := SessionToken} = _Context, Req], Time, Result)
when is_map(Body) ->
Username =
case dgiot_auth:get_session(SessionToken) of
#{<<"username">> := Name} ->
dgiot_utils:to_binary(Name);
_ ->
<<"">>
#{<<"username">> := Name} -> Name;
_ -> <<"">>
end,
log(Req, Time, Result, Body#{<<"username">> => Username});
get_log(check_auth, [_OperationID, Args, Req], Time, Result) ->
log(Req, Time, Result, Args);
get_log(_Fun, _, _, _) ->
pass.
log(#{peer := {PeerName, _}} = Req, Time, Result, Map) when is_map(Map) ->
Ip = dgiot_utils:get_ip(PeerName),
NewReq = maps:with([method, path], Req),
UserName = maps:get(<<"username">>, Map, <<"">>),
Body = maps:without([<<"username">>, <<"password">>], Map),
{Code, Reason} =
case Result of
{200, _} ->
{200, <<"success">>};
{not_fun, Msg} ->
{<<"not_fun">>, Msg};
{no_call, _} ->
{<<"no_call">>, <<"no_call">>};
no_call ->
{<<"no_call">>, <<"no_call">>};
_ ->
{<<"error">>, <<"error">>}
end,
?MLOG(info, NewReq#{<<"code">> => Code, <<"reason">> => Reason, <<"ip">> => Ip, <<"username">> => Username, <<"elapsedtime">> => Time}, ['parse_api']);
?MLOG(info, NewReq#{
<<"code">> => Code,
<<"reason">> => Reason,
<<"ip">> => Ip,
<<"username">> => UserName,
<<"body">> => Body,
<<"elapsedtime">> => Time},
['parse_api']);
log(_Req, _Time, _Result, _Map) ->
pass.
get_log(Req, _, _) ->
?LOG(info, "Req22222 ~p", [Req]).

View File

@ -120,7 +120,6 @@ is_authorized(OperationID, Token, Req) ->
put_session(UserInfo#{<<"sessionToken">> => Token}, ttl()),
%%
Action = list_to_binary(string:to_upper(atom_to_list(OperationID))),
%% ?LOG(info,"Action ~p Rules ~p",[Action, Rules]),
case lists:member(Action, Rules) of
false ->
{forbidden, #{<<"code">> => 119, <<"error">> => <<Action/binary, " Forbidden">>}, Req};

View File

@ -21,3 +21,251 @@
-define(ROLE_USER_ETS, role_user_ets).
-define(USER_ROLE_ETS, user_role_ets).
-define(ROLE_PARENT_ETS, role_parent_ets).
%%public enum ErrorCode
%%{
%%/// <summary>
%%/// Error code indicating that an unknown error or an error unrelated to Parse
%%/// occurred.
%%/// </summary>
%%OtherCause = -1,
%%
%%/// <summary>
%%/// Error code indicating that something has gone wrong with the server.
%%/// If you get this error code, it is Parse's fault. Please report the bug to
%%/// https://parse.com/help.
%%/// </summary>
%%InternalServerError = 1,
%%
%%/// <summary>
%%/// Error code indicating the connection to the Parse servers failed.
%%/// </summary>
%%ConnectionFailed = 100,
%%
%%/// <summary>
%%/// Error code indicating the specified object doesn't exist.
%%/// </summary>
%%ObjectNotFound = 101,
%%
%%/// <summary>
%%/// Error code indicating you tried to query with a datatype that doesn't
%%/// support it, like exact matching an array or object.
%%/// </summary>
%%InvalidQuery = 102,
%%
%%/// <summary>
%%/// Error code indicating a missing or invalid classname. Classnames are
%%/// case-sensitive. They must start with a letter, and a-zA-Z0-9_ are the
%%/// only valid characters.
%%/// </summary>
%%InvalidClassName = 103,
%%
%%/// <summary>
%%/// Error code indicating an unspecified object id.
%%/// </summary>
%%MissingObjectId = 104,
%%
%%/// <summary>
%%/// Error code indicating an invalid key name. Keys are case-sensitive. They
%%/// must start with a letter, and a-zA-Z0-9_ are the only valid characters.
%%/// </summary>
%%InvalidKeyName = 105,
%%
%%/// <summary>
%%/// Error code indicating a malformed pointer. You should not see this unless
%%/// you have been mucking about changing internal Parse code.
%%/// </summary>
%%InvalidPointer = 106,
%%
%%/// <summary>
%%/// Error code indicating that badly formed JSON was received upstream. This
%%/// either indicates you have done something unusual with modifying how
%%/// things encode to JSON, or the network is failing badly.
%%/// </summary>
%%InvalidJSON = 107,
%%
%%/// <summary>
%%/// Error code indicating that the feature you tried to access is only
%%/// available internally for testing purposes.
%%/// </summary>
%%CommandUnavailable = 108,
%%
%%/// <summary>
%%/// You must call Parse.initialize before using the Parse library.
%%/// </summary>
%%NotInitialized = 109,
%%
%%/// <summary>
%%/// Error code indicating that a field was set to an inconsistent type.
%%/// </summary>
%%IncorrectType = 111,
%%
%%/// <summary>
%%/// Error code indicating an invalid channel name. A channel name is either
%%/// an empty string (the broadcast channel) or contains only a-zA-Z0-9_
%%/// characters and starts with a letter.
%%/// </summary>
%%InvalidChannelName = 112,
%%
%%/// <summary>
%%/// Error code indicating that push is misconfigured.
%%/// </summary>
%%PushMisconfigured = 115,
%%
%%/// <summary>
%%/// Error code indicating that the object is too large.
%%/// </summary>
%%ObjectTooLarge = 116,
%%
%%/// <summary>
%%/// Error code indicating that the operation isn't allowed for clients.
%%/// </summary>
%%OperationForbidden = 119,
%%
%%/// <summary>
%%/// Error code indicating the result was not found in the cache.
%%/// </summary>
%%CacheMiss = 120,
%%
%%/// <summary>
%%/// Error code indicating that an invalid key was used in a nested
%%/// JSONObject.
%%/// </summary>
%%InvalidNestedKey = 121,
%%
%%/// <summary>
%%/// Error code indicating that an invalid filename was used for ParseFile.
%%/// A valid file name contains only a-zA-Z0-9_. characters and is between 1
%%/// and 128 characters.
%%/// </summary>
%%InvalidFileName = 122,
%%
%%/// <summary>
%%/// Error code indicating an invalid ACL was provided.
%%/// </summary>
%%InvalidACL = 123,
%%
%%/// <summary>
%%/// Error code indicating that the request timed out on the server. Typically
%%/// this indicates that the request is too expensive to run.
%%/// </summary>
%%Timeout = 124,
%%
%%/// <summary>
%%/// Error code indicating that the email address was invalid.
%%/// </summary>
%%InvalidEmailAddress = 125,
%%
%%/// <summary>
%%/// Error code indicating that a unique field was given a value that is
%%/// already taken.
%%/// </summary>
%%DuplicateValue = 137,
%%
%%/// <summary>
%%/// Error code indicating that a role's name is invalid.
%%/// </summary>
%%InvalidRoleName = 139,
%%
%%/// <summary>
%%/// Error code indicating that an application quota was exceeded. Upgrade to
%%/// resolve.
%%/// </summary>
%%ExceededQuota = 140,
%%
%%/// <summary>
%%/// Error code indicating that a Cloud Code script failed.
%%/// </summary>
%%ScriptFailed = 141,
%%
%%/// <summary>
%%/// Error code indicating that a Cloud Code validation failed.
%%/// </summary>
%%ValidationFailed = 142,
%%
%%/// <summary>
%%/// Error code indicating that deleting a file failed.
%%/// </summary>
%%FileDeleteFailed = 153,
%%
%%/// <summary>
%%/// Error code indicating that the application has exceeded its request limit.
%%/// </summary>
%%RequestLimitExceeded = 155,
%%
%%/// <summary>
%%/// Error code indicating that the provided event name is invalid.
%%/// </summary>
%%InvalidEventName = 160,
%%
%%/// <summary>
%%/// Error code indicating that the username is missing or empty.
%%/// </summary>
%%UsernameMissing = 200,
%%
%%/// <summary>
%%/// Error code indicating that the password is missing or empty.
%%/// </summary>
%%PasswordMissing = 201,
%%
%%/// <summary>
%%/// Error code indicating that the username has already been taken.
%%/// </summary>
%%UsernameTaken = 202,
%%
%%/// <summary>
%%/// Error code indicating that the email has already been taken.
%%/// </summary>
%%EmailTaken = 203,
%%
%%/// <summary>
%%/// Error code indicating that the email is missing, but must be specified.
%%/// </summary>
%%EmailMissing = 204,
%%
%%/// <summary>
%%/// Error code indicating that a user with the specified email was not found.
%%/// </summary>
%%EmailNotFound = 205,
%%
%%/// <summary>
%%/// Error code indicating that a user object without a valid session could
%%/// not be altered.
%%/// </summary>
%%SessionMissing = 206,
%%
%%/// <summary>
%%/// Error code indicating that a user can only be created through signup.
%%/// </summary>
%%MustCreateUserThroughSignup = 207,
%%
%%/// <summary>
%%/// Error code indicating that an an account being linked is already linked
%%/// to another user.
%%/// </summary>
%%AccountAlreadyLinked = 208,
%%
%%/// <summary>
%%/// Error code indicating that the current session token is invalid.
%%/// </summary>
%%InvalidSessionToken = 209,
%%
%%/// <summary>
%%/// Error code indicating that a user cannot be linked to an account because
%%/// that account's id could not be found.
%%/// </summary>
%%LinkedIdMissing = 250,
%%
%%/// <summary>
%%/// Error code indicating that a user with a linked (e.g. Facebook) account
%%/// has an invalid session.
%%/// </summary>
%%InvalidLinkedSession = 251,
%%
%%/// <summary>
%%/// Error code indicating that a service being linked (e.g. Facebook or
%%/// Twitter) is unsupported.
%%/// </summary>
%%UnsupportedService = 252
%%}

View File

@ -83,7 +83,6 @@ init(Req0, Map) ->
Method = maps:get(<<"_method">>, RecvMap, dgiot_req:method(Req)),
Index = maps:get(Method, Map),
{ok, {_, Config}} = dgiot_router:get_state(Index),
emqx_logger:debug("Parse js call ~p,~p~n", [Config, RecvMap]),
OperationId = maps:get(operationid, Config, not_allowed),
Produces = maps:get(produces, Config, []),
%% 使cookiesparse js放在body里面

View File

@ -75,11 +75,15 @@ save_to_parse(Channel, Requests) ->
{ok, Results} ->
dgiot_metrics:inc(dgiot_parse, <<"parse_save_success">>, length(Requests)),
do_result(Requests, Results);
#{<<"code">> := 105,<<"error">> := _Error} ->
io:format("_Error ~p ~n",[_Error]),
pass;
Result ->
io:format("Result ~p ~n",[Result]),
log(Requests, Result),
dgiot_metrics:inc(dgiot_parse, <<"parse_save_fail">>, length(Requests)),
%%
%% save_to_cache(Channel, Requests),
save_to_cache(Channel, Requests),
ok
end.

View File

@ -202,7 +202,11 @@ send(#{error_logger := _Error_logger, mfa := {M, F, A}} = _Meta, Payload) ->
Mfa = <<(atom_to_binary(M, utf8))/binary, $/, (atom_to_binary(F, utf8))/binary, $/, (integer_to_binary(A))/binary>>,
Topic = <<"logger_trace/", Mfa/binary>>,
dgiot_mqtt:publish(Mfa, Topic, Payload),
dgiot_parse_cache:save_to_cache(#{<<"method">> => <<"POST">>, <<"path">> => <<"/classes/Log">>, <<"body">> => Payload});
Map = jiffy:decode(Payload, [return_maps]),
NewMap = maps:with([<<"time">>, <<"pid">>, <<"msg">>, <<"mfa">>, <<"line">>, <<"level">>, <<"clientid">>, <<"topic">>, <<"peername">>],Map),
dgiot_parse_cache:save_to_cache(#{<<"method">> => <<"POST">>,
<<"path">> => <<"/classes/Log">>,
<<"body">> => get_body(NewMap)});
send(#{mfa := _MFA} = _Meta, Payload) ->
Map = jiffy:decode(Payload, [return_maps]),
@ -228,10 +232,11 @@ send(#{mfa := _MFA} = _Meta, Payload) ->
<<"logger_trace/log/", Mfa/binary, "/", Line/binary>>
end,
dgiot_mqtt:publish(Mfa, Topic, Payload),
NewMap = maps:with([<<"time">>, <<"pid">>, <<"msg">>, <<"mfa">>, <<"line">>, <<"level">>, <<"clientid">>, <<"topic">>, <<"peername">>],Map),
dgiot_parse_cache:save_to_cache(#{
<<"method">> => <<"POST">>,
<<"path">> => <<"/classes/Log">>,
<<"body">> => get_body(Map)});
<<"body">> => get_body(NewMap)});
send(_Meta, Payload) ->
dgiot_mqtt:publish(<<"logger_trace_other">>, <<"logger_trace/other">>, Payload),