Procházet zdrojové kódy

Merge pull request #8194 from zmstone/0613-uptime-format-as-string-in-cli-printout

fix: uptime display in CLI printout should be human-readable
Zaiming (Stone) Shi před 3 roky
rodič
revize
b18290626d

+ 11 - 1
apps/emqx/src/emqx_datetime.erl

@@ -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).

+ 3 - 1
apps/emqx_management/src/emqx_mgmt_cli.erl

@@ -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])