Skip to content

Commit

Permalink
Merge pull request #965 from basho/mas-i1775-vclocktimestamp
Browse files Browse the repository at this point in the history
Mas i1775 vclocktimestamp
  • Loading branch information
martinsumner authored Dec 13, 2020
2 parents 3903efc + 9f6b7b3 commit e817ee7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/riak_core_handoff_status.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

-include("riak_core_handoff.hrl").

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-export([handoff_summary/3,
handoff_details/3,
collect_node_transfer_data/0]).
Expand Down Expand Up @@ -130,10 +134,10 @@ format_transfer_type(resize) ->
format_transfer_type(repair) ->
"Rp".

format_transfer_size({Num, objects}) ->
format_transfer_size({Num, objects}) when is_integer(Num) ->
io_lib:format("~B Objs", [Num]);
format_transfer_size({Num, bytes}) ->
riak_core_format:human_size_fmt("~B", Num);
format_transfer_size({Num, bytes}) when is_integer(Num) ->
riak_core_format:human_size_fmt("~.2f", Num);
format_transfer_size(_) ->
"--".

Expand Down Expand Up @@ -389,3 +393,19 @@ maybe_add_down_nodes(DownNodes, Output) ->
NodesDown = clique_status:alert([clique_status:list("(unreachable)", DownNodes)]),
Output ++ [NodesDown]
end.



%% ===================================================================
%% EUnit tests
%% ===================================================================
-ifdef(TEST).

transfer_bytes_test() ->
?assertMatch("1.00 B", format_transfer_size({1, bytes})),
?assertMatch("976.56 KB", format_transfer_size({1000000, bytes})),
?assertMatch("1.00 MB", format_transfer_size({1048576, bytes})),
?assertMatch("1.86 GB", format_transfer_size({2000000000, bytes})),
?assertMatch("--", format_transfer_size({23.4, bytes})).

-endif.
21 changes: 20 additions & 1 deletion src/vclock.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
all_nodes/1,
equal/2,
prune/3,
timestamp/0]).
timestamp/0,
last_modified/1]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
Expand Down Expand Up @@ -170,6 +171,14 @@ get_timestamp(Node, VClock) ->
false -> undefined
end.

% @doc Get the last timestamp from a clock in a friendly format
-spec last_modified(VClock :: vclock()) -> calendar:datetime() | undefined.
last_modified([]) ->
undefined;
last_modified(VClock) ->
TSL = lists:map(fun({_, {_Ctr, TS}}) -> TS end, VClock),
calendar:gregorian_seconds_to_datetime(lists:last(lists:sort(TSL))).

% @doc Get the entry `dot()' for `vclock_node()' from `vclock()'.
-spec get_dot(Node :: vclock_node(), VClock :: vclock()) -> {ok, dot()} | undefined.
get_dot(Node, VClock) ->
Expand Down Expand Up @@ -353,6 +362,16 @@ accessor_test() ->
?assertEqual(undefined, get_timestamp(<<"3">>, VC)),
?assertEqual([<<"1">>, <<"2">>], all_nodes(VC)).

last_modified_test() ->
DT1 = {{1972, 5, 6}, {16, 13, 0}},
DT2 = {{2020, 7, 12}, {15, 14, 0}},
VC0 = vclock:fresh(),
?assertMatch(undefined, last_modified(VC0)),
VC1 =
[{<<"Clarke!">>, {1, calendar:datetime_to_gregorian_seconds(DT1)}},
{<<"Pablo!">>, {1, calendar:datetime_to_gregorian_seconds(DT2)}}],
?assertMatch(DT2, last_modified(VC1)).

merge_test() ->
VC1 = [{<<"1">>, {1, 1}},
{<<"2">>, {2, 2}},
Expand Down

0 comments on commit e817ee7

Please sign in to comment.