From ef372e415d78e04bb0a1f9244f20f282443d385c Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 25 Aug 2021 11:21:44 +0800 Subject: [PATCH] chore(gw): fix the time format --- .../src/emqx_gateway_insta_sup.erl | 11 +++++++--- apps/emqx_gateway/src/emqx_gateway_intr.erl | 21 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl index ff49b5736..47f0104a9 100644 --- a/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl +++ b/apps/emqx_gateway/src/emqx_gateway_insta_sup.erl @@ -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 diff --git a/apps/emqx_gateway/src/emqx_gateway_intr.erl b/apps/emqx_gateway/src/emqx_gateway_intr.erl index bafdb233e..b2a8b0484 100644 --- a/apps/emqx_gateway/src/emqx_gateway_intr.erl +++ b/apps/emqx_gateway/src/emqx_gateway_intr.erl @@ -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.