Merge pull request #5306 from DDDHuang/sys_info

chore: sys uptime by millisecond
This commit is contained in:
DDDHuang 2021-08-05 13:51:06 +08:00 committed by GitHub
commit e7cb9491c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 28 deletions

View File

@ -89,7 +89,7 @@ version() -> emqx_app:get_release().
sysdescr() -> emqx_app:get_description().
%% @doc Get sys uptime
-spec(uptime() -> string()).
-spec(uptime() -> Milliseconds :: integer()).
uptime() ->
gen_server:call(?SYS, uptime).
@ -142,7 +142,7 @@ handle_cast(Msg, State) ->
{noreply, State}.
handle_info({timeout, TRef, heartbeat}, State = #state{heartbeat = TRef}) ->
publish_any(uptime, iolist_to_binary(uptime(State))),
publish_any(uptime, integer_to_binary(uptime(State))),
publish_any(datetime, iolist_to_binary(datetime())),
{noreply, heartbeat(State)};
@ -167,22 +167,7 @@ terminate(_Reason, #state{heartbeat = TRef1, ticker = TRef2}) ->
%%-----------------------------------------------------------------------------
uptime(#state{start_time = Ts}) ->
Secs = timer:now_diff(erlang:timestamp(), Ts) div 1000000,
lists:flatten(uptime(seconds, Secs)).
uptime(seconds, Secs) when Secs < 60 ->
[integer_to_list(Secs), " seconds"];
uptime(seconds, Secs) ->
[uptime(minutes, Secs div 60), integer_to_list(Secs rem 60), " seconds"];
uptime(minutes, M) when M < 60 ->
[integer_to_list(M), " minutes, "];
uptime(minutes, M) ->
[uptime(hours, M div 60), integer_to_list(M rem 60), " minutes, "];
uptime(hours, H) when H < 24 ->
[integer_to_list(H), " hours, "];
uptime(hours, H) ->
[uptime(days, H div 24), integer_to_list(H rem 24), " hours, "];
uptime(days, D) ->
[integer_to_list(D), " days, "].
timer:now_diff(erlang:timestamp(), Ts) div 1000.
publish_any(Name, Value) ->
_ = publish(Name, Value),

View File

@ -39,12 +39,6 @@ end_per_suite(_Config) ->
% t_sysdescr(_) ->
% error('TODO').
t_uptime(_) ->
?assertEqual(<<"1 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 1))),
?assertEqual(<<"1 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 60))),
?assertEqual(<<"1 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 3600))),
?assertEqual(<<"1 days, 0 hours, 0 minutes, 0 seconds">>, iolist_to_binary(emqx_sys:uptime(seconds, 86400))).
% t_datetime(_) ->
% error('TODO').

View File

@ -114,7 +114,7 @@ postcondition(_State, {call, emqx_sys, info, []}, Info) ->
postcondition(_State, {call, emqx_sys, version, []}, Version) ->
is_list(Version);
postcondition(_State, {call, emqx_sys, uptime, []}, Uptime) ->
is_list(Uptime);
is_integer(Uptime);
postcondition(_State, {call, emqx_sys, datetime, []}, Datetime) ->
is_list(Datetime);
postcondition(_State, {call, emqx_sys, sysdescr, []}, Sysdescr) ->

View File

@ -149,7 +149,7 @@ node_info(Node) when Node =:= node() ->
max_fds => proplists:get_value(max_fds, lists:usort(lists:flatten(erlang:system_info(check_io)))),
connections => ets:info(emqx_channel, size),
node_status => 'Running',
uptime => iolist_to_binary(proplists:get_value(uptime, BrokerInfo)),
uptime => proplists:get_value(uptime, BrokerInfo),
version => iolist_to_binary(proplists:get_value(version, BrokerInfo))
};
node_info(Node) ->

View File

@ -82,8 +82,8 @@ node_schema() ->
type => integer,
description => <<"Number of used processes">>},
uptime => #{
type => string,
description => <<"EMQ X Broker runtime">>},
type => integer,
description => <<"EMQ X Broker runtime, millisecond">>},
version => #{
type => string,
description => <<"EMQ X Broker version">>},