fix: uptime display in CLI printout should be human-readable

This commit is contained in:
Zaiming (Stone) Shi 2022-06-13 11:44:26 +02:00
parent b14998ea7f
commit ba23771a0a
2 changed files with 14 additions and 2 deletions

View File

@ -20,7 +20,8 @@
%% API %% API
-export([ -export([
to_epoch_millisecond/1, to_epoch_millisecond/1,
to_epoch_second/1 to_epoch_second/1,
human_readable_duration_string/1
]). ]).
-export([ -export([
epoch_to_rfc3339/1, epoch_to_rfc3339/1,
@ -61,6 +62,15 @@ epoch_to_rfc3339(TimeStamp) ->
epoch_to_rfc3339(TimeStamp, Unit) when is_integer(TimeStamp) -> epoch_to_rfc3339(TimeStamp, Unit) when is_integer(TimeStamp) ->
list_to_binary(calendar:system_time_to_rfc3339(TimeStamp, [{unit, Unit}])). list_to_binary(calendar:system_time_to_rfc3339(TimeStamp, [{unit, Unit}])).
-spec human_readable_duration_string(integer()) -> string().
human_readable_duration_string(Milliseconds) ->
Seconds = Milliseconds div 1000,
{D, {H, M, S}} = calendar:seconds_to_daystime(Seconds),
L0 = [{D, " days"}, {H, " hours"}, {M, " minutes"}, {S, " seconds"}],
L1 = lists:dropwhile(fun({K, _}) -> K =:= 0 end, L0),
L2 = lists:map(fun({Time, Unit}) -> [integer_to_list(Time), Unit] end, L1),
lists:flatten(lists:join(", ", L2)).
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-compile(nowarn_export_all). -compile(nowarn_export_all).

View File

@ -82,7 +82,9 @@ status(_) ->
broker([]) -> broker([]) ->
Funs = [sysdescr, version, datetime], Funs = [sysdescr, version, datetime],
[emqx_ctl:print("~-10s: ~ts~n", [Fun, emqx_sys:Fun()]) || Fun <- Funs], [emqx_ctl:print("~-10s: ~ts~n", [Fun, emqx_sys:Fun()]) || Fun <- Funs],
emqx_ctl:print("~-10s: ~p~n", [uptime, emqx_sys:uptime()]); emqx_ctl:print("~-10s: ~ts~n", [
uptime, emqx_datetime:human_readable_duration_string(emqx_sys:uptime())
]);
broker(["stats"]) -> broker(["stats"]) ->
[ [
emqx_ctl:print("~-30s: ~w~n", [Stat, Val]) emqx_ctl:print("~-30s: ~w~n", [Stat, Val])