chore(gw): fix the time format

This commit is contained in:
JianBo He 2021-08-25 11:21:44 +08:00 committed by turtleDeng
parent e239fb07cd
commit ef372e415d
2 changed files with 27 additions and 5 deletions

View File

@ -45,7 +45,8 @@
child_pids :: [pid()],
gw_state :: emqx_gateway_impl:state() | undefined,
created_at :: integer(),
started_at :: integer() | undefined
started_at :: integer() | undefined,
stopped_at :: integer() | undefined
}).
%%--------------------------------------------------------------------
@ -126,7 +127,8 @@ do_deinit_context(Ctx) ->
handle_call(info, _From, State = #state{gw = Gateway}) ->
GwInfo = Gateway#{status => State#state.status,
created_at => State#state.created_at,
started_at => State#state.started_at
started_at => State#state.started_at,
stopped_at => State#state.stopped_at
},
{reply, GwInfo, State};
@ -259,8 +261,10 @@ cb_gateway_unload(State = #state{gw = Gateway = #{name := GwName},
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
CbMod:on_gateway_unload(Gateway, GwState),
{ok, State#state{child_pids = [],
status = stopped,
gw_state = undefined,
status = stopped}}
started_at = undefined,
stopped_at = erlang:system_time(millisecond)}}
catch
Class : Reason : Stk ->
logger:error("Failed to unload gateway (~0p, ~0p) crashed: "
@ -282,6 +286,7 @@ cb_gateway_load(State = #state{gw = Gateway = #{name := GwName},
status = running,
child_pids = ChildPids,
gw_state = GwState,
stopped_at = undefined,
started_at = erlang:system_time(millisecond)
}}
end

View File

@ -40,8 +40,14 @@ gateways(Status) ->
case emqx_gateway:lookup(GwName) of
undefined -> #{name => GwName, status => unloaded};
GwInfo = #{rawconf := RawConf} ->
GwInfo1 = maps:with(
[name, started_at, craeted_at, status], GwInfo),
GwInfo0 = unix_ts_to_rfc3339(
[created_at, started_at, stopped_at],
GwInfo),
GwInfo1 = maps:with([name,
status,
created_at,
started_at,
stopped_at], GwInfo0),
GwInfo1#{listeners => get_listeners_status(GwName, RawConf)}
end
@ -70,3 +76,14 @@ get_listeners_status(GwName, RawConf) ->
%% @private
listener_name(GwName, Type, LisName) ->
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
%% @private
unix_ts_to_rfc3339(Keys, Map) when is_list(Keys) ->
lists:foldl(fun(K, Acc) -> unix_ts_to_rfc3339(K, Acc) end, Map, Keys);
unix_ts_to_rfc3339(Key, Map) ->
case maps:get(Key, Map, undefined) of
undefined -> Map;
Ts ->
Map#{Key =>
emqx_rule_funcs:unix_ts_to_rfc3339(Ts, <<"millisecond">>)}
end.